|
| 1 | +""" |
| 2 | +This example demonstrates how to maintain state for each user in a multi-user environment. |
| 3 | +
|
| 4 | +The shopping list is stored in a dictionary, organized by user ID and session ID. |
| 5 | +
|
| 6 | +Agno automatically creates the "current_user_id" and "current_session_id" variables in the session state. |
| 7 | +
|
| 8 | +You can access these variables in your functions using the `agent.session_state` dictionary. |
| 9 | +""" |
| 10 | + |
| 11 | +import json |
| 12 | + |
| 13 | +from agno.agent import Agent |
| 14 | +from agno.models.openai import OpenAIChat |
| 15 | +from agno.utils.log import log_info |
| 16 | + |
| 17 | +# In-memory database to store user shopping lists |
| 18 | +# Organized by user ID and session ID |
| 19 | +shopping_list = {} |
| 20 | + |
| 21 | + |
| 22 | +def add_item(agent: Agent, item: str) -> str: |
| 23 | + """Add an item to the current user's shopping list.""" |
| 24 | + current_user_id = agent.session_state["current_user_id"] |
| 25 | + current_session_id = agent.session_state["current_session_id"] |
| 26 | + shopping_list.setdefault(current_user_id, {}).setdefault( |
| 27 | + current_session_id, [] |
| 28 | + ).append(item) |
| 29 | + return f"Item {item} added to the shopping list" |
| 30 | + |
| 31 | + |
| 32 | +def remove_item(agent: Agent, item: str) -> str: |
| 33 | + """Remove an item from the current user's shopping list.""" |
| 34 | + current_user_id = agent.session_state["current_user_id"] |
| 35 | + current_session_id = agent.session_state["current_session_id"] |
| 36 | + |
| 37 | + if ( |
| 38 | + current_user_id not in shopping_list |
| 39 | + or current_session_id not in shopping_list[current_user_id] |
| 40 | + ): |
| 41 | + return f"No shopping list found for user {current_user_id} and session {current_session_id}" |
| 42 | + |
| 43 | + if item not in shopping_list[current_user_id][current_session_id]: |
| 44 | + return f"Item '{item}' not found in the shopping list for user {current_user_id} and session {current_session_id}" |
| 45 | + |
| 46 | + shopping_list[current_user_id][current_session_id].remove(item) |
| 47 | + return f"Item {item} removed from the shopping list" |
| 48 | + |
| 49 | + |
| 50 | +def get_shopping_list(agent: Agent) -> str: |
| 51 | + """Get the current user's shopping list.""" |
| 52 | + current_user_id = agent.session_state["current_user_id"] |
| 53 | + current_session_id = agent.session_state["current_session_id"] |
| 54 | + return f"Shopping list for user {current_user_id} and session {current_session_id}: \n{json.dumps(shopping_list[current_user_id][current_session_id], indent=2)}" |
| 55 | + |
| 56 | + |
| 57 | +# Create an Agent that maintains state |
| 58 | +agent = Agent( |
| 59 | + model=OpenAIChat(id="gpt-4o-mini"), |
| 60 | + tools=[add_item, remove_item, get_shopping_list], |
| 61 | + # Reference the in-memory database |
| 62 | + instructions=[ |
| 63 | + "Current User ID: {current_user_id}", |
| 64 | + "Current Session ID: {current_session_id}", |
| 65 | + ], |
| 66 | + # Important: Add the state in the instructions |
| 67 | + add_state_in_messages=True, |
| 68 | + markdown=True, |
| 69 | +) |
| 70 | + |
| 71 | +user_id_1 = "john_doe" |
| 72 | +user_id_2 = "mark_smith" |
| 73 | +user_id_3 = "carmen_sandiago" |
| 74 | + |
| 75 | +# Example usage |
| 76 | +agent.print_response( |
| 77 | + "Add milk, eggs, and bread to the shopping list", |
| 78 | + stream=True, |
| 79 | + user_id=user_id_1, |
| 80 | + session_id="user_1_session_1", |
| 81 | +) |
| 82 | +agent.print_response( |
| 83 | + "Add tacos to the shopping list", |
| 84 | + stream=True, |
| 85 | + user_id=user_id_2, |
| 86 | + session_id="user_2_session_1", |
| 87 | +) |
| 88 | +agent.print_response( |
| 89 | + "Add apples and grapesto the shopping list", |
| 90 | + stream=True, |
| 91 | + user_id=user_id_3, |
| 92 | + session_id="user_3_session_1", |
| 93 | +) |
| 94 | +agent.print_response( |
| 95 | + "Remove milk from the shopping list", |
| 96 | + stream=True, |
| 97 | + user_id=user_id_1, |
| 98 | + session_id="user_1_session_1", |
| 99 | +) |
| 100 | +agent.print_response( |
| 101 | + "Add minced beef to the shopping list", |
| 102 | + stream=True, |
| 103 | + user_id=user_id_2, |
| 104 | + session_id="user_2_session_1", |
| 105 | +) |
| 106 | + |
| 107 | +# What is on Mark Smith's shopping list? |
| 108 | +agent.print_response( |
| 109 | + "What is on Mark Smith's shopping list?", |
| 110 | + stream=True, |
| 111 | + user_id=user_id_2, |
| 112 | + session_id="user_2_session_1", |
| 113 | +) |
| 114 | + |
| 115 | +# New session, so new shopping list |
| 116 | +agent.print_response( |
| 117 | + "Add chicken and soup to my list.", |
| 118 | + stream=True, |
| 119 | + user_id=user_id_2, |
| 120 | + session_id="user_3_session_2", |
| 121 | +) |
| 122 | + |
| 123 | +print(f"Final shopping lists: \n{json.dumps(shopping_list, indent=2)}") |
0 commit comments