Spaces:
Paused
Paused
File size: 7,713 Bytes
0fdb130 |
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"id": "2ea22fb4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\r\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip available: \u001b[0m\u001b[31;49m22.3\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.3.1\u001b[0m\r\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\r\n"
]
}
],
"source": [
"!pip install -qU google-api-python-client"
]
},
{
"cell_type": "markdown",
"id": "5d8d76d9",
"metadata": {},
"source": [
"# Conversation buffer memory"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "0d9e8e8f",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from dotenv import load_dotenv\n",
"from langchain.agents import AgentExecutor, Tool, ZeroShotAgent\n",
"from langchain.chains import LLMChain\n",
"from langchain.llms import OpenAI\n",
"from langchain.memory import ConversationBufferMemory, ReadOnlySharedMemory\n",
"from langchain.prompts import PromptTemplate\n",
"from langchain.utilities import GoogleSearchAPIWrapper\n",
"\n",
"llm = OpenAI(temperature=0)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "96286dd4",
"metadata": {},
"outputs": [],
"source": [
"template = \"\"\"This is a piece of financial report, namely Form 10-K, section 7:\n",
"\n",
"{chat_history}\n",
"\n",
"Summarize this text into 2-3 sentences as best as you can.\n",
"\"\"\"\n",
"\n",
"prompt = PromptTemplate(input_variables=[\"chat_history\"], template=template)\n",
"memory = ConversationBufferMemory(memory_key=\"chat_history\")\n",
"readonlymemory = ReadOnlySharedMemory(memory=memory)\n",
"summary_chain = LLMChain(\n",
" llm=OpenAI(),\n",
" prompt=prompt,\n",
" verbose=True,\n",
" memory=readonlymemory,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "d76360b0",
"metadata": {},
"outputs": [],
"source": [
"search = GoogleSearchAPIWrapper()\n",
"tools = [\n",
" Tool(\n",
" name=\"Search\",\n",
" func=search.run,\n",
" description=\"useful for when you need to answer questions about current events or find some relevant information on the internet.\",\n",
" ),\n",
" Tool(\n",
" name=\"Summary\",\n",
" func=summary_chain.run,\n",
" description=\"useful for when you need to summarize a piece of financial report text. The input to this tool should be a string.\",\n",
" ),\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5c6a7dc8",
"metadata": {},
"outputs": [],
"source": [
"prefix = \"\"\"\n",
"You are the best broker in the world. You are asked to read the financial report for some company.\n",
"Then you should suggest what is the best action: sell, buy or hold. You need to return only of those three options.\n",
"\"\"\"\n",
"suffix = \"\"\"Begin!\n",
"\n",
"{chat_history}\n",
"Question: {input}\n",
"{agent_scratchpad}\"\"\"\n",
"\n",
"prompt = ZeroShotAgent.create_prompt(\n",
" tools,\n",
" prefix=prefix,\n",
" suffix=suffix,\n",
" input_variables=[\"text_chunk\", \"chat_history\", \"agent_scratchpad\"],\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3bd67b6d",
"metadata": {},
"outputs": [],
"source": [
"llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)\n",
"agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)\n",
"agent_chain = AgentExecutor.from_agent_and_tools(\n",
" agent=agent, tools=tools, verbose=True, memory=memory\n",
")"
]
},
{
"cell_type": "markdown",
"id": "925c632e",
"metadata": {},
"source": [
"# Conversation summarization memory"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6c72c255",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "8af6c4ab",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "9885e240",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "1c7b7e74",
"metadata": {},
"source": [
"# Entity memory"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "e3423486",
"metadata": {},
"outputs": [],
"source": [
"from langchain.llms import OpenAI\n",
"from langchain.memory import ConversationEntityMemory\n",
"llm = OpenAI(temperature=0)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "d36440a7",
"metadata": {},
"outputs": [],
"source": [
"memory = ConversationEntityMemory(llm=llm)\n",
"_input = {\"input\": \"Deven & Sam are working on a hackathon project\"}\n",
"memory.load_memory_variables(_input)\n",
"memory.save_context(\n",
" _input,\n",
" {\"output\": \" That sounds like a great project! What kind of project are they working on?\"}\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "95b32eb8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'history': 'Human: Deven & Sam are working on a hackathon project\\nAI: That sounds like a great project! What kind of project are they working on?',\n",
" 'entities': {'Sam': 'Sam is working on a hackathon project with Deven.'}}"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"memory.load_memory_variables({\"input\": 'who is Sam'})"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "8b763a07",
"metadata": {},
"outputs": [],
"source": [
"memory = ConversationEntityMemory(llm=llm, return_messages=True)\n",
"_input = {\"input\": \"Deven & Sam are working on a hackathon project\"}\n",
"memory.load_memory_variables(_input)\n",
"memory.save_context(\n",
" _input,\n",
" {\"output\": \" That sounds like a great project! What kind of project are they working on?\"}\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "a95a2393",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'history': [HumanMessage(content='Deven & Sam are working on a hackathon project'),\n",
" AIMessage(content=' That sounds like a great project! What kind of project are they working on?')],\n",
" 'entities': {'Sam': 'Sam is working on a hackathon project with Deven.'}}"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"memory.load_memory_variables({\"input\": 'who is Sam'})"
]
}
],
"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.11.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|