Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions .github/workflows/trunk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,7 @@ jobs:

- name: Check formatting
if: ${{ steps.src-changed.outputs.change == 'true' || steps.deps-changed.outputs.change == 'true' }}
run: |
find src -name "*.cpp" -o -name "*.hpp" | xargs clang-format --dry-run -Werror

- name: Lint sources (cppcheck)
if: ${{ steps.src-changed.outputs.change == 'true' || steps.deps-changed.outputs.change == 'true' }}
run: |
cppcheck --enable=all --error-exitcode=1 --suppress=missingIncludeSystem src/
run: bash ./tools/shell/format.sh

- name: Lint sources (clang-tidy)
if: ${{ steps.src-changed.outputs.change == 'true' || steps.deps-changed.outputs.change == 'true' }}
Expand Down
8 changes: 1 addition & 7 deletions .github/workflows/validate-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,7 @@ jobs:

- name: Check formatting
if: ${{ steps.src-changed.outputs.change == 'true' || steps.deps-changed.outputs.change == 'true' }}
run: |
find src -name "*.cpp" -o -name "*.hpp" | xargs clang-format --dry-run -Werror

- name: Lint sources (cppcheck)
if: ${{ steps.src-changed.outputs.change == 'true' || steps.deps-changed.outputs.change == 'true' }}
run: |
cppcheck --enable=all --error-exitcode=1 --suppress=missingIncludeSystem src/
run: bash ./tools/shell/format.sh

- name: Lint sources (clang-tidy)
if: ${{ steps.src-changed.outputs.change == 'true' || steps.deps-changed.outputs.change == 'true' }}
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.20)

project(cpp-workspace VERSION 0.1.0 LANGUAGES CXX)
project(cpp-workspace VERSION 0.1.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand Down
4 changes: 3 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ int main(int argc, const char* argv[]) {

OllamaClient client = OllamaClient();

client.query_model_list();

struct OllamaPromptConfig config;
config.model = model;
config.prompt = prompt;
config.stream = stream;

client.curl_request(config);
client.query_model(config);

return 0;
}
99 changes: 87 additions & 12 deletions src/ollama_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ static size_t WriteCallback(char* contents, size_t size, size_t nmemb, std::stri

// Create an instance of OllamaClient.
OllamaClient::OllamaClient() {
this->url = "http://localhost:11434/api/generate";
this->urls = {
generate_url : "http://localhost:11434/api/generate",
tags_url : "http://localhost:11434/api/tags"
};
this->headers = nullptr;
this->curl = nullptr;

this->check_ollama();

this->init_curl();

printf("\n");
}

Expand Down Expand Up @@ -70,6 +72,7 @@ void OllamaClient::process_response(std::string response) {
<< std::endl;
} catch (const json::exception& err) {
fprintf(stderr, "JSON parse error: %s\n", err.what());
exit(1);
}
}

Expand All @@ -94,23 +97,28 @@ void OllamaClient::process_response_stream(std::string response) {
}
} catch (const json::exception& err) {
fprintf(stderr, "JSON parse error: %s\n", err.what());
exit(1);
}
}
}

std::cout << std::endl;
}

