LangChain
Overview
LangChain is a framework for building LLM applications. Confident AI provides a callback handlers in both python and typescript SDKs to trace and evaluate your LangChain applications automatically.
The callback handler captures the following spans from your LangChain application:
- LLM spans — model name, provider, input messages, output content, tool calls made by the model, and token usage
- Tool spans — tool name, input parameters, and output; also aggregated at the trace level as
tools_called - Retriever spans — query input and retrieved document output
- Chain spans — inputs and outputs for top-level chains (used to set trace-level input/output)
Tracing Quickstart
Configure LangChain
Provide DeepEval’s CallbackHandler to your LangChain application’s invoke method.
DeepEval’s CallbackHandler extends LangChain’s
BaseCallbackHandler
or LangChain.js’
BaseCallbackHandler.
Run LangChain
Invoke your application by executing the script:
You can directly view the traces on Confident AI by clicking on the link in the output printed in the console.
Advanced Features
Set trace attributes
Confident AI’s LLM tracing advanced features provide teams with the ability to set certain attributes for each trace when invoking your LangChain application.
For example, thread_id and user_id are used to group related traces together, and are useful for chat apps, agents, or any multi-turn interactions. You can learn more about threads here.
You can set these attributes in the CallbackHandler when invoking your LangChain application.
View Trace Attributes
The name of the trace. Learn more.
Tags are string labels that help you group related traces. Learn more.
Attach any metadata to the trace. Learn more.
Supply the thread or conversation ID to view and evaluate conversations. Learn more.
Supply the user ID to enable user analytics. Learn more.
Attach a test case ID to associate this trace with a specific test case.
Supply a turn ID to identify individual turns in a multi-turn conversation.
A list of metrics to run against the root span of this trace. Used for offline (development) evaluations.
The name of a metric collection on Confident AI to use for online (production) evaluations.
Each attribute is optional, and works the same way as the native tracing
features on Confident AI. Python uses
snake_case (e.g. thread_id) and TypeScript uses camelCase (e.g.
threadId).
Logging prompts
If you are managing prompts on Confident AI and wish to log them, pass your Prompt object to the language model instance’s metadata parameter.
Logging prompts lets you attribute specific prompts to LangChain LLM spans. Be sure to pull the prompt before logging it, otherwise the prompt will not be visible on Confident AI.
Evals Usage
Online evals
If your LangChain application is in production, and you still want to run evaluations on your traces, use online evals. It lets you run evaluations on all incoming traces on Confident AI’s server.
Create metric collection
Create a metric collection on Confident AI with the metrics you wish to use to evaluate your LangChain application. Copy the name of the metric collection.
The current LangChain integration supports metrics that only evaluate Input
and Actual Output in addition to the Task Completion metric.
Run evals
Set the metric_collection name to evaluate various components of your LangChain application.
Agent Span
LLM Span
Tool Span
This is the top level component of your LangChain application. Also a very ideal component to evaluate with the Task Completion metric.
All incoming traces will now be evaluated using metrics from your metric collection.
View on Confident AI
You can view the evals on Confident AI by clicking on the link in the output printed in the console.