Skip to main content

目标

Aim使得可视化和调试LangChain的执行变得非常简单。Aim跟踪LLM和工具的输入和输出,以及代理的操作。

使用Aim,您可以轻松调试和检查单个执行:

此外,您还可以选择并排比较多个执行:

Aim是完全开源的,在GitHub上了解更多

让我们继续前进,看看如何启用和配置Aim回调。

使用Aim跟踪LangChain执行

在这个笔记本中,我们将探索三种使用场景。首先,我们将安装必要的软件包并导入某些模块。随后,我们将配置两个环境变量,这些变量可以在Python脚本中或通过终端来设置。

pip install aim
pip install langchain
pip install openai
pip install google-search-results
import os
from datetime import datetime

from langchain.llms import OpenAI
from langchain.callbacks import AimCallbackHandler, StdOutCallbackHandler

我们的示例使用GPT模型作为LLM,OpenAI为此提供了一个API。您可以从以下链接获取密钥:https://platform.openai.com/account/api-keys。

我们将使用SerpApi从Google检索搜索结果。要获取SerpApi密钥,请访问https://serpapi.com/manage-api-key。

os.environ["OPENAI_API_KEY"] = "..."
os.environ["SERPAPI_API_KEY"] = "..."

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

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

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

flush_tracker函数用于在Aim上记录LangChain资产。默认情况下,会重置会话而不是直接终止会话。

场景1

在第一个场景中,我们将使用OpenAI LLM。
# 场景1 - LLM
llm_result = llm.generate(["Tell me a joke", "Tell me a poem"] * 3)
aim_callback.flush_tracker(
langchain_asset=llm,
experiment_name="场景2:具有多个子链的链在多个生成中",
)

场景2

场景二涉及在多个生成中使用多个子链进行链接。
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
# 场景2 - 链
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="场景3:具有工具的代理"
)

场景3

第三个场景涉及具有工具的代理。
from langchain.agents import initialize_agent, load_tools
from langchain.agents import AgentType
# 场景3 - 具有工具的代理
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)
    

> 进入新的AgentExecutor链...
我需要找出谁是Leonardo DiCaprio的女朋友,然后计算她的年龄的0.43次方。
动作:搜索
动作输入:"Leo DiCaprio girlfriend"
观察:Leonardo DiCaprio似乎在与女友Camila Morrone分手后不久证明了一个长期以来的理论...
思考:我需要找出Camila Morrone的年龄
动作:搜索
动作输入:"Camila Morrone age"
观察:25岁
思考:我需要计算25的0.43次方
动作:计算器
动作输入:25^0.43
观察:答案:3.991298452658078

思考:我现在知道最终答案了
最终答案:Camila Morrone是Leonardo DiCaprio的女朋友,她的年龄的0.43次方是3.991298452658078。

> 完成链。