With wrappers
The simplest way to add Literal AI to your codebase is to use the provided wrappers. Wrappers are based on the AsyncLocalStorage API and are designed to play nice with async code.
Wrappers will automatically handle the thread and step IDs for you, and will also automatically send entities to the Literal AI API upon exiting. When a step wrapper is exited, the following actions will be taken :
- Start and end time are logged
- The output from the wrapped function is logged as the output of the step
- The updated step is sent to the Literal AI API
Basic usage
Thread wrapper
Step wrapper
Decoration wrapper
This wrapper is used to add metadata and tags to everything that will be logged inside it. It can also be used to specify in advance the ID of any generation created inside it.
Advanced usage
Nesting wrappers
One advantage of using wrappers is that the structure of your code will closely represent the structure of the conversation as it is logged on Literal AI. For example consider the following code :
This will result in the following structure on Literal AI :
Nested threads and steps
Wrapping existing threads or steps
When you fetch a thread or step from the client, you can wrap it with the wrap
method. This will allow you to add additional steps or threads to the existing one.
Updating steps and threads
Using the context
You can also update the current step or thread using the context object. The context object is available in the wrapped function and it will be sent to Literal AI upon exiting the wrapped function.
There are two ways to access the context object:
- It is provided as an argument to the wrapper function
- It is available as a property of the client object :
client.getCurrentThread()
andclient.getCurrentStep()
The getCurrentThread
and getCurrentStep
methods are type-safe and they will throw an error if called outside of a wrapped context.
Using the wrapper’s callback
The wrappers take a second argument that allows you to update the current step or thread upon exiting the wrapped function. The second argument can either be a static object, or a function that takes the wrapper’s output as an argument and returns a valid step or thread object.
Was this page helpful?