Net als de Chat Completions-API ondersteunt de Assistants-API function calling. Met function calling kun je functies beschrijven aan de Assistants-API en deze op een intelligente manier de functies laten teruggeven die aangeroepen moeten worden samen met hun argumenten.
In dit voorbeeld maken we een weer-assistant aan en definiëren we twee functies, get_current_temperature en get_rain_probability, als tools die de Assistant kan aanroepen. In ons voorbeeld dat parallelle function calling gebruikt, vragen we de Assistant hoe het weer in San Francisco vandaag is en wat de kans op regen is. We laten ook zien hoe je de response van de Assistant met streaming kunt uitvoeren.Bij het aanmaken van je assistant definieer je eerst de functies onder de tools-parameter van de assistant.
Assistant := TsgcAIOpenAIAssistant.Create(nil);
Assistant.OpenAIOptions.ApiKey := 'sk-askdjfalskdjfl23kjkjasdefasdfj';
Assistant.AssistantOptions.Name := 'Delphi Weather Bot';
Assistant.AssistantOptions.Instructions.Text := 'You are a weather bot. Use the provided functions to answer questions.';
Assistant.AssistantOptions.Model := 'gpt-4o';
Assistant.AssistantOptions.Tools.Functions.Enabled := False;
Assistant.AssistantOptions.Tools.Functions.Functions.Text := '[{"type":"function","function":{"name":"get_current_temperature","description":"Get the current temperature for a specific location","parameters":{"type":"object","properties":{"location":{"type":"string","description":"The city and state, e.g., San Francisco, CA"},"unit":{"type":"string","enum":["Celsius","Fahrenheit"],"description":"The temperature unit to use. Infer this from the user location."}},"required":["location","unit"]}}},{"type":"function","function":{"name":"get_rain_probability","description":"Get the probability of rain for a specific location","parameters":{"type":"object","properties":{"location":{"type":"string","description":"The city and state, e.g., San Francisco, CA"}},"required":["location"]}}}]'
Assistant.AssistantOptions.Tools.FileSearch.Enabled := False;
Assistant.AssistantOptions.Tools.CodeInterpreter.Enabled := False;
Stap 2: Een Thread aanmaken en Messages toevoegen
Maak een Thread aan zodra een gebruiker een gesprek start en voeg Messages toe aan de Thread terwijl de gebruiker vragen stelt.
procedure SendMessage()
var
i: Integer;
oMessage: TsgcOpenAIClass_Message;
oMessages: TsgcOpenAIClass_Response_List_Messages;
oRun: TsgcOpenAIClass_Run;
begin
DoLog('[user]: ' + memoMessage.Lines.Text);
Screen.Cursor := crHourGlass;
Try
oMessage := Assistant.CreateMessageText('thread_id', 'What is the weather in San Francisco today and the likelihood it will rain?');
if Assigned(oMessage) then
begin
oRun := Assistant.CreateRunAndWait('thread_id');
if Assigned(oRun) then
begin
oMessages := Assistant.GetMessages('thread_id', oRun.Id);
if Assigned(oMessages) and (Length(oMessages.Messages) > 0) then
begin
memoMessage.Lines.Text := '';
for i := 0 to Length(oMessages.Messages) - 1 do
DoLog('[assistant]: ' + DoFormatResponse(oMessages.Messages[i]
.ContentText + #13#10));
end;
end;
end;
Finally
Screen.Cursor := crDefault;
End;
end;
Stap 3: Het OnFunctionCall-event afhandelen
Wanneer het component detecteert dat een functieparameter-waarde vereist is, wordt het event OnFunctionCall aangeroepen. Gebruik de parameter Request._Function om de requestdetails te kennen en gebruik Response.Output om de response te versturen.
procedure TFRMOpenAIAssistant.AssistantFunctionCall(Sender: TObject;
const aRequest: TsgcOpenAIClass_ToolCall;
const aResponse: TsgcHTTPOpenAI_ToolCall_Response);
begin
if aRequest._Function._Name = 'get_current_temperature' then
aResponse.Output := 30
else if aRequest._Function._Name = 'get_rain_probability' then
aResponse.Output := 10;
end;
});
Delphi-demo
Hieronder vind je de Delphi-demo gecompileerd voor Windows.
