Skip to content

Commit 370e440

Browse files
author
Chris Warren-Smith
committed
LLAMA: replace test_main with nitro agent application
1 parent 702026e commit 370e440

7 files changed

Lines changed: 294 additions & 192 deletions

File tree

llama/CMakeLists.txt

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -150,27 +150,68 @@ set_target_properties(llm PROPERTIES
150150
)
151151

152152
# -----------------------------
153-
# Optional test application
153+
# nitro agent application
154+
# (only built when notcurses is available)
154155
# -----------------------------
155-
add_executable(llm_test
156-
test_main.cpp
157-
)
156+
find_package(PkgConfig QUIET)
157+
158+
set(NC_FOUND FALSE)
159+
set(NC_TARGET "")
160+
161+
if(DEFINED NOTCURSES_DIR)
162+
# Explicit path — create an imported target manually
163+
find_library(NC_LIB NAMES notcurses HINTS "${NOTCURSES_DIR}/lib" REQUIRED)
164+
find_library(NC_CORE_LIB NAMES notcurses-core HINTS "${NOTCURSES_DIR}/lib")
165+
add_library(notcurses_imported INTERFACE IMPORTED)
166+
target_include_directories(notcurses_imported INTERFACE "${NOTCURSES_DIR}/include")
167+
target_link_libraries(notcurses_imported INTERFACE ${NC_LIB})
168+
if(NC_CORE_LIB)
169+
target_link_libraries(notcurses_imported INTERFACE ${NC_CORE_LIB})
170+
endif()
171+
set(NC_TARGET notcurses_imported)
172+
set(NC_FOUND TRUE)
173+
174+
elseif(PkgConfig_FOUND)
175+
# IMPORTED_TARGET gives a PkgConfig::NC target with full lib paths baked in
176+
pkg_check_modules(NC QUIET IMPORTED_TARGET notcurses)
177+
if(NC_FOUND)
178+
set(NC_TARGET PkgConfig::NC)
179+
endif()
158180

159-
target_include_directories(llm_test PRIVATE
160-
${LLAMA_DIR}/include
161-
${LLAMA_DIR}/ggml/include
162-
${CMAKE_CURRENT_SOURCE_DIR}/../include
163-
)
181+
else()
182+
find_library(NC_LIB NAMES notcurses)
183+
find_library(NC_CORE_LIB NAMES notcurses-core)
184+
if(NC_LIB AND NC_CORE_LIB)
185+
add_library(notcurses_imported INTERFACE IMPORTED)
186+
target_link_libraries(notcurses_imported INTERFACE ${NC_LIB} ${NC_CORE_LIB})
187+
set(NC_TARGET notcurses_imported)
188+
set(NC_FOUND TRUE)
189+
endif()
190+
endif()
164191

165-
target_link_libraries(llm_test PRIVATE
166-
llm
167-
llama
168-
ggml
169-
)
192+
if(NC_FOUND)
193+
message(STATUS "notcurses found — building nitro")
170194

171-
set_target_properties(llm_test PROPERTIES
172-
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
173-
)
195+
add_executable(nitro
196+
nitro.cpp
197+
)
198+
target_include_directories(nitro PRIVATE
199+
${LLAMA_DIR}/include
200+
${LLAMA_DIR}/ggml/include
201+
${CMAKE_CURRENT_SOURCE_DIR}/../include
202+
)
203+
target_link_libraries(nitro PRIVATE
204+
llm
205+
llama
206+
ggml
207+
${NC_TARGET} # imported target carries include + lib paths
208+
)
209+
set_target_properties(nitro PROPERTIES
210+
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
211+
)
212+
else()
213+
message(STATUS "notcurses not found — skipping nitro (set -DNOTCURSES_DIR=... to enable)")
214+
endif()
174215

175216
# -----------------------------
176217
# RAG indexer

llama/llama-sb-rag.cpp

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// Copyright(C) 2026 Chris Warren-Smith
77

88
#include "llama-sb.h"
9+
#include "llama-sb-rag.h"
910

1011
#include <algorithm>
1112
#include <cmath>
@@ -17,55 +18,6 @@
1718
#include <string>
1819
#include <vector>
1920

