Package: langsmith
LangSmith Client SDKThis package contains the Python client for interacting with theLangSmith platform.To install:pipinstall-UlangsmithexportLANGCHAIN_TRACING_V2=trueexportLANGCHAIN_API_KEY=ls_...Then trace:importopenaifromlangsmith.wrappersimportwrap_openaiclient=wrap_openai(openai.Client())client.chat.completions.create(messages=[{"role":"user","content":"Hello, world"}],model="gpt-3.5-turbo")LangSmith helps you and your team develop and evaluate language models and intelligent agents. It is compatible with any LLM application.Cookbook:For tutorials on how to get more value out of LangSmith, check out theLangsmith Cookbookrepo.A typical workflow looks like:Set up an account with LangSmith.Log traces while debugging and prototyping.Run benchmark evaluations and continuously improve with the collected data.We'll walk through these steps in more detail below.1. Connect to LangSmithSign up forLangSmithusing your GitHub, Discord accounts, or an email address and password. If you sign up with an email, make sure to verify your email address before logging in.Then, create a unique API key on theSettings Page, which is found in the menu at the top right corner of the page.Note: Save the API Key in a secure location. It will not be shown again.2. Log TracesYou can log traces natively using the LangSmith SDK or within your LangChain application.Logging Traces with LangChainLangSmith seamlessly integrates with the Python LangChain library to record traces from your LLM applications.Copy the environment variables from the Settings Page and add them to your application.Tracing can be activated by setting the following environment variables or by manually specifying the LangChainTracer.importosos.environ["LANGCHAIN_TRACING_V2"]="true"os.environ["LANGCHAIN_ENDPOINT"]="https://api.smith.langchain.com"os.environ["LANGCHAIN_API_KEY"]="<YOUR-LANGSMITH-API-KEY>"# os.environ["LANGCHAIN_PROJECT"] = "My Project Name" # Optional: "default" is used if not setTip:Projects are groups of traces. All runs are logged to a project. If not specified, the project is set todefault.Run an Agent, Chain, or Language Model in LangChainIf the environment variables are correctly set, your application will automatically connect to the LangSmith platform.fromlangchain_core.runnablesimportchain@chaindefadd_val(x:dict)->dict:return{"val":x["val"]+1}add_val({"val":1})Logging Traces Outside LangChainYou can still use the LangSmith development platform without depending on any
LangChain code.Copy the environment variables from the Settings Page and add them to your application.importosos.environ["LANGCHAIN_ENDPOINT"]="https://api.smith.langchain.com"os.environ["LANGCHAIN_API_KEY"]="<YOUR-LANGSMITH-API-KEY>"# os.environ["LANGCHAIN_PROJECT"] = "My Project Name" # Optional: "default" is used if not setLog tracesThe easiest way to log traces using the SDK is via the@traceabledecorator. Below is an example.fromdatetimeimportdatetimefromtypingimportList,Optional,Tupleimportopenaifromlangsmithimporttraceablefromlangsmith.wrappersimportwrap_openaiclient=wrap_openai(openai.Client())@traceabledefargument_generator(query:str,additional_description:str="")->str:returnclient.chat.completions.create([{"role":"system","content":"You are a debater making an argument on a topic."f"{additional_description}"f" The current time is{datetime.now()}"},{"role":"user","content":f"The discussion topic is{query}"}]).choices[0].message.content@traceabledefargument_chain(query:str,additional_description:str="")->str:argument=argument_generator(query,additional_description)# ... Do other processing or call other functions...returnargumentargument_chain("Why is blue better than orange?")Alternatively, you can manually log events using theClientdirectly or using aRunTree, which is what the traceable decorator is meant to manage for you!A RunTree tracks your application. Each RunTree object is required to have anameandrun_type. These and other important attributes are as follows:name:str- used to identify the component's purposerun_type:str- Currently one of "llm", "chain" or "tool"; more options will be added in the futureinputs:dict- the inputs to the componentoutputs:Optional[dict]- the (optional) returned values from the componenterror:Optional[str]- Any error messages that may have arisen during the callfromlangsmith.run_treesimportRunTreeparent_run=RunTree(name="My Chat Bot",run_type="chain",inputs={"text":"Summarize this morning's meetings."},# project_name= "Defaults to the LANGCHAIN_PROJECT env var")parent_run.post()# .. My Chat Bot calls an LLMchild_llm_run=parent_run.create_child(name="My Proprietary LLM",run_type="llm",inputs={"prompts":["You are an AI Assistant. The time is XYZ."" Summarize this morning's meetings."]},)child_llm_run.post()child_llm_run.end(outputs={"generations":["I should use the transcript_loader tool"" to fetch meeting_transcripts from XYZ"]})child_llm_run.patch()# ..  My Chat Bot takes the LLM output and calls# a tool / function for fetching transcripts ..child_tool_run=parent_run.create_child(name="transcript_loader",run_type="tool",inputs={"date":"XYZ","content_type":"meeting_transcripts"},)child_tool_run.post()# The tool returns meeting notes to the chat botchild_tool_run.end(outputs={"meetings":["Meeting1 notes.."]})child_tool_run.patch()child_chain_run=parent_run.create_child(name="Unreliable Component",run_type="tool",inputs={"input":"Summarize these notes..."},)child_chain_run.post()try:# .... the component does workraiseValueError("Something went wrong")child_chain_run.end(outputs={"output":"foo"}child_chain_run.patch()exceptExceptionase:child_chain_run.end(error=f"I errored again{e}")child_chain_run.patch()pass# .. The chat agent recoversparent_run.end(outputs={"output":["The meeting notes are as follows:..."]})res=parent_run.patch()res.result()Create a Dataset from Existing RunsOnce your runs are stored in LangSmith, you can convert them into a dataset.
For this example, we will do so using the Client, but you can also do this using
the web interface, as explained in theLangSmith docs.fromlangsmithimportClientclient=Client()dataset_name="Example Dataset"# We will only use examples from the top level AgentExecutor run here,# and exclude runs that errored.runs=client.list_runs(project_name="my_project",execution_order=1,error=False,)dataset=client.create_dataset(dataset_name,description="An example dataset")forruninruns:client.create_example(inputs=run.inputs,outputs=run.outputs,dataset_id=dataset.id,)Evaluating RunsCheck out theLangSmith Testing & Evaluation dosfor up-to-date workflows.For generating automated feedback on individual runs, you can run evaluations directly using the LangSmith client.fromtypingimportOptionalfromlangsmith.evaluationimportStringEvaluatordefjaccard_chars(output:str,answer:str)->float:"""Naive Jaccard similarity between two strings."""prediction_chars=set(output.strip().lower())answer_chars=set(answer.strip().lower())intersection=prediction_chars.intersection(answer_chars)union=prediction_chars.union(answer_chars)returnlen(intersection)/len(union)defgrader(run_input:str,run_output:str,answer:Optional[str])->dict:"""Compute the score and/or label for this run."""ifanswerisNone:value="AMBIGUOUS"score=0.5else:score=jaccard_chars(run_output,answer)value="CORRECT"ifscore>0.9else"INCORRECT"returndict(score=score,value=value)evaluator=StringEvaluator(evaluation_name="Jaccard",grading_function=grader)runs=client.list_runs(project_name="my_project",execution_order=1,error=False,)forruninruns:client.evaluate_run(run,evaluator)IntegrationsLangSmith easily integrates with your favorite LLM framework.OpenAI SDKWe provide a convenient wrapper for theOpenAI SDK.In order to use, you first need to set your LangSmith API key.exportLANGCHAIN_API_KEY=<your-api-key>Next, you will need to install the LangSmith SDK:pipinstall-UlangsmithAfter that, you can wrap the OpenAI client:fromopenaiimportOpenAIfromlangsmithimportwrappersclient=wrappers.wrap_openai(OpenAI())Now, you can use the OpenAI client as you normally would, but now everything is logged to LangSmith!client.chat.completions.create(model="gpt-4",messages=[{"role":"user","content":"Say this is a test"}],)Oftentimes, you use the OpenAI client inside of other functions.
You can get nested traces by using this wrapped client and decorating those functions with@traceable.
Seethis documentationfor more documentation how to use this decoratorfromlangsmithimporttraceable@traceable(name="Call OpenAI")defmy_function(text:str):returnclient.chat.completions.create(model="gpt-4",messages=[{"role":"user","content":f"Say{text}"}],)my_function("hello world")InstructorWe provide a convenient integration withInstructor, largely by virtue of it essentially just using the OpenAI SDK.In order to use, you first need to set your LangSmith API key.exportLANGCHAIN_API_KEY=<your-api-key>Next, you will need to install the LangSmith SDK:pipinstall-UlangsmithAfter that, you can wrap the OpenAI client:fromopenaiimportOpenAIfromlangsmithimportwrappersclient=wrappers.wrap_openai(OpenAI())After this, you can patch the OpenAI client usinginstructor:importinstructorclient=instructor.patch(OpenAI())Now, you can useinstructoras you normally would, but now everything is logged to LangSmith!frompydanticimportBaseModelclassUserDetail(BaseModel):name:strage:intuser=client.chat.completions.create(model="gpt-3.5-turbo",response_model=UserDetail,messages=[{"role":"user","content":"Extract Jason is 25 years old"},])Oftentimes, you useinstructorinside of other functions.
You can get nested traces by using this wrapped client and decorating those functions with@traceable.
Seethis documentationfor more documentation how to use this decorator@traceable()defmy_function(text:str)->UserDetail:returnclient.chat.completions.create(model="gpt-3.5-turbo",response_model=UserDetail,messages=[{"role":"user","content":f"Extract{text}"},])my_function("Jason is 25 years old")Additional DocumentationTo learn more about the LangSmith platform, check out thedocs.