This repository has been archived on 2023-11-16. You can view files and clone it, but cannot push or open issues or pull requests.
GOA/godot/LLMApiRequests.cs

117 lines
3.0 KiB
C#
Raw Permalink Normal View History

using Godot;
2023-09-23 21:52:03 +02:00
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
public partial class LLMApiRequests : HttpRequest
{
2023-09-23 21:52:03 +02:00
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
2023-09-23 21:52:03 +02:00
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
2023-09-23 21:52:03 +02:00
AnswerRequest? request;
#nullable disable
2023-09-23 21:52:03 +02:00
if (Context is null)
{
if (string.IsNullOrWhiteSpace(Scenario.Text)) return;
2023-09-23 21:52:03 +02:00
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);
2023-09-23 21:52:03 +02:00
}
// GD.Print(JsonConvert.SerializeObject(request));
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
}
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());
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
if (callback is null) return;
callback();
}
}
2023-09-23 21:52:03 +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
public record Choice(
int Index,
Message Message,
string FinishReason
);