Skip to content

Commit a0223d0

Browse files
author
Chris Warren-Smith
committed
LLAMA: update nitro sample
1 parent 555cb1f commit a0223d0

3 files changed

Lines changed: 71 additions & 65 deletions

File tree

llama/llama-sb.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,14 @@ bool Llama::construct(string model_path, int n_ctx, int n_batch, int n_gpu_layer
138138
cparams.n_ubatch = n_batch;
139139
cparams.no_perf = true;
140140
cparams.attention_type = LLAMA_ATTENTION_TYPE_UNSPECIFIED;
141-
cparams.flash_attn_type = LLAMA_FLASH_ATTN_TYPE_AUTO;
141+
cparams.flash_attn_type = LLAMA_FLASH_ATTN_TYPE_ENABLED;
142+
143+
// or Q4_0 for more aggressive saving
144+
cparams.type_k = GGML_TYPE_Q8_0;
145+
cparams.type_v = GGML_TYPE_Q8_0;
146+
147+
// keep KV cache on GPU
148+
cparams.offload_kqv = true;
142149

143150
_ctx = llama_init_from_model(_model, cparams);
144151
if (!_ctx) {

llama/samples/nitro.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ Requirements:
104104
- Write complete and valid content
105105
- Do not overwrite without clear intent
106106
- Preserve formatting
107+
- The complete format is TOOL:WRITE filename file-content-string
107108

108109
---
109110

llama/samples/nitro_cli.bas

Lines changed: 62 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import llm
77

88
' --- Configuration ---
9-
const model = "models/google_gemma-4-E4B-it-Q4_K_L.gguf"
9+
#const model = "models/google_gemma-4-E4B-it-Q4_K_L.gguf"
10+
const model = "models/qwen2.5-coder-3b-instruct-q8_0.gguf"
1011
const knowledge_files = ["nitro.md"]
1112

1213
' ANSI Color Codes
@@ -20,7 +21,7 @@ const BOLD_CYAN = chr(27) + "[1;36m"
2021
const CHANNEL_END = "<channel|>"
2122

2223
' llama configuration
23-
const n_ctx = 8000
24+
const n_ctx = 32768
2425
const n_batch = 512
2526
const n_max_tokens = 4096
2627
const n_temperature = 0.2
@@ -36,13 +37,6 @@ sandbox_home = cwd
3637
' Displays the welcome message
3738
'
3839
sub welcome_message()
39-
print
40-
print BOLD_CYAN;
41-
print " . · ✦ . · "
42-
print " · . · "
43-
print " ✦ P · I · C · A · R · D ✦ "
44-
print " . · . "
45-
print " . · ✦ . · "
4640
print
4741
print BOLD_CYAN + " P I C A R D A G E N T S Y S T E M v1.0" + RESET
4842
print
@@ -57,14 +51,14 @@ end sub
5751
' handles the TOOL:LIST command
5852
'
5953
func list_files(arg)
60-
if (arg == "./") then
54+
if (arg == "./") then
6155
arg = sandbox_home + arg
6256
else if (len(arg) == 0 or arg == ".") then
6357
arg = sandbox_home
6458
endif
65-
59+
6660
local result = []
67-
61+
6862
func walker(node)
6963
if (node.depth == 0) then
7064
if (node.dir && left(node.name, 1) != ".") then
@@ -90,27 +84,63 @@ func read_file(arg)
9084
result = "ERROR: File not found or unreadable."
9185
end try
9286
return result
93-
end
87+
end
88+
89+
'
90+
' removes markdown backticks from code blocks
91+
'
92+
func strip_code_fences(s)
93+
local result = s
94+
local pos = instr(s, "```")
95+
if (pos) then
96+
local nl = instr(pos + 3, s, chr(10))
97+
if (nl) then
98+
result = mid(s, nl + 1)
99+
pos = instr(result, "```")
100+
if (pos) then
101+
result = left(result, pos - 1)
102+
endif
103+
endif
104+
endif
105+
return result
106+
end
94107

95108
'
96109
' handles the TOOL:WRITE command
97110
'
98-
func write_file(arg)
99-
result = "OK: Data written successfully to " + arg
111+
func write_file(arg, s)
112+
try
113+
tsave sandbox_home + arg, s
114+
result = "OK: Data written successfully to " + arg
115+
catch e
116+
result = "ERROR: " + e
117+
end try
100118
return result
101119
end
102120

103121
'
104122
' Handles file system commands received from the LLM.
105123
'
106-
func handle_cmd(cmd)
107-
local v, result
124+
func process_tool(cmd)
125+
local result, op, arg1, arg2
126+
127+
local pos1 = instr(cmd, " ")
128+
if (pos1 > 0) then
129+
op = left(cmd, pos1 - 1)
130+
local pos2 = instr(pos1 + 1, cmd, " ")
131+
if (pos2 > 0) then
132+
arg1 = mid(cmd, pos1 + 1, pos2 - pos1 - 1)
133+
arg2 = mid(cmd, pos2 + 1)
134+
else
135+
arg1 = mid(cmd, pos1 + 1)
136+
endif
137+
endif
108138

109-
split(cmd, " ", v)
110-
local op = v[0]
111-
local arg = iff(len(v) == 2, v[1], "")
112-
113-
print RED + "TOOL:" + op + " - " + arg + RESET
139+
' print RED
140+
' print "["+op+"]"
141+
' print "["+arg1+"]"
142+
' print "["+arg2+"]"
143+
' print RESET
114144

115145
select case op
116146
case "TOOL:DATE"
@@ -120,11 +150,11 @@ func handle_cmd(cmd)
120150
case "TOOL:RND"
121151
result = rnd
122152
case "TOOL:LIST"
123-
result = list_files(arg)
153+
result = list_files(arg1)
124154
case "TOOL:READ"
125-
result = read_file(arg)
155+
result = read_file(arg1)
126156
case "TOOL:WRITE"
127-
result = write_file(arg)
157+
result = write_file(arg1, strip_code_fences(arg2))
128158
case else
129159
result = "ERROR: unknown command " + op
130160
end select
@@ -150,26 +180,9 @@ func initialize_agent()
150180
end try
151181
next
152182

153-
' Set the initial system prompt for the LLM
154-
print YELLOW;
155-
print " ╔═══════════════════════════════════════╗"
156-
print " ║ > PICARD_ ║"
157-
print " ║ > STATUS: ENGAGED ║"
158-
print " ║ > STARDATE: 42026.421 ║"
159-
print " ║ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ 100% READY ║"
160-
print " ╚═══════════════════════════════════════╝"
161-
print
162-
print RESET
163183
return prompt
164184
end
165185

166-
'
167-
' Execute the given tool
168-
'
169-
func process_tool(tool)
170-
return handle_cmd(trim(tool))
171-
end
172-
173186
'
174187
' Returns the user user input
175188
'
@@ -213,42 +226,28 @@ sub main()
213226

214227
while iter.has_next()
215228
buffer += iter.next()
216-
local chan_end = instr(buffer, CHANNEL_END)
217-
218-
if chan_end != 0 then
219-
' print buffer up to channel_end
220-
buffer = left(buffer, chan_end - 1)
221-
print text_colour + buffer + RESET
222-
print
223-
224-
' print buffer following channel_end
225-
text_colour = CYAN
226-
print text_colour + mid(buffer, chan_end + len(CHANNEL_END)) + RESET;
227-
228-
' reset buffer
229-
buffer = ""
230-
endif
231-
232-
' Only print non-command tokens
233229
local nl = instr(buffer, chr(10))
234230
if nl then
235231
local text_line = left(buffer, nl - 1)
236232
buffer = mid(buffer, nl + 1)
237-
if text_line == "</|think|>" then
238-
text_colour = CYAN
233+
if left(trim(text_line), 5) == "TOOL:" then
234+
text_line += buffer + " " + iter.all()
235+
iter = llama.add_message("tool", process_tool(text_line))
236+
buffer = ""
239237
else
240238
print text_colour + text_line + RESET
239+
endif
240+
if text_line == "</|think|>" then
241+
text_colour = CYAN
241242
end if
242243
end if
243244
wend
244245

245246
' Flush remaining line buffer
246247
if len(buffer) > 0 and left(trim(buffer), 5) == "TOOL:" then
247-
' TOOL:xxx should always appear on the final line
248248
iter = llama.add_message("tool", process_tool(buffer))
249249
else
250250
if len(buffer) > 0 then
251-
' TODO: trim any trailing <|turn|>
252251
print text_colour + buffer + RESET
253252
endif
254253
print
@@ -260,4 +259,3 @@ end
260259

261260
welcome_message()
262261
main()
263-
'print list_files(".")

0 commit comments

Comments
 (0)