이 예제는 ChatOpenAI 요청 기록을 시작하기 위해 PromptLayer에 연결하는 방법을 보여줍니다.

PromptLayer 설치

OpenAI와 함께 PromptLayer를 사용하려면 promptlayer 패키지가 필요합니다. pip을 사용하여 promptlayer를 설치하세요.
pip install promptlayer

Imports

import os

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

Environment API Key 설정

www.promptlayer.com에서 네비게이션 바의 설정 톱니바퀴를 클릭하여 PromptLayer API Key를 생성할 수 있습니다. 이를 PROMPTLAYER_API_KEY라는 환경 변수로 설정하세요.
os.environ["PROMPTLAYER_API_KEY"] = "**********"

PromptLayerOpenAI LLM을 일반적인 방법으로 사용

PromptLayer의 태깅 기능으로 요청을 추적하기 위해 선택적으로 pl_tags를 전달할 수 있습니다.
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 추적 기능 중 하나를 사용하려면, request id를 얻기 위해 PromptLayer LLM을 인스턴스화할 때 return_pl_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 대시보드에서 모델의 성능을 추적할 수 있습니다. prompt template을 사용하는 경우, 요청에 template을 첨부할 수도 있습니다. 전반적으로, 이를 통해 PromptLayer 대시보드에서 다양한 template과 model의 성능을 추적할 수 있는 기회를 얻게 됩니다.
Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.
I