From 661093e9a3e4f88be815d9ab8fd4487a5fd5d569 Mon Sep 17 00:00:00 2001 From: finnf28 Date: Sat, 23 Sep 2023 21:52:03 +0200 Subject: [PATCH] more stuff --- godot/LLMApiRequests.cs | 73 +++++++++++++++++++++++++++-------------- godot/Playbutton.cs | 10 +----- godot/UI.tscn | 2 +- 3 files changed, 51 insertions(+), 34 deletions(-) diff --git a/godot/LLMApiRequests.cs b/godot/LLMApiRequests.cs index fd1569c..0e1d703 100644 --- a/godot/LLMApiRequests.cs +++ b/godot/LLMApiRequests.cs @@ -1,36 +1,61 @@ using Godot; +using Newtonsoft.Json; using System; public partial class LLMApiRequests : HttpRequest { + private TextEdit Scenario; + private TextEdit Input; + + private bool PostToApiIsSet = false; + + private Message[]? Context; + + public override void _Ready() + { + SetProcess(false); + Scenario = GetNode("/root/MainMenu/MainMenuCanvas/VBoxContainer/ScenarioTextEdit"); + Input = GetNode("/root/Ingame/IngameCanvas/Eingabe/TextEdit"); + } + + public void PostToApi() + { + if(!PostToApiIsSet) + { + RequestCompleted += OnRequestCompleted; + PostToApiIsSet = true; + } + + AnswerRequest? request; + if (Context is null) + { + request = new(null, Scenario.Text); + } + else + { + request = new(Context, Input.Text); + } + + Request($"http://localhost:5246/LLM/Test", new[] { "Content-Type: application/json" }, HttpClient.Method.Get, JsonConvert.SerializeObject(request)); + } + + public void OnRequestCompleted(long result, long responseCode, string[] headers, byte[] body) + { + // Godot.Collections.Dictionary json = Json.ParseString(System.Text.Encoding.UTF8.GetString(body)).AsGodotDictionary(); + // GD.Print(json["name"]); + // GD.Print(Json.ParseString(System.Text.Encoding.UTF8.GetString(body)).AsGodotDictionary()); + GD.Print(System.Text.Encoding.UTF8.GetString(body)); + + // TODO: Store context in var + } +} + public record AnswerRequest( - Message[] Context, + Message[]? Context, string Prompt ); public record Message( string Role, string? Content - ); - - private bool PostToApiIsSet = false; - - public override void _Ready() - { - SetProcess(false); - } - - public void PostToApi(Action action) - { - if(!PostToApiIsSet) - { - RequestCompleted += (long result, long responseCode, string[] headers, byte[] body) => action(result, responseCode, headers, body); - PostToApiIsSet = true; - } - - var answerRequest = new AnswerRequest(new[] { new Message("user", "Hello!"), }, "aodneris"); - - - Request($"http://localhost:5246/LLM/Test", new[] { "Content-Type: application/json" }, HttpClient.Method.Get, Newtonsoft.Json.JsonConvert.SerializeObject(answerRequest)); - } -} + ); \ No newline at end of file diff --git a/godot/Playbutton.cs b/godot/Playbutton.cs index 1a2fdc1..3809ae1 100644 --- a/godot/Playbutton.cs +++ b/godot/Playbutton.cs @@ -15,7 +15,7 @@ public partial class Playbutton : Button public override void _Pressed() { - llmApiRequests?.PostToApi(OnRequestCompleted); + llmApiRequests?.PostToApi(); // GD.Print("is null? " + llmApiRequests is null); GD.Print("a"); @@ -25,12 +25,4 @@ public partial class Playbutton : Button var GParent = this.GetTree().Root.GetNode("Root/MainMenu/MainMenuCanvas"); GParent.Hide(); } - - private void OnRequestCompleted(long result, long responseCode, string[] headers, byte[] body) - { - // Godot.Collections.Dictionary json = Json.ParseString(System.Text.Encoding.UTF8.GetString(body)).AsGodotDictionary(); - // GD.Print(json["name"]); - // GD.Print(Json.ParseString(System.Text.Encoding.UTF8.GetString(body)).AsGodotDictionary()); - GD.Print(System.Text.Encoding.UTF8.GetString(body)); - } } diff --git a/godot/UI.tscn b/godot/UI.tscn index 2f288bb..dacfe44 100644 --- a/godot/UI.tscn +++ b/godot/UI.tscn @@ -72,7 +72,7 @@ popup/item_10/id = 10 popup/item_11/text = "Krimi 2" popup/item_11/id = 11 -[node name="TextEdit" type="TextEdit" parent="MainMenu/MainMenuCanvas/VBoxContainer"] +[node name="ScenarioTextEdit" type="TextEdit" parent="MainMenu/MainMenuCanvas/VBoxContainer"] custom_minimum_size = Vector2(520, 300) layout_direction = 1 layout_mode = 2