跳到主要內容
Open In ColabOpen on GitHub

PromptLayerChatOpenAI

這個範例展示如何連接到 PromptLayer 以開始記錄您的 ChatOpenAI 請求。

安裝 PromptLayer

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

pip install promptlayer

導入

import os

from langchain_community.chat_models import PromptLayerChatOpenAI
from langchain_core.messages import HumanMessage
API 參考文檔:PromptLayerChatOpenAI | 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 儀表板中追蹤不同範本和模型的效能。


此頁面是否對您有幫助?