@@ -42,13 +42,13 @@ function TDelphiAIDevAIChatGPT.GetResponse(const AQuestion: string): string;
4242 LApiUrl: string;
4343 LQuestion: string;
4444 LResponse: IResponse;
45- LReturnValue : TJSONValue;
46- LChoicesValue : TJSONValue;
47- LChoicesArray : TJSONArray;
48- LChoicesObj : TJSONObject;
49- LMessageValue : TJSONValue;
50- LMessageObj : TJSONObject;
51- I : Integer;
45+ LJsonValueAll : TJSONValue;
46+ LJsonValueChoices : TJSONValue;
47+ LJsonArrayChoices : TJSONArray;
48+ LJsonObjChoices : TJSONObject;
49+ LJsonValueMessage : TJSONValue;
50+ LJsonObjMessage : TJSONObject;
51+ LItemChoices : Integer;
5252begin
5353 Result := ' ' ;
5454 LApiUrl := FSettings.BaseUrlOpenAI;
@@ -65,37 +65,33 @@ function TDelphiAIDevAIChatGPT.GetResponse(const AQuestion: string): string;
6565 if LResponse.StatusCode <> 200 then
6666 Exit(' Question cannot be answered' + sLineBreak + ' Return: ' + LResponse.Content);
6767
68- // TJSONValue COMPLETE
69- LReturnValue := TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(LResponse.Content), 0 );
70- if not (LReturnValue is TJSONObject) then
68+ LJsonValueAll := TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(LResponse.Content), 0 );
69+ if not (LJsonValueAll is TJSONObject) then
7170 Exit(' The question cannot be answered, return object not found.' + sLineBreak +
7271 ' Return: ' + LResponse.Content);
7372
74- // GET CHOICES LIKE TJSONValue
75- LChoicesValue := TJSONObject(LReturnValue).GetValue(' choices' );
76-
77- if not (LChoicesValue is TJSONArray) then
73+ LJsonValueChoices := TJSONObject(LJsonValueAll).GetValue(' choices' );
74+ if not (LJsonValueChoices is TJSONArray) then
7875 Exit(' The question cannot be answered, choices not found.' + sLineBreak +
7976 ' Return: ' + LResponse.Content);
8077
81- // CAST CHOICES LIKE TJSONArray
82- LChoicesArray := LChoicesValue as TJSONArray;
83- for I := 0 to Pred(LChoicesArray.Count) do
78+ LJsonArrayChoices := LJsonValueChoices as TJSONArray;
79+ for LItemChoices := 0 to Pred(LJsonArrayChoices.Count) do
8480 begin
85- if not (LChoicesArray .Items[I ] is TJSONObject) then
81+ if not (LJsonArrayChoices .Items[LItemChoices ] is TJSONObject) then
8682 Continue;
8783
8884 // CAST ITEM CHOICES LIKE TJSONObject
89- LChoicesObj := LChoicesArray .Items[I ] as TJSONObject;
85+ LJsonObjChoices := LJsonArrayChoices .Items[LItemChoices ] as TJSONObject;
9086
9187 // GET MESSAGE LIKE TJSONValue
92- LMessageValue := LChoicesObj .GetValue(' message' );
93- if not (LMessageValue is TJSONObject) then
88+ LJsonValueMessage := LJsonObjChoices .GetValue(' message' );
89+ if not (LJsonValueMessage is TJSONObject) then
9490 Continue;
9591
9692 // GET MESSAGE LIKE TJSONObject
97- LMessageObj := LMessageValue as TJSONObject;
98- Result := Result + TJSONString(LMessageObj .GetValue(' content' )).Value + sLineBreak;
93+ LJsonObjMessage := LJsonValueMessage as TJSONObject;
94+ Result := Result + TJSONString(LJsonObjMessage .GetValue(' content' )).Value .Trim + sLineBreak;
9995 end ;
10096
10197 Result := Result.Trim;
0 commit comments