Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toolsprompting #20

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
263 changes: 239 additions & 24 deletions lab-tools-prompting.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
Expand All @@ -60,7 +68,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 2,
"id": "5efb4170-b95b-4d29-8f57-09509f3ba6df",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -213,7 +230,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 7,
"id": "f02f1dce-76e7-4ca9-9bac-5af496131fe1",
"metadata": {},
"outputs": [],
Expand All @@ -238,7 +255,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 8,
"id": "f8623e03-60eb-4439-b57b-ecbcebc61b58",
"metadata": {},
"outputs": [
Expand All @@ -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"
]
}
Expand Down Expand Up @@ -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"
}
Expand Down Expand Up @@ -330,7 +347,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 10,
"id": "faee95e0-4095-4310-991f-9e9465c6738e",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -378,7 +395,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 11,
"id": "d0ea3b2a-8fb2-4016-83c8-a5d3e78fedbc",
"metadata": {},
"outputs": [
Expand All @@ -388,7 +405,7 @@
"15.0"
]
},
"execution_count": 21,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -409,7 +426,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 12,
"id": "0555b384-fde6-4404-86e0-7ea199003d58",
"metadata": {},
"outputs": [
Expand All @@ -419,7 +436,7 @@
"53.83784653"
]
},
"execution_count": 22,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -441,7 +458,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 13,
"id": "45404406-859d-4caa-8b9d-5838162c80a0",
"metadata": {},
"outputs": [
Expand All @@ -453,7 +470,7 @@
" 'output': 53.83784653}"
]
},
"execution_count": 23,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -483,11 +500,209 @@
"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": 48,
"id": "2f2eb88d",
"metadata": {},
"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",
")\n",
"\n",
"chain = prompt | model\n",
"\n",
"chain.invoke({\"input\": \"what's 5 minus 3\"})"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "2f514573",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'name': 'divide', 'arguments': {'x': 10, 'y': 2}}"
]
},
"execution_count": 49,
"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": 50,
"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": 51,
"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": 52,
"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": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -501,7 +716,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down