LlamaLib  v2.0.2
Cross-platform library for local LLMs
Loading...
Searching...
No Matches
completion_processor.h
1
2#pragma once
3#include <string>
4#include <vector>
5#include <functional>
6#include "defs.h"
7#include <iostream>
8
14public:
15 ResponseConcatenator() = default;
16
22 bool process_chunk(const std::string& chunk_data);
23
27 json build_concatenated_json() const;
28
32 const std::string& get_content() const { return concatenated_content_; }
33
37 const std::vector<int>& get_tokens() const { return concatenated_tokens_; }
38
42 std::string get_result_json() const;
43
47 bool has_error() const { return has_error_; }
48
52 const json& get_error() const { return error_; }
53
57 bool is_complete() const { return is_complete_; }
58
62 size_t chunk_count() const { return chunk_count_; }
63
67 void set_callback(CharArrayFn callback, bool callWithJSON=false) {
68 callback_ = std::move(callback);
69 callWithJSON_ = callWithJSON;
70 }
71
75 void reset();
76
77private:
78 void accumulate_result(const json& item);
79
80 std::string concatenated_content_;
81 std::vector<int> concatenated_tokens_;
82 json last_chunk_ = json::object();
83 json error_ = json::object();
84 bool has_error_ = false;
85 bool is_complete_ = false;
86 size_t chunk_count_ = 0;
87 CharArrayFn callback_;
88 bool callWithJSON_ = false;
89};
Handles concatenation of LLM response chunks (both streaming and non-streaming) Accumulates content a...
bool has_error() const
Check if processing encountered an error.
bool process_chunk(const std::string &chunk_data)
Process a single chunk and accumulate its content/tokens.
void reset()
Reset the concatenator state.
size_t chunk_count() const
Get the number of chunks processed.
const std::vector< int > & get_tokens() const
Get the concatenated tokens.
bool is_complete() const
Check if response is complete.
json build_concatenated_json() const
Build the final concatenated JSON result.
const json & get_error() const
Get the error JSON if any.
std::string get_result_json() const
Get the complete result as JSON string.
const std::string & get_content() const
Get the concatenated content string.
void set_callback(CharArrayFn callback, bool callWithJSON=false)
Set a callback to be invoked after each chunk is processed.
File with basic definitions.