from langchain_openai import ChatOpenAIfrom langchain_core.prompts import ChatPromptTemplatefrom langchain_core.output_parsers import StrOutputParserprompt = ChatPromptTemplate.from_messages([ ("system", "You are a helpful assistant. Please respond to the user's request only based on the given context."), ("user", "Question: {question}\nContext: {context}")])model = ChatOpenAI(model="gpt-5.4-mini")output_parser = StrOutputParser()chain = prompt | model | output_parserquestion = "Can you summarize this morning's meetings?"context = "During this morning's meeting, we solved all world conflict."chain.invoke({"question": question, "context": context})
# 你可以选择特定的调用进行追踪...import langsmith as lswith ls.tracing_context(enabled=True): chain.invoke({"question": "Am I using a callback?", "context": "I'm using a callback"})# 这将不会被追踪(假设未设置 LANGSMITH_TRACING)chain.invoke({"question": "Am I being traced?", "context": "I'm not being traced"})# 即使 LANGSMITH_TRACING=true,这也不会被追踪with ls.tracing_context(enabled=False): chain.invoke({"question": "Am I being traced?", "context": "I'm not being traced"})
# 你可以使用 project_name 参数设置项目名称。import langsmith as lswith ls.tracing_context(project_name="My Project", enabled=True): chain.invoke({"question": "Am I using a context manager?", "context": "I'm using a context manager"})
# 在 LangChain 中进行追踪时,运行名称默认为被追踪对象的类名(例如 'ChatOpenAI')。configured_chain = chain.with_config({"run_name": "MyCustomChain"})configured_chain.invoke({"input": "What is the meaning of life?"})# 你也可以在调用时配置运行名称,如下所示chain.invoke({"input": "What is the meaning of life?"}, {"run_name": "MyCustomChain"})
import uuidfrom langchain_openai import ChatOpenAIfrom langchain_core.prompts import ChatPromptTemplatefrom langchain_core.output_parsers import StrOutputParserprompt = ChatPromptTemplate.from_messages([ ("system", "You are a helpful assistant. Please respond to the user's request only based on the given context."), ("user", "Question: {question}\n\nContext: {context}")])model = ChatOpenAI(model="gpt-5.4-mini")output_parser = StrOutputParser()chain = prompt | model | output_parserquestion = "Can you summarize this morning's meetings?"context = "During this morning's meeting, we solved all world conflict."my_uuid = uuid.uuid4()result = chain.invoke({"question": question, "context": context}, {"run_id": my_uuid})print(my_uuid)
from langchain_openai import ChatOpenAIfrom langchain_core.prompts import ChatPromptTemplatefrom langchain_core.output_parsers import StrOutputParserfrom langsmith import traceableprompt = ChatPromptTemplate.from_messages([ ("system", "You are a helpful assistant. Please respond to the user's request only based on the given context."), ("user", "Question: {question}\nContext: {context}")])model = ChatOpenAI(model="gpt-5.4-mini")output_parser = StrOutputParser()chain = prompt | model | output_parser# 上面的链将作为 traceable 函数的子运行被追踪@traceable( tags=["openai", "chat"], metadata={"foo": "bar"})def invoke_runnnable(question, context): result = chain.invoke({"question": question, "context": context}) return "The response is: " + resultinvoke_runnnable("Can you summarize this morning's meetings?", "During this morning's meeting, we solved all world conflict.")