// Send a request to the Ollama endpoint using CURL.
void OllamaClient::curl_request(OllamaPromptConfig config) {
// Send a request to the Ollama `generate` endpoint using CURL.
void OllamaClient::query_model(OllamaPromptConfig config) {
this->init_curl();

printf("💬 prompt: %s\n", config.prompt.c_str());

json request_payload = {
{"model", config.model}, {"prompt", config.prompt}, {"stream", config.stream}};

std::string post_data = request_payload.dump();

std::string response;

curl_easy_setopt(this->curl, CURLOPT_URL, this->url.c_str());
curl_easy_setopt(this->curl, CURLOPT_URL, this->urls.generate_url.c_str());
curl_easy_setopt(this->curl, CURLOPT_POSTFIELDS, post_data.c_str());
curl_easy_setopt(this->curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(this->curl, CURLOPT_WRITEDATA, &response);
Expand All @@ -121,12 +129,21 @@ void OllamaClient::curl_request(OllamaPromptConfig config) {
this->headers = curl_slist_append(this->headers, "Content-Type: application/json");
curl_easy_setopt(this->curl, CURLOPT_HTTPHEADER, this->headers);

CURLcode res = curl_easy_perform(curl);
CURLcode result = curl_easy_perform(curl);

long status = 0;
curl_easy_getinfo(this->curl, CURLINFO_RESPONSE_CODE, &status);

if (res != CURLE_OK) {
fprintf(stderr, "CURL error: %s\n", curl_easy_strerror(res));
curl_slist_free_all(this->headers);
curl_easy_cleanup(this->curl);
curl_easy_cleanup(this->curl);
curl_slist_free_all(this->headers);
this->headers = nullptr;

if (result != CURLE_OK) {
fprintf(stderr, "CURL error: %s\n", curl_easy_strerror(result));
exit(1);
}
if (status != 200) {
fprintf(stderr, "Ollama returned HTTP status: %ld\n", status);
exit(1);
}

Expand All @@ -135,7 +152,65 @@ void OllamaClient::curl_request(OllamaPromptConfig config) {
} else {
this->process_response(response);
}
}

// Send a request to the Ollama endpoint for listing local models using CURL.
void OllamaClient::query_model_list() {
this->init_curl();

std::string response;

curl_easy_setopt(this->curl, CURLOPT_URL, this->urls.tags_url.c_str());
curl_easy_setopt(this->curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(this->curl, CURLOPT_WRITEDATA, &response);

curl_easy_setopt(this->curl, CURLOPT_XFERINFOFUNCTION, ProgressSpinner::progress_callback);
curl_easy_setopt(this->curl, CURLOPT_NOPROGRESS, 0L);

this->headers = curl_slist_append(this->headers, "Content-Type: application/json");
curl_easy_setopt(this->curl, CURLOPT_HTTPHEADER, this->headers);

CURLcode result = curl_easy_perform(this->curl);

long status = 0;
curl_easy_getinfo(this->curl, CURLINFO_RESPONSE_CODE, &status);

curl_slist_free_all(this->headers);
curl_easy_cleanup(this->curl);
curl_slist_free_all(this->headers);
this->headers = nullptr;

if (result != CURLE_OK) {
fprintf(stderr, "CURL error: %s\n", curl_easy_strerror(result));
exit(1);
}
if (status != 200) {
fprintf(stderr, "Ollama returned HTTP status: %ld\n", status);
exit(1);
}

try {
const json data = json::parse(response);

if (!data.contains("models") || !data["models"].is_array()) {
fprintf(stderr, "Response has no models array.\n");
exit(1);
}

printf("📃 available models:\n");

for (const auto& model : data["models"]) {
const std::string name = model.value("name", "unknown");
const std::string modified_at = model.value("modified_at", "unknown");
const std::string size =
model.contains("size")
? std::to_string(model["size"].get<long long>() / 1024 / 1024) + " Mb"
: "size unknown";

std::cout << modified_at << "\t (" << size << ") \t" << name << "\n";
}
printf("\n");
} catch (const json::exception& err) {
fprintf(stderr, "JSON parse error: %s\n", err.what());
exit(1);
}
}
10 changes: 8 additions & 2 deletions src/ollama_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ struct OllamaPromptConfig {
bool stream;
};

struct OllamaServerURLs {
std::string generate_url;
std::string tags_url;
};

class OllamaClient {
private:
CURL* curl;
std::string url;
OllamaServerURLs urls;
struct curl_slist* headers;
ProgressSpinner spinner{ProgressSpinner()};

Expand All @@ -26,5 +31,6 @@ class OllamaClient {
public:
OllamaClient();

void curl_request(OllamaPromptConfig config);
void query_model(OllamaPromptConfig config);
void query_model_list();
};
8 changes: 7 additions & 1 deletion tools/shell/format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ set -euo pipefail

clang-format src/* -i -Werror

cppcheck --enable=all --error-exitcode=1 --suppress=missingIncludeSystem src/
cppcheck --std=c++20 \
--enable=all \
--error-exitcode=1 \
--suppress=missingIncludeSystem \
--suppress=unusedLabel \
-I src/ \
src/
Loading