Open LittleLittleCloud opened 1 year ago
I created something similar for another openai lib, although using reflection instead of source generation https://www.linkedin.com/pulse/using-openai-function-calling-net-andr%C3%A1s-velv%C3%A1rt/
It even goes one step further by streamlining the actual function calling if the AI so requires.
I'd love to contribute by implementing it for Azure SDK.
I got this working for Azure OpenAI SDK:
completionOptions.Functions = FunctionCallingHelper.GetFunctionDefinitions<Calculator>();
...
var functionCall = firstChoice.Message.FunctionCall;
var functionCallResult = FunctionCallingHelper.CallFunction<float>(functionCall, calculator);
...
public class Calculator
{
[FunctionDescription("Adds two numbers.")]
public float Add(float a, float b)
{
return a + b;
}
[FunctionDescription("Subtracts two numbers.")]
public float Subtract(float a, float b)
{
return a - b;
}
Please let me know, and I'll prepare a PR.
Just find another similar issue from dotnet-interactive
https://github.com/dotnet/interactive/issues/3243
@jsquire Any action plan for that? Generally, we can make FunctionCall
much easier in C# and it would be something really useful given the fact that there are already three wheels implemented to facilitate FunctionCall
under this thread
@LittleLittleCloud: I have no insight. We'll need an update from the OpenAI team.
@jsquire Thanks, Could you please loop OpenAI SDK team under this thread.
also @trrwilson, any suggestions/feedback for supporting function_all
with type? I already have an implementation and I can create a draft PR if you like.
Library name
Azure.AI.OpenAI
Please describe the feature.
To leverage typed method into Chat Function, we can use source generator to generate
FunctionDefinition
and a wrapper function, which takes in a string as input, retrieve arguments from that string and mapping back to original typed method.This can also reduce the effort of consuming function call with GPT, where a
FunctionDefinition
object needs to be manually crafted, arguments need to be manually parsed and the two steps need to be synchronized for every change.Both FunctionDefinition and wrapper function can be generated according to syntax information like function name, argument type and its comments if there's any.
Here's one implementation https://github.com/LittleLittleCloud/groupchat-example-for-dotnet/tree/main/src/GroupChatExample.SourceGenerator
Example
We can make use of its function name, parameter list and structured xml comment, and generate
FunctionDefinition
and it's wrapping function for consumption. This can save the effort of creating function definition object and keep parsing code in place with function parameters.Nice to have