Skip to content

Commit 17c2240

Browse files
committed
cleanup
1 parent 6f28db7 commit 17c2240

13 files changed

+129
-38
lines changed

godot/examples/simple/message.gd

+27-8
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,42 @@
11
class_name Message
22
extends Node
33

4+
var ai_avatar = preload ("res://addons/godot-llama-cpp/assets/godot-llama-cpp-1024x1024.svg")
5+
var user_avatar = preload ("res://examples/simple/user.svg")
6+
var system_avatar = preload("res://examples/simple/system.svg")
7+
var stylebox: StyleBoxTexture = StyleBoxTexture.new()
8+
49
@onready var text_container = %Text
510
@onready var icon = %Panel
6-
@export_enum("user", "assistant") var sender: String
11+
@export_enum("user", "assistant", "system") var sender: String:
12+
get:
13+
return sender
14+
set(value):
15+
sender = value
16+
if icon == null: return
17+
if value == "user":
18+
stylebox.texture = user_avatar
19+
elif value == "assistant":
20+
stylebox.texture = ai_avatar
21+
else:
22+
stylebox.texture = system_avatar
23+
icon.add_theme_stylebox_override("panel", stylebox)
724
@export var include_in_prompt: bool = true
8-
var text:
25+
@export var text: String:
926
get:
10-
return text_container.text
27+
return text
1128
set(value):
29+
text = value
30+
if text_container == null:
31+
return
1232
text_container.text = value
1333

1434
var completion_id: int = -1
1535
var pending: bool = false
1636
var errored: bool = false
1737

18-
func set_text(new_text: String):
19-
text_container.text = new_text
20-
21-
func append_text(new_text: String):
22-
text_container.text += new_text
38+
func _ready():
39+
text_container.text = text
40+
sender = sender
41+
2342

godot/examples/simple/message.tscn

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
[gd_scene load_steps=5 format=3 uid="uid://t862t0v8ht2q"]
1+
[gd_scene load_steps=4 format=3 uid="uid://t862t0v8ht2q"]
22

33
[ext_resource type="Script" path="res://examples/simple/message.gd" id="1_pko33"]
4-
[ext_resource type="Texture2D" uid="uid://dplw232htshgc" path="res://addons/godot-llama-cpp/assets/godot-llama-cpp-1024x1024.svg" id="2_dvc7y"]
4+
[ext_resource type="Texture2D" uid="uid://dplw232htshgc" path="res://addons/godot-llama-cpp/assets/godot-llama-cpp-1024x1024.svg" id="2_xf8it"]
55

6-
[sub_resource type="StyleBoxTexture" id="StyleBoxTexture_t8bgj"]
7-
texture = ExtResource("2_dvc7y")
8-
9-
[sub_resource type="Theme" id="Theme_bw3pb"]
10-
Panel/styles/panel = SubResource("StyleBoxTexture_t8bgj")
6+
[sub_resource type="StyleBoxTexture" id="StyleBoxTexture_ki268"]
7+
texture = ExtResource("2_xf8it")
118

129
[node name="RichTextLabel" type="HBoxContainer"]
1310
anchors_preset = 15
@@ -18,14 +15,13 @@ grow_vertical = 2
1815
size_flags_horizontal = 3
1916
theme_override_constants/separation = 20
2017
script = ExtResource("1_pko33")
21-
sender = "assistant"
2218

2319
[node name="Panel" type="Panel" parent="."]
2420
unique_name_in_owner = true
2521
custom_minimum_size = Vector2(80, 80)
2622
layout_mode = 2
2723
size_flags_vertical = 0
28-
theme = SubResource("Theme_bw3pb")
24+
theme_override_styles/panel = SubResource("StyleBoxTexture_ki268")
2925

3026
[node name="Text" type="RichTextLabel" parent="."]
3127
unique_name_in_owner = true

godot/examples/simple/simple.gd

+4-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ func _on_text_edit_submit(input: String) -> void:
99
handle_input(input)
1010

