Json Schema for LLMs

This commit is contained in:
Michael Schimmel
2026-01-06 19:56:27 +01:00
parent 60952494c1
commit 4618573d6c
5 changed files with 730 additions and 475 deletions
+18 -11
View File
@@ -288,7 +288,7 @@ begin
CompilerStageBox.BringToFront;
ClearButtonClick(Self);
Memo1.Lines.Add(TAstSchema.GenerateFullSystemPrompt);
Memo1.Lines.Add(TAstSchema.GenerateFullSystemPrompt(FEnvironment.RootScope));
end;
procedure TForm1.ShowVizualization;
@@ -743,45 +743,52 @@ end;
procedure TForm1.FromJSONButtonClick(Sender: TObject);
var
jsonString: string;
jsonObj: TJSONObject;
jsonValue: TJSONValue;
converter: IJsonAstConverter;
begin
Memo1.Lines.BeginUpdate;
try
jsonString := Memo1.Lines.Text;
Memo1.Lines.Clear;
if jsonString.IsEmpty then
begin
Memo1.Lines.Clear;
Memo1.Lines.Add('Memo is empty. Please paste an AST JSON string.');
exit;
end;
try
converter := TJsonAstConverter.Create;
jsonObj := TJSONObject.ParseJSONValue(jsonString) as TJSONObject;
if not Assigned(jsonObj) then
raise Exception.Create('Invalid JSON format.');
// TJSONValue.ParseJSONValue ist der korrekte Einstiegspunkt
jsonValue := TJSONValue.ParseJSONValue(jsonString);
if not Assigned(jsonValue) then
raise Exception.Create('Invalid JSON syntax.');
try
FCurrUnboundAst := converter.Deserialize(jsonObj);
if not (jsonValue is TJSONArray) then
raise Exception.Create('AST Root must be a JSON Array.');
// Run the full pipeline via environment
FCurrUnboundAst := converter.Deserialize(jsonValue);
// Die Pipeline nutzt den Environment-Helper (Managed Record)
// Compile akzeptiert IAstNode und erzeugt intern ein Lambda
FCurrCompiled := FEnvironment.Compile(FCurrUnboundAst);
FCurrExec := FEnvironment.Link(FCurrCompiled);
Memo1.Lines.Clear;
Memo1.Lines.Add('AST deserialized and bound successfully from JSON.');
Memo1.Lines.Add('You can now visualize it (Middle Mouse Click) or pretty-print it.');
finally
jsonObj.Free;
jsonValue.Free;
end;
except
on E: Exception do
begin
Memo1.Lines.Clear;
Memo1.Lines.Add('Error deserializing AST from JSON:');
Memo1.Lines.Add(E.Message);
Memo1.Lines.Add('--- Original JSON ---');
Memo1.Lines.Text := Memo1.Lines.Text + sLineBreak + jsonString;
Memo1.Lines.Add(jsonString);
end;
end;
finally