20-
struct RagChunk {
21-
std::string text;
22-
std::string source;
23-
std::string type;
24-
std::vector<float> embedding;
25-
};
26-
27-
struct RagDB {
28-
std::vector<RagChunk> chunks;
29-
int embed_dim = 0;
30-
31-
int size() const { return (int)chunks.size(); }
32-
bool empty() const { return chunks.empty(); }
33-
};
34-
35-
//
36-
// per-session deduplication + token budget
37-
//
38-
struct RagSession {
39-
std::vector<bool> seen; /* sized to db.size() on init */
40-
int tokens_used = 0;
41-
int tokens_max = 0; /* set to your n_ctx */
42-
float score_threshold = 0.60f; /* skip weak matches */
43-
44-
void init(int n_chunks, int ctx_size) {
45-
seen.assign(n_chunks, false);
46-
tokens_used = 0;
47-
tokens_max = ctx_size;
48-
}
49-
50-
void reset() {
51-
std::fill(seen.begin(), seen.end(), false);
52-
tokens_used = 0;
53-
}
54-
55-
bool is_seen(int idx) const { return idx < (int)seen.size() && seen[idx]; }
56-
void mark(int idx) { if (idx < (int)seen.size()) seen[idx] = true; }
57-
58-
/* rough token estimate: 1 token ≈ 4 chars */
59-
bool budget_ok(const std::string &text) const {
60-
return tokens_max == 0 ||
61-
(tokens_used + (int)text.size() / 4) < (int)(tokens_max * 0.85f);
62-
}
63-
64-
void charge(const std::string &text) {
65-
tokens_used += (int)text.size() / 4;
66-
}
67-
};
68-
6921
bool Llama::embed_text(const std::string &text, std::vector<float> &out, int embed_dim) {
7022
vector<llama_token> tokens = tokenize(text);
7123
if (tokens.size() == 0) {

llama/llama-sb-rag.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// This file is part of SmallBASIC
2+
//
3+
// This program is distributed under the terms of the GPL v2.0 or later
4+
// Download the GNU Public License (GPL) from www.gnu.org
5+
//
6+
// Copyright(C) 2026 Chris Warren-Smith
7+
8+
#pragma once
9+
10+
struct RagChunk {
11+
std::string text;
12+
std::string source;
13+
std::string type;
14+
std::vector<float> embedding;
15+
};
16+
17+
struct RagDB {
18+
std::vector<RagChunk> chunks;
19+
int embed_dim = 0;
20+
21+
int size() const { return (int)chunks.size(); }
22+
bool empty() const { return chunks.empty(); }
23+
};
24+
25+
//
26+
// per-session deduplication + token budget
27+
//
28+
struct RagSession {
29+
std::vector<bool> seen; /* sized to db.size() on init */
30+
int tokens_used = 0;
31+
int tokens_max = 0; /* set to your n_ctx */
32+
float score_threshold = 0.60f; /* skip weak matches */
33+
34+
void init(int n_chunks, int ctx_size) {
35+
seen.assign(n_chunks, false);
36+
tokens_used = 0;
37+
tokens_max = ctx_size;
38+
}
39+
40+
void reset() {
41+
std::fill(seen.begin(), seen.end(), false);
42+
tokens_used = 0;
43+
}
44+
45+
bool is_seen(int idx) const { return idx < (int)seen.size() && seen[idx]; }
46+
void mark(int idx) { if (idx < (int)seen.size()) seen[idx] = true; }
47+
48+
/* rough token estimate: 1 token ≈ 4 chars */
49+
bool budget_ok(const std::string &text) const {
50+
return tokens_max == 0 ||
51+
(tokens_used + (int)text.size() / 4) < (int)(tokens_max * 0.85f);
52+
}
53+
54+
void charge(const std::string &text) {
55+
tokens_used += (int)text.size() / 4;
56+
}
57+
};
58+

llama/llama-sb.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,6 @@ void Llama::reset() {
130130
_min_p = 0.0f;
131131
_max_tokens = 150;
132132
_n_past = 0;
133-
_is_gemma4 = false;
134-
_grammar_src.clear();
135-
_grammar_root.clear();
136-
_template.clear();
137133
_seed = LLAMA_DEFAULT_SEED;
138134
if (_ctx) {
139135
llama_memory_clear(llama_get_memory(_ctx), true);

llama/llama.cpp

0 commit comments

Comments
 (0)