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.
Comet SDK | Minimum SDK version | Minimum LangChain version |
---|---|---|
LLM-SDK | 2.0.2 | 0.0.346 |
Start logging¶
- Import the
CometTracer
callback into your script - Pass the callback to your language model, chain, or agent.
import comet_llm
from langchain.callbacks.tracers.comet import CometTracer
from langchain.chat_models import ChatOpenAI
from langchain.schema import HumanMessage
comet_llm.init()
callbacks = [CometTracer()]
chat_model = ChatOpenAI(callbacks=callbacks)
text = "What would be a good company name for a company that makes colorful socks?"
messages = [HumanMessage(content=text)]
chat_model.invoke(messages)
Log automatically¶
Comet will log the following information from your Langchain scrips:
- Language Model Parameters
- Input Text Prompts
- Generated Output Text
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.0.346" openai google-search-results numexpr
Set Third Party Credentials¶
You will need an OpenAI API Key and a SerpAPI API Key to run the following examples
export OPENAI_API_KEY="Your OpenAI API Key"
export SERPAPI_API_KEY="Your SerpAPI 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.llms import OpenAI
comet_llm.init(project="comet-example-langchain-doc")
comet_callback = CometTracer()
callbacks = [comet_callback]
llm = OpenAI(temperature=0.9, callbacks=callbacks, verbose=True)
tools = load_tools(["serpapi", "llm-math"], llm=llm, callbacks=callbacks, verbose=True)
agent = initialize_agent(
tools,
llm,
agent="zero-shot-react-description",
callbacks=callbacks,
verbose=True,
)
agent.run(
"Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?",
callbacks=callbacks,
)
comet_callback.flush()
Try it out!¶
Try out some examples for using Comet with Langchain in this Colab Notebook.
Dec. 19, 2023