mirror of
https://github.com/anthropics/claude-cookbooks.git
synced 2025-10-06 01:00:28 +03:00
Tool use GA
This commit is contained in:
@@ -115,7 +115,7 @@
|
||||
"def chat_with_claude(user_message):\n",
|
||||
" print(f\"\\n{'='*50}\\nUser Message: {user_message}\\n{'='*50}\")\n",
|
||||
"\n",
|
||||
" message = client.beta.tools.messages.create(\n",
|
||||
" message = client.messages.create(\n",
|
||||
" model=MODEL_NAME,\n",
|
||||
" max_tokens=4096,\n",
|
||||
" messages=[{\"role\": \"user\", \"content\": user_message}],\n",
|
||||
@@ -138,7 +138,7 @@
|
||||
"\n",
|
||||
" print(f\"Tool Result: {tool_result}\")\n",
|
||||
"\n",
|
||||
" response = client.beta.tools.messages.create(\n",
|
||||
" response = client.messages.create(\n",
|
||||
" model=MODEL_NAME,\n",
|
||||
" max_tokens=4096,\n",
|
||||
" messages=[\n",
|
||||
|
||||
@@ -187,7 +187,7 @@
|
||||
" {\"role\": \"user\", \"content\": user_message}\n",
|
||||
" ]\n",
|
||||
"\n",
|
||||
" response = client.beta.tools.messages.create(\n",
|
||||
" response = client.messages.create(\n",
|
||||
" model=MODEL_NAME,\n",
|
||||
" max_tokens=4096,\n",
|
||||
" tools=tools,\n",
|
||||
@@ -227,7 +227,7 @@
|
||||
" },\n",
|
||||
" ]\n",
|
||||
"\n",
|
||||
" response = client.beta.tools.messages.create(\n",
|
||||
" response = client.messages.create(\n",
|
||||
" model=MODEL_NAME,\n",
|
||||
" max_tokens=4096,\n",
|
||||
" tools=tools,\n",
|
||||
|
||||
@@ -113,7 +113,7 @@
|
||||
"Use the `print_summary` tool.\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"response = client.beta.tools.messages.create(\n",
|
||||
"response = client.messages.create(\n",
|
||||
" model=MODEL_NAME,\n",
|
||||
" max_tokens=4096,\n",
|
||||
" tools=tools,\n",
|
||||
@@ -190,7 +190,7 @@
|
||||
"Use the print_entities tool.\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"response = client.beta.tools.messages.create(\n",
|
||||
"response = client.messages.create(\n",
|
||||
" model=MODEL_NAME,\n",
|
||||
" max_tokens=4096,\n",
|
||||
" tools=tools,\n",
|
||||
@@ -263,7 +263,7 @@
|
||||
"Use the print_sentiment_scores tool.\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"response = client.beta.tools.messages.create(\n",
|
||||
"response = client.messages.create(\n",
|
||||
" model=MODEL_NAME,\n",
|
||||
" max_tokens=4096,\n",
|
||||
" tools=tools,\n",
|
||||
@@ -363,7 +363,7 @@
|
||||
"Use the print_classification tool. The categories can be Politics, Sports, Technology, Entertainment, Business.\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"response = client.beta.tools.messages.create(\n",
|
||||
"response = client.messages.create(\n",
|
||||
" model=MODEL_NAME,\n",
|
||||
" max_tokens=4096,\n",
|
||||
" tools=tools,\n",
|
||||
|
||||
@@ -1,550 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "20de56a8",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Function Calling with Claude"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "8e7fe136",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"[THIS NOTEBOOK IS DEPRECATED. IF YOU ARE USING THE ANTHROPIC API, PLEASE USE THE NEW BETA TOOL USE FORMATTING. TO LEARN MORE, VISIT [HERE](https://anthropic.readme.io/claude/docs/tool-use)]\n",
|
||||
"\n",
|
||||
"Let's teach Claude to call some functions!\n",
|
||||
"\n",
|
||||
"We'll do this in a few stages:\n",
|
||||
"1. Explain to Claude what the function is and how to use it.\n",
|
||||
"2. Tell Claude to do something that requires Claude to call the function\n",
|
||||
"3. When Claude calls the function, pause the response, call the function in code\n",
|
||||
"4. Call Claude again, giving it the value returned by the code\n",
|
||||
"5. Return a final response to the user\n",
|
||||
"\n",
|
||||
"Note: There is a more automated function calling repository that you can view and use at https://github.com/anthropics/anthropic-tools. This notebook is designed to walk you through the details of how that repo works. Additionally, expect overall function calling performance and convenience to improve soon."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "e5e1a230",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Function calling can allow us to get around limitations of large language models. One of those is multiplying large numbers together. We'll implement a \"calculator\" function that makes this work like a dream."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "fc39114b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from anthropic import Anthropic\n",
|
||||
"import re\n",
|
||||
"client = Anthropic()\n",
|
||||
"MODEL_NAME = \"claude-3-opus-20240229\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "1e27c99c",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"First, let's look at Claude's default behavior."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "c28ca1ad",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"To multiply 1,984,135 by 9,343,116, we can use the standard multiplication algorithm. Let's start by multiplying each digit of the second number by the first number, and then add the results.\n",
|
||||
"\n",
|
||||
" 1,984,135\n",
|
||||
"x 9,343,116\n",
|
||||
"-----------\n",
|
||||
" 11,904,810 (1,984,135 x 6)\n",
|
||||
" 17,857,215 (1,984,135 x 9)\n",
|
||||
" 13,888,945 (1,984,135 x 7)\n",
|
||||
" 3,968,270 (1,984,135 x 2)\n",
|
||||
" 1,984,135 (1,984,135 x 1)\n",
|
||||
" 17,857,215 (1,984,135 x 9)\n",
|
||||
" 11,904,810 (1,984,135 x 6)\n",
|
||||
" 3,968,270 (1,984,135 x 2)\n",
|
||||
" 17,857,215 (1,984,135 x 9)\n",
|
||||
" 13,888,945 (1,984,135 x 7)\n",
|
||||
" 15,873,080 (1,984,135 x 8)\n",
|
||||
" 5,952,405 (1,984,135 x 3)\n",
|
||||
"-----------\n",
|
||||
"18,529,877,865,540\n",
|
||||
"\n",
|
||||
"Therefore, 1,984,135 multiplied by 9,343,116 equals 18,529,877,865,540.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"multiplication_message = {\n",
|
||||
" \"role\": \"user\", \n",
|
||||
" \"content\": \"Multiply 1,984,135 by 9,343,116\"\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"message = client.messages.create(\n",
|
||||
" model=MODEL_NAME,\n",
|
||||
" max_tokens=1024,\n",
|
||||
" messages=[multiplication_message]\n",
|
||||
").content[0].text\n",
|
||||
"print(message)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "d03d4bae",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"18,538,003,464,660\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"answer = 1984135 * 9343116\n",
|
||||
"print(f\"{answer:,}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0699a2f7",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Claude was within 0.01% of the right answer, but didn't get it exactly right. Let's fix that. First, we'll define our calculator function."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "08626553",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def do_pairwise_arithmetic(num1, num2, operation):\n",
|
||||
" if operation == '+':\n",
|
||||
" return num1 + num2\n",
|
||||
" elif operation == \"-\":\n",
|
||||
" return num1 - num2\n",
|
||||
" elif operation == \"*\":\n",
|
||||
" return num1 * num2\n",
|
||||
" elif operation == \"/\":\n",
|
||||
" return num1 / num2\n",
|
||||
" else:\n",
|
||||
" return \"Error: Operation not supported.\"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "39275885",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Now we'll write a docstring for Claude to read."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "155e088a",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"<tool_description>\n",
|
||||
"<tool_name>calculator</tool_name>\n",
|
||||
"<description>\n",
|
||||
"Calculator function for doing basic arithmetic. \n",
|
||||
"Supports addition, subtraction, multiplication\n",
|
||||
"</description>\n",
|
||||
"<parameters>\n",
|
||||
"<parameter>\n",
|
||||
"<name>first_operand</name>\n",
|
||||
"<type>int</type>\n",
|
||||
"<description>First operand (before the operator)</description>\n",
|
||||
"</parameter>\n",
|
||||
"<parameter>\n",
|
||||
"<name>second_operand</name>\n",
|
||||
"<type>int</type>\n",
|
||||
"<description>Second operand (after the operator)</description>\n",
|
||||
"</parameter>\n",
|
||||
"<parameter>\n",
|
||||
"<name>operator</name>\n",
|
||||
"<type>str</type>\n",
|
||||
"<description>The operation to perform. Must be either +, -, *, or /</description>\n",
|
||||
"</parameter>\n",
|
||||
"</parameters>\n",
|
||||
"</tool_description>\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def construct_format_tool_for_claude_prompt(name, description, parameters):\n",
|
||||
" constructed_prompt = (\n",
|
||||
" \"<tool_description>\\n\"\n",
|
||||
" f\"<tool_name>{name}</tool_name>\\n\"\n",
|
||||
" \"<description>\\n\"\n",
|
||||
" f\"{description}\\n\"\n",
|
||||
" \"</description>\\n\"\n",
|
||||
" \"<parameters>\\n\"\n",
|
||||
" f\"{construct_format_parameters_prompt(parameters)}\\n\"\n",
|
||||
" \"</parameters>\\n\"\n",
|
||||
" \"</tool_description>\"\n",
|
||||
" )\n",
|
||||
" return constructed_prompt\n",
|
||||
"\n",
|
||||
"tool_name = \"calculator\"\n",
|
||||
"tool_description = \"\"\"Calculator function for doing basic arithmetic. \n",
|
||||
"Supports addition, subtraction, multiplication\"\"\"\n",
|
||||
"\n",
|
||||
"def construct_format_parameters_prompt(parameters):\n",
|
||||
" constructed_prompt = \"\\n\".join(f\"<parameter>\\n<name>{parameter['name']}</name>\\n<type>{parameter['type']}</type>\\n<description>{parameter['description']}</description>\\n</parameter>\" for parameter in parameters)\n",
|
||||
"\n",
|
||||
" return constructed_prompt\n",
|
||||
"\n",
|
||||
"parameters = [\n",
|
||||
" {\n",
|
||||
" \"name\": \"first_operand\",\n",
|
||||
" \"type\": \"int\",\n",
|
||||
" \"description\": \"First operand (before the operator)\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"name\": \"second_operand\",\n",
|
||||
" \"type\": \"int\",\n",
|
||||
" \"description\": \"Second operand (after the operator)\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"name\": \"operator\",\n",
|
||||
" \"type\": \"str\",\n",
|
||||
" \"description\": \"The operation to perform. Must be either +, -, *, or /\"\n",
|
||||
" }\n",
|
||||
"]\n",
|
||||
"tool = construct_format_tool_for_claude_prompt(tool_name, tool_description, parameters)\n",
|
||||
"print(tool)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "64e94c65",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Now we'll insert this tool description into a longer prompt template to form the system prompt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "6c066ac6",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"In this environment you have access to a set of tools you can use to answer the user's question.\n",
|
||||
"\n",
|
||||
"You may call them like this:\n",
|
||||
"<function_calls>\n",
|
||||
"<invoke>\n",
|
||||
"<tool_name>$TOOL_NAME</tool_name>\n",
|
||||
"<parameters>\n",
|
||||
"<$PARAMETER_NAME>$PARAMETER_VALUE</$PARAMETER_NAME>\n",
|
||||
"...\n",
|
||||
"</parameters>\n",
|
||||
"</invoke>\n",
|
||||
"</function_calls>\n",
|
||||
"\n",
|
||||
"Here are the tools available:\n",
|
||||
"<tools>\n",
|
||||
"<tool_description>\n",
|
||||
"<tool_name>calculator</tool_name>\n",
|
||||
"<description>\n",
|
||||
"Calculator function for doing basic arithmetic. \n",
|
||||
"Supports addition, subtraction, multiplication\n",
|
||||
"</description>\n",
|
||||
"<parameters>\n",
|
||||
"<parameter>\n",
|
||||
"<name>first_operand</name>\n",
|
||||
"<type>int</type>\n",
|
||||
"<description>First operand (before the operator)</description>\n",
|
||||
"</parameter>\n",
|
||||
"<parameter>\n",
|
||||
"<name>second_operand</name>\n",
|
||||
"<type>int</type>\n",
|
||||
"<description>Second operand (after the operator)</description>\n",
|
||||
"</parameter>\n",
|
||||
"<parameter>\n",
|
||||
"<name>operator</name>\n",
|
||||
"<type>str</type>\n",
|
||||
"<description>The operation to perform. Must be either +, -, *, or /</description>\n",
|
||||
"</parameter>\n",
|
||||
"</parameters>\n",
|
||||
"</tool_description>\n",
|
||||
"</tools>\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def construct_tool_use_system_prompt(tools):\n",
|
||||
" tool_use_system_prompt = (\n",
|
||||
" \"In this environment you have access to a set of tools you can use to answer the user's question.\\n\"\n",
|
||||
" \"\\n\"\n",
|
||||
" \"You may call them like this:\\n\"\n",
|
||||
" \"<function_calls>\\n\"\n",
|
||||
" \"<invoke>\\n\"\n",
|
||||
" \"<tool_name>$TOOL_NAME</tool_name>\\n\"\n",
|
||||
" \"<parameters>\\n\"\n",
|
||||
" \"<$PARAMETER_NAME>$PARAMETER_VALUE</$PARAMETER_NAME>\\n\"\n",
|
||||
" \"...\\n\"\n",
|
||||
" \"</parameters>\\n\"\n",
|
||||
" \"</invoke>\\n\"\n",
|
||||
" \"</function_calls>\\n\"\n",
|
||||
" \"\\n\"\n",
|
||||
" \"Here are the tools available:\\n\"\n",
|
||||
" \"<tools>\\n\"\n",
|
||||
" + '\\n'.join([tool for tool in tools]) +\n",
|
||||
" \"\\n</tools>\"\n",
|
||||
" )\n",
|
||||
" return tool_use_system_prompt\n",
|
||||
"\n",
|
||||
"system_prompt = construct_tool_use_system_prompt([tool])\n",
|
||||
"print(system_prompt)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6d56278d",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Now all we need to do is make the harness for calling Claude with this prompt. First, let's look at Claude's output when we give it this system prompt and ask the same question we asked it before."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"id": "7a9e0ed3",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Okay, let's break this down step-by-step:\n",
|
||||
"<function_calls>\n",
|
||||
"<invoke>\n",
|
||||
"<tool_name>calculator</tool_name>\n",
|
||||
"<parameters>\n",
|
||||
"<first_operand>1984135</first_operand>\n",
|
||||
"<second_operand>9343116</second_operand>\n",
|
||||
"<operator>*</operator>\n",
|
||||
"</parameters>\n",
|
||||
"</invoke>\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"function_calling_message = client.messages.create(\n",
|
||||
" model=MODEL_NAME,\n",
|
||||
" max_tokens=1024,\n",
|
||||
" messages=[multiplication_message],\n",
|
||||
" system=system_prompt,\n",
|
||||
" stop_sequences=[\"\\n\\nHuman:\", \"\\n\\nAssistant\", \"</function_calls>\"]\n",
|
||||
").content[0].text\n",
|
||||
"print(function_calling_message)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "cd4492fd",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Claude did a great job calling the function correctly. Now let's extract the parameters and send them to the do_pairwise_arithmetic function."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"id": "443ad932",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"18,538,003,464,660\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def extract_between_tags(tag: str, string: str, strip: bool = False) -> list[str]:\n",
|
||||
" ext_list = re.findall(f\"<{tag}>(.+?)</{tag}>\", string, re.DOTALL)\n",
|
||||
" if strip:\n",
|
||||
" ext_list = [e.strip() for e in ext_list]\n",
|
||||
" return ext_list\n",
|
||||
"\n",
|
||||
"first_operand = int(extract_between_tags(\"first_operand\", function_calling_message)[0])\n",
|
||||
"second_operand = int(extract_between_tags(\"second_operand\", function_calling_message)[0])\n",
|
||||
"operator = extract_between_tags(\"operator\", function_calling_message)[0]\n",
|
||||
"\n",
|
||||
"result = do_pairwise_arithmetic(first_operand, second_operand, operator)\n",
|
||||
"print(f\"{result:,}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "74043369",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Perfect! Now all that's left to do is to pass the return value into Claude and return the final value to the user. First we'll format in the way that Claude expects."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"id": "bd847a70",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"<function_results>\n",
|
||||
"<result>\n",
|
||||
"<tool_name>do_pairwise_arithmetic</tool_name>\n",
|
||||
"<stdout>\n",
|
||||
"18538003464660\n",
|
||||
"</stdout>\n",
|
||||
"</result>\n",
|
||||
"</function_results>\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def construct_successful_function_run_injection_prompt(invoke_results):\n",
|
||||
" constructed_prompt = (\n",
|
||||
" \"<function_results>\\n\"\n",
|
||||
" + '\\n'.join(\n",
|
||||
" f\"<result>\\n<tool_name>{res['tool_name']}</tool_name>\\n<stdout>\\n{res['tool_result']}\\n</stdout>\\n</result>\" \n",
|
||||
" for res in invoke_results\n",
|
||||
" ) + \"\\n</function_results>\"\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" return constructed_prompt\n",
|
||||
"\n",
|
||||
"formatted_results = [{\n",
|
||||
" 'tool_name': 'do_pairwise_arithmetic',\n",
|
||||
" 'tool_result': result\n",
|
||||
"}]\n",
|
||||
"function_results = construct_successful_function_run_injection_prompt(formatted_results)\n",
|
||||
"print(function_results)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "9ecee356",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Next we'll combine the original message, Claude's partial return up to where it called the function, and the function results, to get the prompt we'll give to Claude to produce its final output. We use a prefilled message in the Assistant role to facilitate this."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"id": "eb61ee06",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Okay, let's break this down step-by-step:\n",
|
||||
"<function_calls>\n",
|
||||
"<invoke>\n",
|
||||
"<tool_name>calculator</tool_name>\n",
|
||||
"<parameters>\n",
|
||||
"<first_operand>1984135</first_operand>\n",
|
||||
"<second_operand>9343116</second_operand>\n",
|
||||
"<operator>*</operator>\n",
|
||||
"</parameters>\n",
|
||||
"</invoke>\n",
|
||||
"</function_calls><function_results>\n",
|
||||
"<result>\n",
|
||||
"<tool_name>do_pairwise_arithmetic</tool_name>\n",
|
||||
"<stdout>\n",
|
||||
"18538003464660\n",
|
||||
"</stdout>\n",
|
||||
"</result>\n",
|
||||
"</function_results>\n",
|
||||
"\n",
|
||||
"Therefore, 1,984,135 multiplied by 9,343,116 equals 18,538,003,464,660.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"partial_assistant_message = function_calling_message + \"</function_calls>\" + function_results\n",
|
||||
"\n",
|
||||
"final_message = client.messages.create(\n",
|
||||
" model=MODEL_NAME,\n",
|
||||
" max_tokens=1024,\n",
|
||||
" messages=[\n",
|
||||
" multiplication_message,\n",
|
||||
" {\n",
|
||||
" \"role\": \"assistant\",\n",
|
||||
" \"content\": partial_assistant_message\n",
|
||||
" }\n",
|
||||
" ],\n",
|
||||
" system=system_prompt\n",
|
||||
").content[0].text\n",
|
||||
"print(partial_assistant_message + final_message)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "5e5854c0",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Success! You can use the prompt constructors and function calling patterns defined here to implement your own functions. For instance, search, SQL, or calls to the internet. For best results, use the exact system prompt formatting and <function_calls>/<function_results> formatting shown here and in the anthropic-tools repo."
|
||||
]
|
||||
}
|
||||
],
|
||||
"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.12"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -104,7 +104,7 @@
|
||||
" If you think a user's question involves something in the future that hasn't happened yet, use the search tool.\n",
|
||||
" \"\"\"\n",
|
||||
"\n",
|
||||
" response = client.beta.tools.messages.create(\n",
|
||||
" response = client.messages.create(\n",
|
||||
" system=system_prompt,\n",
|
||||
" model=MODEL_NAME,\n",
|
||||
" messages=messages,\n",
|
||||
@@ -307,7 +307,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def analyze_tweet_sentiment(query):\n",
|
||||
" response = client.beta.tools.messages.create(\n",
|
||||
" response = client.messages.create(\n",
|
||||
" model=MODEL_NAME,\n",
|
||||
" max_tokens=4096,\n",
|
||||
" tools=tools,\n",
|
||||
@@ -399,7 +399,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def analyze_tweet_sentiment(query):\n",
|
||||
" response = client.beta.tools.messages.create(\n",
|
||||
" response = client.messages.create(\n",
|
||||
" model=MODEL_NAME,\n",
|
||||
" max_tokens=4096,\n",
|
||||
" tools=tools,\n",
|
||||
@@ -481,7 +481,7 @@
|
||||
" <tweet>{query}</tweet>\n",
|
||||
" \"\"\"\n",
|
||||
" \n",
|
||||
" response = client.beta.tools.messages.create(\n",
|
||||
" response = client.messages.create(\n",
|
||||
" model=MODEL_NAME,\n",
|
||||
" max_tokens=4096,\n",
|
||||
" tools=tools,\n",
|
||||
@@ -569,7 +569,7 @@
|
||||
"def sms_chatbot(user_message):\n",
|
||||
" messages = [{\"role\": \"user\", \"content\":user_message}]\n",
|
||||
"\n",
|
||||
" response = client.beta.tools.messages.create(\n",
|
||||
" response = client.messages.create(\n",
|
||||
" system=system_prompt,\n",
|
||||
" model=MODEL_NAME,\n",
|
||||
" max_tokens=4096,\n",
|
||||
|
||||
@@ -205,7 +205,7 @@
|
||||
" {\"role\": \"user\", \"content\": user_message}\n",
|
||||
" ]\n",
|
||||
"\n",
|
||||
" message = client.beta.tools.messages.create(\n",
|
||||
" message = client.messages.create(\n",
|
||||
" model=MODEL_NAME,\n",
|
||||
" max_tokens=4096,\n",
|
||||
" tools=tools,\n",
|
||||
@@ -229,7 +229,7 @@
|
||||
"\n",
|
||||
" print(f\"Tool Result: {save_note_response}\")\n",
|
||||
"\n",
|
||||
" response = client.beta.tools.messages.create(\n",
|
||||
" response = client.messages.create(\n",
|
||||
" model=MODEL_NAME,\n",
|
||||
" max_tokens=4096,\n",
|
||||
" messages=[\n",
|
||||
|
||||
@@ -137,7 +137,7 @@
|
||||
" }\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"response = client.beta.tools.messages.create(\n",
|
||||
"response = client.messages.create(\n",
|
||||
" model=MODEL_NAME,\n",
|
||||
" max_tokens=4096,\n",
|
||||
" messages=message_list,\n",
|
||||
|
||||
Reference in New Issue
Block a user