1
- #include " llama_context.h"
1
+ #include " llama_context.h"
2
+ #include " llama.h"
3
+ #include " llama_model.h"
4
+ #include < godot_cpp/classes/engine.hpp>
5
+ #include < godot_cpp/classes/os.hpp>
6
+ #include < godot_cpp/core/class_db.hpp>
7
+ #include < godot_cpp/variant/utility_functions.hpp>
8
+
9
+ using namespace godot ;
10
+
11
+ void LlamaContext::set_model (const Ref<LlamaModel> p_model) {
12
+ model = p_model;
13
+ }
14
+
15
+ Ref<LlamaModel> LlamaContext::get_model () {
16
+ return model;
17
+ }
18
+
19
+ void LlamaContext::_ready () {
20
+ // TODO: remove this and use runtime classes once godot 4.3 lands, see https://github.com/godotengine/godot/pull/82554
21
+ if (Engine::get_singleton ()->is_editor_hint ()) {
22
+ return ;
23
+ }
24
+
25
+ if (model->model == NULL ) {
26
+ UtilityFunctions::printerr (vformat (" %s: Failed to initialize llama context, model property not defined" , __func__));
27
+ return ;
28
+ }
29
+
30
+ ctx_params.n_ctx = 2048 ;
31
+ int32_t n_threads = OS::get_singleton ()->get_processor_count ();
32
+ ctx_params.n_threads = n_threads;
33
+ ctx_params.n_threads_batch = n_threads;
34
+
35
+ ctx = llama_new_context_with_model (model->model , ctx_params);
36
+ if (ctx == NULL ) {
37
+ UtilityFunctions::printerr (vformat (" %s: Failed to initialize llama context, null ctx" , __func__));
38
+ return ;
39
+ }
40
+ UtilityFunctions::print (vformat (" %s: Context initialized" , __func__));
41
+ }
42
+
43
+ LlamaContext::~LlamaContext () {
44
+ if (ctx) {
45
+ llama_free (ctx);
46
+ }
47
+ }
48
+
49
+ void LlamaContext::_bind_methods () {
50
+ ClassDB::bind_method (D_METHOD (" set_model" , " model" ), &LlamaContext::set_model);
51
+ ClassDB::bind_method (D_METHOD (" get_model" ), &LlamaContext::get_model);
52
+ ClassDB::add_property (" LlamaContext" , PropertyInfo (Variant::OBJECT, " model" , PROPERTY_HINT_RESOURCE_TYPE, " LlamaModel" ), " set_model" , " get_model" );
53
+ }
0 commit comments