Skip to content

Integrate with Langchain

LangChain is a framework for developing applications powered by language models. Langchain provides APIs to build data aware, agentic applications that allow language models to incorporate external data sources and interact with their compute environments.

Open In Colab

Comet SDKMinimum SDK versionMinimum LangChain version
LLM-SDK2.0.20.2.2

Start logging

  1. Install the comet_llm SDK
  2. Configure your API Key with comet_llm.init
  3. Import the CometTracer callback
  4. Pass the CometTracer callback when calling invoke or batch on your chains
import comet_llm
from langchain.callbacks.tracers.comet import CometTracer
from langchain_openai import OpenAI

comet_llm.init(project="comet-example-langchain-doc")

chat_model = OpenAI()

text = "What would be a good company name for a company that makes colorful socks?"

chat_model.invoke(text, {"callbacks": [CometTracer()]})

Log automatically

Comet will log the following information from your Langchain scrips:

  1. Language Model Parameters
  2. Input Text Prompts
  3. Generated Output Text
  4. Tool calls and configuration

End to End Example

If you can't wait, check out the results of this example Langchain project for a preview of what's to come.

Install Dependencies

pip install -U comet_llm "langchain>=0.2.2" "langchain-community>=0.2.2" "langchain-openai" openai numexpr

Set Third Party Credentials

You will need an OpenAI API Key to run the following examples

export OPENAI_API_KEY="Your OpenAI API Key"

Run the example

import comet_llm

from langchain.agents import initialize_agent, load_tools
from langchain.callbacks.tracers.comet import CometTracer
from langchain_openai import OpenAI

comet_llm.init(project="comet-example-langchain-doc")

llm = OpenAI(temperature=0.9)

tools = load_tools(["llm-math"], llm=llm)
agent = initialize_agent(
    tools,
    llm,
    agent="zero-shot-react-description",
)

callback = CometTracer()
agent.run("What is 2 raised to .123243 power?", callbacks=[callbacks])

Try it out!

Try out some examples for using Comet with Langchain in this Colab Notebook.

Open In Colab

Jun. 5, 2024