ollama
This commit is contained in:
@@ -176,10 +176,12 @@
|
|||||||
$imgui/*.cpp \
|
$imgui/*.cpp \
|
||||||
-DRENDERER_OPENGL \
|
-DRENDERER_OPENGL \
|
||||||
-DWINDOW_GLFW \
|
-DWINDOW_GLFW \
|
||||||
-DMAINGUI_DYNAMIC \
|
-DOLLAMA_DYNAMIC \
|
||||||
-fpic -shared \
|
-fpic -shared \
|
||||||
-I src -I include -I $imgui -I . \
|
-I src -I include -I $imgui -I . \
|
||||||
-lGL -lglfw -lGLEW \
|
-lGL -lglfw -lGLEW \
|
||||||
|
$(curl-config --cflags) \
|
||||||
|
$(curl-config --libs) \
|
||||||
-Wall \
|
-Wall \
|
||||||
-o $name
|
-o $name
|
||||||
'';
|
'';
|
||||||
|
|||||||
@@ -7,11 +7,12 @@
|
|||||||
#include <array>
|
#include <array>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <variant>
|
#include <variant>
|
||||||
#include <any>
|
|
||||||
|
#include <future>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
//#include <chrono>
|
#include <chrono>
|
||||||
//#include <thread>
|
//#include <thread>
|
||||||
|
|
||||||
#include <dlfcn.h>
|
#include <dlfcn.h>
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
#include "Ollama.h"
|
#include "Ollama.h"
|
||||||
#include "modules/ImguiModule/src/ImguiModule.h"
|
#include "modules/ImguiModule/src/ImguiModule.h"
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
Ollama::Ollama(Archimedes::App* a, void* h) : Archimedes::Module(a, h) {
|
Ollama::Ollama(Archimedes::App* a, void* h) : Archimedes::Module(a, h) {
|
||||||
|
|
||||||
name = "Ollama";
|
name = "Ollama";
|
||||||
@@ -10,7 +12,17 @@ Ollama::Ollama(Archimedes::App* a, void* h) : Archimedes::Module(a, h) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Ollama::~Ollama() {
|
Ollama::~Ollama() {
|
||||||
|
if(curl) {
|
||||||
|
curl_easy_cleanup(curl);
|
||||||
|
curl = nullptr;
|
||||||
|
}
|
||||||
|
curl_global_cleanup();
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::ostream* userp)
|
||||||
|
{
|
||||||
|
userp->write(static_cast<char*>(contents), size * nmemb);
|
||||||
|
return size * nmemb;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Ollama::onLoad() {
|
void Ollama::onLoad() {
|
||||||
@@ -24,14 +36,31 @@ void Ollama::onLoad() {
|
|||||||
|
|
||||||
ImGui::SetCurrentContext(im->getContext());
|
ImGui::SetCurrentContext(im->getContext());
|
||||||
|
|
||||||
|
curl_global_init(CURL_GLOBAL_ALL);
|
||||||
curl = curl_easy_init();
|
curl = curl_easy_init();
|
||||||
|
|
||||||
|
if(curl) {
|
||||||
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
||||||
|
//curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
|
||||||
|
//curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Ollama::run() {
|
void Ollama::run() {
|
||||||
|
|
||||||
static std::string s, url, response = "";
|
static std::string s, url, response = "";
|
||||||
|
|
||||||
|
static nlohmann::json sendObj;
|
||||||
|
|
||||||
|
static std::ostringstream oss;
|
||||||
|
|
||||||
|
static nlohmann::json jsonObj;
|
||||||
|
|
||||||
|
static std::future<CURLcode> result;
|
||||||
|
|
||||||
|
static bool inFlight = false;
|
||||||
|
|
||||||
ImGui::Begin("Ollama Module");
|
ImGui::Begin("Ollama Module");
|
||||||
|
|
||||||
ImGui::InputText("url: ", &url);
|
ImGui::InputText("url: ", &url);
|
||||||
@@ -39,10 +68,34 @@ void Ollama::run() {
|
|||||||
ImGui::InputTextMultiline("prompt: ", &s);
|
ImGui::InputTextMultiline("prompt: ", &s);
|
||||||
|
|
||||||
if(ImGui::Button("send")) {
|
if(ImGui::Button("send")) {
|
||||||
|
sendObj["model"] = "llama3.2";
|
||||||
|
sendObj["stream"] = false;
|
||||||
|
sendObj["prompt"] = s;
|
||||||
|
curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, sendObj.dump().c_str());
|
||||||
|
result = std::async(curl_easy_perform, curl);
|
||||||
|
inFlight = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
ImGui::Text("%s", response.c_str());
|
ImGui::Text("%s", jsonObj["response"].dump().c_str());
|
||||||
|
|
||||||
ImGui::End();
|
ImGui::End();
|
||||||
|
|
||||||
|
if(curl) {
|
||||||
|
curl_easy_setopt(curl, CURLOPT_URL, (url + "/api/generate").c_str());
|
||||||
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(inFlight && result.wait_for(std::chrono::seconds(0)) == std::future_status::ready) {
|
||||||
|
CURLcode code = result.get();
|
||||||
|
if(code != CURLE_OK) {
|
||||||
|
std::cerr << "curl_easy_perform() failed!: " << curl_easy_strerror(code) << std::endl;
|
||||||
|
app->stopModule(getName());
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
jsonObj = nlohmann::json::parse(response);
|
||||||
|
|
||||||
|
std::cout << "Full json object:\n" << jsonObj.dump() << std::endl;
|
||||||
|
}
|
||||||
|
inFlight = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user