117 lines
3.0 KiB
C#
117 lines
3.0 KiB
C#
using Godot;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
public partial class LLMApiRequests : HttpRequest
|
|
{
|
|
private TextEdit Scenario;
|
|
private TextEdit Input;
|
|
|
|
private bool PostToApiIsSet = false;
|
|
|
|
#nullable enable
|
|
public Message[]? Context { get; private set; }
|
|
public Choice[]? Choices { get; private set; }
|
|
#nullable disable
|
|
|
|
public override void _Ready()
|
|
{
|
|
SetProcess(false);
|
|
|
|
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");
|
|
}
|
|
|
|
public void PostToApi(Action action = null)
|
|
{
|
|
// 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;
|
|
}
|
|
|
|
if(!PostToApiIsSet)
|
|
{
|
|
RequestCompleted += (long result, long responseCode, string[] headers, byte[] body)
|
|
=> OnRequestCompleted(result, responseCode, headers, body, action);
|
|
PostToApiIsSet = true;
|
|
}
|
|
#nullable enable
|
|
AnswerRequest? request;
|
|
#nullable disable
|
|
if (Context is null)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(Scenario.Text)) return;
|
|
|
|
request = new(null, Scenario.Text);
|
|
}
|
|
else
|
|
{
|
|
if (string.IsNullOrWhiteSpace(Input.Text)) return;
|
|
var temp = new List<Message>(Context)
|
|
{
|
|
Choices[0].Message
|
|
};
|
|
request = new(temp.ToArray(), Input.Text);
|
|
}
|
|
// GD.Print(JsonConvert.SerializeObject(request));
|
|
|
|
Request($"http://localhost:5246/LLM/Answer", new[] { "Content-Type: application/json" }, HttpClient.Method.Get, JsonConvert.SerializeObject(request));
|
|
}
|
|
|
|
public void OnRequestCompleted(long result, long responseCode, string[] headers, byte[] body, Action callback)
|
|
{
|
|
// 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());
|
|
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();
|
|
|
|
if (callback is null) return;
|
|
|
|
callback();
|
|
}
|
|
}
|
|
|
|
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
|
|
);
|
|
|
|
public record Choice(
|
|
int Index,
|
|
Message Message,
|
|
string FinishReason
|
|
); |