![]() |
LlamaLib
v2.0.2
Cross-platform library for local LLMs
|
High-level conversational agent for LLM interactions. More...
#include <LLM_agent.h>
Public Member Functions | |
| LLMAgent (LLMLocal *llm, const std::string &system_prompt="") | |
| Constructor for LLM agent. | |
| std::string | tokenize_json (const json &data) override |
| Tokenize input (override) | |
| std::string | detokenize_json (const json &data) override |
| Convert tokens back to text. | |
| std::string | embeddings_json (const json &data) override |
| Generate embeddings with HTTP response support. | |
| std::string | completion_json (const json &data, CharArrayFn callback=nullptr, bool callbackWithJSON=true) override |
| Generate completion (delegate to wrapped LLM) | |
| std::string | apply_template_json (const json &data) override |
| Apply a chat template to message data. | |
| std::string | slot_json (const json &data) override |
| Manage slots with HTTP response support. | |
| void | cancel (int id_slot) override |
| Cancel request (delegate to wrapped LLM) | |
| int | get_next_available_slot () override |
| Get available slot (delegate to wrapped LLM) | |
| virtual json | build_completion_json (const std::string &prompt) |
| Build completion JSON with agent's slot. | |
| virtual std::string | completion (const std::string &prompt, CharArrayFn callback=nullptr, bool return_response_json=false) |
| Generate completion with agent's slot. | |
| virtual json | build_slot_json (const std::string &action, const std::string &filepath) |
| Build slot operation JSON with agent's slot. | |
| virtual std::string | save_slot (const std::string &filepath) |
| Save agent's slot state. | |
| virtual std::string | load_slot (const std::string &filepath) |
| Load agent's slot state. | |
| virtual void | cancel () |
| Cancel agent's current request. | |
| int | get_slot () |
| Get current processing slot ID. | |
| void | set_slot (int id_slot) |
| Set processing slot ID. | |
| void | set_system_prompt (const std::string &system_prompt_) |
| Set system prompt. | |
| std::string | get_system_prompt () const |
| Get current system prompt. | |
| void | set_history (const json &history_) |
| Set conversation history. | |
| json | get_history () const |
| Get conversation history. | |
| void | add_user_message (const std::string &content) |
| Add a user message to conversation history. | |
| void | add_assistant_message (const std::string &content) |
| Add an assistant message to conversation history. | |
| void | clear_history () |
| Clear all conversation history. | |
| void | remove_last_message () |
| Remove the last message from history. | |
| void | save_history (const std::string &filepath) const |
| Save conversation history to file. | |
| void | load_history (const std::string &filepath) |
| Load conversation history from file. | |
| size_t | get_history_size () const |
| Get number of messages in history. | |
| std::string | chat (const std::string &user_prompt, bool add_to_history=true, CharArrayFn callback=nullptr, bool return_response_json=false, bool debug_prompt=false) |
| Conduct a chat interaction. | |
Public Member Functions inherited from LLMLocal | |
| virtual std::string | save_slot (int id_slot, const std::string &filepath) |
| Save slot state to file. | |
| virtual std::string | load_slot (int id_slot, const std::string &filepath) |
| Load slot state from file. | |
Public Member Functions inherited from LLM | |
| virtual | ~LLM ()=default |
| Virtual destructor. | |
| virtual std::vector< int > | tokenize (const std::string &query) |
| Tokenize text. | |
| virtual std::string | detokenize (const std::vector< int32_t > &tokens) |
| Convert tokens to text. | |
| virtual std::vector< float > | embeddings (const std::string &query) |
| Generate embeddings. | |
| virtual void | set_completion_params (json completion_params_) |
| Set completion parameters. | |
| virtual std::string | get_completion_params () |
| Get current completion parameters. | |
| virtual std::string | completion (const std::string &prompt, CharArrayFn callback=nullptr, int id_slot=-1, bool return_response_json=false) |
| Generate completion. | |
| virtual void | set_grammar (std::string grammar_) |
| Set grammar for constrained generation. | |
| virtual std::string | get_grammar () |
| Get current grammar specification. | |
| virtual std::string | apply_template (const json &messages) |
| Apply template to messages. | |
Public Attributes | |
| const std::string | USER_ROLE = "user" |
| const std::string | ASSISTANT_ROLE = "assistant" |
Public Attributes inherited from LLM | |
| int32_t | n_keep = 0 |
| Number of tokens to keep from the beginning of the context. | |
| std::string | grammar = "" |
| Grammar specification in GBNF format or JSON schema. | |
| json | completion_params |
| JSON object containing completion parameters. | |
Protected Member Functions | |
| void | set_n_keep () |
| virtual void | add_message (const std::string &role, const std::string &content) |
| Add a message to conversation history. | |
Protected Member Functions inherited from LLMLocal | |
| virtual std::string | slot (int id_slot, const std::string &action, const std::string &filepath) |
| Perform slot operation. | |
| virtual json | build_slot_json (int id_slot, const std::string &action, const std::string &filepath) |
| Build JSON for slot operations. | |
| virtual std::string | parse_slot_json (const json &result) |
| Parse slot operation result. | |
Protected Member Functions inherited from LLM | |
| virtual json | build_apply_template_json (const json &messages) |
| Build JSON for template application. | |
| virtual std::string | parse_apply_template_json (const json &result) |
| Parse template application result. | |
| virtual json | build_tokenize_json (const std::string &query) |
| Build JSON for tokenization. | |
| virtual std::vector< int > | parse_tokenize_json (const json &result) |
| Parse tokenization result. | |
| virtual json | build_detokenize_json (const std::vector< int32_t > &tokens) |
| Build JSON for detokenization. | |
| virtual std::string | parse_detokenize_json (const json &result) |
| Parse detokenization result. | |
| virtual json | build_embeddings_json (const std::string &query) |
| Build JSON for embeddings generation. | |
| virtual std::vector< float > | parse_embeddings_json (const json &result) |
| Parse embeddings result. | |
| virtual json | build_completion_json (const std::string &prompt, int id_slot=-1) |
| Build JSON for completion generation. | |
| virtual std::string | parse_completion_json (const json &result) |
| Parse completion result. | |
Additional Inherited Members | |
Static Public Member Functions inherited from LLM | |
| static bool | has_gpu_layers (const std::string &command) |
| Check if command line arguments specify GPU layers. | |
| static std::string | LLM_args_to_command (const std::string &model_path, int num_slots=1, int num_threads=-1, int num_GPU_layers=0, bool flash_attention=false, int context_size=4096, int batch_size=2048, bool embedding_only=false, const std::vector< std::string > &lora_paths={}) |
| Convert LLM parameters to command line arguments. | |
High-level conversational agent for LLM interactions.
Provides a conversation-aware interface that manages chat history and applies chat template formatting
Definition at line 59 of file LLM_agent.h.
| LLMAgent::LLMAgent | ( | LLMLocal * | llm, |
| const std::string & | system_prompt = "" ) |
Constructor for LLM agent.
| llm | Pointer to LLMLocal instance to wrap |
| system_prompt | Initial system prompt for conversation context |
Creates an agent that manages conversations with the specified LLM backend
Definition at line 5 of file LLM_agent.cpp.
|
inline |
Add an assistant message to conversation history.
| content | Assistant message content |
Convenience method for adding assistant messages
Definition at line 201 of file LLM_agent.h.
|
protectedvirtual |
Add a message to conversation history.
| role | Message role identifier |
| content | Message content text |
Appends a new message to the conversation history
Definition at line 81 of file LLM_agent.cpp.
|
inline |
Add a user message to conversation history.
| content | User message content |
Convenience method for adding user messages
Definition at line 196 of file LLM_agent.h.
|
inlineoverridevirtual |
Apply a chat template to message data.
| data | JSON object containing messages to format |
Pure virtual method for applying chat templates to conversation data
Implements LLM.
Definition at line 100 of file LLM_agent.h.
|
inlinevirtual |
Build completion JSON with agent's slot.
| prompt | Input prompt text |
Override that automatically uses the agent's assigned slot
Definition at line 123 of file LLM_agent.h.
|
inlinevirtual |
Build slot operation JSON with agent's slot.
| action | Slot operation action ("save" or "restore") |
| filepath | File path for slot operation |
Override that automatically uses the agent's assigned slot
Definition at line 141 of file LLM_agent.h.
|
inlinevirtual |
Cancel agent's current request.
Cancels any running request on the agent's slot
Definition at line 157 of file LLM_agent.h.
|
inlineoverridevirtual |
Cancel request (delegate to wrapped LLM)
| data | JSON cancellation request |
Implements LLMLocal.
Definition at line 110 of file LLM_agent.h.
| std::string LLMAgent::chat | ( | const std::string & | user_prompt, |
| bool | add_to_history = true, | ||
| CharArrayFn | callback = nullptr, | ||
| bool | return_response_json = false, | ||
| bool | debug_prompt = false ) |
Conduct a chat interaction.
| user_prompt | User's input message |
| add_to_history | Whether to add messages to conversation history |
| callback | Optional callback for streaming responses |
| return_response_json | Whether to return full JSON response |
| debug_prompt | Whether to display the complete prompt (default: false) |
Main chat method that processes user input, applies conversation context, generates a response, and optionally updates conversation history
Definition at line 44 of file LLM_agent.cpp.
| void LLMAgent::clear_history | ( | ) |
Clear all conversation history.
Removes all messages from the conversation history
Definition at line 27 of file LLM_agent.cpp.
|
inlinevirtual |
Generate completion with agent's slot.
| prompt | Input prompt text |
| callback | Optional streaming callback |
| return_response_json | Whether to return JSON response |
Override that automatically uses the agent's assigned slot
Definition at line 131 of file LLM_agent.h.
|
inlineoverridevirtual |
Generate completion (delegate to wrapped LLM)
| data | JSON completion request |
| callback | Optional streaming callback |
| callbackWithJSON | Whether callback uses JSON |
Implements LLM.
Definition at line 94 of file LLM_agent.h.
|
inlineoverridevirtual |
Convert tokens back to text.
| data | JSON object containing token IDs |
Pure virtual method for converting token sequences back to text
Implements LLM.
Definition at line 81 of file LLM_agent.h.
|
inlineoverridevirtual |
Generate embeddings with HTTP response support.
| data | JSON object containing embedding request |
Protected method used internally for server-based embedding generation
Implements LLM.
Definition at line 87 of file LLM_agent.h.
|
inline |
Get conversation history.
Returns the complete conversation history as JSON
Definition at line 189 of file LLM_agent.h.
|
inline |
Get number of messages in history.
Returns the count of messages currently stored in history
Definition at line 224 of file LLM_agent.h.
|
inlineoverridevirtual |
Get available slot (delegate to wrapped LLM)
Implements LLMLocal.
Definition at line 114 of file LLM_agent.h.
|
inline |
Get current processing slot ID.
Returns the slot ID used for this agent's operations
Definition at line 163 of file LLM_agent.h.
|
inline |
Get current system prompt.
Definition at line 179 of file LLM_agent.h.
| void LLMAgent::load_history | ( | const std::string & | filepath | ) |
Load conversation history from file.
| filepath | Path to the history file to load |
Loads conversation history from a JSON file, replacing current history
Definition at line 116 of file LLM_agent.cpp.
|
inlinevirtual |
Load agent's slot state.
| filepath | Path to load slot state from |
Restores the agent's processing state from file
Definition at line 153 of file LLM_agent.h.
| void LLMAgent::remove_last_message | ( | ) |
Remove the last message from history.
Removes the most recently added message
Definition at line 87 of file LLM_agent.cpp.
| void LLMAgent::save_history | ( | const std::string & | filepath | ) | const |
Save conversation history to file.
| filepath | Path to save the history file |
Saves the current conversation history as JSON to the specified file
Definition at line 95 of file LLM_agent.cpp.
|
inlinevirtual |
Save agent's slot state.
| filepath | Path to save slot state |
Saves the agent's current processing state to file
Definition at line 147 of file LLM_agent.h.
|
inline |
Set conversation history.
| history_ | JSON array of chat messages |
Replaces current conversation history with provided messages
Definition at line 184 of file LLM_agent.h.
|
protected |
Definition at line 33 of file LLM_agent.cpp.
| void LLMAgent::set_slot | ( | int | id_slot | ) |
Set processing slot ID.
| id_slot | Slot ID to use for operations |
Assigns a specific slot for this agent's processing (not available for remote LLMClient)
Definition at line 11 of file LLM_agent.cpp.
|
inline |
Set system prompt.
| system_prompt_ | New system prompt text |
Sets the system prompt and clears conversation history
Definition at line 175 of file LLM_agent.h.
|
inlineoverridevirtual |
Manage slots with HTTP response support.
| data | JSON object with slot operation |
Protected method used internally for server-based slot management
Implements LLMLocal.
Definition at line 106 of file LLM_agent.h.
|
inlineoverridevirtual |
Tokenize input (override)
| data | JSON object containing text to tokenize |
Implements LLM.
Definition at line 75 of file LLM_agent.h.
| const std::string LLMAgent::ASSISTANT_ROLE = "assistant" |
Definition at line 63 of file LLM_agent.h.
| const std::string LLMAgent::USER_ROLE = "user" |
Definition at line 62 of file LLM_agent.h.