ollama/ollama_client.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "ollama_client.hpp" | ||
| 2 | #include <algorithm> | ||
| 3 | #include <cstdlib> | ||
| 4 | #include <curl/curl.h> | ||
| 5 | #include <format> | ||
| 6 | #include <iostream> | ||
| 7 | #include <nlohmann/json.hpp> | ||
| 8 | #include <sstream> | ||
| 9 | #include <string> | ||
| 10 | |||
| 11 | using nlohmann::json_abi_v3_11_2::json; | ||
| 12 | |||
| 13 | /** | ||
| 14 | * Constructs an OllamaClient connected to a UserInterface. | ||
| 15 | * | ||
| 16 | * Initializes the Ollama API endpoint URLs, verifies the Ollama binary | ||
| 17 | * is installed, and creates a ProgressSpinner for network operations. | ||
| 18 | * | ||
| 19 | * @param config Pointer to the Ollama Client configuration options. | ||
| 20 | * @param ui Pointer to the UserInterface for displaying output. | ||
| 21 | * @param curl Pointer to a CurlUtils instance for HTTP communication with Qdrant. | ||
| 22 | * @param system_utils Pointer to a SustemUtils instance for `system()` call status handling. | ||
| 23 | * | ||
| 24 | * @note Calls check_ollama() during construction. Exits if Ollama is not found. | ||
| 25 | */ | ||
| 26 | ✗ | OllamaClient::OllamaClient(std::shared_ptr<OllamaClientConfig> config, std::shared_ptr<UserInterface> ui, | |
| 27 | ✗ | std::shared_ptr<CurlUtils> curl, std::shared_ptr<SystemUtils> system_utils) | |
| 28 | ✗ | : config(config), ui(ui), curl(curl), system_utils(system_utils) { | |
| 29 | this->urls = { | ||
| 30 | generate_url : "http://localhost:11434/api/generate", | ||
| 31 | tags_url : "http://localhost:11434/api/tags", | ||
| 32 | embed_url : "http://localhost:11434/api/embed" | ||
| 33 | ✗ | }; | |
| 34 | |||
| 35 | ✗ | this->check_ollama(); | |
| 36 | |||
| 37 | ✗ | if (this->ui->backend_running() == false) { | |
| 38 | ✗ | UserInterface::print_message_noninteractive("\n"); | |
| 39 | } | ||
| 40 | ✗ | } | |
| 41 | |||
| 42 | /** | ||
| 43 | * Destroys the OllamaClient instance and clears the Ollama history file. | ||
| 44 | * | ||
| 45 | * Runs a shell command that overwrites ~/.ollama/history with an empty string, truncating its contents. | ||
| 46 | * Passes the return status of the shell command to system_utils->catch_error() for handling. | ||
| 47 | * | ||
| 48 | * Note: This destructor depends on system_utils remaining valid for the duration of destruction, and it relies on | ||
| 49 | * the shell to expand the ~ path. Because it runs a shell command, it will also fail silently in environments | ||
| 50 | * where system() is unavailable or where the history file does not exist. | ||
| 51 | */ | ||
| 52 | ✗ | OllamaClient::~OllamaClient() { | |
| 53 | ✗ | int status = system("echo \"\" > ~/.ollama/history"); | |
| 54 | ✗ | this->system_utils->catch_error(status); | |
| 55 | ✗ | } | |
| 56 | |||
| 57 | /** | ||
| 58 | * Retrieves a configuration value by property name as a const reference of the requested type. | ||
| 59 | * | ||
| 60 | * @tparam T: Type of the configuration value to return. Supported types are std::string for "model", "embedding_model", | ||
| 61 | * and "prompt", and bool for "stream". | ||
| 62 | * @param property_name: Name of the configuration property to look up. | ||
| 63 | * @return: Const reference to the matching member of config, valid for as long as config is alive. | ||
| 64 | * @throws std::runtime_error: If property_name is not a supported property, or if T does not match the property's type. | ||
| 65 | * | ||
| 66 | * Compares property_name against each supported property in turn. For a matching name, returns a reference to the | ||
| 67 | * corresponding config member only when T matches that member's type. The type checks are resolved at compile time | ||
| 68 | * with if constexpr, so each instantiation contains only the branch for its type. | ||
| 69 | * | ||
| 70 | * Note: If the name matches but T does not, control falls through to the remaining checks and ends at the exception. | ||
| 71 | * The returned reference refers into config, so it must not outlive the OllamaClient instance. | ||
| 72 | */ | ||
| 73 | ✗ | template <typename T> const T& OllamaClient::get_config_value(const std::string& property_name) const { | |
| 74 | ✗ | if (property_name == "model") { | |
| 75 | if constexpr (std::is_same_v<T, std::string>) { | ||
| 76 | ✗ | return this->config->model; | |
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | ✗ | if (property_name == "embedding_model") { | |
| 81 | if constexpr (std::is_same_v<T, std::string>) { | ||
| 82 | ✗ | return this->config->embedding_model; | |
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | ✗ | if (property_name == "prompt") { | |
| 87 | if constexpr (std::is_same_v<T, std::string>) { | ||
| 88 | ✗ | return this->config->prompt; | |
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | ✗ | if (property_name == "stream") { | |
| 93 | if constexpr (std::is_same_v<T, bool>) { | ||
| 94 | ✗ | return this->config->stream; | |
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | ✗ | throw std::runtime_error("Property not supported: " + property_name); | |
| 99 | } | ||
| 100 | template const std::string& OllamaClient::get_config_value<std::string>(const std::string&) const; | ||
| 101 | template const bool& OllamaClient::get_config_value<bool>(const std::string&) const; | ||
| 102 | |||
| 103 | /** | ||
| 104 | * Retrieves a URL value by property name as a const reference to a std::string. | ||
| 105 | * | ||
| 106 | * @param property_name: Name of the URL property to look up; supported values are "generate_url" and "embed_url". | ||
| 107 | * @return: Const reference to the matching URL string stored in the urls member, valid for as long as this OllamaClient | ||
| 108 | * instance is alive. | ||
| 109 | * @throws std::runtime_error: If property_name is not a supported URL property. | ||
| 110 | * | ||
| 111 | * Compares property_name against "generate_url" and then "embed_url", returning a reference to the corresponding field | ||
| 112 | * of urls on a match. | ||
| 113 | * | ||
| 114 | * Note: The returned reference refers into urls, so it must not outlive the OllamaClient instance. Unlike | ||
| 115 | * get_config_value(), this method has no type parameter because every URL property is a std::string. | ||
| 116 | */ | ||
| 117 | ✗ | const std::string& OllamaClient::get_url_value(const std::string& property_name) const { | |
| 118 | ✗ | if (property_name == "generate_url") { | |
| 119 | ✗ | return this->urls.generate_url; | |
| 120 | } | ||
| 121 | |||
| 122 | ✗ | if (property_name == "embed_url") { | |
| 123 | ✗ | return this->urls.embed_url; | |
| 124 | } | ||
| 125 | |||
| 126 | ✗ | throw std::runtime_error("Property not supported: " + property_name); | |
| 127 | } | ||
| 128 | |||
| 129 | /** | ||
| 130 | * Verifies that the `is available on the system. | ||
| 131 | * | ||
| 132 | * Uses the `which ollama` command to check if Ollama is in the PATH. | ||
| 133 | * - If found: displays a checkmark and continues. | ||
| 134 | * - If not found: displays an error message with installation instructions | ||
| 135 | * and exits or ends the session. | ||
| 136 | * | ||
| 137 | * Output is routed through the UserInterface (if available) or stdout/stderr. | ||
| 138 | * | ||
| 139 | * Displays a checkmark on success. | ||
| 140 | */ | ||
| 141 | ✗ | void OllamaClient::check_ollama() { | |
| 142 | ✗ | std::string message{}; | |
| 143 | ✗ | if (system("which ollama > /dev/null 2>&1")) { | |
| 144 | message = "Ollama binary does not exist.\nInstallation instructions: " | ||
| 145 | ✗ | "https://docs.ollama.com/linux\n"; | |
| 146 | ✗ | if (this->ui->backend_running() == false) { | |
| 147 | ✗ | UserInterface::print_error_noninteractive(message); | |
| 148 | } else { | ||
| 149 | ✗ | this->ui->print_error(message); | |
| 150 | } | ||
| 151 | ✗ | this->ui->end_session(EXIT_FAILURE); | |
| 152 | } else { | ||
| 153 | ✗ | message = "Ollama binary exists.\n"; | |
| 154 | ✗ | if (this->ui->backend_running() == false) { | |
| 155 | ✗ | UserInterface::print_success_noninteractive(message); | |
| 156 | } else { | ||
| 157 | ✗ | this->ui->print_success(message); | |
| 158 | } | ||
| 159 | } | ||
| 160 | ✗ | }; | |
| 161 | |||
| 162 | /** | ||
| 163 | * Removes leading and trailing whitespace from a string. | ||
| 164 | * | ||
| 165 | * Uses std::find_if with lambda predicates to efficiently trim both ends | ||
| 166 | * without creating unnecessary intermediate strings. | ||
| 167 | * | ||
| 168 | * @param response The input string. | ||
| 169 | * @return The trimmed string. | ||
| 170 | */ | ||
| 171 | ✗ | std::string OllamaClient::trim_response(std::string response) { | |
| 172 | ✗ | response.erase(response.begin(), | |
| 173 | ✗ | std::find_if(response.begin(), response.end(), [](unsigned char ch) { return !std::isspace(ch); })); | |
| 174 | |||
| 175 | ✗ | response.erase( | |
| 176 | ✗ | std::find_if(response.rbegin(), response.rend(), [](unsigned char ch) { return !std::isspace(ch); }).base(), | |
| 177 | ✗ | response.end()); | |
| 178 | ✗ | return response; | |
| 179 | } | ||
| 180 | |||
| 181 | /** | ||
| 182 | * Parses and displays a complete JSON response from Ollama. | ||
| 183 | * | ||
| 184 | * Extracts the "response" field, trims whitespace, and displays it with | ||
| 185 | * a llama emoji header and separator. On JSON parse error, displays an | ||
| 186 | * error message and exits or ends the session. | ||
| 187 | * | ||
| 188 | * Output is routed through the UserInterface or stdout/stderr. | ||
| 189 | * | ||
| 190 | * @param response The complete JSON response from the Ollama API. | ||
| 191 | */ | ||
| 192 | ✗ | void OllamaClient::process_response(std::string response) { | |
| 193 | try { | ||
| 194 | ✗ | json response_json = json::parse(response); | |
| 195 | |||
| 196 | ✗ | if (this->ui->backend_running() == false) { | |
| 197 | std::string message = std ::format( | ||
| 198 | ✗ | "🦙 llama:\n{}\n---\n", this->trim_response(response_json["response"].get<std::string>()).c_str()); | |
| 199 | ✗ | UserInterface::print_message_noninteractive(message); | |
| 200 | ✗ | } else { | |
| 201 | ✗ | this->ui->print_message("🦙 llama:"); | |
| 202 | ✗ | this->ui->print_message(this->trim_response(response_json["response"].get<std::string>())); | |
| 203 | ✗ | this->ui->print_message("---"); | |
| 204 | } | ||
| 205 | ✗ | } catch (const json::exception& err) { | |
| 206 | ✗ | std::string message = std::format("JSON parse error: {}\n", err.what()); | |
| 207 | ✗ | if (this->ui->backend_running() == false) { | |
| 208 | ✗ | UserInterface::print_error_noninteractive(message); | |
| 209 | } else { | ||
| 210 | ✗ | this->ui->print_error(message); | |
| 211 | } | ||
| 212 | ✗ | this->ui->end_session(EXIT_FAILURE); | |
| 213 | ✗ | } | |
| 214 | ✗ | } | |
| 215 | |||
| 216 | /** | ||
| 217 | * Parses one line of a streamed Ollama response and displays its text fragment. | ||
| 218 | * | ||
| 219 | * @param line: One JSON-encoded chunk from the streaming response, expected to contain a "response" string and a "done" | ||
| 220 | * boolean. | ||
| 221 | * | ||
| 222 | * Parses line as JSON and extracts the "response" text fragment. A static flag tracks whether this is the first chunk | ||
| 223 | * of the current response. The first chunk starts a new message entry, and later chunks are appended to it. The text is | ||
| 224 | * shown through the UI when it is running, or printed non-interactively otherwise. When the "done" field is true, the | ||
| 225 | * flag is reset so the next response starts a new entry, and the method returns. | ||
| 226 | * | ||
| 227 | * Parse failures and other json::exception errors are caught and reported through print_error_noninteractive(), | ||
| 228 | * including the error ID and the offending line. | ||
| 229 | * | ||
| 230 | * Note: When compiled with DEBUG defined, the first 100 characters of each line are logged if the UI is not running. | ||
| 231 | * The first_chunk flag is static, so it is shared by all OllamaClient instances. If a stream ends or fails to parse | ||
| 232 | * before a chunk with "done" set to true, the flag is not reset and the next response will be appended to the previous | ||
| 233 | * message. | ||
| 234 | */ | ||
| 235 | ✗ | void OllamaClient::process_response_stream(const std::string& line) { | |
| 236 | #ifdef DEBUG | ||
| 237 | ✗ | if (this->ui->backend_running() == false) { | |
| 238 | ✗ | UserInterface::print_message_noninteractive( | |
| 239 | ✗ | std::format("\t[DEBUG] process_response_stream called with line: {}\n", line.substr(0, 100))); | |
| 240 | } | ||
| 241 | #endif | ||
| 242 | |||
| 243 | try { | ||
| 244 | ✗ | json chunk_json = json::parse(line); | |
| 245 | ✗ | std::string text = chunk_json["response"].get<std::string>(); | |
| 246 | |||
| 247 | // First chunk: stream=false (new entry), subsequent chunks: stream=true (append) | ||
| 248 | static bool first_chunk = true; | ||
| 249 | ✗ | bool is_streaming = !first_chunk; | |
| 250 | ✗ | first_chunk = false; | |
| 251 | |||
| 252 | ✗ | if (this->ui->backend_running() == false) { | |
| 253 | ✗ | UserInterface::print_message_noninteractive(text); | |
| 254 | } else { | ||
| 255 | ✗ | this->ui->print_message(text, is_streaming); | |
| 256 | } | ||
| 257 | |||
| 258 | ✗ | if (chunk_json["done"].get<bool>()) { | |
| 259 | ✗ | first_chunk = true; // Reset for next response | |
| 260 | ✗ | return; | |
| 261 | } | ||
| 262 | ✗ | } catch (const json::exception& err) { | |
| 263 | std::string message = std::format("JSON parse error in process_response_stream: {}\nError ID: {}\nLine was: {}", | ||
| 264 | ✗ | err.what(), err.id, line); | |
| 265 | ✗ | UserInterface::print_error_noninteractive(message); | |
| 266 | ✗ | } | |
| 267 | } | ||
| 268 | |||
| 269 | /** | ||
| 270 | * Generates embeddings for a list of text strings using the Ollama embedding model. | ||
| 271 | * | ||
| 272 | * This method sends each text to the Ollama embedding endpoint and retrieves | ||
| 273 | * the corresponding embedding vectors. Embeddings are processed sequentially. | ||
| 274 | * | ||
| 275 | * @param texts A vector of text strings to embed | ||
| 276 | * @param config_opt Configuration object specifying the embedding model to use | ||
| 277 | * | ||
| 278 | * @return A vector of embedding vectors, where each inner vector contains | ||
| 279 | * the float values representing the embedding for the corresponding text. | ||
| 280 | * The order of embeddings matches the order of input texts. | ||
| 281 | * | ||
| 282 | * @throws std::exception if the web request fails or the response cannot be parsed | ||
| 283 | */ | ||
| 284 | ✗ | std::vector<std::vector<float>> OllamaClient::generate_embeddings(const std::vector<std::string>& texts, | |
| 285 | std::optional<OllamaClientConfig> config_opt) { | ||
| 286 | ✗ | std::vector<std::vector<float>> embeddings{}; | |
| 287 | |||
| 288 | json request_payload = { | ||
| 289 | ✗ | {"model", config_opt != std::nullopt ? config_opt->embedding_model : config->embedding_model}, {"input", ""}}; | |
| 290 | |||
| 291 | ✗ | for (const auto& text : texts) { | |
| 292 | ✗ | request_payload["input"] = text; | |
| 293 | |||
| 294 | ✗ | std::string post_data = request_payload.dump(); | |
| 295 | |||
| 296 | ✗ | std::string response = this->curl->fetch_web_content(this->urls.embed_url, post_data); | |
| 297 | |||
| 298 | #ifdef DEBUG | ||
| 299 | ✗ | if (this->ui->backend_running() == false) { | |
| 300 | ✗ | UserInterface::print_message_noninteractive( | |
| 301 | ✗ | std::format("\t[DEBUG] generate_embeddings response {}\n", response)); | |
| 302 | } | ||
| 303 | #endif | ||
| 304 | |||
| 305 | ✗ | const json data = json::parse(response); | |
| 306 | |||
| 307 | ✗ | embeddings.push_back(data["embeddings"][0].get<std::vector<float>>()); | |
| 308 | ✗ | } | |
| 309 | |||
| 310 | ✗ | return embeddings; | |
| 311 | ✗ | } | |
| 312 | |||
| 313 | /** | ||
| 314 | * Sends a prompt query to the Ollama model and processes the response. | ||
| 315 | * | ||
| 316 | * Displays the formatted user message to either the console or UI based on | ||
| 317 | * the UI's running state, then constructs a JSON request payload with the | ||
| 318 | * model, prompt, and stream settings. Fetches the response from the Ollama | ||
| 319 | * server and processes it either as a stream or as a complete response. | ||
| 320 | * | ||
| 321 | * @param config_opt The prompt configuration containing the model name, prompt | ||
| 322 | * text, and stream preference | ||
| 323 | */ | ||
| 324 | ✗ | void OllamaClient::query_model(std::optional<OllamaClientConfig> config_opt) { | |
| 325 | std::string message = | ||
| 326 | ✗ | std::format("💬 user: {}\n", config_opt != std::nullopt ? config_opt->prompt.c_str() : config->prompt.c_str()); | |
| 327 | ✗ | if (this->ui->backend_running() == false) { | |
| 328 | ✗ | UserInterface::print_message_noninteractive(message); | |
| 329 | } else { | ||
| 330 | ✗ | this->ui->print_message(message); | |
| 331 | } | ||
| 332 | |||
| 333 | ✗ | json request_payload; | |
| 334 | ✗ | if (config_opt != std::nullopt) { | |
| 335 | request_payload = { | ||
| 336 | ✗ | {"model", config_opt->model}, {"prompt", config_opt->prompt}, {"stream", config_opt->stream}}; | |
| 337 | } else { | ||
| 338 | ✗ | request_payload = {{"model", config->model}, {"prompt", config->prompt}, {"stream", config->stream}}; | |
| 339 | } | ||
| 340 | |||
| 341 | ✗ | std::string post_data = request_payload.dump(); | |
| 342 | |||
| 343 | ✗ | bool stream = config_opt != std::nullopt ? config_opt->stream : config->stream; | |
| 344 | ✗ | if (stream) { | |
| 345 | WebContentStream response = | ||
| 346 | ✗ | this->curl->fetch_web_content_stream(this->urls.generate_url, post_data, [this](const std::string& chunk) { | |
| 347 | ✗ | this->process_response_stream(chunk); | |
| 348 | ✗ | }); | |
| 349 | ✗ | message = std::format("Stream successful: {}, Status: {}\n", response.success, response.status); | |
| 350 | ✗ | if (this->ui->backend_running() == false) { | |
| 351 | ✗ | UserInterface::print_message_noninteractive(message); | |
| 352 | } else { | ||
| 353 | ✗ | this->ui->print_message(message); | |
| 354 | } | ||
| 355 | ✗ | } else { | |
| 356 | ✗ | std::string response = this->curl->fetch_web_content(this->urls.generate_url, post_data); | |
| 357 | ✗ | this->process_response(response); | |
| 358 | ✗ | } | |
| 359 | ✗ | } | |
| 360 | |||
| 361 | /** | ||
| 362 | * Retrieves a list of available models from the Ollama server. | ||
| 363 | * | ||
| 364 | * Fetches the models list from the Ollama tags endpoint and parses the JSON | ||
| 365 | * response. Validates that the response contains a valid models array. Extracts | ||
| 366 | * and displays model metadata including name, modification timestamp, and size | ||
| 367 | * (converted from bytes to megabytes). Collects valid model names into a vector | ||
| 368 | * for return. Handles errors (invalid response structure or JSON parsing failures) | ||
| 369 | * by terminating with an error message directed to either stderr or the UI based | ||
| 370 | * on the UI's running state. | ||
| 371 | * | ||
| 372 | * @return A vector of available model names extracted from the server response. | ||
| 373 | * Returns an empty vector if an error occurs during fetch or parsing. | ||
| 374 | */ | ||
| 375 | ✗ | std::vector<std::string> OllamaClient::query_model_list() { | |
| 376 | ✗ | UserInterface::print_message_noninteractive("running query_model_list"); | |
| 377 | ✗ | std::string message{}; | |
| 378 | ✗ | std::vector<std::string> models; | |
| 379 | |||
| 380 | ✗ | std::string response = this->curl->fetch_web_content(this->urls.tags_url); | |
| 381 | |||
| 382 | try { | ||
| 383 | ✗ | const json data = json::parse(response); | |
| 384 | |||
| 385 | ✗ | if (!data.contains("models") || !data["models"].is_array()) { | |
| 386 | ✗ | message = "Response has no models array.\n"; | |
| 387 | ✗ | if (this->ui->backend_running() == false) { | |
| 388 | ✗ | UserInterface::print_error_noninteractive(message); | |
| 389 | } else { | ||
| 390 | ✗ | this->ui->print_error(message); | |
| 391 | } | ||
| 392 | ✗ | this->ui->end_session(EXIT_FAILURE); | |
| 393 | } | ||
| 394 | |||
| 395 | ✗ | message = "📃 available models:\n"; | |
| 396 | ✗ | if (this->ui->backend_running() == false) { | |
| 397 | ✗ | UserInterface::print_message_noninteractive(message); | |
| 398 | } else { | ||
| 399 | ✗ | this->ui->print_message(message); | |
| 400 | } | ||
| 401 | |||
| 402 | ✗ | for (const auto& model : data["models"]) { | |
| 403 | ✗ | const std::string name = model.value("name", "unknown"); | |
| 404 | ✗ | const std::string modified_at = model.value("modified_at", "unknown"); | |
| 405 | ✗ | const std::string size = model.contains("size") | |
| 406 | ✗ | ? std::to_string(model["size"].get<long long>() / 1024 / 1024) + " Mb" | |
| 407 | ✗ | : "size unknown"; | |
| 408 | |||
| 409 | ✗ | if (name != "unknown") { | |
| 410 | ✗ | models.push_back(name); | |
| 411 | } | ||
| 412 | |||
| 413 | ✗ | message = std::format("{}\t ({}) \t{}\n", modified_at, size, name); | |
| 414 | ✗ | if (this->ui->backend_running() == false) { | |
| 415 | ✗ | UserInterface::print_message_noninteractive(message); | |
| 416 | } else { | ||
| 417 | ✗ | this->ui->print_message(message); | |
| 418 | } | ||
| 419 | ✗ | } | |
| 420 | ✗ | UserInterface::print_message_noninteractive("\n"); | |
| 421 | ✗ | } catch (const json::exception& err) { | |
| 422 | ✗ | message = std::format("JSON parse error: {}\n", err.what()); | |
| 423 | ✗ | if (this->ui->backend_running() == false) { | |
| 424 | ✗ | UserInterface::print_error_noninteractive(message); | |
| 425 | } else { | ||
| 426 | ✗ | this->ui->print_error(message); | |
| 427 | } | ||
| 428 | ✗ | this->ui->end_session(EXIT_FAILURE); | |
| 429 | ✗ | } | |
| 430 | |||
| 431 | ✗ | return models; | |
| 432 | ✗ | } | |
| 433 |