提示层聊天OpenAI
此示例展示了如何连接到 PromptLayer 以开始记录您的 ChatOpenAI 请求。
安装 PromptLayer
这promptlayerpackage 才能将 PromptLayer 与 OpenAI 一起使用。安装promptlayer使用 pip.
pip install promptlayer
进口
import os
from langchain_community.chat_models import PromptLayerChatOpenAI
from langchain_core.messages import HumanMessage
API 参考:PromptLayerChatOpenAI | HumanMessage (人工消息)
设置环境 API Key
您可以通过单击导航栏中的设置齿轮在 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 Track
如果要使用任何 PromptLayer 跟踪功能,则需要传递参数return_pl_id实例化 PromptLayer LLM 以获取请求 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 仪表板中跟踪不同模板和模型的性能。