first version of EU AI Act game
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
extends Control
|
||||
|
||||
@onready var _prompt_edit: TextEdit = $MarginContainer/Scroll/VBox/PromptEdit
|
||||
@onready var main: Control = $"."
|
||||
@onready var _http: HTTPRequest = $HTTPRequest
|
||||
@onready var output_text: RichTextLabel = $MarginContainer/Scroll/VBox/OutputText
|
||||
@onready var option_button: OptionButton = $MarginContainer/Scroll/VBox/OptionButton
|
||||
@onready var generate_button: Button = $MarginContainer/Scroll/VBox/GenerateRow/GenerateButton
|
||||
@onready var helper = $VBoxContainer/SubViewportContainer/SubViewport/base_node/helper
|
||||
|
||||
const API_URL := "" #TODO insert your url to the llm / api-gateway here
|
||||
|
||||
var document_id
|
||||
|
||||
signal closing_chatbot
|
||||
|
||||
|
||||
func _ready():
|
||||
_http.request_completed.connect(Callable(self, "_on_request_completed"))
|
||||
document_id = option_button.selected
|
||||
helper.play_idle()
|
||||
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
if not main.visible:
|
||||
return
|
||||
print(option_button.selected)
|
||||
if event.is_action_pressed("drop"):
|
||||
get_viewport().set_input_as_handled()
|
||||
_on_close_pressed()
|
||||
|
||||
#if event.is_action_pressed("advance_dialogue"):
|
||||
# get_viewport().set_input_as_handled()
|
||||
# _on_generate_pressed()
|
||||
|
||||
|
||||
func _on_generate_pressed() -> void:
|
||||
$VBoxContainer/SubViewportContainer/SubViewport/AnimationPlayer.play("walk_helper")
|
||||
helper.play_think_walk()
|
||||
var user_text := _prompt_edit.text.strip_edges()
|
||||
document_id = option_button.selected
|
||||
TextToSpeech._stop_dialogue()
|
||||
if user_text.is_empty():
|
||||
print("Prompt ist leer")
|
||||
return
|
||||
|
||||
var document = _load_txt("res://addons/godot_llama/demo/documents/%s.md" % [document_id]).strip_edges()
|
||||
print(document_id)
|
||||
print(document)
|
||||
var combined_user_message := ""
|
||||
|
||||
if not document.is_empty():
|
||||
combined_user_message = "Dokument:\n%s\n\nFrage: %s" % [document, user_text]
|
||||
else:
|
||||
combined_user_message = user_text
|
||||
|
||||
var body := {
|
||||
"question": combined_user_message
|
||||
}
|
||||
|
||||
var headers = [
|
||||
"Content-Type: application/json",
|
||||
#"Authorization: Bearer %s" % API_TOKEN
|
||||
]
|
||||
|
||||
var json_body := JSON.stringify(body)
|
||||
|
||||
|
||||
var err = _http.request(
|
||||
API_URL,
|
||||
headers,
|
||||
HTTPClient.METHOD_POST,
|
||||
json_body
|
||||
)
|
||||
|
||||
if err != OK:
|
||||
print("HTTP Fehler: ", err)
|
||||
|
||||
generate_button.disabled = true
|
||||
|
||||
func _on_request_completed(
|
||||
result: int,
|
||||
response_code: int,
|
||||
headers: PackedStringArray,
|
||||
body: PackedByteArray
|
||||
) -> void:
|
||||
$VBoxContainer/SubViewportContainer/SubViewport/AnimationPlayer.stop()
|
||||
helper.play_idle()
|
||||
var txt := body.get_string_from_utf8()
|
||||
|
||||
if response_code != 200:
|
||||
return
|
||||
|
||||
var json = JSON.parse_string(txt)
|
||||
|
||||
if json == null:
|
||||
print("JSON Fehler")
|
||||
return
|
||||
|
||||
if json.has("content"):
|
||||
print('content:')
|
||||
print(json["content"])
|
||||
var response_text = json["content"]
|
||||
output_text.text = response_text
|
||||
|
||||
if json.has("message"):
|
||||
print('message:')
|
||||
print(json["message"])
|
||||
var response_text = json["message"]["content"]
|
||||
output_text.text = response_text
|
||||
var line = {"text": output_text.text}
|
||||
TextToSpeech._on_dialogue(line)
|
||||
generate_button.disabled = false
|
||||
|
||||
func _load_txt(path: String) -> String:
|
||||
var file := FileAccess.open(path, FileAccess.READ)
|
||||
print(file)
|
||||
if file == null:
|
||||
return ""
|
||||
|
||||
var content := file.get_as_text()
|
||||
file.close()
|
||||
|
||||
return content
|
||||
|
||||
func _on_close_pressed() -> void:
|
||||
main.visible = false
|
||||
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
||||
TextToSpeech._stop_dialogue()
|
||||
Global.chatbot_opened = false
|
||||
TextToSpeech._stop_dialogue()
|
||||
closing_chatbot.emit()
|
||||
|
||||
|
||||
func _on_prompt_edit_gui_input(event: InputEvent) -> void:
|
||||
if event is InputEventKey and event.pressed:
|
||||
print(event.keycode)
|
||||
if event.keycode == KEY_ENTER:
|
||||
if event.shift_pressed:
|
||||
_prompt_edit.insert_text_at_caret("\n")
|
||||
else:
|
||||
_on_generate_pressed()
|
||||
accept_event()
|
||||
Reference in New Issue
Block a user