summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile13
-rw-r--r--README.md26
-rw-r--r--corpus/lotr.txt (renamed from context.txt)0
-rw-r--r--npc.c8
-rw-r--r--prompts/lotr.h (renamed from system_prompt.h)4
-rw-r--r--prompts/lotr.txt (renamed from system_prompt.txt)0
6 files changed, 34 insertions, 17 deletions
diff --git a/Makefile b/Makefile
index ba4deab..0d27bd6 100644
--- a/Makefile
+++ b/Makefile
@@ -10,9 +10,12 @@ LDFLAGS = -L$(LLAMA_DIR)/build/src -L$(LLAMA_DIR)/build/ggml/src \
-lpthread -lm -ldl -lstdc++ -g \
-lllama -lggml -lggml-cpu -lggml-base
+PROMPT_TXT := $(wildcard prompts/*.txt)
+PROMPT_HEADERS := $(PROMPT_TXT:.txt=.h)
+
help: .help
-build/npc: run/system-prompt npc.c vectordb.c models.h # Build npc binary for testing
+build/npc: build/prompts npc.c vectordb.c models.h # Build npc binary for testing
$(CC) $(CFLAGS) npc.c vectordb.c -o npc $(LDFLAGS)
build/context: context.c vectordb.c models.h # Build context binary for testing
@@ -24,6 +27,8 @@ build/llama.cpp: .assure # Build llama.cpp libraries
cmake ../ -DBUILD_SHARED_LIBS=OFF && \
make -j8
+build/prompts: $(PROMPT_HEADERS) # Generate C style header
+
run/fetch-models: .assure # Fetch GGUF models
-mkdir -p models
cd models && wget -nc -i ../models.txt
@@ -32,10 +37,10 @@ run/docker: .assure # Runs npc in Docker container
docker build -t npcd .
docker run -it npcd
-run/system-prompt: .assure # Generate C style header
- xxd -i system_prompt.txt > system_prompt.h
-
run/clean: # Cleans up all the build artefacts
-rm -f npc
cd $(LLAMA_DIR)/build && make clean
-rm -Rf $(LLAMA_DIR)/build
+
+prompts/%.h: prompts/%.txt .assure
+ xxd -i $< > $@
diff --git a/README.md b/README.md
index fee82f8..a20bcef 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,18 @@
-# llmnpc
+An experiment using tiny LLMs as NPCs that could be embedded into the game.
-Command-line tooling for NPC-focused LLM experiments with lightweight context
-retrieval, powered by [llama.cpp](https://github.com/ggerganov/llama.cpp).
+Goals of the experiment:
+
+- Have LLM be run only on CPU, this is why small LLMs have been chosen in this
+ experiment, so they can be used in other games.
+- To produce a simple C library that can be reused elsewhere.
+- Test existing small and tiny LLMs and provide some useful results on how they
+ behave.
+
+> [!NOTE]
+> This project is just for fun, to see how LLMs would fare as NPCs. Because of
+> the non-deterministic nature of LLMs, the results vary and are often quite
+> funny. A lot of tweaking would be needed to make this really useful in real
+> games, but not impossible.
## Building
@@ -26,6 +37,7 @@ retrieval, powered by [llama.cpp](https://github.com/ggerganov/llama.cpp).
3. Build binaries:
```bash
make build/context
+ make build/prompts
make build/npc
```
@@ -37,8 +49,8 @@ retrieval, powered by [llama.cpp](https://github.com/ggerganov/llama.cpp).
produces a binary vector database file.
```bash
-./context -i context.txt -o context.vdb
-./context -m flan-t5-small -i context.txt -o context.vdb
+./context -i corpus/lotr.txt -o corpus/lotr.vdb
+./context -m flan-t5-small -i corpus/lotr.txt -o corpus/lotr.vdb
```
### Run an NPC query with retrieved context
@@ -47,8 +59,8 @@ produces a binary vector database file.
lines by cosine similarity, and runs the NPC system prompt against that context.
```bash
-./npc -m flan-t5-small -p "Who is Gandalf?" -c context.vdb
-./npc -m flan-t5-small -p "Who is Frodo?" -c context.vdb
+./npc -m flan-t5-small -p "Who is Gandalf?" -c corpus/lotr.vdb
+./npc -m flan-t5-small -p "Who is Frodo?" -c corpus/lotr.vdb
```
### context options
diff --git a/context.txt b/corpus/lotr.txt
index 1d97eb3..1d97eb3 100644
--- a/context.txt
+++ b/corpus/lotr.txt
diff --git a/npc.c b/npc.c
index 01c980c..5450866 100644
--- a/npc.c
+++ b/npc.c
@@ -11,7 +11,7 @@
#include <string.h>
#include <getopt.h>
-#include "system_prompt.h"
+#include "prompts/lotr.h"
static void llama_log_callback(enum ggml_log_level level, const char *text, void *user_data) {
(void)level;
@@ -54,13 +54,13 @@ static int execute_prompt_with_context(const ModelConfig *cfg, const char *promp
return 1;
}
- char *system_prefix = (char *)malloc(system_prompt_txt_len + 1);
+ char *system_prefix = (char *)malloc(prompts_lotr_txt_len + 1);
if (system_prefix == NULL) {
log_message(stderr, LOG_ERROR, "Failed to allocate system prompt");
return 1;
}
- memcpy(system_prefix, system_prompt_txt, system_prompt_txt_len);
- system_prefix[system_prompt_txt_len] = '\0';
+ memcpy(system_prefix, prompts_lotr_txt, prompts_lotr_txt_len);
+ system_prefix[prompts_lotr_txt_len] = '\0';
ggml_backend_load_all();
diff --git a/system_prompt.h b/prompts/lotr.h
index 119b7f2..5210021 100644
--- a/system_prompt.h
+++ b/prompts/lotr.h
@@ -1,4 +1,4 @@
-unsigned char system_prompt_txt[] = {
+unsigned char prompts_lotr_txt[] = {
0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x3a, 0x20, 0x41, 0x6e, 0x73, 0x77,
0x65, 0x72, 0x20, 0x75, 0x73, 0x69, 0x6e, 0x67, 0x20, 0x6f, 0x6e, 0x6c,
0x79, 0x20, 0x74, 0x68, 0x65, 0x20, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x78,
@@ -12,4 +12,4 @@ unsigned char system_prompt_txt[] = {
0x74, 0x68, 0x61, 0x74, 0x20, 0x69, 0x6e, 0x66, 0x6f, 0x72, 0x6d, 0x61,
0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x0a
};
-unsigned int system_prompt_txt_len = 138;
+unsigned int prompts_lotr_txt_len = 138;
diff --git a/system_prompt.txt b/prompts/lotr.txt
index 2f997b9..2f997b9 100644
--- a/system_prompt.txt
+++ b/prompts/lotr.txt