You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Prior to the release of this library, I built my own (much more limited) library to access the OpenAI API, but I have a code generator package that automatically builds the JSON for tools, as well as dispatching code to deserialize incoming parameters and serialize output. Is that something that would be appreciated for this? It'd be relatively easy to port over and I'd be happy to put in a PR, but it would have to be a separate NuGet package I believe, due to the way code generators are referenced.
Below is an example usage of the code generator I have in place. I think this is a higher level of abstraction than would be appropriate for this library, but the core tool code generation could be helpful.
publicinterfaceTestMixin{[Tool("Ciphers/deciphers a string")]asyncTask<string>Cipher([Desc("string","The string to encrypt or decrypt")]stringvalue){Console.WriteLine($"Ciphering '{value}'");returnnew(value.ToCharArray().Select(s =>(char)(s>=97&&s<=122?s+13>122?s-13:s+13:s>=65&&s<=90?s+13>90?s-13:s+13:s)).ToArray());}}partialclassGptTest:GPT,TestMixin{//public override string Model => "gpt-4-turbo";publicoverridestringSystemMessage=>"You are part of an automated test suite for GPT integration. Please utilize the tools accessible to you.";[Tool("reverse","Reverses a given string")]stringReverse([Desc("The string to reverse")]stringvalue){Console.WriteLine($"Reversing '{value}'");returnstring.Join("",value.Reverse());}[Tool("register_animals","Registers a set of animals with the system")]boolRegisterAnimals([Desc("The animals to register")]IReadOnlyList<string>animals){Console.WriteLine($"The list of animals: {string.Join(", ",animals)}");returntrue;}[Tool("register_weather","Informs the user of average weather in a collection of cities")]voidExpectedWeather([Desc("A set of city names and their expected high temperatures")]IReadOnlyDictionary<string,float>temps){Console.WriteLine($"The cities and temps: {string.Join(", ",temps.Select(x =>$"{x.Key} -> {x.Value}"))}");}[Tool("Smile to show your happiness")]voidSmile(){Console.WriteLine("GPT is smiling at you");}[Tool("Frown to show your displeasure")]voidFrown(){Console.WriteLine("GPT is frowning at you");}}
Generates:
partialclassGptTest{protectedoverrideasyncTask<JToken>CallTool(stringtoolName,JTokenargs){switch(toolName){case"reverse":{if(argsis not JObjectargo)returnnewJValue("Invalid parameters");try{if(argo["value"]is not JValue__0_v||__0_v.Valueis not string__0)thrownewArgumentException("value");varret=((GptTest)this).Reverse(__0);returnnewJValue(ret);}catch(ArgumentExceptione){returnnewJValue($"Invalid parameter type for '{e.Message}'");}}case"register_animals":{if(argsis not JObjectargo)returnnewJValue("Invalid parameters");try{if(argo["animals"]is not JArray__0_a)thrownewArgumentException("animals");var__0=__0_a.Select(__1 =>{if(__1is not JValue__2_v||__2_v.Valueis not string__2)thrownewArgumentException("animals");return__2;}).ToList();varret=((GptTest)this).RegisterAnimals(__0);returnnewJValue(ret);}catch(ArgumentExceptione){returnnewJValue($"Invalid parameter type for '{e.Message}'");}}case"register_weather":{if(argsis not JObjectargo)returnnewJValue("Invalid parameters");try{if(argo["temps"]is not JArray__0_a)thrownewArgumentException("temps");var__0=__0_a.Select(__1 =>{if(__1is not JObject__1_o||!__1_o.ContainsKey("key")||!__1_o.ContainsKey("value"))thrownewArgumentException("temps");if(__1_o["key"]is not JValue__2_v||__2_v.Valueis not string__2)thrownewArgumentException("temps");if(__1_o["value"]is not JValue__3_v)thrownewArgumentException("temps");return(Key:__2,Value:__3_v.ToObject<float>());}).ToDictionary(__1 =>__1.Key, __1 =>__1.Value);((GptTest)this).ExpectedWeather(__0);returnnewJValue((object)null);}catch(ArgumentExceptione){returnnewJValue($"Invalid parameter type for '{e.Message}'");}}case"Smile":{try{((GptTest)this).Smile();returnnewJValue((object)null);}catch(ArgumentExceptione){returnnewJValue($"Invalid parameter type for '{e.Message}'");}}case"Frown":{try{((GptTest)this).Frown();returnnewJValue((object)null);}catch(ArgumentExceptione){returnnewJValue($"Invalid parameter type for '{e.Message}'");}}case"Cipher":{if(argsis not JObjectargo)returnnewJValue("Invalid parameters");try{if(argo["string"]is not JValue__0_v||__0_v.Valueis not string__0)thrownewArgumentException("string");varret=await((TestMixin)this).Cipher(__0);returnnewJValue(ret);}catch(ArgumentExceptione){returnnewJValue($"Invalid parameter type for '{e.Message}'");}}default:returnnewJValue($"INVALID TOOL '{toolName}'");}}protectedoverrideJArrayToolObj=>SToolObj;readonlystaticJArraySToolObj=newJArray{newJObject{["type"]="function",["function"]=newJObject{["name"]="reverse",["description"]="Reverses a given string",["parameters"]=newJObject{["type"]="object",["properties"]=newJObject{["value"]=newJObject{["description"]="The string to reverse",["type"]="string",},},["required"]=newJArray(newJValue("value"))}}},newJObject{["type"]="function",["function"]=newJObject{["name"]="register_animals",["description"]="Registers a set of animals with the system",["parameters"]=newJObject{["type"]="object",["properties"]=newJObject{["animals"]=newJObject{["description"]="The animals to register",["type"]="array",["items"]=newJObject{["type"]="string",},},},["required"]=newJArray(newJValue("animals"))}}},newJObject{["type"]="function",["function"]=newJObject{["name"]="register_weather",["description"]="Informs the user of average weather in a collection of cities",["parameters"]=newJObject{["type"]="object",["properties"]=newJObject{["temps"]=newJObject{["description"]="A set of city names and their expected high temperatures",["type"]="array",["items"]=newJObject{["type"]="object",["properties"]=newJObject{["key"]=newJObject{["type"]="string",},["value"]=newJObject{["type"]="number",},},},},},["required"]=newJArray(newJValue("temps"))}}},newJObject{["type"]="function",["function"]=newJObject{["name"]="Smile",["description"]="Smile to show your happiness",}},newJObject{["type"]="function",["function"]=newJObject{["name"]="Frown",["description"]="Frown to show your displeasure",}},newJObject{["type"]="function",["function"]=newJObject{["name"]="Cipher",["description"]="Ciphers/deciphers a string",["parameters"]=newJObject{["type"]="object",["properties"]=newJObject{["string"]=newJObject{["description"]="The string to encrypt or decrypt",["type"]="string",},},["required"]=newJArray(newJValue("string"))}}},};protectedoverrideboolHasTools=>true;}
The text was updated successfully, but these errors were encountered:
Prior to the release of this library, I built my own (much more limited) library to access the OpenAI API, but I have a code generator package that automatically builds the JSON for tools, as well as dispatching code to deserialize incoming parameters and serialize output. Is that something that would be appreciated for this? It'd be relatively easy to port over and I'd be happy to put in a PR, but it would have to be a separate NuGet package I believe, due to the way code generators are referenced.
Below is an example usage of the code generator I have in place. I think this is a higher level of abstraction than would be appropriate for this library, but the core tool code generation could be helpful.
Generates:
The text was updated successfully, but these errors were encountered: