Skip to content

Commit

Permalink
WIP tool function calling
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Apr 5, 2024
1 parent 01241cd commit 27ec106
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 60 deletions.
191 changes: 139 additions & 52 deletions sandbox/ConsoleApp1/GeneratedMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@

//using Claudia;
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
//using System.Xml.Linq;

//static partial class FunctionTools
//static partial class FunctionTools2
//{

// public const string SystemPrompt = @$"
//In this environment you have access to a set of tools you can use to answer the user's question. If there are multiple <invoke> tags, please consolidate them into a single <function_calls> block.
//In this environment you have access to a set of tools you can use to answer the user's question. If your solution involves the use of multiple tools, please include multiple <invoke>s within a single <function_calls> tag. Each step-by-step answer or tag is not required. Only a single <function_calls> tag should be returned at the beginning.

//You may call them like this:
//<function_calls>
Expand All @@ -37,59 +38,50 @@
// ...
// </parameters>
// </invoke>
// <invoke>
// <tool_name>$TOOL_NAME</tool_name>
// <parameters>
// <$PARAMETER_NAME>$PARAMETER_VALUE</$PARAMETER_NAME>
// ...
// </parameters>
// </invoke>
// ...
//</function_calls>

//Here are the tools available:

//{PromptXml.ToolsAll}

//Again, including multiple <function_calls> tags in the reply is prohibited.
//";

// public static class PromptXml
// {
// public const string ToolsAll = @$"
//{Today}
//{Sum}
//<tools>
//{TimeOfDay}
//{DoPairwiseArithmetic}
//{GetHtmlFromWeb}
//</tools>
//";

// public const string Today = @"
// public const string TimeOfDay = @"
//<tool_description>
// <tool_name>Today</tool_name>
// <description>Date of target location.</description>
// <tool_name>TimeOfDay</tool_name>
// <description>Retrieve the current time of day in Hour-Minute-Second format for a specified time zone. Time zones should be written in standard formats such as UTC, US/Pacific, Europe/London.</description>
// <parameters>
// <parameter>
// <name>timeZoneId</name>
// <name>timeZone</name>
// <type>string</type>
// <description>TimeZone of localtion like 'Tokeyo Standard Time', 'Eastern Standard Time', etc.</description>
// <description>The time zone to get the current time for, such as UTC, US/Pacific, Europe/London.</description>
// </parameter>
// </parameters>
//</tool_description>
//";

// public const string Sum = @"
//<tool_description>
// <tool_name>Sum</tool_name>
// <description>Sum of two integer parameters.</description>
// <parameters>
// <parameter>
// <name>x</name>
// <type>int</type>
// <description>parameter1.</description>
// </parameter>
// <parameter>
// <name>y</name>
// <type>int</type>
// <description>parameter2.</description>
// </parameter>
// </parameters>
//</tool_description>
//";

// public const string DoPairwiseArithmetic = @"
//<tool_description>
// <tool_name>DoPairwiseArithmetic</tool_name>
// <description>Calculator function for doing basic arithmetic.
// Supports addition, subtraction, multiplication</description>
// Supports addition, subtraction, multiplication</description>
// <parameters>
// <parameter>
// <name>firstOperand</name>
Expand All @@ -109,18 +101,118 @@
// </parameters>
//</tool_description>
//";
// public const string GetHtmlFromWeb = @"
//<tool_description>
// <tool_name>GetHtmlFromWeb</tool_name>
// <description>Retrieves the HTML from the specified URL.</description>
// <parameters>
// <parameter>
// <name>url</name>
// <type>string</type>
// <description>The URL to retrieve the HTML from.</description>
// </parameter>
// </parameters>
//</tool_description>
//";
// public static class Tools
// {
// public static readonly Tool TimeOfDay = new Tool
// {
// Name = "TimeOfDay",
// Description = "Retrieve the current time of day in Hour-Minute-Second format for a specified time zone. Time zones should be written in standard formats such as UTC, US/Pacific, Europe/London.",
// InputSchema = new InputSchema
// {
// Type = "object",
// Properties = new Dictionary<string, ToolProperty>
// {
// {
// "timeZone", new ToolProperty()
// {
// Type = "string",
// Description = "Retrieve the current time of day in Hour-Minute-Second format for a specified time zone. Time zones should be written in standard formats such as UTC, US/Pacific, Europe/London."
// }
// },

// },
// Required = new[] { timeZone }
// }

// };
// public static readonly Tool DoPairwiseArithmetic = new Tool
// {
// Name = "DoPairwiseArithmetic",
// Description = "Calculator function for doing basic arithmetic.
// Supports addition,
// subtraction,
// multiplication",
// InputSchema = new InputSchema
// {
// Type = "object",
// Properties = new Dictionary<string, ToolProperty>
// {
// {
// "firstOperand", new ToolProperty()
// {
// Type = "double",
// Description = "Calculator function for doing basic arithmetic.
// Supports addition, subtraction, multiplication"
// },
// },
// {
// "secondOperand", new ToolProperty()
// {
// Type = "double",
// Description = "Calculator function for doing basic arithmetic.
// Supports addition, subtraction, multiplication"
// },
// },
// {
// "operator", new ToolProperty()
// {
// Type = "string",
// Description = "Calculator function for doing basic arithmetic.
// Supports addition, subtraction, multiplication"
// },
// },

