跳到主要內容

PromptLayerChatOpenAI

此範例展示如何連線至 PromptLayer 以開始記錄您的 ChatOpenAI 請求。

安裝 PromptLayer

使用 OpenAI 的 PromptLayer 需要 promptlayer 套件。使用 pip 安裝 promptlayer

pip install promptlayer

匯入

import os

from langchain_community.chat_models import PromptLayerChatOpenAI
from langchain_core.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 Track

如果您想使用任何 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 儀表板中追蹤不同範本和模型的效能。


此頁面是否有幫助?