2023-09-23 21:07:46 +02:00
|
|
|
using Godot;
|
2023-09-23 21:52:03 +02:00
|
|
|
using Newtonsoft.Json;
|
2023-09-23 21:07:46 +02:00
|
|
|
using System;
|
2023-09-24 13:19:47 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2023-09-23 21:07:46 +02:00
|
|
|
|
|
|
|
public partial class LLMApiRequests : HttpRequest
|
|
|
|
{
|
2023-09-23 21:52:03 +02:00
|
|
|
private TextEdit Scenario;
|
|
|
|
private TextEdit Input;
|
2023-09-23 21:07:46 +02:00
|
|
|
|
|
|
|
private bool PostToApiIsSet = false;
|
|
|
|
|
2023-09-24 13:19:47 +02:00
|
|
|
#nullable enable
|
|
|
|
public Message[]? Context { get; private set; }
|
|
|
|
public Choice[]? Choices { get; private set; }
|
|
|
|
#nullable disable
|
2023-09-23 21:52:03 +02:00
|
|
|
|
2023-09-23 21:07:46 +02:00
|
|
|
public override void _Ready()
|
|
|
|
{
|
|
|
|
SetProcess(false);
|
2023-09-24 13:19:47 +02:00
|
|
|
|
|
|
|
Scenario = GetTree().Root.GetNode<TextEdit>("Root/MainMenu/MainMenuCanvas/VBoxContainer/ScenarioTextEdit");
|
|
|
|
Input = GetTree().Root.GetNode<TextEdit>("Root/Ingame/IngameCanvas/Eingabe/TextEdit");
|
|
|
|
GD.Print(nameof(LLMApiRequests) + " is ready");
|
2023-09-23 21:07:46 +02:00
|
|
|
}
|
|
|
|
|
2023-09-24 13:19:47 +02:00
|
|
|
public void PostToApi(Action action = null)
|
2023-09-23 21:07:46 +02:00
|
|
|
{
|
2023-09-24 13:19:47 +02:00
|
|
|
// GD.Print(nameof(PostToApi) + " called");
|
|
|
|
|
|
|
|
if (Scenario is null || Input is null)
|
|
|
|
{
|
|
|
|
GD.PrintErr(
|
|
|
|
(Scenario is null ? $"{nameof(Scenario)} is null. " : "")
|
|
|
|
+ (Input is null ? $"{nameof(Input)} is null" : ""));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-23 21:07:46 +02:00
|
|
|
if(!PostToApiIsSet)
|
|
|
|
{
|
2023-09-24 13:19:47 +02:00
|
|
|
RequestCompleted += (long result, long responseCode, string[] headers, byte[] body)
|
|
|
|
=> OnRequestCompleted(result, responseCode, headers, body, action);
|
2023-09-23 21:07:46 +02:00
|
|
|
PostToApiIsSet = true;
|
|
|
|
}
|
2023-09-24 13:19:47 +02:00
|
|
|
#nullable enable
|
2023-09-23 21:52:03 +02:00
|
|
|
AnswerRequest? request;
|
2023-09-24 13:19:47 +02:00
|
|
|
#nullable disable
|
2023-09-23 21:52:03 +02:00
|
|
|
if (Context is null)
|
|
|
|
{
|
2023-09-24 13:19:47 +02:00
|
|
|
if (string.IsNullOrWhiteSpace(Scenario.Text)) return;
|
|
|
|
|
2023-09-23 21:52:03 +02:00
|
|
|
request = new(null, Scenario.Text);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-09-24 13:19:47 +02:00
|
|
|
if (string.IsNullOrWhiteSpace(Input.Text)) return;
|
|
|
|
var temp = new List<Message>(Context)
|
|
|
|
{
|
|
|
|
Choices[0].Message
|
|
|
|
};
|
|
|
|
request = new(temp.ToArray(), Input.Text);
|
2023-09-23 21:52:03 +02:00
|
|
|
}
|
2023-09-24 13:19:47 +02:00
|
|
|
// GD.Print(JsonConvert.SerializeObject(request));
|
2023-09-23 21:07:46 +02:00
|
|
|
|
2023-09-24 13:19:47 +02:00
|
|
|
Request($"http://localhost:5246/LLM/Answer", new[] { "Content-Type: application/json" }, HttpClient.Method.Get, JsonConvert.SerializeObject(request));
|
2023-09-23 21:52:03 +02:00
|
|
|
}
|
|
|
|
|
2023-09-24 13:19:47 +02:00
|
|
|
public void OnRequestCompleted(long result, long responseCode, string[] headers, byte[] body, Action callback)
|
2023-09-23 21:52:03 +02:00
|
|
|
{
|
|
|
|
// 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());
|
2023-09-24 13:19:47 +02:00
|
|
|
var content = System.Text.Encoding.UTF8.GetString(body);
|
|
|
|
// GD.Print(content);
|
|
|
|
var response = JsonConvert.DeserializeObject<AnswerResponse>(content);
|
|
|
|
|
|
|
|
// GD.Print("context: " + JsonConvert.SerializeObject(elem.Context));
|
|
|
|
// GD.Print();
|
|
|
|
// GD.Print("choices: " + JsonConvert.SerializeObject(elem.Choices));
|
|
|
|
|
|
|
|
if (response is null) return;
|
|
|
|
|
|
|
|
Context = response.Context;
|
|
|
|
Choices = response.Choices.ToArray();
|
2023-09-23 21:52:03 +02:00
|
|
|
|
2023-09-24 13:19:47 +02:00
|
|
|
if (callback is null) return;
|
|
|
|
|
|
|
|
callback();
|
2023-09-23 21:07:46 +02:00
|
|
|
}
|
|
|
|
}
|
2023-09-23 21:52:03 +02:00
|
|
|
|
2023-09-24 13:19:47 +02:00
|
|
|
public record AnswerRequest(
|
|
|
|
#nullable enable
|
|
|
|
Message[]? Context,
|
|
|
|
#nullable disable
|
|
|
|
string Prompt
|
|
|
|
);
|
|
|
|
|
|
|
|
public record AnswerResponse(
|
|
|
|
Message[] Context,
|
|
|
|
IEnumerable<Choice> Choices
|
|
|
|
);
|
|
|
|
|
|
|
|
public record Message(
|
|
|
|
string Role,
|
|
|
|
#nullable enable
|
|
|
|
string? Content
|
|
|
|
#nullable disable
|
|
|
|
);
|
2023-09-23 21:52:03 +02:00
|
|
|
|
2023-09-24 13:19:47 +02:00
|
|
|
public record Choice(
|
|
|
|
int Index,
|
|
|
|
Message Message,
|
|
|
|
string FinishReason
|
|
|
|
);
|