// },
// Required = new[] { firstOperand, secondOperand, operator }
// }

// };
// public static readonly Tool GetHtmlFromWeb = new Tool
// {
// Name = "GetHtmlFromWeb",
// Description = "Retrieves the HTML from the specified URL.",
// InputSchema = new InputSchema
// {
// Type = "object",
// Properties = new Dictionary<string, ToolProperty>
// {
// {
// "url", new ToolProperty()
// {
// Type = "string",
// Description = "Retrieves the HTML from the specified URL."
// },
// },

// },
// Required = new[] { url }
// }

// };
// }
// }

//#pragma warning disable CS1998
// public static async ValueTask<string?> InvokeAsync(MessageResponse message)
// {
// var content = message.Content.FirstOrDefault(x => x.Text != null);
// if (content == null) return null;

// var text = content.Text;
// var tagStart = text .IndexOf("<function_calls>");
// var tagStart = text.IndexOf("<function_calls>");
// if (tagStart == -1) return null;

// var functionCalls = text.Substring(tagStart) + "</function_calls>";
Expand All @@ -135,34 +227,33 @@
// var name = (string)item.Element("tool_name")!;
// switch (name)
// {
// case "Today":
// case "TimeOfDay":
// {
// var parameters = item.Element("parameters")!;

// var _0 = (string)parameters.Element("timeZoneId")!;
// var _0 = (string)parameters.Element("timeZone")!;

// BuildResult(sb, "Today", Today(_0));
// BuildResult(sb, "TimeOfDay", TimeOfDay(_0));
// break;
// }
// case "Sum":
// case "DoPairwiseArithmetic":
// {
// var parameters = item.Element("parameters")!;

// var _0 = (int)parameters.Element("x")!;
// var _1 = (int)parameters.Element("y")!;
// var _0 = (double)parameters.Element("firstOperand")!;
// var _1 = (double)parameters.Element("secondOperand")!;
// var _2 = (string)parameters.Element("operator")!;

// BuildResult(sb, "Sum", Sum(_0, _1));
// BuildResult(sb, "DoPairwiseArithmetic", DoPairwiseArithmetic(_0, _1, _2));
// break;
// }
// case "DoPairwiseArithmetic":
// case "GetHtmlFromWeb":
// {
// var parameters = item.Element("parameters")!;

// var _0 = (double)parameters.Element("firstOperand")!;
// var _1 = (double)parameters.Element("secondOperand")!;
// var _2 = (string)parameters.Element("operator")!;
// var _0 = (string)parameters.Element("url")!;

// BuildResult(sb, "DoPairwiseArithmetic", DoPairwiseArithmetic(_0, _1, _2));
// BuildResult(sb, "GetHtmlFromWeb", await GetHtmlFromWeb(_0).ConfigureAwait(false));
// break;
// }

Expand All @@ -177,14 +268,10 @@

// static void BuildResult<T>(StringBuilder sb, string toolName, T result)
// {
// sb.AppendLine(@$"
// <result>
// sb.AppendLine(@$" <result>
// <tool_name>{toolName}</tool_name>
// <stdout>
// {result}
// </stdout>
// </result>
//");
// <stdout>{result}</stdout>
// </result>");
// }
// }
//#pragma warning restore CS1998
Expand Down
16 changes: 9 additions & 7 deletions sandbox/ConsoleApp1/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,28 @@




anthropic.HttpClient.DefaultRequestHeaders.Add("anthropic-beta", "tools-2024-04-04");

var input = new Message
{
Role = Roles.User,
Content = """
What time is it in Seattle and Tokyo?
Incidentally multiply 1,984,135 by 9,343,116.
"""
Content = "What time is it in Tokyo?",
};

var message = await anthropic.Messages.CreateAsync(new()
{
Model = Models.Claude3Haiku,
MaxTokens = 1024,
System = FunctionTools.SystemPrompt, // set generated prompt
StopSequences = [StopSequnces.CloseFunctionCalls], // set </function_calls> as stop sequence

//System = FunctionTools.SystemPrompt, // set generated prompt
//StopSequences = [StopSequnces.CloseFunctionCalls], // set </function_calls> as stop sequence

Tools = [FunctionTools.PromptXml.Tools.TimeOfDay],
Messages = [input],
});



var partialAssistantMessage = await FunctionTools.InvokeAsync(message);

var callResult = await anthropic.Messages.CreateAsync(new()
Expand Down
Loading

0 comments on commit 27ec106

Please sign in to comment.