Skip to main content

Documentation Index

Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt

Use this file to discover all available pages before exploring further.

Python/TypeScript 中的 wrap_openai/wrapOpenAI 方法允许你包装你的 OpenAI 客户端,以便自动记录追踪——无需装饰器或函数包装!使用包装器可确保消息(包括工具调用和多模态内容块)在 LangSmith 中渲染良好。另外请注意,该包装器与 @traceable 装饰器或 traceable 函数无缝配合,你可以在同一个应用程序中同时使用两者。
必须将 LANGSMITH_TRACING 环境变量设置为 'true',才能将追踪记录到 LangSmith,即使在使用 wrap_openaiwrapOpenAI 时也是如此。这允许你在不更改代码的情况下开启和关闭追踪。此外,你需要将 LANGSMITH_API_KEY 环境变量设置为你自己的 API 密钥(有关更多信息,请参阅 设置)。如果你的 LangSmith API 密钥链接到多个工作区,请设置 LANGSMITH_WORKSPACE_ID 环境变量以指定要使用哪个工作区。默认情况下,追踪将记录到名为 default 的项目中。要将追踪记录到不同的项目,请参阅 将追踪记录到特定项目
import openai
from langsmith import traceable
from langsmith.wrappers import wrap_openai

client = wrap_openai(openai.Client())

@traceable(run_type="tool", name="Retrieve Context")
def my_tool(question: str) -> str:
  return "During this morning's meeting, we solved all world conflict."

@traceable(name="Chat Pipeline")
def chat_pipeline(question: str):
  context = my_tool(question)
  messages = [
      { "role": "system", "content": "You are a helpful assistant. Please respond to the user's request only based on the given context." },
      { "role": "user", "content": f"Question: {question}\nContext: {context}"}
  ]
  chat_completion = client.chat.completions.create(
      model="gpt-5.2", messages=messages
  )
  return chat_completion.choices[0].message.content

chat_pipeline("Can you summarize this morning's meetings?")