PromptLayer is a platform for prompt engineering. It also helps with the LLM observability to visualize requests, version prompts, and track usage. While PromptLayer does have LLMs that integrate directly with LangChain (e.g. PromptLayerOpenAI), using a callback is the recommended way to integrate PromptLayer with LangChain.
在本指南中,我们将介绍如何设置 PromptLayerCallbackHandler 更多信息请参阅 PromptLayer 文档

安装与设置

pip install -qU  langchain-community promptlayer --upgrade

获取 API 凭证

如果您还没有 PromptLayer 帐号,请在 promptlayer.com 创建一个。然后点击导航栏中的设置齿轮获取 API 密钥,并将其设置为名为 PROMPTLAYER_API_KEY 的环境变量。

用法

开始使用 PromptLayerCallbackHandler 相当简单,它接受两个可选参数:
  1. pl_tags - 可选字符串列表,将在 PromptLayer 中作为标签跟踪。
  2. pl_id_callback - 可选函数,接收 promptlayer_request_id 作为参数。该 ID 可用于 PromptLayer 的所有跟踪功能,用于跟踪元数据、得分和提示使用情况。

简单的 OpenAI 示例

在这个简单示例中,我们将 PromptLayerCallbackHandlerChatOpenAI 一起使用,并添加名为 chatopenai 的 PromptLayer 标签:
import promptlayer  # Don't forget this 🍰
from langchain_community.callbacks.promptlayer_callback import (
    PromptLayerCallbackHandler,
)
from langchain.messages import HumanMessage
from langchain_openai import ChatOpenAI

chat_llm = ChatOpenAI(
    temperature=0,
    callbacks=[PromptLayerCallbackHandler(pl_tags=["chatopenai"])],
)
llm_results = chat_llm.invoke(
    [
        HumanMessage(content="What comes after 1,2,3 ?"),
        HumanMessage(content="Tell me another joke?"),
    ]
)
print(llm_results)

GPT4All 示例

from langchain_community.llms import GPT4All

model = GPT4All(model="./models/gpt4all-model.bin", n_ctx=512, n_threads=8)
callbacks = [PromptLayerCallbackHandler(pl_tags=["langchain", "gpt4all"])]

response = model.invoke(
    "Once upon a time, ",
    config={"callbacks": callbacks},
)

完整示例

在此示例中,我们释放 PromptLayer 更强大的功能。 PromptLayer 允许您以可视化方式创建、版本化和跟踪提示模板。使用 Prompt Registry,我们可以以编程方式获取名为 example 的提示模板。 我们还定义了一个 pl_id_callback 函数,它接收 promptlayer_request_id,记录得分、元数据并关联所使用的提示模板。有关跟踪的更多信息,请参阅我们的文档
from langchain_openai import OpenAI


def pl_id_callback(promptlayer_request_id):
    print("prompt layer id ", promptlayer_request_id)
    promptlayer.track.score(
        request_id=promptlayer_request_id, score=100
    )  # score is an integer 0-100
    promptlayer.track.metadata(
        request_id=promptlayer_request_id, metadata={"foo": "bar"}
    )  # metadata is a dictionary of key value pairs that is tracked on PromptLayer
    promptlayer.track.prompt(
        request_id=promptlayer_request_id,
        prompt_name="example",
        prompt_input_variables={"product": "toasters"},
        version=1,
    )  # link the request to a prompt template


openai_llm = OpenAI(
    model_name="gpt-3.5-turbo-instruct",
    callbacks=[PromptLayerCallbackHandler(pl_id_callback=pl_id_callback)],
)

example_prompt = promptlayer.prompts.get("example", version=1, langchain=True)
openai_llm.invoke(example_prompt.format(product="toasters"))
就是这么简单!设置完成后,所有请求都会显示在 PromptLayer 仪表板上。 该回调同样适用于 LangChain 上实现的任何 LLM。
Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.