Skip to content

Commit 9dd5cc2

Browse files
committed
add accumulator to chain
1 parent e5978ef commit 9dd5cc2

File tree

2 files changed

+249
-134
lines changed

2 files changed

+249
-134
lines changed

src/unchained.gleam

Lines changed: 82 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub fn new() -> Chain {
7676
}
7777

7878
// Add an LLM step to the chain
79-
pub fn add_llm(chain: Chain, llm_config: LLMConfig) -> Chain {
79+
pub fn add_llm(chain: Chain, config llm_config: LLMConfig) -> Chain {
8080
Chain(..chain, steps: list.append(chain.steps, [LLMStep(llm_config)]))
8181
}
8282

@@ -245,49 +245,84 @@ fn execute_steps(
245245
input: String,
246246
memory: Memory,
247247
llm_engine: fn(String, LLMConfig) -> Result(LLMResponse, Error),
248+
) -> Result(ChainEval, Error) {
249+
execute_steps_helper(steps, input, "", memory, llm_engine)
250+
}
251+
252+
fn execute_steps_helper(
253+
steps: List(ChainStep),
254+
input: String,
255+
accumulated_input: String,
256+
memory: Memory,
257+
llm_engine: fn(String, LLMConfig) -> Result(LLMResponse, Error),
248258
) -> Result(ChainEval, Error) {
249259
case steps {
250260
[] -> Ok(ChainEval(input, memory))
251261

252262
[LLMStep(llm_config), ..rest] -> {
253-
// Call Ollama with the prompt and input
254-
case llm_engine(input, llm_config) {
263+
// Call Ollama with the prompt and accumulated input
264+
case llm_engine(accumulated_input, llm_config) {
255265
Ok(Response(output)) -> {
256266
let new_memory = update_memory_history(memory, input, output)
257-
execute_steps(rest, output, new_memory, llm_engine)
267+
let accumulated_output = accumulated_input <> "\n" <> output
268+
execute_steps_helper(
269+
rest,
270+
output,
271+
accumulated_output,
272+
new_memory,
273+
llm_engine,
274+
)
258275
}
259276
Error(e) -> Error(e)
260277
}
261278
}
262279

263280
[LLMStepWithToolSelection(llm_config, tool_selectors, template), ..rest] -> {
264-
// Call Ollama with the prompt and input
281+
// Call Ollama with the prompt and accumulated input
265282
let tool_prompt =
266283
interpolate_template(template, memory.variables, tool_selectors)
267-
case llm_engine(input <> "\n" <> tool_prompt, llm_config) {
284+
case
285+
llm_engine(
286+
accumulated_input <> "\n" <> input <> "\n" <> tool_prompt,
287+
llm_config,
288+
)
289+
{
268290
Ok(Response(output)) -> {
269291
let new_memory = update_memory_history(memory, input, output)
270-
let foun_tool =
292+
let found_tool =
271293
list.find_map(tool_selectors, fn(tool_selector) {
272294
let ToolSelector(selector, tool) = tool_selector
273295
selector(output)
274296
|> option.map(fn(tool_input) { #(tool_input, tool) })
275297
|> option.to_result(Nil)
276298
})
277-
let fun_output = case foun_tool {
299+
let fun_output = case found_tool {
278300
Ok(#(tool_input, tool)) -> {
279301
tool.function(tool_input)
280302
}
281303
Error(Nil) -> Ok(output)
282304
}
283305
case fun_output {
284-
Ok(output) -> {
285-
let new_memory = case foun_tool {
306+
Ok(tool_output) -> {
307+
let new_memory = case found_tool {
286308
Ok(#(_, tool)) ->
287-
update_memory_history_tool(new_memory, input, output, tool)
309+
update_memory_history_tool(
310+
new_memory,
311+
input,
312+
tool_output,
313+
tool,
314+
)
288315
Error(Nil) -> new_memory
289316
}
290-
execute_steps(rest, output, new_memory, llm_engine)
317+
let accumulated_output =
318+
accumulated_input <> "\n" <> output <> "\n" <> tool_output
319+
execute_steps_helper(
320+
rest,
321+
output,
322+
accumulated_output,
323+
new_memory,
324+
llm_engine,
325+
)
291326
}
292327
Error(e) -> Error(e)
293328
}
@@ -301,30 +336,59 @@ fn execute_steps(
301336
Ok(output) -> {
302337
let new_memory =
303338
update_memory_history_tool(memory, input, output, tool)
304-
execute_steps(rest, output, new_memory, llm_engine)
339+
let accumulated_output = accumulated_input <> "\n" <> output
340+
execute_steps_helper(
341+
rest,
342+
output,
343+
accumulated_output,
344+
new_memory,
345+
llm_engine,
346+
)
305347
}
306348
Error(e) -> Error(e)
307349
}
308350
}
309351

310352
[PromptTemplate(template), ..rest] -> {
311353
let output = interpolate_template(template, memory.variables, [])
312-
execute_steps(rest, output, memory, llm_engine)
354+
let accumulated_output = accumulated_input <> "\n" <> output
355+
let new_memory = update_memory_history(memory, input, output)
356+
execute_steps_helper(
357+
rest,
358+
output,
359+
accumulated_output,
360+
new_memory,
361+
llm_engine,
362+
)
313363
}
314364

315365
[ChainBreaker(should_break), ..rest] ->
316-
case should_break(input) {
366+
case should_break(accumulated_input) {
317367
option.Some(output) -> {
368+
let accumulated_output = accumulated_input <> "\n" <> output
318369
let new_memory =
319370
Memory(
320371
variables: memory.variables,
321372
history: list.append(memory.history, [
322-
ChainBreak(input, output, birl.now()),
373+
ChainBreak(accumulated_input, output, birl.now()),
323374
]),
324375
)
325-
execute_steps([], output, new_memory, llm_engine)
376+
execute_steps_helper(
377+
[],
378+
output,
379+
accumulated_output,
380+
new_memory,
381+
llm_engine,
382+
)
326383
}
327-
_ -> execute_steps(rest, input, memory, llm_engine)
384+
_ ->
385+
execute_steps_helper(
386+
rest,
387+
input,
388+
accumulated_input,
389+
memory,
390+
llm_engine,
391+
)
328392
}
329393
}
330394
}

0 commit comments

Comments
 (0)