Skip to main content
Aim 让可视化和调试 LangChain 执行过程变得极为简便。Aim 可追踪 LLM 和工具的输入输出,以及 Agent 的行为。 使用 Aim,您可以轻松调试和检查单次执行: 227784778 06b806c7 74a1 4d15 ab85 9ece09b458aa 此外,您还可以并排比较多次执行: 227784994 699b24b7 e69b 48f9 9ffa e6a6142fd719 Aim 完全开源,了解更多 让我们继续了解如何启用和配置 Aim 回调。

使用 Aim 追踪 LangChain 执行

本 notebook 将探索三种使用场景。首先,我们将安装必要的包并导入相关模块,然后配置两个可在 Python 脚本或终端中设置的环境变量。
pip install -qU  aim
pip install -qU  langchain
pip install -qU  langchain-openai
pip install -qU  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
示例中使用 GPT 模型作为 LLM,OpenAI 提供了相应的 API。您可以从以下链接获取密钥:platform.openai.com/account/api-keys 我们将使用 SerpApi 从 Google 获取搜索结果。请访问 serpapi.com/manage-api-key 获取 SerpApi 密钥。
os.environ["OPENAI_API_KEY"] = "..."
os.environ["SERPAPI_API_KEY"] = "..."
AimCallbackHandler 的事件方法接受 LangChain 模块或 Agent 作为输入,并将至少包含提示和生成结果,以及 LangChain 模块的序列化版本记录到指定的 Aim 运行中。
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)
flush_tracker 函数用于将 LangChain 资产记录到 Aim。默认情况下,会话将被重置而非直接终止。

场景一

第一个场景中,我们将使用 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",
)

场景二

场景二涉及跨多轮生成使用包含多个子链的链式结构。
from langchain_classic.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"
)

场景三

第三个场景涉及带有工具的 Agent。
from langchain.agents import create_agent, load_tools
# scenario 3 - Agent with Tools
tools = load_tools(["serpapi", "llm-math"], llm=llm, callbacks=callbacks)

agent = create_agent(
    model=llm,
    tools=tools,
    callbacks=callbacks,
)

agent.invoke(
    "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)
> Entering new AgentExecutor chain...
 I need to find out who Leo DiCaprio's girlfriend is and then calculate her age raised to the 0.43 power.
Action: Search
Action Input: "Leo DiCaprio girlfriend"
Observation: Leonardo DiCaprio seemed to prove a long-held theory about his love life right after splitting from girlfriend Camila Morrone just months ...
Thought: I need to find out Camila Morrone's age
Action: Search
Action Input: "Camila Morrone age"
Observation: 25 years
Thought: I need to calculate 25 raised to the 0.43 power
Action: Calculator
Action Input: 25^0.43
Observation: Answer: 3.991298452658078

Thought: I now know the final answer
Final Answer: Camila Morrone is Leo DiCaprio's girlfriend and her current age raised to the 0.43 power is 3.991298452658078.

> Finished chain.