Skip to content

Commit

Permalink
function generator, WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Mar 12, 2024
1 parent 0456099 commit 872e5e7
Show file tree
Hide file tree
Showing 10 changed files with 570 additions and 38 deletions.
11 changes: 9 additions & 2 deletions Claudia.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp1", "sandbox\Cons
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{1B4BD6F6-8528-4409-BA55-085DA5486D36}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Claudia.Tests", "tests\Claudia.Tests\Claudia.Tests.csproj", "{B4A34AB7-FD1B-4CC4-9859-603D26D7D0A4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Claudia.Tests", "tests\Claudia.Tests\Claudia.Tests.csproj", "{B4A34AB7-FD1B-4CC4-9859-603D26D7D0A4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorApp1", "sandbox\BlazorApp1\BlazorApp1.csproj", "{8EEB0F69-132B-4887-959D-25531588FCD2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorApp1", "sandbox\BlazorApp1\BlazorApp1.csproj", "{8EEB0F69-132B-4887-959D-25531588FCD2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Claudia.FunctionGenerator", "src\Claudia.FunctionGenerator\Claudia.FunctionGenerator.csproj", "{8C464111-AD67-4D2B-9AE2-0B52AB077EBD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -39,6 +41,10 @@ Global
{8EEB0F69-132B-4887-959D-25531588FCD2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8EEB0F69-132B-4887-959D-25531588FCD2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8EEB0F69-132B-4887-959D-25531588FCD2}.Release|Any CPU.Build.0 = Release|Any CPU
{8C464111-AD67-4D2B-9AE2-0B52AB077EBD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8C464111-AD67-4D2B-9AE2-0B52AB077EBD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8C464111-AD67-4D2B-9AE2-0B52AB077EBD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8C464111-AD67-4D2B-9AE2-0B52AB077EBD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -48,6 +54,7 @@ Global
{7D6C6A91-7B2E-4616-A40F-ACA1A0BDBEB4} = {E61BFC87-2B96-4699-9B69-EE4B008AE0A0}
{B4A34AB7-FD1B-4CC4-9859-603D26D7D0A4} = {1B4BD6F6-8528-4409-BA55-085DA5486D36}
{8EEB0F69-132B-4887-959D-25531588FCD2} = {E61BFC87-2B96-4699-9B69-EE4B008AE0A0}
{8C464111-AD67-4D2B-9AE2-0B52AB077EBD} = {B54A8855-F8F0-4015-80AA-86974E65AC2D}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B7CEBA02-BB0C-4102-AE58-DFD114C3192A}
Expand Down
7 changes: 6 additions & 1 deletion sandbox/ConsoleApp1/ConsoleApp1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="R3" Version="1.1.7" />
<PackageReference Include="R3" Version="1.1.7" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Claudia\Claudia.csproj" />
<ProjectReference Include="..\..\src\Claudia.FunctionGenerator\Claudia.FunctionGenerator.csproj">
<OutputItemType>Analyzer</OutputItemType>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
</ItemGroup>

</Project>
314 changes: 279 additions & 35 deletions sandbox/ConsoleApp1/Program.cs
Original file line number Diff line number Diff line change
@@ -1,52 +1,278 @@
using Claudia;
using R3;
using System.Xml.Linq;

var imageBytes = File.ReadAllBytes(@"dish.jpg");
// function calling
// https://github.com/anthropics/anthropic-cookbook/blob/main/function_calling/function_calling.ipynb

var anthropic = new Anthropic();

var systemPrompt = """
In this environment you have access to a set of tools you can use to answer the user's question.
You may call them like this:
<function_calls>
<invoke>
<tool_name>$TOOL_NAME</tool_name>
<parameters>
<$PARAMETER_NAME>$PARAMETER_VALUE</$PARAMETER_NAME>
...
</parameters>
</invoke>
</function_calls>
Here are the tools available:
<tools>
<tool_description>
<tool_name>DoPairwiseArithmetic</tool_name>
<description>
Calculator function for doing basic arithmetic.
Supports addition, subtraction, multiplication
</description>
<parameters>
<parameter>
<name>first_operand</name>
<type>int</type>
<description>First operand (before the operator)</description>
</parameter>
<parameter>
<name>second_operand</name>
<type>int</type>
<description>Second operand (after the operator)</description>
</parameter>
<parameter>
<name>operator</name>
<type>str</type>
<description>The operation to perform. Must be either +, -, *, or /</description>
</parameter>
</parameters>
</tool_description>
</tools>
""";

var userInput = "Multiply 1,984,135 by 9,343,116";

// Claude generate tool and parameters XML
var message = await anthropic.Messages.CreateAsync(new()
{
Model = "claude-3-opus-20240229",
Model = Models.Claude3Opus,
MaxTokens = 1024,
Messages = [new()
{
Role = "user",
Content = [
new()
{
Type = "image",
Source = new()
{
Type = "base64",
MediaType = "image/jpeg",
Data = imageBytes
}
},
new()
{
Type = "text",
Text = "Describe this image."
}
]
}],
System = systemPrompt,
StopSequences = ["</function_calls>"],
Messages = [
new() { Role = Roles.User, Content = userInput },
],
});
Console.WriteLine(message);

var simple = await anthropic.Messages.CreateAsync(new()
// Parse function_calls XML

// Okay, let's break this down step-by-step:
// <function_calls>
// <invoke>
// <tool_name>DoPairwiseArithmetic</tool_name>
// <parameters>
// <first_operand>1984135</first_operand>
// <second_operand>9343116</second_operand>
// <operator>*</operator>
// </parameters>
// </invoke>
var text = message.Content[0].Text!;
var tagStart = text.IndexOf("<function_calls>");
var xmlResult = XElement.Parse(text.Substring(tagStart) + message.StopSequence);
var parameters = xmlResult.Descendants("parameters").Elements();

var first = (double)parameters.First(x => x.Name == "first_operand");
var second = (double)parameters.First(x => x.Name == "second_operand");
var operation = (string)parameters.First(x => x.Name == "operator");

// Execute local function
var result = DoPairwiseArithmetic(first, second, operation);

// Setup result message in XML
var partialAssistantMessage = $$"""
<function_calls>
<invoke>
<tool_name>DoPairwiseArithmetic</tool_name>
<parameters>
<first_operand>{{first}}</first_operand>
<second_operand>{{second}}</second_operand>
<operator>{{operation}}</operator>
</parameters>
</invoke>
</function_calls>
<function_results>
<result>
<tool_name>DoPairwiseArithmetic</tool_name>
<stdout>
{{result}}
</stdout>
</result>
</function_results>
""";

var callResult = await anthropic.Messages.CreateAsync(new()
{
Model = Models.Claude3Opus,
MaxTokens = 1024,
Messages = [new()
{
Role = Roles.User,
Content = [
new(imageBytes, "image/jpeg"),
"Describe this image."
]
}],
System = systemPrompt,
Messages = [
new() { Role = Roles.User, Content = userInput },
new() { Role = Roles.Assistant, Content = partialAssistantMessage },
],
});
Console.WriteLine(simple);

// Show last result.
// Therefore, 1,984,135 multiplied by 9,343,116 equals 18,538,003,464,660.
Console.WriteLine(callResult);

static double DoPairwiseArithmetic(double num1, double num2, string operation)
{
return operation switch
{
"+" => num1 + num2,
"-" => num1 - num2,
"*" => num1 * num2,
"/" => num1 / num2,
_ => throw new ArgumentException("Operation not supported")
};
}






//var systemPrompt = """
//In this environment you have access to a set of tools you can use to answer the user's question.

//You may call them like this:
//<function_calls>
// <invoke>
// <tool_name>$TOOL_NAME</tool_name>
// <parameters>
// <$PARAMETER_NAME>$PARAMETER_VALUE</$PARAMETER_NAME>
// ...
// </parameters>
// </invoke>
//</function_calls>

//Here are the tools available:
//<tools>
// <tool_description>
// <tool_name>calculator</tool_name>
// <description>
// Calculator function for doing basic arithmetic.
// Supports addition, subtraction, multiplication
// </description>
// <parameters>
// <parameter>
// <name>first_operand</name>
// <type>int</type>
// <description>First operand (before the operator)</description>
// </parameter>
// <parameter>
// <name>second_operand</name>
// <type>int</type>
// <description>Second operand (after the operator)</description>
// </parameter>
// <parameter>
// <name>operator</name>
// <type>str</type>
// <description>The operation to perform. Must be either +, -, *, or /</description>
// </parameter>
// </parameters>
// </tool_description>
//</tools>
//""";


//var message = await anthropic.Messages.CreateAsync(new()
//{
// Model = Models.Claude3Opus,
// MaxTokens = 1024,
// System = systemPrompt,
// StopSequences = ["\n\nHuman:", "\n\nAssistant", "</function_calls>"],
// Messages = [new() { Role = "user", Content = "Multiply 1,984,135 by 9,343,116" }],
//});

//// Result XML::


//var text = message.Content[0].Text!;
//var tagStart = text.IndexOf("<function_calls>");
//var xmlResult = XElement.Parse(text.Substring(tagStart) + message.StopSequence);
//var parameters = xmlResult.Descendants("parameters").Elements();

//var first = (double)parameters.First(x => x.Name == "first_operand");
//var second = (double)parameters.First(x => x.Name == "second_operand");
//var operation = (string)parameters.First(x => x.Name == "operator");

//var result = DoPairwiseArithmetic(first, second, operation);

//Console.WriteLine(result);
















//var imageBytes = File.ReadAllBytes(@"dish.jpg");

//var anthropic = new Anthropic();

//var message = await anthropic.Messages.CreateAsync(new()
//{
// Model = "claude-3-opus-20240229",
// MaxTokens = 1024,
// Messages = [new()
// {
// Role = "user",
// Content = [
// new()
// {
// Type = "image",
// Source = new()
// {
// Type = "base64",
// MediaType = "image/jpeg",
// Data = imageBytes
// }
// },
// new()
// {
// Type = "text",
// Text = "Describe this image."
// }
// ]
// }],
//});
//Console.WriteLine(message);

//var simple = await anthropic.Messages.CreateAsync(new()
//{
// Model = Models.Claude3Opus,
// MaxTokens = 1024,
// Messages = [new()
// {
// Role = Roles.User,
// Content = [
// new(imageBytes, "image/jpeg"),
// "Describe this image."
// ]
// }],
//});
//Console.WriteLine(simple);

//// convert to array.
//var array = await stream.ToObservable().ToArrayAsync();
Expand Down Expand Up @@ -153,4 +379,22 @@
//}, new()
//{
// Timeout = TimeSpan.FromSeconds(5)
//});
//});


/// <summary>
/// AIUEO!
/// </summary>
public static partial class FunctionTools
{
/// <summary>
/// foobarbaz
/// </summary>
/// <param name="x">p1</param>
/// <param name="y">p2</param>
[ClaudiaFunction]
public static int Sum(int x, int y)
{
return x + y;
}
}
Loading

0 comments on commit 872e5e7

Please sign in to comment.