跳到主要内容
此示例展示了如何连接到 PromptLayer 以开始记录您的 ChatOpenAI 请求。

安装 PromptLayer

使用 PromptLayer 和 OpenAI 需要 promptlayer 包。使用 pip 安装 promptlayer
pip install promptlayer

导入

import os

from langchain_community.chat_models import PromptLayerChatOpenAI
from langchain.messages import HumanMessage

设置环境变量 API 密钥

您可以在 www.promptlayer.com 通过单击导航栏中的设置齿轮创建 PromptLayer API 密钥。 将其设置为名为 PROMPTLAYER_API_KEY 的环境变量。
os.environ["PROMPTLAYER_API_KEY"] = "**********"

像使用普通的 PromptLayerOpenAI LLM 一样

您可以选择传入 pl_tags 以使用 PromptLayer 的标记功能跟踪您的请求。
chat = PromptLayerChatOpenAI(pl_tags=["langchain"])
chat([HumanMessage(content="I am a cat and I want")])
AIMessage(content='to take a nap in a cozy spot. I search around for a suitable place and finally settle on a soft cushion on the window sill. I curl up into a ball and close my eyes, relishing the warmth of the sun on my fur. As I drift off to sleep, I can hear the birds chirping outside and feel the gentle breeze blowing through the window. This is the life of a contented cat.', additional_kwargs={})
上述请求现在应该会显示在您的 PromptLayer 控制台中。

使用 PromptLayer 跟踪

如果您想使用任何 PromptLayer 跟踪功能,您需要在实例化 PromptLayer LLM 时传入参数 return_pl_id 以获取请求 ID。
import promptlayer

chat = PromptLayerChatOpenAI(return_pl_id=True)
chat_results = chat.generate([[HumanMessage(content="I am a cat and I want")]])

for res in chat_results.generations:
    pl_request_id = res[0].generation_info["pl_request_id"]
    promptlayer.track.score(request_id=pl_request_id, score=100)
使用此功能,您可以在 PromptLayer 控制台中跟踪模型的性能。如果您正在使用提示模板,您还可以将模板附加到请求。总而言之,这使您有机会在 PromptLayer 控制台中跟踪不同模板和模型的性能。
以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.