Log to phospho in Python Langchain
Add AI analytics to your Langchain agent with phospho
phospho can be added to a Langchain agent as a callback handler. By default, the task input is the beginning of the chain, and the task output is the end result. Intermediate steps are also logged.
Detailed setup in a retrieval agent
Set the following environment variables:
Learn how to get your project id and api key by clicking here!
Install requirements:
The phospho module implements the Langchain callback as well as other helpful tools to interact with phospho. Learn more in the python doc.
The phospho module is an open source work in progress. Your help is deeply appreciated!
Create a file called main.py
with the agent code.
phospho is integrated with langchain via the PhosphoLangchainCallbackHandler
callback handler. This callback handler will log the input and output of the agent to phospho.
The integration with phospho is done by adding the PhosphoLangchainCallbackHandler
to the config of the chain. You can learn more about callbacks in the langchain doc.
Start the RAG agent and ask questions about the documents.
The agent answers question based on retrieved documents (RAG, Retrieval Augmented Generation).
The conversation and the intermediate retrievals steps (such as the documents retrieved) are logged to phospho.
Custom logging in langchain
For more advanced manual logging with a langchain, you can inherit from the PhosphoLangchainCallbackHandler
and add custom behaviour.
The callback has a reference to the phospho
object, which can be used to log custom data.
You can refer to the langchain doc to have the full list of callbacks available.
Was this page helpful?