from openai import OpenAIfrom langsmith.wrappers import wrap_openaiclient = wrap_openai(OpenAI())docs = [ "Acme Cloud supports unlimited users on Enterprise plans. Starter plans are limited to 5 users.", "To reset your password, click 'Forgot password' on the login page and follow the instructions sent to your email.", "API rate limits are 1,000 requests per hour on the Starter plan and 10,000 requests per hour on Enterprise.",]def retriever(query: str) -> list[str]: return docsdef support_bot(question: str) -> str: context = retriever(question) system_message = ( "You are a helpful customer support agent. " "Answer using only the information provided below:\n\n" + "\n".join(context) ) response = client.chat.completions.create( model="gpt-5.4-mini", messages=[ {"role": "system", "content": system_message}, {"role": "user", "content": question}, ], ) return response.choices[0].message.contentif __name__ == "__main__": print(support_bot("How many users can I have on the Starter plan?"))
调用 support_bot("How many users can I have on the Starter plan?") 会生成OpenAI调用的追踪。
from openai import OpenAIfrom langsmith import traceablefrom langsmith.wrappers import wrap_openaiclient = wrap_openai(OpenAI())docs = [ "Acme Cloud supports unlimited users on Enterprise plans. Starter plans are limited to 5 users.", "To reset your password, click 'Forgot password' on the login page and follow the instructions sent to your email.", "API rate limits are 1,000 requests per hour on the Starter plan and 10,000 requests per hour on Enterprise.",]def retriever(query: str) -> list[str]: return docs@traceabledef support_bot(question: str) -> str: context = retriever(question) system_message = ( "You are a helpful customer support agent. " "Answer using only the information provided below:\n\n" + "\n".join(context) ) response = client.chat.completions.create( model="gpt-5.4-mini", messages=[ {"role": "system", "content": system_message}, {"role": "user", "content": question}, ], ) return response.choices[0].message.contentif __name__ == "__main__": print(support_bot("How many users can I have on the Starter plan?"))
调用 support_bot("How many users can I have on the Starter plan?") 现在会生成完整RAG管道的追踪。
from openai import OpenAIfrom langsmith import traceable, Client, uuid7 from langsmith.wrappers import wrap_openaiclient = wrap_openai(OpenAI())docs = [ "Acme Cloud supports unlimited users on Enterprise plans. Starter plans are limited to 5 users.", "To reset your password, click 'Forgot password' on the login page and follow the instructions sent to your email.", "API rate limits are 1,000 requests per hour on the Starter plan and 10,000 requests per hour on Enterprise.",]def retriever(query: str) -> list[str]: return docs@traceabledef support_bot(question: str) -> str: context = retriever(question) system_message = ( "You are a helpful customer support agent. " "Answer using only the information provided below:\n\n" + "\n".join(context) ) response = client.chat.completions.create( model="gpt-5.4-mini", messages=[ {"role": "system", "content": system_message}, {"role": "user", "content": question}, ], ) return response.choices[0].message.contentif __name__ == "__main__": run_id = str(uuid7()) support_bot( "How many users can I have on the Starter plan?", langsmith_extra={"run_id": run_id}, ) ls_client = Client() ls_client.create_feedback(run_id, key="user-score", score=1.0)
from openai import OpenAIfrom langsmith import traceablefrom langsmith.wrappers import wrap_openaiclient = wrap_openai(OpenAI())docs = [ "Acme Cloud supports unlimited users on Enterprise plans. Starter plans are limited to 5 users.", "To reset your password, click 'Forgot password' on the login page and follow the instructions sent to your email.", "API rate limits are 1,000 requests per hour on the Starter plan and 10,000 requests per hour on Enterprise.",]@traceable(run_type="retriever")def retriever(query: str) -> list[str]: return docs@traceable(metadata={"llm": "gpt-5.4-mini"})def support_bot(question: str) -> str: context = retriever(question) system_message = ( "You are a helpful customer support agent. " "Answer using only the information provided below:\n\n" + "\n".join(context) ) response = client.chat.completions.create( model="gpt-5.4-mini", messages=[ {"role": "system", "content": system_message}, {"role": "user", "content": question}, ], ) return response.choices[0].message.contentif __name__ == "__main__": support_bot("How many users can I have on the Starter plan?")