langchain/docs/extras/integrations/providers/agent_with_wandb_tracing.ipynb

186 lines
126 KiB
Plaintext
Raw Normal View History

{
"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",
2023-06-01 21:21:15 +00:00
"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": []
},
2023-06-01 21:21:15 +00:00
"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"
]
},
{
"cell_type": "markdown",
"id": "438fd64d",
"metadata": {},
"source": [
"**Here's a view of wandb dashboard for the above tracing session:**\n",
"\n",
"\n",
"![](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA8IAAAGUCAYAAAD+o5tuAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABmJLR0QA/wD/AP+gvaeTAAAAB3RJTUUH5wUSBx0fgk7mkgAAgABJREFUeNrs/XeQHmd+4Hl+07ze1lvee1TBO1rQN9lkd0ttZEd2NDrN7MXd6iLuYk47iovQzkTsbezO3sxGzMzdzKxGUktqqVvdLTa76QGS8N4VqgCU996+9Xqb5v6oAgiAoAEIAiDr9+lAs960Tz6Zb+b7y8cp+XzeRgghhBBCCCGE2CDUB50AIYQQQgghhBDifpJAWAghhBBCCCHEhiKBsBBCCCGEEEKIDUUCYSGEEEIIIYQQG4oEwkIIIYQQQgghNhQJhIUQQgghhBBCbCgSCAshhBBCCCGE2FD05eXlB50GIYQQQgghhBDivlFSqZT9oBMhhBBCCCGEEELcL7rD4XjQaRBCCCGEEEIIIe4baSMshBBCCCGEEGJD0R90AoQQQgghhBBC3B3btlEUBUVRHnRSHsixXzv+OyWBsBBCCCGEEEJ8SSmKgmmaJJNJDMN40Mm5r8ft9/txuVx3FQwr+XxeOssSQgghhBBCiC8h27aZnZ0lk8ngdDqx7a9+eHct+Lcsi/r6elwu1x1vQ0qEhRBCCCGEEOJLyrIsYrEYHR0dbKSOkBVFYWRkhFQqhdvtvuMXABIICyGEEEIIIcSX1I1thC3L2hBthW3bRlVVVF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FbLy8sUCoW7ij/v
"\n"
]
}
],
"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
}