Godot 4.x GDExtension wrapper for llama.cpp.
Implemented:
godot-cppandthird_party/llama.cppsubmodules- GDExtension build setup via
SConstruct - Native classes registered to Godot:
LlamaModelLlamaSamplerLlamaContextLlamaAsyncWorker
- Addon manifest and GDScript facade in
addons/godot_llama/ LlamaModelnow uses realllama.cppmodel loading, tokenization, detokenization, vocab size, and metadata APIs.LlamaContextnow uses realllama.cppcontext creation, sampling, synchronous generation, streaming generation signals, cancellation, and perf stats.- Runnable demo scene with GUI controls for:
- GGUF file selection (file picker)
- model/context creation
- prompt/system/world-state editing
- generation settings and streaming
- short conversation memory and memory reset
git submodule update --init --recursivegodot-cpp is set to branch 4.4.
Prereqs:
- Python 3.8+
- SCons 4+
- C++ toolchain for your platform
Build llama.cpp shared libs (Windows):
cmake -S third_party/llama.cpp -B third_party/llama.cpp/build_shared -DGGML_SHARED=ON -DBUILD_SHARED_LIBS=ON
cmake --build third_party/llama.cpp/build_shared --config ReleaseBuild extension:
$env:LLAMA_CPP_BUILD_DIR="third_party/llama.cpp/build_shared"
scons target=template_debug platform=windows use_static_cpp=no -j8
scons target=template_release platform=windows use_static_cpp=no -j8The SCons build automatically copies required runtime DLLs (llama.dll, ggml.dll, ggml-base.dll, ggml-cpu.dll, mtmd.dll) to:
addons/godot_llama/bin/- repo root (
./) for editor-time Windows DLL resolution.
Build llama.cpp static libs (Linux):
cmake -S third_party/llama.cpp -B third_party/llama.cpp/build \
-DBUILD_SHARED_LIBS=OFF \
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
cmake --build third_party/llama.cpp/build --config ReleaseBuild extension (Linux):
scons target=template_debug platform=linux use_static_cpp=no -j8
scons target=template_release platform=linux use_static_cpp=no -j8Artifacts are emitted under:
addons/godot_llama/bin/
Optional override for non-default llama build output:
LLAMA_CPP_BUILD_DIR(example:third_party/llama.cpp/build_shared)LLAMA_CPP_LINK_STATIC=1to force static llama.cpp linking (Linux defaults to static when the variable is unset)LLAMA_CPP_OPENMP=0to skip linking OpenMP on Linux (use this if you built llama.cpp with-DGGML_OPENMP=OFF)use_static_cpp=nois required on Linux to avoid crashes from mixing static libstdc++ with Godot's runtime
addons/godot_llama/godot_llama.gdextensionaddons/godot_llama/godot_llama.gd
This repo intentionally does not include a project.godot.
Demo files are included inside the addon:
addons/godot_llama/demo/demo.tscnaddons/godot_llama/demo/demo.gd
To run the demo:
- Open your own Godot 4.4+ project.
- Copy or include
addons/godot_llama/in that project. - Open
res://addons/godot_llama/demo/demo.tscn. - Click
Select Model...and choose a.gguffile. - Click
Load Model, thenCreate Context. - Set
System prompt,World state, andhistory_turnsas needed. - Optionally tune advanced generation controls:
top_k,min_p, repetition/presence/frequency penalties,penalty_last_n, and comma-separated stop sequences. - Use state controls when needed:
Clear KVclears context cache.Save File/Load Filepersist and restore context state at the providedstate path(for exampleuser://llama_state.session).Save Blob/Load Blobsave and restore state in memory.
- Enter a player prompt and click
Generate.
Recommended starting settings for NPC dialog:
n_ctx:1024or2048temperature:0.6to0.8top_p:0.85to0.95max_tokens:80to160
Generation parameter keys accepted by LlamaContext.generate(...) / generate_stream(...):
max_tokens(int)temperature(float)top_k(int)top_p(float)min_p(float)seed(int)repeat_penalty(float)frequency_penalty(float)presence_penalty(float)penalty_last_n(int)stop(String,Array[String], orPackedStringArray)stop_sequences(alias forstop)reuse_kv(bool, defaultfalse; settrueonly when intentionally continuing from current KV state)
State/session helpers on LlamaContext:
clear_kv_cache()save_state() -> PackedByteArrayload_state(state: PackedByteArray) -> Errorsave_state_file(path: String) -> Errorload_state_file(path: String) -> Error