misc_bb 7 Posted March 3 Hi, Just trying to explore further on utilizing OpenAI with Delphi. Any here tried utilizing this https://github.com/HemulGM/DelphiOpenAI? I wanted to explore a little bit more on using Structured outputs but would appreciate some examples. Thank you. Share this post Link to post
misc_bb 7 Posted April 13 I was able to come out of a simply just using REST components instead of the DelphiOpenAI. Here's what I have so that others can also make use of it if they want to. You can still improve this code but this one works fine. This is utilizing the Function Calling with structured output. function TMappingLabels.MappingLabels: TJSONObject; var fRESTRequest: TRESTRequest; fRESTClient: TRESTClient; jsonTools, jsonPayload: TJdoJsonObject; jsonMessages1, jsonMessages2: TJSONObject; jsonMessages3, JSONResult: TJSONObject; begin try Result := TJSONObject.Create;; jsonPayLoad := TJSONObject.Create; jsonMessages1 := TJSONObject.Create; jsonMessages2 := TJSONObject.Create; jsonMessages3 := TJSONObject.Create; JSONResult := TJSONObject.Create; jsonPayLoad.S['model'] := 'gpt-4-turbo'; jsonPayLoad.I['max_tokens'] := 2000; jsonMessages1 := TJdoJsonObject.Parse('{"role": "system", "content": "You are an AI that maps financial data labels to corresponding account labels based on best matching terms."}') as TJSONObject; jsonMessages2 := TJdoJsonObject.Parse('{"role": "user", "content": "Map the following financial data to the closest matching account labels."}') as TJSONObject; jsonMessages3.S['role'] := 'user'; jsonMessages3.S['content'] := JSONDataLabels['data'].ObjectValue.ToString; jsonTools := TJdoJsonObject.Parse('{"type": "function","function": { "name": "map_financial_labels", ' + '"description": "Map the financial data labels to the most fitting account labels", ' + '"parameters": { "type": "object", "properties": { "mapping": { "type": "array", ' + ' "items": { "type": "object", "properties": { "FinancialLabel": {"type": "string"}, ' + ' "MappedAccountLabel": {"type": "string"} }, "required": ["FinancialLabel", "MappedAccountLabel"] ' + ' } } } } } }') as TJSONObject; jsonPayLoad.A['messages'].AddObject(jsonMessages1); jsonPayLoad.A['messages'].AddObject(jsonMessages2); jsonPayLoad.A['messages'].AddObject(jsonMessages3); jsonPayLoad.A['tools'].AddObject(jsonTools); jsonPayLoad.S['tool_choice'] := 'auto'; //API Call fRESTRequest := TRESTRequest.Create(nil); fRESTRequest.Accept := 'application/json, text/plain; q=0.9, text/html;q=0.8,'; fRESTRequest.AcceptCharset := 'utf-8, *;q=0.8'; fRESTRequest.Client := TRESTClient.Create(fRESTRequest); fRESTRequest.Client.UserAgent := 'Embarcadero RESTClient/1.0'; fRESTRequest.Client.AutoCreateParams := true; fRESTRequest.Client.BaseURL := 'https://api.openai.com/v1/chat/completions'; fRESTRequest.Client.ContentType := 'application/json'; fRESTRequest.SynchronizedEvents := false; fRESTRequest.HandleRedirects := true; fRESTRequest.params.Add; fRESTRequest.Params[0].Name := 'Authorization'; fRESTRequest.Params[0].Value := 'Bearer ' + API_KEY; fRESTRequest.Params[0].Options := [poDoNotEncode]; fRESTRequest.Params[0].Kind := pkHTTPHEADER; fRESTRequest.params.Add; fRESTRequest.Params[1].Name := 'Body'; fRESTRequest.Params[1].Value := jsonPayLoad.ToJSON; fRESTRequest.Params[1].ContentType := 'application/json'; fRESTRequest.Params[1].Kind := pkREQUESTBODY; try fRESTRequest.Method := rmPOST; fRESTRequest.Execute; JSONResult := TJdoJSONObject.Parse(fRESTRequest.Response.Content) as TJSONObject; //here just to extract the JSON object we want to get but you can simply display the result Result := ExtractArgumentsWithJsonDataObjects(JSONResult.ToJSON({Compact:false})); except on E: Exception do Codesite.Send('Error: ' + E.Message); end; finally end; end; You have to get your own API_KEY. The goal for this function is simply to request OpenAI to mapping some labels based on an existing map that we have and send also a list of labels you have to be map against it. It will then return the corresponding map based on the JSON structure as instructed. 🙂 Share this post Link to post
limelect 51 Posted 3 hours ago In order to test your idea, is it possible to put it here a full source as there are a lot of unknown Share this post Link to post