使用Aim追踪LangChain的执行流程:从初学者到专家的指南

# 使用Aim追踪LangChain的执行流程:从初学者到专家的指南

## 引言
在现代AI应用中,调试和可视化是不可或缺的步骤。Aim是一个开源工具,专注于追踪和调试LangChain的执行。通过Aim,你可以轻松追踪LLM(大语言模型)和工具的输入输出,并查看并排执行结果。本文将详细介绍如何启用和配置Aim回调,以便更好地管理和调试LangChain的执行。

## 主要内容

### 安装与配置
首先,我们需要安装必要的Python软件包,并配置环境变量以支持OpenAI和SerpApi的API访问。

```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"] = "your-openai-api-key"
os.environ["SERPAPI_API_KEY"] = "your-serpapi-api-key"

使用Aim跟踪LangChain执行

接下来,通过AimCallbackHandler,我们可以记录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)

代码示例

Scenario 1: 使用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",
)

Scenario 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"
)

Scenario 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代理服务来确保访问稳定性。可以在代码中将{AI_URL}替换为实际的代理服务端点。

  • 环境配置错误:确保所有环境变量配置正确,API密钥需要保密并妥善存储。

总结与进一步学习资源

Aim为LangChain的调试和可视化提供了强大的支持,帮助开发者更方便地管理和优化AI模型的执行过程。推荐查看Aim的GitHub仓库以获取更多文档和示例。

参考资料

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

---END---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值