Chaining calls (Sessions)
Chaining calls (Sessions)
Chaining calls (Sessions)
This guide contains 3 sections. We’ll start with an example Python script that makes a series of calls to an LLM upon receiving a user request. In the first section, we’ll log these calls to Humanloop. In the second section, we’ll link up these calls to a single session so they can be easily inspected on Humanloop. Finally, we’ll explore how to deal with nested logs within a session.
By following this guide, you will:
If you don’t use Python, you can checkout our TypeScript SDK or the underlying API in our Postman collection for the corresponding endpoints.
To set up your local environment to run this script, you will need to have installed Python 3 and the following libraries:
pip install openai google-search-results.
To send logs to Humanloop, we’ll install and use the Humanloop Python SDK.
Add the following lines to the top of the example file. (Get your API key from your Organisation Settings page)
Replace your openai.ChatCompletion.create() call under # Check for abuse with a humanloop.chat() call.
Instead of replacing your model call with humanloop.chat()you can
alternatively add a humanloop.log()call after your model call. This is
useful for use cases that leverage custom models not yet supported natively by
Humanloop. See our Using your own model guide
for more information.
You have now connected your multiple calls to Humanloop, logging them to individual projects. While each one can be inspected individually, we can’t yet view them together to evaluate and improve our pipeline.
To view the logs for a single user_request together, we can log them to a session. This requires a simple change of just passing in the same session id to the different calls.
This is the updated version of the example script above with Humanloop fully integrated. Running this script yields sessions that can be inspected on Humanloop.
A more complicated trace involving nested logs, such as those recording an Agent’s behaviour, can also be logged and viewed in Humanloop.
First, post a log to a session, specifying both session_reference_id and reference_id. Then, pass in this reference_id as parent_reference_id in a subsequent log request. This indicates to Humanloop that this second log should be nested under the first.
Deferred output population
In most cases, you don’t know the output for a parent log until all of its children have completed. For instance, the root-level Agent will spin off multiple LLM requests before it can retrieve an output. To support this case, we allow logging without an output. The output can then be updated after the session is complete with a separate humanloop.logs_api.update_by_reference_id(reference_id, output) call.