3.2 KiB
| title |
|---|
| LangChain |
:::tip If you are not already using ClearML, see ClearML Setup instructions. :::
LangChain is a popular framework for developing applications powered by
language models. You can integrate ClearML into your LangChain code using the built-in ClearMLCallbackHandler. This
class is used to create a ClearML Task to log LangChain assets and metrics.
Integrate ClearML with the following steps:
-
Set up the
ClearMLCallbackHandler. The following code creates a ClearML Task calledllmin thelangchain_callback_demoproject, which captures your script's information, including Git details, uncommitted code, and Python environment:from langchain.callbacks import ClearMLCallbackHandler from langchain_openai import OpenAI # Set up and use the ClearML Callback clearml_callback = ClearMLCallbackHandler( task_type="inference", project_name="langchain_callback_demo", task_name="llm", tags=["test"], # Change the following parameters based on the amount of detail you want tracked visualize=True, complexity_metrics=True, stream_logs=True, ) llm = OpenAI(temperature=0, callbacks=[clearml_callback])You can also pass the following parameters to the
ClearMLCallbackHandlerobject:task_type– The type of ClearML task to create (see task types)tags– A list of tags to add to the taskvisualize- Set toTruefor ClearML to capture the run's Dependencies and Entities plots to the ClearML taskcomplexity_metrics- Set toTrueto log complexity metricsstream_logs- Set toTrueto stream callback actions to ClearML Parameters.
-
Use
ClearMLCallbackHandler.flush_tracker()after each model request to make sure all outputs, including metrics and prompts, are logged to ClearML:llm_result = llm.generate(["Tell me a joke", "Tell me a poem"] * 3) clearml_callback.flush_tracker(langchain_asset=llm, name="simple_sequential")Specify the following parameters:
name- A string identifying a context for the logged information (Tip: Use different names to log different model conversations).langchain_asset- The LangChain asset to save. This can also be a LangChain agent, and ClearML will track its results.finish- Set toTrueto close the ClearML task after logging. If set toTrue, the Callback cannot be used anymore.
Additional Logging Options
To augment its automatic logging, ClearML also provides an explicit logging interface.
See more information about explicitly logging information to a ClearML Task:
- Models
- Configuration (e.g. parameters, configuration files)
- Artifacts (e.g. output files or Python objects created by a task)
- Scalars
- Text/Plots/Debug Samples