forked from openai/openai-agents-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsynap_memory.py
More file actions
73 lines (56 loc) · 2.3 KB
/
synap_memory.py
File metadata and controls
73 lines (56 loc) · 2.3 KB
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
"""Example: Synap memory tools.
Demonstrates how to give an OpenAI Agent persistent, cross-session memory using
Synap (https://maximem.ai) — a managed memory layer for AI agents.
`synap-openai-agents` provides two tool factories:
- `create_search_tool` — semantic search over the user's stored memories
- `create_store_tool` — persist explicit facts to the user's memory
Both return async callables you can wrap with `@function_tool` or pass to
`FunctionTool` directly.
Install:
pip install synap-openai-agents maximem-synap openai-agents
Set `SYNAP_API_KEY` in your environment. Get a free key at
https://synap.maximem.ai.
Open source: https://github.com/maximem-ai/maximem_synap_sdk
"""
import asyncio
import os
from agents import Agent, FunctionTool, Runner
from maximem_synap import MaximemSynapSDK
from synap_openai_agents import create_search_tool, create_store_tool
async def main() -> None:
sdk = MaximemSynapSDK(api_key=os.environ["SYNAP_API_KEY"])
await sdk.initialize()
user_id = "demo-user-001"
search_fn = create_search_tool(sdk, user_id=user_id, customer_id="acme_corp")
store_fn = create_store_tool(sdk, user_id=user_id, customer_id="acme_corp")
agent = Agent(
name="memory-assistant",
instructions=(
"You are a helpful assistant with long-term memory. "
"Call search_memory to recall what you know about the user. "
"Call store_memory to save important new facts."
),
tools=[
FunctionTool(
search_fn,
name="search_memory",
description="Search the user's long-term memory.",
),
FunctionTool(
store_fn,
name="store_memory",
description="Store an explicit fact in the user's long-term memory.",
),
],
)
print("=== Turn 1: teach the agent something ===")
result = await Runner.run(
agent,
"I'm a software engineer who prefers concise answers and is allergic to peanuts.",
)
print(result.final_output)
print("\n=== Turn 2: agent recalls from Synap ===")
result = await Runner.run(agent, "What do you know about my dietary restrictions?")
print(result.final_output)
if __name__ == "__main__":
asyncio.run(main())