Podobnie jak API Chat Completions, API Asystentów obsługuje function calling. Function calling pozwala opisać funkcje dla API Asystentów i sprawić, by inteligentnie zwracało funkcje, które trzeba wywołać, wraz z ich argumentami.
W tym przykładzie stworzymy asystenta pogodowego i zdefiniujemy dwie funkcje, get_current_temperature i get_rain_probability, jako narzędzia, które asystent może wywołać. W naszym przykładzie używającym równoległego function calling zapytamy asystenta, jaka jest dziś pogoda w San Francisco i jakie są szanse na deszcz. Pokażemy też, jak wyświetlić odpowiedź asystenta ze strumieniowaniem.Tworząc asystenta, najpierw zdefiniujesz funkcje w parametrze tools asystenta.
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;
Krok 2: Utwórz wątek i dodaj wiadomości
Utwórz wątek, gdy użytkownik rozpoczyna rozmowę, i dodawaj wiadomości do wątku, gdy użytkownik zadaje pytania.
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;
Krok 3: Obsłuż zdarzenie OnFunctionCall
Gdy komponent wykryje, że wymagana jest wartość parametru funkcji, wywoływane jest zdarzenie OnFunctionCall. Użyj parametru Request._Function, aby poznać szczegóły żądania, i użyj Response.Output, aby wysłać odpowiedź.
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
Poniżej znajdziesz the delphi demo compiled for windows.
