From d3d6c50f9afb9f7a247fdd292aa70a40ea25efed Mon Sep 17 00:00:00 2001 From: Mehak Date: Thu, 16 Jan 2025 15:15:12 +0100 Subject: [PATCH 1/3] tools --- lab-tools-prompting.ipynb | 65 ++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/lab-tools-prompting.ipynb b/lab-tools-prompting.ipynb index 9704130..149e69c 100644 --- a/lab-tools-prompting.ipynb +++ b/lab-tools-prompting.ipynb @@ -42,10 +42,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "8c556c5e-b785-428b-8e7d-efd34a2a1adb", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], "source": [ "%pip install --upgrade --quiet langchain langchain-community" ] @@ -60,7 +68,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 2, "id": "5efb4170-b95b-4d29-8f57-09509f3ba6df", "metadata": {}, "outputs": [], @@ -89,10 +97,19 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 3, "id": "424be968-2806-4d1a-a6aa-5499ae20fac5", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\mehak\\AppData\\Local\\Temp\\ipykernel_11292\\1427064109.py:3: LangChainDeprecationWarning: The class `Ollama` was deprecated in LangChain 0.3.1 and will be removed in 1.0.0. An updated version of the class exists in the :class:`~langchain-ollama package and should be used instead. To use it run `pip install -U :class:`~langchain-ollama` and import as `from :class:`~langchain_ollama import OllamaLLM``.\n", + " model = Ollama(model=\"phi3\")\n" + ] + } + ], "source": [ "from langchain_community.llms import Ollama\n", "\n", @@ -213,7 +230,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 7, "id": "f02f1dce-76e7-4ca9-9bac-5af496131fe1", "metadata": {}, "outputs": [], @@ -238,7 +255,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 8, "id": "f8623e03-60eb-4439-b57b-ecbcebc61b58", "metadata": {}, "outputs": [ @@ -247,11 +264,11 @@ "output_type": "stream", "text": [ "{\n", - " \"name\": \"add\",\n", - " \"arguments\": {\n", - " \"x\": 3,\n", - " \"y\": 1132\n", - " }\n", + " \"name\": \"add\",\n", + " \"arguments\": {\n", + " \"x\": 3,\n", + " \"y\": 1132\n", + " }\n", "}\n" ] } @@ -280,17 +297,17 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 9, "id": "f129f5bd-127c-4c95-8f34-8f437da7ca8f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "{'name': 'multiply', 'arguments': {'x': 13.0, 'y': 4.0}}" + "{'name': 'multiply', 'arguments': {'x': 13, 'y': 4}}" ] }, - "execution_count": 19, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -330,7 +347,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 10, "id": "faee95e0-4095-4310-991f-9e9465c6738e", "metadata": {}, "outputs": [], @@ -378,7 +395,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 11, "id": "d0ea3b2a-8fb2-4016-83c8-a5d3e78fedbc", "metadata": {}, "outputs": [ @@ -388,7 +405,7 @@ "15.0" ] }, - "execution_count": 21, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -409,7 +426,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 12, "id": "0555b384-fde6-4404-86e0-7ea199003d58", "metadata": {}, "outputs": [ @@ -419,7 +436,7 @@ "53.83784653" ] }, - "execution_count": 22, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -441,7 +458,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 13, "id": "45404406-859d-4caa-8b9d-5838162c80a0", "metadata": {}, "outputs": [ @@ -453,7 +470,7 @@ " 'output': 53.83784653}" ] }, - "execution_count": 23, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -487,7 +504,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -501,7 +518,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.8" + "version": "3.12.7" } }, "nbformat": 4, From 4bc2cebeaedfb8499852bf86afb68ae0de4d9188 Mon Sep 17 00:00:00 2001 From: Mehak Date: Thu, 16 Jan 2025 15:54:41 +0100 Subject: [PATCH 2/3] toolss --- lab-tools-prompting.ipynb | 187 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 187 insertions(+) diff --git a/lab-tools-prompting.ipynb b/lab-tools-prompting.ipynb index 149e69c..4a68a96 100644 --- a/lab-tools-prompting.ipynb +++ b/lab-tools-prompting.ipynb @@ -500,6 +500,193 @@ "1. Provide few shot examples.\n", "2. Add error handling (e.g., catch the exception and feed it back to the LLM to ask it to correct its previous output)." ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "461c49f9", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_core.tools import tool\n", + "\n", + "# Define the subtract tool\n", + "@tool\n", + "def subtract(x: int, y: int) -> int:\n", + " \"\"\"Subtract two numbers.\"\"\"\n", + " return x - y\n", + "\n", + "@tool\n", + "def divide(x: float, y: float) -> float:\n", + " \"\"\"Divide two numbers.\"\"\"\n", + " if y == 0:\n", + " raise ValueError(\"Cannot divide by zero!\")\n", + " return x / y\n", + "\n", + "@tool\n", + "def power(x: float, y: int) -> float:\n", + " \"\"\"Raise x to the power of y.\"\"\"\n", + " return x ** y\n", + "\n", + "# List the new tools\n", + "tools = [subtract, divide, power]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "5d8f0008", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "subtract(x: int, y: int) -> int - Subtract two numbers.\n", + "divide(x: float, y: float) -> float - Divide two numbers.\n", + "power(x: float, y: int) -> float - Raise x to the power of y.\n" + ] + } + ], + "source": [ + "# Render the description of the tools\n", + "from langchain_core.tools import render_text_description\n", + "rendered_tools = render_text_description(tools)\n", + "print(rendered_tools)" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "dd914d4f", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "<>:12: SyntaxWarning: invalid escape sequence '\\ '\n", + "<>:12: SyntaxWarning: invalid escape sequence '\\ '\n", + "C:\\Users\\mehak\\AppData\\Local\\Temp\\ipykernel_11292\\2665789327.py:12: SyntaxWarning: invalid escape sequence '\\ '\n", + " \"\"\"\n" + ] + } + ], + "source": [ + "system_prompt = f\"\"\"\\ \n", + "You are an assistant that has access to the following set of tools. \n", + "Here are the names and descriptions for each tool:\n", + "\n", + "{rendered_tools}\n", + "\n", + "Given the user input, return the name and input of the tool to use. \n", + "Return your response as a JSON blob with 'name' and 'arguments' keys.\n", + "\n", + "The `arguments` should be a dictionary, with keys corresponding \n", + "to the argument names and the values corresponding to the requested values.\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "2f2eb88d", + "metadata": {}, + "outputs": [], + "source": [ + "prompt = ChatPromptTemplate.from_messages(\n", + " [(\"system\", system_prompt), (\"user\", \"{input}\")]\n", + ")\n", + "\n", + "chain = prompt | model\n", + "\n", + "message = chain.invoke({\"input\": \"what's 5 minus 3\"})" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "2f514573", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'name': 'divide', 'arguments': {'x': 10, 'y': 2}}" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Use the JsonOutputParser\n", + "chain = prompt | model | JsonOutputParser()\n", + "\n", + "# Example: Testing the 'divide' operation\n", + "chain.invoke({\"input\": \"what's 10 divided by 2\"})" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "id": "89adf3aa", + "metadata": {}, + "outputs": [], + "source": [ + "def invoke_tool(\n", + " tool_call_request: ToolCallRequest, config: Optional[RunnableConfig] = None\n", + "):\n", + " \"\"\"A function that we can use the perform a tool invocation.\"\"\"\n", + " tool_name_to_tool = {tool.name: tool for tool in tools}\n", + " name = tool_call_request[\"name\"]\n", + " requested_tool = tool_name_to_tool[name]\n", + " return requested_tool.invoke(tool_call_request[\"arguments\"], config=config)" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "52d1a87f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'name': 'power', 'arguments': {'x': 2, 'y': 3}}\n" + ] + } + ], + "source": [ + "try:\n", + " result = chain.invoke({\"input\": \"what's 2 to the power of 3\"})\n", + " print(result)\n", + "except OutputParserException as e:\n", + " print(f\"Output parsing error: {e}\")\n", + " print(f\"Raw LLM output: {e.llm_output}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "9ba251f4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8.0\n" + ] + } + ], + "source": [ + "chain = prompt | model | JsonOutputParser() | invoke_tool\n", + "result = chain.invoke({\"input\": \"what's 2 to the power of 3\"})\n", + "print(result)" + ] } ], "metadata": { From 1b015b2592726d1888e271baa57cb9a30dbcff03 Mon Sep 17 00:00:00 2001 From: Mehak Date: Thu, 16 Jan 2025 16:03:20 +0100 Subject: [PATCH 3/3] tools --- lab-tools-prompting.ipynb | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/lab-tools-prompting.ipynb b/lab-tools-prompting.ipynb index 4a68a96..abc7d21 100644 --- a/lab-tools-prompting.ipynb +++ b/lab-tools-prompting.ipynb @@ -589,10 +589,21 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 48, "id": "2f2eb88d", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'{\\n \"name\": \"subtcur\",\\n \"arguments\": {\\n \"x\": 5,\\n \"y\": 3\\n }\\n}\\n\\nIn this case, the user wants to subtract two numbers. The correct tool is `subtract`, and its arguments are clearly defined as x=5 and y=3 for subtraction of (5-3).'" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "prompt = ChatPromptTemplate.from_messages(\n", " [(\"system\", system_prompt), (\"user\", \"{input}\")]\n", @@ -600,12 +611,12 @@ "\n", "chain = prompt | model\n", "\n", - "message = chain.invoke({\"input\": \"what's 5 minus 3\"})" + "chain.invoke({\"input\": \"what's 5 minus 3\"})" ] }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 49, "id": "2f514573", "metadata": {}, "outputs": [ @@ -615,7 +626,7 @@ "{'name': 'divide', 'arguments': {'x': 10, 'y': 2}}" ] }, - "execution_count": 44, + "execution_count": 49, "metadata": {}, "output_type": "execute_result" } @@ -630,7 +641,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 50, "id": "89adf3aa", "metadata": {}, "outputs": [], @@ -647,7 +658,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 51, "id": "52d1a87f", "metadata": {}, "outputs": [ @@ -670,7 +681,7 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 52, "id": "9ba251f4", "metadata": {}, "outputs": [