Skip to content

Commit

Permalink
chore: Update graph examples
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-paliychuk committed Oct 15, 2024
1 parent e312f00 commit dd57c52
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 7 deletions.
1 change: 1 addition & 0 deletions .fernignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ poetry.lock
README.md
.github
Makefile
.gitignore
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ dist/
.mypy_cache/
__pycache__/
poetry.toml
.idea/
.vscode/
*.env
.venv/
venv/
99 changes: 99 additions & 0 deletions examples/graph_example/group_graph_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
"""
Example of using the Zep Python SDK asynchronously with Graph functionality.
This script demonstrates the following functionality:
- Creating a group.
- Updating a group.
- Adding episodes to the group (text and JSON).
- Retrieving nodes from the group.
- Retrieving edges from the group.
- Searching the group for specific content.
The script showcases various operations using the Zep Graph API, including
group management, adding different types of episodes, and querying the graph structure.
"""

# ... rest of the file remains unchanged ...

import asyncio
import os
import uuid

from dotenv import find_dotenv, load_dotenv

from zep_cloud.client import AsyncZep

load_dotenv(
dotenv_path=find_dotenv()
) # load environment variables from .env file, if present

API_KEY = os.environ.get("ZEP_API_KEY") or "YOUR_API_KEY"


async def main() -> None:
client = AsyncZep(
api_key=API_KEY,
)

group_id = uuid.uuid4().hex
print(f"Creating group {group_id}...")
group = await client.group.add(
group_id=group_id,
name="My Group",
description="This is my group",
)
print(f"Group {group_id} created {group}")

print(f"Updating group {group_id}...")
group = await client.group.update(
group_id=group_id,
name="My Group 2",
description="This is my group 2",
)
print(f"Group {group_id} updated {group}")

print(f"Adding episode to group {group_id}...")
await client.graph.add(
group_id=group_id,
text="This is a test episode",
)

print(f"Adding more meaningful episode to group {group_id}...")
await client.graph.add(
group_id=group_id,
text="Eric Clapton is a rock star",
)

print(f"Adding a JSON episode to group {group_id}...")
json_string = '{"name": "Eric Clapton", "age": 78, "genre": "Rock"}'
await client.graph.add(
group_id=group_id,
json=json_string,
)
await asyncio.sleep(10)

# TODO: Need to enable non-message episodic content retrieval
# print(f"Getting episodes from group {group_id}...")
# results = await client.graph.episode.get_by_group_id(group_id, lastn=2)
# print(f"Episodes from group {group_id} {results.episodes}")
# episode = await client.graph.episode.get_by_uuid(results.episodes[0].uuid_)
# print(f"Episode {episode.uuid_} from group {group_id} {episode}")

print(f"Getting nodes from group {group_id}...")
nodes = await client.graph.node.get_by_group_id(group_id)
print(f"Nodes from group {group_id} {nodes}")

print(f"Getting edges from group {group_id}...")
edges = await client.graph.edge.get_by_group_id(group_id)
print(f"Edges from group {group_id} {edges}")

print(f"Searching group {group_id}...")
search_results = await client.graph.search(
group_id == group_id,
query="Eric Clapton",
)
print(f"Search results from group {group_id} {search_results}")


if __name__ == "__main__":
asyncio.run(main())
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
"""
Example of using the Zep Python SDK asynchronously.
Example of using the Zep Python SDK asynchronously with Graph functionality.
This script demonstrates the following functionality:
- Creating a user.
- Creating a session associated with the created user.
- Adding messages to the session.
- Searching the session memory for a specific query.
- Searching the session memory with MMR reranking.
- Searching the session memory with a metadata filter.
- optionally deleting the session.
- Retrieving episodes, edges, and nodes for a user.
- Searching the user's graph memory.
- Adding text and JSON episodes to the graph.
- Performing a centered search on a specific node.
The script showcases various operations using the Zep Graph API, including
user and session management, adding different types of episodes, and querying
the graph structure.
"""

# ... rest of the file remains unchanged ...

import asyncio
import os
import uuid
Expand Down Expand Up @@ -38,7 +44,7 @@ async def main() -> None:
print(f"User {user_id} created")
await client.memory.add_session(session_id=session_id, user_id=user_id)
print(f"Session {session_id} created")
for message in history[1]:
for message in history[2]:
await client.memory.add(
session_id,
messages=[
Expand All @@ -50,6 +56,8 @@ async def main() -> None:
],
)

print("Waiting for the graph to be updated...")
await asyncio.sleep(10)
episode_result = await client.graph.episode.get_by_user_id(user_id, lastn=3)
episodes = episode_result.episodes
print(f"Episodes for user {user_id}:")
Expand Down Expand Up @@ -92,7 +100,7 @@ async def main() -> None:

print("Waiting for the graph to be updated...")
# wait for the graph to be updated
await asyncio.sleep(15)
await asyncio.sleep(30)

print("Getting nodes from the graph...")
nodes = await client.graph.node.get_by_user_id(user_id)
Expand Down

0 comments on commit dd57c52

Please sign in to comment.