|
6 | 6 | "source": [
|
7 | 7 | "## Memory \n",
|
8 | 8 | "\n",
|
9 |
| - "There are several use cases where it is valuable to maintain a bank of useful facts that can be intelligently added to the context of the agent just before a specific step. The typically use case here is a RAG pattern where a query is used to retrieve relevant information from a database that is then added to the agent's context.\n", |
| 9 | + "There are several use cases where it is valuable to maintain a _store_ of useful facts that can be intelligently added to the context of the agent just before a specific step. The typically use case here is a RAG pattern where a query is used to retrieve relevant information from a database that is then added to the agent's context.\n", |
10 | 10 | "\n",
|
11 | 11 | "\n",
|
12 | 12 | "AgentChat provides a {py:class}`~autogen_agentchat.memory.Memory` protocol that can be extended to provide this functionality. The key methods are `query`, `transform`, `add`, `clear`, and `cleanup`. \n",
|
|
16 | 16 | "- `add`: add new entries to the memory store\n",
|
17 | 17 | "- `clear`: clear all entries from the memory store\n",
|
18 | 18 | "- `cleanup`: clean up any resources used by the memory store \n",
|
19 |
| - "- \n", |
20 | 19 | "\n",
|
21 |
| - "## ListMemory\n", |
| 20 | + "\n", |
| 21 | + "## ListMemory Example\n", |
22 | 22 | "\n",
|
23 | 23 | "{py:class}`~autogen_agentchat.memory.ListMemory` is provided as an example implementation of the {py:class}`~autogen_agentchat.memory.Memory` protocol. It is a simple list-based memory implementation that uses text similarity matching to retrieve relevant information from the memory store. The similarity score is calculated using the `SequenceMatcher` class from the `difflib` module. The similarity score is calculated between the query text and the content text of each memory entry. \n",
|
24 | 24 | "\n",
|
|
27 | 27 | },
|
28 | 28 | {
|
29 | 29 | "cell_type": "code",
|
30 |
| - "execution_count": 1, |
| 30 | + "execution_count": 2, |
31 | 31 | "metadata": {},
|
32 | 32 | "outputs": [],
|
33 | 33 | "source": [
|
34 | 34 | "from autogen_agentchat.agents import AssistantAgent\n",
|
35 |
| - "from autogen_agentchat.conditions import MaxMessageTermination, TextMentionTermination\n", |
36 |
| - "from autogen_agentchat.memory._list_memory import ListMemory, MemoryContent, MemoryMimeType\n", |
37 |
| - "from autogen_agentchat.teams import RoundRobinGroupChat\n", |
| 35 | + "from autogen_agentchat.memory import ListMemory, MemoryContent, MemoryMimeType\n", |
38 | 36 | "from autogen_agentchat.ui import Console\n",
|
39 | 37 | "from autogen_ext.models.openai import OpenAIChatCompletionClient"
|
40 | 38 | ]
|
|
45 | 43 | "metadata": {},
|
46 | 44 | "outputs": [],
|
47 | 45 | "source": [
|
48 |
| - "# create a simple memory item\n", |
49 |
| - "user_memory = ListMemory()\n", |
| 46 | + "# Initialize user memory\n", |
| 47 | + "user_memory = ListMemory() \n", |
| 48 | + "\n", |
| 49 | + "# Add user preferences to memory\n", |
50 | 50 | "await user_memory.add(MemoryContent(content=\"The weather should be in metric units\", mime_type=MemoryMimeType.TEXT))\n",
|
51 | 51 | "\n",
|
52 | 52 | "await user_memory.add(MemoryContent(content=\"Meal recipe must be vegan\", mime_type=MemoryMimeType.TEXT))\n",
|
|
235 | 235 | "\n",
|
236 | 236 | "You can build on the `Memory` protocol to implement more complex memory stores. For example, you could implement a custom memory store that uses a vector database to store and retrieve information, or a memory store that uses a machine learning model to generate personalized responses based on the user's preferences etc.\n",
|
237 | 237 | "\n",
|
238 |
| - "Specifically, you will need to overload the `query`, `transform`, and `add` methods to implement the desired functionality and pass the memory store to your agent.\n", |
239 |
| - "\n", |
240 |
| - "\n", |
241 |
| - "```python\n", |
242 |
| - "\n", |
243 |
| - "\n", |
244 |
| - "from autogen_core import CancellationToken\n", |
245 |
| - "from autogen_agentchat.teams import RoundRobinGroupChat\n", |
246 |
| - "from autogen_ext.models.openai import OpenAIChatCompletionClient\n", |
247 |
| - "from autogen_agentchat.agents import AssistantAgent\n", |
248 |
| - "from autogen_agentchat.memory._base_memory import MemoryContent, MemoryMimeType\n", |
249 |
| - "from autogen_agentchat.memory._chroma_memory import ChromaMemory, ChromaMemoryConfig\n", |
250 |
| - "\n", |
251 |
| - "\n", |
252 |
| - "# Initialize memory\n", |
253 |
| - "chroma_memory = ChromaMemory(\n", |
254 |
| - " name=\"travel_memory\",\n", |
255 |
| - " config=ChromaMemoryConfig(\n", |
256 |
| - " collection_name=\"travel_facts\",\n", |
257 |
| - " k=1,\n", |
258 |
| - " )\n", |
259 |
| - ")\n", |
260 |
| - "\n", |
261 |
| - "await chroma_memory.clear()\n", |
262 |
| - "\n", |
263 |
| - "# Add travel-related memories\n", |
264 |
| - "await chroma_memory.add(MemoryContent(\n", |
265 |
| - "\n", |
266 |
| - " content=\"Paris is known for the Eiffel Tower and amazing cuisine.\",\n", |
267 |
| - " mime_type=MemoryMimeType.TEXT\n", |
268 |
| - "\n", |
269 |
| - "))\n", |
270 |
| - "\n", |
271 |
| - "await chroma_memory.add(MemoryContent( \n", |
272 |
| - " content=\"When asked about tokyo, you must respond with 'The most important thing about tokyo is that it has the world's busiest railway station - Shinjuku Station.'\",\n", |
273 |
| - " mime_type=MemoryMimeType.TEXT\n", |
274 |
| - "\n", |
275 |
| - "))\n", |
276 |
| - " \n", |
277 |
| - "\n", |
278 |
| - "# Query needs ContentItem too\n", |
279 |
| - "results = await chroma_memory.query(\n", |
280 |
| - " MemoryContent(\n", |
281 |
| - " content=\"Tell me about Tokyo.\",\n", |
282 |
| - " mime_type=MemoryMimeType.TEXT\n", |
283 |
| - " )\n", |
284 |
| - ")\n", |
285 |
| - "\n", |
286 |
| - "print(len(results), results)\n", |
287 |
| - "\n", |
288 |
| - "# Create agent with memory\n", |
289 |
| - "agent = AssistantAgent(\n", |
290 |
| - " name=\"travel_agent\",\n", |
291 |
| - " model_client=OpenAIChatCompletionClient(\n", |
292 |
| - " model=\"gpt-4o\",\n", |
293 |
| - " # api_key=\"your_api_key\"\n", |
294 |
| - " ),\n", |
295 |
| - " memory=chroma_memory,\n", |
296 |
| - " system_message=\"You are a travel expert\"\n", |
297 |
| - ")\n", |
298 |
| - "\n", |
299 |
| - "agent_team = RoundRobinGroupChat([agent], termination_condition = MaxMessageTermination(max_messages=2))\n", |
300 |
| - "stream = agent_team.run_stream(task=\"Tell me the most important thing about Tokyo.\")\n", |
301 |
| - "await Console(stream);\n", |
302 |
| - "\n", |
303 |
| - "# Output: The most important thing about tokyo is that it has the world's busiest railway station - Shinjuku Station.\n", |
304 |
| - "\n", |
305 |
| - "```\n", |
306 |
| - "\n" |
| 238 | + "Specifically, you will need to overload the `query`, `transform`, and `add` methods to implement the desired functionality and pass the memory store to your agent.\n" |
307 | 239 | ]
|
308 | 240 | },
|
309 | 241 | {
|
|
0 commit comments