From d5ce749716e73707d07634094c85d118de5cdfaa Mon Sep 17 00:00:00 2001 From: marcodel13 Date: Fri, 17 May 2024 08:56:18 +0200 Subject: [PATCH] nb on how to use deterministic functions --- .../agents_with_deterministic_functions.ipynb | 624 ++++++++++++++++++ 1 file changed, 624 insertions(+) create mode 100644 notebooks/agents_with_deterministic_functions.ipynb diff --git a/notebooks/agents_with_deterministic_functions.ipynb b/notebooks/agents_with_deterministic_functions.ipynb new file mode 100644 index 00000000..6fa7b94d --- /dev/null +++ b/notebooks/agents_with_deterministic_functions.ipynb @@ -0,0 +1,624 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![Cohere-Logo-Color-RGB.png](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAuMAAADwCAYAAACqoGq4AAAACXBIWXMAAC4jAAAuIwF4pT92AAAgAElEQVR4nO3dvVYbydbG8Z53TY5jEnMSUmsSpWYihWaU4cT4CqzJnBlnZANXYJFApoFQkUWqZERKclBCeqwr8LsK757pYemzPnrvav1/a5HMOYZWq7vqqd3VVT99//69AAAAAFC//+OcAwAAADoI4wAAAIASwjgAAACghDAOAAAAKCGMAwAAAEoI4wAAAIASwjgAAACghDAOAAAAKCGMAwAAAEoI4wAAAIASwjgAAACghDAOAAAAKCGMAwAAAEoI4wAAAIASwjgAAACghDAOAAAAKCGMAwAAAEoI4wAAAIASwjgAAACghDAOAAAAKCGMAwAAAEoI4wAAAIASwjgAAACghDAOAAAAKCGMAwAAAEoI4wAAAIASwjgAAACghDAOAAAAKCGMAwAAAEoI4wAAAIASwjgAAACghDAOAAAAKCGMAwAAAEoI4wAAAIASwjgAAACghDAOAAAAKCGMAwAAAEoI4wAAAIASwjgAAACghDAOAAAAKCGMAwAAAEoI4wAAAIASwjgAAACghDAOAAAAKCGMAwAAAEoI4wAAAIASwjgAAACghDAOAAAAKCGMAwAAAEoI4wAAAIASwjgAAACghDAOAAAAKCGMAwAAAEoI4wAAAIASwjgAAACghDAOAAAAK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AAAASgjjAAAAgBLCOAAAAKCEMA4AAAAoIYwDAAAASgjjAAAAgBLCOAAAAKCEMA4AAAAoIYwDAAAASgjjAAAAgBLCOAAAAKCEMA4AAAAoIYwDAAAASgjjAAAAgBLCOAAAAKChKIr/B9RSVF1ZF/7hAAAAAElFTkSuQmCC)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "source": [ + "# Notebook Overview\n", + "\n", + "## Motivation\n", + "Agents can play a crucial role in different enterprise scenarios. One such example is creating a request to fetch results from an existing enterprise API with specific requirements. For example, given a user query:\n", + "\n", + "```Retrieve id 7410e652-639d-402e-984e-8fd7025f0aac 8bb21b93-2ddf-4a63-af63-ddb6b1be49a1, ref GLPNT0005GUJOGGE GLSBR000BGASOBRE, nmg 0000234GLCHL0200ARTINDIUS'```\n", + "\n", + "The expected API request is:\n", + "\n", + "```[{\"uuid\":[\"7410e652-639d-402e-984e-8fd7025f0aac\",\"8bb21b93-2ddf-4a63-af63-ddb6b1be49a1\"],\"page\":0,\"size\":10}, {\"objref\":[\"GLPNT0005GUJOGGE\",\"GLSBR000BGASOBRE\"],\"page\":0,\"size\":10},{\"nmgs\":[\"0000234GLCHL0200ARTINDIUS\"],\"page\":0,\"size\":10}]```\n", + "\n", + "This kind of task poses a major challenge, namely, the model must be very precise at identifying the codes in the query, based on their alphanumeric patterns, and matching these codes with the right parameter - for example, the code ```GLSBR000BGASOBRE``` needs to map to\n", + "```objref```. The task is even more challenging when multiple APIs are available, and the model has to deal with a plethora of parameters.\n", + "\n", + "## Solution\n", + "In this notebook, we propose a simple but effective solution to the challenge defined above: We create a [Langchain ReAct Agent](https://github.com/langchain-ai/langchain-cohere/blob/main/libs/cohere/langchain_cohere/cohere_agent.py) that has access to a deterministic tool that extracts the alphanumeric patterns in the query, and returns a dictionary in which the keys are the parameters, and the values the extracted patterns. The output of the tool is then used to generate the final request.\n", + "\n", + "Using such a tool is just one of the possibilities that the Agent has to generate the query: As we will see below, when a more semantic understanding of the query is required, we can ignore the tool and leverage the linguistic capabilities of the LLM powering the Agent to generate the final output.\n", + "\n", + "With this approach, we bring together the best of two worlds: on the one hand, the ability of LLMs to use tool and generate outputs, on the other one, the reliability and efficiency of deterministic functions.\n", + "\n", + "## Table of Contents\n", + "\n", + "- [Step 1: Setup](#sec_step1)\n", + "- [Step 2: Define the Tool and the Agent](#sec_step2)\n", + "- [Step 3: Run the Agent](#sec_step3)\n", + "- [Conclusions](#sec_conclusion)\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "source": [ + "\n", + "# Step 1: Setup" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [], + "source": [ + "####################################################################################################\n", + "#\n", + "# Uncomment if you need to install the following packages\n", + "#\n", + "####################################################################################################\n", + "\n", + "# !pip install cohere\n", + "# !pip install python-dotenv\n", + "# !pip install pandas\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [], + "source": [ + "import os\n", + "import json\n", + "import re\n", + "import getpass\n", + "from langchain.agents import AgentExecutor\n", + "from langchain_cohere.chat_models import ChatCohere\n", + "from langchain_cohere.react_multi_hop.agent import create_cohere_react_agent\n", + "from langchain_core.prompts import ChatPromptTemplate\n", + "from langchain_core.pydantic_v1 import BaseModel, Field\n", + "from langchain_core.tools import tool" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [], + "source": [ + "# load the cohere api key\n", + "os.environ[\"COHERE_API_KEY\"] = getpass.getpass()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "source": [ + "\n", + "# Step 2: Define the Tool and the Agent\n", + "Here we create a tool which implements the deterministic function to extract alphanumeric strings from the user's query and match them to the right parameter." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [], + "source": [ + "@tool\n", + "def regex_extractor(user_query: str) -> dict:\n", + " \"\"\"Function which, given the query from the user, returns a dictionary parameter:value.\"\"\"\n", + "\n", + " uuid = re.findall(\"\\s([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})\", user_query)\n", + " nmgs = re.findall(\"(0000[A-Z0-9]{21})\", user_query)\n", + " objref = re.findall(\"\\s([A-Z]{5,9}\\d{3,4}[A-Z]{3,8})\", user_query)\n", + " urn = re.findall(\"urn:[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}\",user_query,)\n", + " d = {\"uuid\": uuid,\n", + " \"nmgs\": nmgs,\n", + " \"objref\": objref,\n", + " \"urn\": urn}\n", + " d_filtered = {k: v for k, v in d.items() if v != []}\n", + " return d_filtered\n", + "\n", + "\n", + "class extract_code_v1productssearch(BaseModel):\n", + " user_query: str = Field(\n", + " description=\"This is the full input query as received from the user. Do not truncate or modify the query in any way.\"\n", + " )\n", + "\n", + "regex_extractor.name = \"regex_extractor\"\n", + "regex_extractor.args_schema = extract_code_v1productssearch\n", + "tools=[regex_extractor]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [], + "source": [ + "# Let's define the preamble for the Agent.\n", + "# The preamble includes info about:\n", + "# - the tool the Agent has access to\n", + "# - the cases in which the Agent has to produce an output without using the tool\n", + "# - some examples to clarify the task\n", + "preamble = \"\"\"You are an assistant that given a user's query about, generates a request an API.\n", + " You can use the tool named \"regex_extractor\".\n", + " Pass to the \"regex_extractor\" tool the entire text of the the input query.\n", + " The tool returns a dictionary, in which keys are the name of the codes, and values a list of extracted codes.\n", + " Create a JSON for each of the key-value pairs in the dictionary.\n", + "\n", + " Return a list of JSONs. Make sure each JSON is properly defined. Do not return any other explanation or comment.\n", + " You MUST generate a perfect JSON object: make sure that each string in the lists is included between quotes.\n", + "\n", + " If the request mentions one of the tags listed below, or a related word, create a dictionary in which the key is \"taxonomies\" and the value the list of capitalized tags.\n", + " Tags list: cars, trucks, clothes, sport\n", + "\n", + "\n", + " Find a list of examples here:\n", + " User question | parameter for the tool | what you should understand\n", + " Look products GLCMS004AGTCAMIS; 0000234GLCMS0100ANORAKCAA, GLCHL000CGUCHALE | Look products GLCMS004AGTCAMIS; 0000234GLCMS0100ANORAKCAA, GLCHL000CGUCHALE | [{\"objref\":[\"GLCMS004AGTCAMIS\",\"GLCHL000CGUCHALE\"]},{\"nmgs\":[\"0000234GLCMS0100ANORAKCAA\"]}]\n", + " Retrieve id 7410e652-639d-402e-984e-8fd7025f0aac 8bb21b93-2ddf-4a63-af63-ddb6b1be49a1, ref GLPNT0005GUJOGGE GLSBR000BGASOBRE, nmg 0000234GLCHL0200ARTINDIUS | Retrieve id 7410e652-639d-402e-984e-8fd7025f0aac 8bb21b93-2ddf-4a63-af63-ddb6b1be49a1, ref GLPNT0005GUJOGGE GLSBR000BGASOBRE, nmg 0000234GLCHL0200ARTINDIUS | [{\"uuid\":[\"7410e652-639d-402e-984e-8fd7025f0aac\",\"8bb21b93-2ddf-4a63-af63-ddb6b1be49a1\"]}, {\"objref\":[\"GLPNT0005GUJOGGE\",\"GLSBR000BGASOBRE\"]},{\"nmgs\":[\"0000234GLCHL0200ARTINDIUS\"]}]\n", + " Look for items of cars and trucks | Look for items of pants and t-shirts | [{'taxonomies': ['CARS', 'TRUCKS']}]\n", + " Search products sport | Search products dress and jumpsuit | [{'taxonomies': ['SPORT']}]\n", + " \"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [], + "source": [ + "# Define the prompt\n", + "prompt = ChatPromptTemplate.from_template(\"{input}\")\n", + "\n", + "# Define the agent\n", + "llm = ChatCohere(model=\"command-r-plus\", temperature=0)\n", + "\n", + "# instantiate agent and agent executor\n", + "agent = create_cohere_react_agent(\n", + " llm=llm,\n", + " tools=tools,\n", + " prompt=prompt,\n", + ")\n", + "agent_executor = AgentExecutor(agent=agent,\n", + " tools=tools,\n", + " verbose=True,\n", + " return_intermediate_steps=True\n", + " )" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [], + "source": [ + "# finally, let's write a function to convert the Agents output to a json\n", + "def convert_to_json(string: str) -> json:\n", + " return json.loads(\n", + " string.replace(\"\\xa0\", \" \")\n", + " .replace(\"json\", \"\")\n", + " .replace(\"`\", \"\")\n", + " .replace(\"`\", \"\")\n", + " )\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "source": [ + "\n", + "# Step 3: Run the Agent\n", + "Let's now test the Agent we just defined!" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", + "\u001b[32;1m\u001b[1;3m\n", + "I will use the regex_extractor tool to extract the codes from the user query.\n", + "{'tool_name': 'regex_extractor', 'parameters': {'user_query': 'Look for urn:75f2b737-06dd-4399-9206-a6c11b65138e, GLCMS004AGTCAMIS; 0000234GLCMS0100ANORAKCAA, GLCHL000CGUCHALE'}}\n", + "\u001b[0m\u001b[36;1m\u001b[1;3m{'nmgs': ['0000234GLCMS0100ANORAKCAA'], 'objref': ['GLCMS004AGTCAMIS', 'GLCHL000CGUCHALE'], 'urn': ['urn:75f2b737-06dd-4399-9206-a6c11b65138e']}\u001b[0m\u001b[32;1m\u001b[1;3mRelevant Documents: 0\n", + "Cited Documents: 0\n", + "Answer: ```json\n", + "[\n", + "    {\n", + "        \"urn\": [\"urn:75f2b737-06dd-4399-9206-a6c11b65138e\"],\n", + "        \"objref\": [\"GLCMS004AGTCAMIS\", \"GLCHL000CGUCHALE\"],\n", + "        \"nmgs\": [\"0000234GLCMS0100ANORAKCAA\"]\n", + "    }\n", + "]\n", + "```\n", + "Grounded answer: ```json\n", + " [\n", + "    {\n", + "        \"urn\": [\"urn:75f2b737-06dd-4399-9206-a6c11b65138e\"],\n", + "        \"objref\": [\"GLCMS004AGTCAMIS\", \"GLCHL000CGUCHALE\"],\n", + "        \"nmgs\": [\"0000234GLCMS0100ANORAKCAA\"]\n", + "    }\n", + "]\n", + "```\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + } + ], + "source": [ + "query_1 = \"Look for urn:75f2b737-06dd-4399-9206-a6c11b65138e, GLCMS004AGTCAMIS; 0000234GLCMS0100ANORAKCAA, GLCHL000CGUCHALE\"\n", + "response_1 = agent_executor.invoke(\n", + " {\n", + " \"input\": query_1,\n", + " \"preamble\": preamble,\n", + " }\n", + " )" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "source": [ + "In the reasoning chain above, we can see that the Agent uses the tool we provided it to extract the strings in the query.\n", + "The output of the tool is then used to generate the request." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'urn': ['urn:75f2b737-06dd-4399-9206-a6c11b65138e'],\n", + " 'objref': ['GLCMS004AGTCAMIS', 'GLCHL000CGUCHALE'],\n", + " 'nmgs': ['0000234GLCMS0100ANORAKCAA']}]" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# let's have a look at the final output\n", + "convert_to_json(response_1['output'])" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "source": [ + "As mentioned above, the Agent can use the tool when specific alphanumeric patterns have to be extracted from the query; however, it can also generate the output based on its semantic understanding of the query. For example:" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", + "\u001b[32;1m\u001b[1;3m\n", + "I will use the regex_extractor tool to extract the relevant information from the user request.\n", + "{'tool_name': 'regex_extractor', 'parameters': {'user_query': 'I need tennis products'}}\n", + "\u001b[0m\u001b[36;1m\u001b[1;3m{}\u001b[0m\u001b[32;1m\u001b[1;3mRelevant Documents: None\n", + "Cited Documents: None\n", + "Answer: ```json\n", + "[\n", + " {\n", + " \"taxonomies\": [\n", + " \"SPORT\"\n", + " ]\n", + " }\n", + "]\n", + "```\n", + "Grounded answer: ```json\n", + " [\n", + " {\n", + " \"taxonomies\": [\n", + " \"SPORT\"\n", + " ]\n", + " }\n", + "]\n", + "```\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + } + ], + "source": [ + "query_2 = \"I need tennis products\"\n", + "\n", + "response_2 = agent_executor.invoke(\n", + " {\n", + " \"input\": query_2,\n", + " \"preamble\": preamble,\n", + " }\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "source": [ + "The Agent run the tool to check if any target string was in the query, then it generated the request body based on its understanding." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'taxonomies': ['SPORT']}]" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "convert_to_json(response_2['output'])" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "source": [ + "Finally, the two paths to generation - deterministic and semantic - can be applied in parallel by the Agent, as shown below:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", + "\u001b[32;1m\u001b[1;3m\n", + "I will use the regex_extractor tool to extract the codes from the user query. Then, I will create a JSON for each of the key-value pairs in the dictionary.\n", + "{'tool_name': 'regex_extractor', 'parameters': {'user_query': 'Look for GLBRL0000GACHALE, nmg 0000234GLCZD0000GUREDTOAA and car products'}}\n", + "\u001b[0m\u001b[36;1m\u001b[1;3m{'nmgs': ['0000234GLCZD0000GUREDTOAA'], 'objref': ['GLBRL0000GACHALE']}\u001b[0m\u001b[32;1m\u001b[1;3mRelevant Documents: 0\n", + "Cited Documents: 0\n", + "Answer: ```json\n", + "[\n", + "    {\n", + "        \"objref\": [\"GLBRL0000GACHALE\"],\n", + "        \"nmgs\": [\"0000234GLCZD0000GUREDTOAA\"]\n", + "    },\n", + "    {\n", + "        \"taxonomies\": [\"CARS\"]\n", + "    }\n", + "]\n", + "```\n", + "Grounded answer: ```json\n", + " [\n", + "    {\n", + "        \"objref\": [\"GLBRL0000GACHALE\"],\n", + "        \"nmgs\": [\"0000234GLCZD0000GUREDTOAA\"]\n", + "    },\n", + "    {\n", + "        \"taxonomies\": [\"CARS\"]\n", + "    }\n", + "]\n", + "```\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + } + ], + "source": [ + "query_3 = \"Look for GLBRL0000GACHALE, nmg 0000234GLCZD0000GUREDTOAA and car products\"\n", + "\n", + "response_3 = agent_executor.invoke(\n", + " {\n", + " \"input\": query_3,\n", + " \"preamble\": preamble,\n", + " }\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'objref': ['GLBRL0000GACHALE'], 'nmgs': ['0000234GLCZD0000GUREDTOAA']},\n", + " {'taxonomies': ['CARS']}]" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "convert_to_json(response_3['output'])" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "source": [ + "\n", + "# Conclusions\n", + "\n", + "In this notebook we showed how Agents can be used to solve a real-world use case, in which the goal is to create API requests based on the user's query. We did it by providing the Agent with a deterministic tool to extract relevant alphanumeric strings in the query, and matching them to the right parameter name. In parallel, the Agent can leverage the semantic understanding of the query provided by the LLM powering it." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [], + "source": [] + } + ], + "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.10.14" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}