added english pictures and fixed text translations
This commit is contained in:
1 parent
21c73df3eb
commit
563b4800ee
50 files changed
+280
-1787
No files matched your search
@@ -8,8 +8,10 @@ extends Control
|
||||
@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
|
||||
|
||||
const API_URL := "https://apphubai.wolke.uni-greifswald.de/v1" #TODO insert your url to the llm / api-gateway here
|
||||
const API_TOKEN := ""
|
||||
const API_MODEL := "unsloth/gemma-4-31B-it-qat-GGUF:UD-Q4_K_XL"
|
||||
const MAX_RESPONSE_TOKENS := 200
|
||||
var document_id
|
||||
|
||||
signal closing_chatbot
|
||||
@@ -17,6 +19,7 @@ signal closing_chatbot
|
||||
|
||||
func _ready():
|
||||
_http.request_completed.connect(Callable(self, "_on_request_completed"))
|
||||
_http.timeout = 180.0
|
||||
document_id = option_button.selected
|
||||
helper.play_idle()
|
||||
|
||||
@@ -35,8 +38,6 @@ func _unhandled_input(event: InputEvent) -> void:
|
||||
|
||||
|
||||
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()
|
||||
@@ -44,73 +45,84 @@ func _on_generate_pressed() -> void:
|
||||
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 := ""
|
||||
|
||||
var document := _load_txt("res://addons/godot_llama/demo/documents/%s.md" % [document_id]).strip_edges()
|
||||
var combined_user_message := user_text
|
||||
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
|
||||
"model": API_MODEL,
|
||||
"messages": [
|
||||
{"role": "system", "content": tr("CHATBOT_SYSTEM_PROMPT")},
|
||||
{"role": "user", "content": combined_user_message},
|
||||
],
|
||||
"max_tokens": MAX_RESPONSE_TOKENS,
|
||||
"temperature": 0.1,
|
||||
}
|
||||
|
||||
var headers = [
|
||||
var headers := PackedStringArray([
|
||||
"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
|
||||
)
|
||||
|
||||
"Authorization: Bearer %s" % API_TOKEN,
|
||||
])
|
||||
var endpoint := API_URL.trim_suffix("/") + "/chat/completions"
|
||||
var err := _http.request(endpoint, headers, HTTPClient.METHOD_POST, JSON.stringify(body))
|
||||
if err != OK:
|
||||
print("HTTP Fehler: ", err)
|
||||
|
||||
_stop_helper_animation()
|
||||
output_text.text = _request_failed("HTTP %s" % error_string(err))
|
||||
generate_button.disabled = false
|
||||
return
|
||||
|
||||
$VBoxContainer/SubViewportContainer/SubViewport/AnimationPlayer.play("walk_helper")
|
||||
helper.play_think_walk()
|
||||
generate_button.disabled = true
|
||||
|
||||
func _on_request_completed(
|
||||
result: int,
|
||||
response_code: int,
|
||||
headers: PackedStringArray,
|
||||
_headers: PackedStringArray,
|
||||
body: PackedByteArray
|
||||
) -> void:
|
||||
$VBoxContainer/SubViewportContainer/SubViewport/AnimationPlayer.stop()
|
||||
helper.play_idle()
|
||||
_stop_helper_animation()
|
||||
generate_button.disabled = false
|
||||
var txt := body.get_string_from_utf8()
|
||||
|
||||
if result != HTTPRequest.RESULT_SUCCESS:
|
||||
output_text.text = _request_failed("result %s" % result)
|
||||
return
|
||||
if response_code != 200:
|
||||
output_text.text = _request_failed("HTTP %s" % response_code)
|
||||
push_warning("Chatbot HTTP %s: %s" % [response_code, txt.left(300)])
|
||||
return
|
||||
|
||||
var json = JSON.parse_string(txt)
|
||||
|
||||
if json == null:
|
||||
print("JSON Fehler")
|
||||
var response_text := _extract_reply(json)
|
||||
if response_text.is_empty():
|
||||
output_text.text = _request_failed("empty response")
|
||||
return
|
||||
|
||||
output_text.text = response_text
|
||||
TextToSpeech._on_dialogue({"text": response_text})
|
||||
|
||||
func _extract_reply(json) -> String:
|
||||
if typeof(json) != TYPE_DICTIONARY:
|
||||
return ""
|
||||
if json.has("choices") and json["choices"] is Array and not json["choices"].is_empty():
|
||||
var choice = json["choices"][0]
|
||||
if choice is Dictionary and choice.get("message") is Dictionary:
|
||||
return str(choice["message"].get("content", "")).strip_edges()
|
||||
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
|
||||
return str(json["content"]).strip_edges()
|
||||
if json.get("message") is Dictionary:
|
||||
return str(json["message"].get("content", "")).strip_edges()
|
||||
return ""
|
||||
|
||||
func _request_failed(detail: String) -> String:
|
||||
if TranslationServer.get_locale().begins_with("en"):
|
||||
return "The request failed (%s)." % detail
|
||||
return "Die Anfrage ist fehlgeschlagen (%s)." % detail
|
||||
|
||||
func _stop_helper_animation() -> void:
|
||||
$VBoxContainer/SubViewportContainer/SubViewport/AnimationPlayer.stop()
|
||||
helper.play_idle()
|
||||
|
||||
func _load_txt(path: String) -> String:
|
||||
var file := FileAccess.open(path, FileAccess.READ)
|
||||
|
||||
Reference in new issue
Block a user