Multi-Session Example
Handling multiple sessions at the same time
View Notebook on Github
Multiple Concurrent Sessions
This example will show you how to run multiple sessions concurrently, assigning LLM calls to a specific session.
First let’s install the required packages:
Then import them:
Next, we’ll set our API keys. There are several ways to do this, the code below is just the most foolproof way for the purposes of this example. It accounts for both users who use environment variables and those who just want to set the API Key here.
-
Create an environment variable in a .env file or other method. By default, the AgentOps
init()
function will look for an environment variable namedAGENTOPS_API_KEY
. Or… -
Replace
<your_agentops_key>
below and pass in the optionalapi_key
parameter to the AgentOpsinit(api_key=...)
function. Remember not to commit your API key to a public repo!
Then, of course, lets init AgentOps. We’re going to bypass creating a session automatically for the sake of showing it below.
Now lets create two sessions, each with an identifiable tag.
LLM Calls
Now lets go ahead and make our first OpenAI LLM call. The challenge with having multiple sessions at the same time is that there is no way for AgentOps to know what LLM call is intended to pertain to what active session. This means we need to do a little extra work in one of two ways.
Patching Function
This method involves wrapping the LLM call withing a function on session. It can look a little counter-intuitive, but it easily tells us what session the call belongs to.
Create patched function
If you’re using the create function multiple times, you can create a new function with the same method
Keyword Argument
Alternatively, you can also pass the session into the LLM function call as a keyword argument. While this method works and is a bit more readable, it is not a “pythonic” pattern and can lead to linting errors in the code, as the base function is not expecting a session
keyword.
Recording Events
Outside of LLM calls, there are plenty of other events that we want to track. You can learn more about these events here.
Recording these events on a session is as simple as session.record(...)
Now let’s go ahead and end the sessions
If you look in the AgentOps dashboard for these sessions, you will see two unique sessions, both with one LLM Event each, one with an Action Event as well.