Agent.run(), it creates a stateless, singular Agent run.
But what if we want to continue this run i.e. have a multi-turn conversation? That’s where sessions come in. A session is collection of consecutive runs.
In practice, a session is a multi-turn conversation between a user and an Agent. Using a session_id, we can connect the conversation history and state across multiple runs.
Let’s outline some key concepts:
- User: A user represents an individual that interacts with the Agent. Each user has associated memories, sessions, and conversation history separate from other users.
- Session: A session is collection of consecutive runs like a multi-turn conversation between a user and an Agent. Sessions are identified by a
session_idand each turn is a run. - Run: Every interaction (i.e. chat or turn) with an Agent is called a run. Runs are identified by a
run_idandAgent.run()creates a newrun_idwhen called. - Messages: are the individual messages sent between the model and the Agent. Messages are the communication protocol between the Agent and model.
run_id is automatically generated, as well as a session_id (because we didn’t provide one to continue the conversation). This run is not yet associated with a user.
Multi-user, multi-session Agents
Each user that is interacting with an Agent gets a unique set of sessions and you can have multiple users interacting with the same Agent at the same time. Set auser_id to connect a user to their sessions with the Agent.
In the example below, we set a session_id to demo how to have multi-turn conversations with multiple users at the same time. In production, the session_id is auto generated.
Note: Multi-user, multi-session currently only works with
Memory.v2, which will become the default memory implementation in the next release.Fetch messages from last N sessions
In some scenarios, you might want to fetch messages from the last N sessions to provide context or continuity in conversations. Here’s an example of how you can achieve this:To enable fetching messages from the last N sessions, you need to use the following flags:
search_previous_sessions_history: Set this toTrueto allow searching through previous sessions.num_history_sessions: Specify the number of past sessions to include in the search. In this example, it is set to2to include only the last 2 sessions. It’s advisable to keep this number to 2 or 3 for now, as a larger number might fill up the context length of the model, potentially leading to performance issues.