1111
func handle_input(input: String) -> void:
12-
var messages = [{ "sender": "system", "text": "You are a pirate chatbot who always responds in pirate speak!" }]
12+
var messages = []
1313
messages.append_array(messages_container.get_children().filter(func(msg: Message): return msg.include_in_prompt).map(
1414
func(msg: Message) -> Dictionary:
1515
return { "text": msg.text, "sender": msg.sender }
@@ -22,7 +22,7 @@ func handle_input(input: String) -> void:
2222

2323
var user_message: Message = message.instantiate()
2424
messages_container.add_child(user_message)
25-
user_message.set_text(input)
25+
user_message.text = input
2626
user_message.sender = "user"
2727
user_message.completion_id = completion_id
2828

@@ -31,7 +31,6 @@ func handle_input(input: String) -> void:
3131
ai_message.sender = "assistant"
3232
ai_message.completion_id = completion_id
3333
ai_message.pending = true
34-
ai_message.grab_focus()
3534

3635
func _on_llama_context_completion_generated(chunk: Dictionary) -> void:
3736
var completion_id = chunk.id
@@ -43,6 +42,6 @@ func _on_llama_context_completion_generated(chunk: Dictionary) -> void:
4342
elif chunk.has("text"):
4443
if msg.pending:
4544
msg.pending = false
46-
msg.set_text(chunk["text"])
45+
msg.text = chunk["text"]
4746
else:
48-
msg.append_text(chunk["text"])
47+
msg.text += chunk["text"]

godot/examples/simple/simple.tscn

+2-6
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,8 @@ theme_override_constants/separation = 30
4545

4646
[node name="RichTextLabel2" parent="Panel/MarginContainer/VBoxContainer/ScrollContainer/MessagesContainer" instance=ExtResource("2_7iip7")]
4747
layout_mode = 2
48-
include_in_prompt = false
49-
50-
[node name="Text" parent="Panel/MarginContainer/VBoxContainer/ScrollContainer/MessagesContainer/RichTextLabel2" index="1"]
51-
text = "How can I help you?"
48+
sender = "system"
49+
text = "You are a pirate chatbot who always responds in pirate speak!"
5250

5351
[node name="HBoxContainer" type="HBoxContainer" parent="Panel/MarginContainer/VBoxContainer"]
5452
layout_mode = 2
@@ -74,5 +72,3 @@ unique_name_in_owner = true
7472
[connection signal="submit" from="Panel/MarginContainer/VBoxContainer/HBoxContainer/TextEdit" to="." method="_on_text_edit_submit"]
7573
[connection signal="pressed" from="Panel/MarginContainer/VBoxContainer/HBoxContainer/Button" to="Panel/MarginContainer/VBoxContainer/HBoxContainer/TextEdit" method="_on_button_pressed"]
7674
[connection signal="completion_generated" from="LlamaContext" to="." method="_on_llama_context_completion_generated"]
77-
78-
[editable path="Panel/MarginContainer/VBoxContainer/ScrollContainer/MessagesContainer/RichTextLabel2"]

godot/examples/simple/system.svg

+9
Loading
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="CompressedTexture2D"
5+
uid="uid://ckurx0p0r4v1t"
6+
path="res://.godot/imported/system.svg-d1b27c65464663409463ff5f8dfc9953.ctex"
7+
metadata={
8+
"vram_texture": false
9+
}
10+
11+
[deps]
12+
13+
source_file="res://examples/simple/system.svg"
14+
dest_files=["res://.godot/imported/system.svg-d1b27c65464663409463ff5f8dfc9953.ctex"]
15+
16+
[params]
17+
18+
compress/mode=0
19+
compress/high_quality=false
20+
compress/lossy_quality=0.7
21+
compress/hdr_compression=1
22+
compress/normal_map=0
23+
compress/channel_pack=0
24+
mipmaps/generate=false
25+
mipmaps/limit=-1
26+
roughness/mode=0
27+
roughness/src_normal=""
28+
process/fix_alpha_border=true
29+
process/premult_alpha=false
30+
process/normal_map_invert_y=false
31+
process/hdr_as_srgb=false
32+
process/hdr_clamp_exposure=false
33+
process/size_limit=0
34+
detect_3d/compress_to=1
35+
svg/scale=1.0
36+
editor/scale_with_editor_scale=false
37+
editor/convert_colors_with_editor_theme=false

godot/examples/simple/user.svg

+1
Loading

godot/examples/simple/user.svg.import

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="CompressedTexture2D"
5+
uid="uid://l8xuk1t4hnof"
6+
path="res://.godot/imported/user.svg-fbe84d041cfa09d8365142161c3b195f.ctex"
7+
metadata={
8+
"vram_texture": false
9+
}
10+
11+
[deps]
12+
13+
source_file="res://examples/simple/user.svg"
14+
dest_files=["res://.godot/imported/user.svg-fbe84d041cfa09d8365142161c3b195f.ctex"]
15+
16+
[params]
17+
18+
compress/mode=0
19+
compress/high_quality=false
20+
compress/lossy_quality=0.7
21+
compress/hdr_compression=1
22+
compress/normal_map=0
23+
compress/channel_pack=0
24+
mipmaps/generate=false
25+
mipmaps/limit=-1
26+
roughness/mode=0
27+
roughness/src_normal=""
28+
process/fix_alpha_border=true
29+
process/premult_alpha=false
30+
process/normal_map_invert_y=false
31+
process/hdr_as_srgb=false
32+
process/hdr_clamp_exposure=false
33+
process/size_limit=0
34+
detect_3d/compress_to=1
35+
svg/scale=1.0
36+
editor/scale_with_editor_scale=false
37+
editor/convert_colors_with_editor_theme=false

godot/project.godot

-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,3 @@ config/icon="res://icon.svg"
1818
[editor_plugins]
1919

2020
enabled=PackedStringArray("res://addons/godot-llama-cpp/plugin.cfg")
21-
22-
[gui]
23-
24-
theme/default_theme_scale=2.0

llama.cpp

src/llama_context.cpp

-5
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ void LlamaContext::_enter_tree() {
6767
return;
6868
}
6969

70-
model->load_model();
71-
7270
if (model->model == NULL) {
7371
UtilityFunctions::printerr(vformat("%s: Failed to initialize llama context, model property not defined", __func__));
7472
return;
@@ -336,9 +334,6 @@ void LlamaContext::_exit_tree() {
336334
if (ctx) {
337335
llama_free(ctx);
338336
}
339-
if (model->model) {
340-
llama_free_model(model->model);
341-
}
342337
if (sampling_ctx) {
343338
llama_sampling_free(sampling_ctx);
344339
}

src/llama_model.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <godot_cpp/classes/project_settings.hpp>
44
#include <godot_cpp/core/class_db.hpp>
55
#include <godot_cpp/variant/utility_functions.hpp>
6+
#include <godot_cpp/classes/engine.hpp>
67

78
using namespace godot;
89

@@ -23,6 +24,10 @@ void LlamaModel::load_model() {
2324
return;
2425
}
2526

27+
if (Engine::get_singleton()->is_editor_hint()) {
28+
return;
29+
}
30+
2631
String absPath = ProjectSettings::get_singleton()->globalize_path(get_path());
2732

2833
model = llama_load_model_from_file(absPath.utf8().get_data(), model_params);

src/llama_model_loader.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Variant godot::LlamaModelLoader::_load(const String &path, const String &origina
2020
}
2121

2222
model->set_path(path);
23+
model->load_model();
2324

2425
return { model };
2526
}

0 commit comments

Comments
 (0)