Implement Function Calling
mainFunction calling allows the model to output JSON arguments for specific functions you define.
- Define your functions as a
TArray<IChatFunction>. - Pass them to
Params.Functionsand setParams.FunctionCall(TFunctionCall.Auto). - Check the
FinishReasonin the response. If it isTFinishReason.FunctionCall, execute your local function using the providedFunctionCallobject. - To continue the conversation, send the function result back to the model by adding the assistant's function call and the function's result to the message history.
// 1. Request function call
var Chat := OpenAI.Chat.Create(
procedure(Params: TChatParams)
begin
Params.Functions(Funcs);
Params.FunctionCall(TFunctionCall.Auto);
Params.Messages([TChatMessageBuild.User(Text)]);
Params.MaxTokens(1024);
end);
// 2. Handle response
try
for var Choice in Chat.Choices do
if Choice.FinishReason = TFinishReason.FunctionCall then
ProcFunction(Choice.Message.FunctionCall)
else
MemoChat.Lines.Add(Choice.Message.Content);
finally
Chat.Free;
end;
// 3. Send result back to continue conversation
procedure ProcFunction(Func: TChatFunctionCall);
begin
var FuncResult := Execute(Func.Name, Func.Arguments);
var Chat := OpenAI.Chat.Create(
procedure(Params: TChatParams)
begin
Params.Functions(Funcs);
Params.FunctionCall(TFunctionCall.Auto);
Params.Messages([
TChatMessageBuild.User(Text),
TChatMessageBuild.NewAsistantFunc(Func.Name, Func.Arguments),
TChatMessageBuild.Func(FuncResult, Func.Name)]);
Params.MaxTokens(1024);
end);
// ... handle response ...
end;