langchain/docs/extras/integrations/providers/wandb_tracing.ipynb
Bagatur b485c3048b
rm base64 images from docs (#10110)
Causing problems indexing docs and notebook images don't render after markdown conversion anyways
2023-09-01 15:15:12 -07:00

174 lines
5.1 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "5371a9bb",
"metadata": {},
"source": [
"# WandB Tracing\n",
"\n",
"There are two recommended ways to trace your LangChains:\n",
"\n",
"1. Setting the `LANGCHAIN_WANDB_TRACING` environment variable to \"true\".\n",
"1. Using a context manager with tracing_enabled() to trace a particular block of code.\n",
"\n",
"**Note** if the environment variable is set, all code will be traced, regardless of whether or not it's within the context manager."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "17c04cc6-c93d-4b6c-a033-e897577f4ed1",
"metadata": {
"ExecuteTime": {
"end_time": "2023-05-18T12:47:46.580776Z",
"start_time": "2023-05-18T12:47:46.577833Z"
},
"tags": []
},
"outputs": [],
"source": [
"import os\n",
"\n",
"os.environ[\"LANGCHAIN_WANDB_TRACING\"] = \"true\"\n",
"\n",
"# wandb documentation to configure wandb using env variables\n",
"# https://docs.wandb.ai/guides/track/advanced/environment-variables\n",
"# here we are configuring the wandb project name\n",
"os.environ[\"WANDB_PROJECT\"] = \"langchain-tracing\"\n",
"\n",
"from langchain.agents import initialize_agent, load_tools\n",
"from langchain.agents import AgentType\n",
"from langchain.llms import OpenAI\n",
"from langchain.callbacks import wandb_tracing_enabled"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "1b62cd48",
"metadata": {
"ExecuteTime": {
"end_time": "2023-05-18T12:47:47.445229Z",
"start_time": "2023-05-18T12:47:47.436424Z"
},
"tags": []
},
"outputs": [],
"source": [
"# Agent run with tracing. Ensure that OPENAI_API_KEY is set appropriately to run this example.\n",
"\n",
"llm = OpenAI(temperature=0)\n",
"tools = load_tools([\"llm-math\"], llm=llm)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bfa16b79-aa4b-4d41-a067-70d1f593f667",
"metadata": {
"ExecuteTime": {
"end_time": "2023-05-18T12:48:01.816137Z",
"start_time": "2023-05-18T12:47:49.109574Z"
},
"tags": []
},
"outputs": [],
"source": [
"agent = initialize_agent(\n",
" tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True\n",
")\n",
"\n",
"agent.run(\"What is 2 raised to .123243 power?\") # this should be traced\n",
"# A url with for the trace sesion like the following should print in your console:\n",
"# https://wandb.ai/<wandb_entity>/<wandb_project>/runs/<run_id>\n",
"# The url can be used to view the trace session in wandb."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "fe833c33-033f-4806-be0c-cc3d147db13d",
"metadata": {
"ExecuteTime": {
"end_time": "2023-05-18T12:48:25.909223Z",
"start_time": "2023-05-18T12:48:09.657895Z"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"\u001b[32;1m\u001b[1;3m I need to use a calculator to solve this.\n",
"Action: Calculator\n",
"Action Input: 5^.123243\u001b[0m\n",
"Observation: \u001b[36;1m\u001b[1;3mAnswer: 1.2193914912400514\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer.\n",
"Final Answer: 1.2193914912400514\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
"\n",
"\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"\u001b[32;1m\u001b[1;3m I need to use a calculator to solve this.\n",
"Action: Calculator\n",
"Action Input: 2^.123243\u001b[0m\n",
"Observation: \u001b[36;1m\u001b[1;3mAnswer: 1.0891804557407723\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer.\n",
"Final Answer: 1.0891804557407723\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"'1.0891804557407723'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Now, we unset the environment variable and use a context manager.\n",
"if \"LANGCHAIN_WANDB_TRACING\" in os.environ:\n",
" del os.environ[\"LANGCHAIN_WANDB_TRACING\"]\n",
"\n",
"# enable tracing using a context manager\n",
"with wandb_tracing_enabled():\n",
" agent.run(\"What is 5 raised to .123243 power?\") # this should be traced\n",
"\n",
"agent.run(\"What is 2 raised to .123243 power?\") # this should not be traced"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}