Spaces:
Sleeping
Sleeping
File size: 3,689 Bytes
0123005 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
{
"cells": [
{
"cell_type": "markdown",
"id": "c730c5b4",
"metadata": {},
"source": [
"# SimpleSequentialChain\n",
"In this series of chains, each individual chain has a single input and a single output, and the output of one step is used as input to the next."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f65a100b",
"metadata": {},
"outputs": [],
"source": [
"!pip install openai\n",
"!pip install langchain"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "454013f3",
"metadata": {},
"outputs": [],
"source": [
"%env OPENAI_API_TYPE=azure\n",
"%env OPENAI_API_VERSION=2022-12-01\n",
"%env OPENAI_API_BASE=\n",
"%env OPENAI_API_KEY="
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c8c0d462",
"metadata": {},
"outputs": [],
"source": [
"from langchain import PromptTemplate, LLMChain\n",
"from langchain.llms import AzureOpenAI\n",
"azllm=AzureOpenAI(deployment_name=\"test-text-davinci\", model_name=\"text-davinci-003\", temperature=0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "af343423",
"metadata": {},
"outputs": [],
"source": [
"# This is an LLMChain to write a synopsis given a title of a play.\n",
"template = \"\"\"You are a playwright. Given the title of play, it is your job to write a synopsis for that title.\n",
"\n",
"Title: {title}\n",
"Playwright: This is a synopsis for the above play:\"\"\"\n",
"prompt_template = PromptTemplate(input_variables=[\"title\"], template=template)\n",
"synopsis_chain = LLMChain(llm=azllm, prompt=prompt_template)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8dddc018",
"metadata": {},
"outputs": [],
"source": [
"# This is an LLMChain to write a review of a play given a synopsis.\n",
"template = \"\"\"You are a play critic from the New York Times. Given the synopsis of play, it is your job to write a review for that play.\n",
"\n",
"Play Synopsis:\n",
"{synopsis}\n",
"Review from a New York Times play critic of the above play:\"\"\"\n",
"prompt_template = PromptTemplate(input_variables=[\"synopsis\"], template=template)\n",
"review_chain = LLMChain(llm=azllm, prompt=prompt_template)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "06767f4a",
"metadata": {},
"outputs": [],
"source": [
"# This is the overall chain where we run these two chains in sequence.\n",
"from langchain.chains import SimpleSequentialChain\n",
"overall_chain = SimpleSequentialChain(chains=[synopsis_chain, review_chain], verbose=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c4e4a202",
"metadata": {},
"outputs": [],
"source": [
"review = overall_chain.run(\"Tragedy at sunset on the beach\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f1327102",
"metadata": {},
"outputs": [],
"source": [
"print(review)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b2119bbf",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|