Code-Formatting
This commit is contained in:
@@ -2,7 +2,8 @@ unit ApiKey;
|
||||
|
||||
interface
|
||||
|
||||
const Value = 'AIzaSyALccoMxn0_wcHswavhu5rzdglLeH6gVlI';
|
||||
const
|
||||
Value = 'AIzaSyALccoMxn0_wcHswavhu5rzdglLeH6gVlI';
|
||||
|
||||
implementation
|
||||
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
program GeminiAccess;
|
||||
|
||||
uses
|
||||
FastMM5,
|
||||
Vcl.Forms,
|
||||
MainUnit in 'MainUnit.pas' {Form1},
|
||||
ApiKey in 'ApiKey.pas';
|
||||
FastMM5,
|
||||
Vcl.Forms,
|
||||
MainUnit in 'MainUnit.pas' {Form1},
|
||||
ApiKey in 'ApiKey.pas';
|
||||
|
||||
{$R *.res}
|
||||
|
||||
begin
|
||||
Application.Initialize;
|
||||
Application.MainFormOnTaskbar := True;
|
||||
Application.CreateForm(TForm1, Form1);
|
||||
Application.Run;
|
||||
Application.Initialize;
|
||||
Application.MainFormOnTaskbar := True;
|
||||
Application.CreateForm(TForm1, Form1);
|
||||
Application.Run;
|
||||
end.
|
||||
|
||||
+100
-76
@@ -3,10 +3,24 @@ unit MainUnit;
|
||||
interface
|
||||
|
||||
uses
|
||||
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
|
||||
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
|
||||
System.Net.HttpClient, System.Net.HttpClientComponent, System.JSON, System.Net.Mime, Vcl.AppEvnts,
|
||||
Myc.Futures, System.Generics.Collections, ApiKey;
|
||||
Winapi.Windows,
|
||||
Winapi.Messages,
|
||||
System.SysUtils,
|
||||
System.Variants,
|
||||
System.Classes,
|
||||
Vcl.Graphics,
|
||||
Vcl.Controls,
|
||||
Vcl.Forms,
|
||||
Vcl.Dialogs,
|
||||
Vcl.StdCtrls,
|
||||
System.Net.HttpClient,
|
||||
System.Net.HttpClientComponent,
|
||||
System.JSON,
|
||||
System.Net.Mime,
|
||||
Vcl.AppEvnts,
|
||||
Myc.Futures,
|
||||
System.Generics.Collections,
|
||||
ApiKey;
|
||||
|
||||
type
|
||||
// NEU: Ein Record, um einen einzelnen Redebeitrag in der Konversation zu speichern
|
||||
@@ -31,7 +45,7 @@ type
|
||||
FChatHistory: TList<TChatTurn>; // NEU: Liste für den Konversationsverlauf
|
||||
procedure SendPrompt;
|
||||
public
|
||||
{ Public declarations }
|
||||
{ Public declarations }
|
||||
end;
|
||||
|
||||
var
|
||||
@@ -127,78 +141,88 @@ begin
|
||||
AnswerMemo.Lines.Add(userPrompt);
|
||||
AnswerMemo.Lines.Add('... sende Anfrage an Gemini ...');
|
||||
|
||||
FAnswer := TFuture<string>.Construct(
|
||||
function: string
|
||||
var
|
||||
jsonRequest: TJSONObject;
|
||||
jsonContents: TJSONArray;
|
||||
requestBody: TStringStream;
|
||||
response: IHTTPResponse;
|
||||
responseJson: TJSONValue;
|
||||
apiUrl: string;
|
||||
turn: TChatTurn;
|
||||
begin
|
||||
try
|
||||
// 1. JSON-Body aus dem gesamten Konversationsverlauf erstellen
|
||||
jsonRequest := TJSONObject.Create;
|
||||
try
|
||||
jsonContents := TJSONArray.Create;
|
||||
jsonRequest.AddPair('contents', jsonContents);
|
||||
|
||||
// NEU: Iteriere durch den Verlauf und baue die JSON-Struktur auf
|
||||
for turn in FChatHistory do
|
||||
begin
|
||||
var jsonTurn := TJSONObject.Create;
|
||||
jsonTurn.AddPair('role', TJSONString.Create(turn.Role));
|
||||
|
||||
var jsonParts := TJSONArray.Create;
|
||||
jsonTurn.AddPair('parts', jsonParts);
|
||||
|
||||
var jsonText := TJSONObject.Create;
|
||||
jsonText.AddPair('text', TJSONString.Create(turn.Text));
|
||||
jsonParts.Add(jsonText);
|
||||
|
||||
jsonContents.Add(jsonTurn);
|
||||
end;
|
||||
|
||||
// 2. Request senden
|
||||
requestBody := TStringStream.Create(jsonRequest.ToString, TEncoding.UTF8);
|
||||
try
|
||||
apiUrl := API_BASE_URL + GEMINI_MODEL + ':generateContent?key=' + FMyApiKey;
|
||||
response := FHttpClient.Post(apiUrl, requestBody, nil);
|
||||
|
||||
// 3. Antwort verarbeiten
|
||||
if (response.StatusCode = 200) then
|
||||
begin
|
||||
responseJson := TJSONObject.ParseJSONValue(response.ContentAsString(TEncoding.UTF8));
|
||||
try
|
||||
Result := responseJson.GetValue<TJSONArray>('candidates')
|
||||
.Items[0].GetValue<TJSONObject>('content')
|
||||
.GetValue<TJSONArray>('parts')
|
||||
.Items[0].GetValue<string>('text');
|
||||
finally
|
||||
responseJson.Free;
|
||||
end;
|
||||
end
|
||||
else
|
||||
begin
|
||||
Result := 'Fehler! Status: ' + response.StatusCode.ToString + ' - ' + response.StatusText +
|
||||
sLineBreak + response.ContentAsString;
|
||||
end;
|
||||
finally
|
||||
requestBody.Free;
|
||||
end;
|
||||
finally
|
||||
jsonRequest.Free;
|
||||
end;
|
||||
except
|
||||
on E: Exception do
|
||||
FAnswer :=
|
||||
TFuture<string>.Construct(
|
||||
function: string
|
||||
var
|
||||
jsonRequest: TJSONObject;
|
||||
jsonContents: TJSONArray;
|
||||
requestBody: TStringStream;
|
||||
response: IHTTPResponse;
|
||||
responseJson: TJSONValue;
|
||||
apiUrl: string;
|
||||
turn: TChatTurn;
|
||||
begin
|
||||
Result := '--- FEHLER IM TASK ---' + sLineBreak + E.Message;
|
||||
end;
|
||||
end;
|
||||
end);
|
||||
try
|
||||
// 1. JSON-Body aus dem gesamten Konversationsverlauf erstellen
|
||||
jsonRequest := TJSONObject.Create;
|
||||
try
|
||||
jsonContents := TJSONArray.Create;
|
||||
jsonRequest.AddPair('contents', jsonContents);
|
||||
|
||||
// NEU: Iteriere durch den Verlauf und baue die JSON-Struktur auf
|
||||
for turn in FChatHistory do
|
||||
begin
|
||||
var jsonTurn := TJSONObject.Create;
|
||||
jsonTurn.AddPair('role', TJSONString.Create(turn.Role));
|
||||
|
||||
var jsonParts := TJSONArray.Create;
|
||||
jsonTurn.AddPair('parts', jsonParts);
|
||||
|
||||
var jsonText := TJSONObject.Create;
|
||||
jsonText.AddPair('text', TJSONString.Create(turn.Text));
|
||||
jsonParts.Add(jsonText);
|
||||
|
||||
jsonContents.Add(jsonTurn);
|
||||
end;
|
||||
|
||||
// 2. Request senden
|
||||
requestBody := TStringStream.Create(jsonRequest.ToString, TEncoding.UTF8);
|
||||
try
|
||||
apiUrl := API_BASE_URL + GEMINI_MODEL + ':generateContent?key=' + FMyApiKey;
|
||||
response := FHttpClient.Post(apiUrl, requestBody, nil);
|
||||
|
||||
// 3. Antwort verarbeiten
|
||||
if (response.StatusCode = 200) then
|
||||
begin
|
||||
responseJson := TJSONObject.ParseJSONValue(response.ContentAsString(TEncoding.UTF8));
|
||||
try
|
||||
Result :=
|
||||
responseJson
|
||||
.GetValue<TJSONArray>('candidates')
|
||||
.Items[0]
|
||||
.GetValue<TJSONObject>('content')
|
||||
.GetValue<TJSONArray>('parts')
|
||||
.Items[0]
|
||||
.GetValue<string>('text');
|
||||
finally
|
||||
responseJson.Free;
|
||||
end;
|
||||
end
|
||||
else
|
||||
begin
|
||||
Result :=
|
||||
'Fehler! Status: '
|
||||
+ response.StatusCode.ToString
|
||||
+ ' - '
|
||||
+ response.StatusText
|
||||
+ sLineBreak
|
||||
+ response.ContentAsString;
|
||||
end;
|
||||
finally
|
||||
requestBody.Free;
|
||||
end;
|
||||
finally
|
||||
jsonRequest.Free;
|
||||
end;
|
||||
except
|
||||
on E: Exception do
|
||||
begin
|
||||
Result := '--- FEHLER IM TASK ---' + sLineBreak + E.Message;
|
||||
end;
|
||||
end;
|
||||
end
|
||||
);
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user