|
| 1 | +/** |
| 2 | + * @file chatbot.hpp |
| 3 | + * @brief Kernel for AI Model Server Chatbot |
| 4 | + * @version 0.1 |
| 5 | + * @date 2025-04-04 |
| 6 | + * |
| 7 | + * @copyright Copyright (c) 2025 |
| 8 | + * |
| 9 | + */ |
| 10 | +#include <curl/curl.h> |
| 11 | +#include <nlohmann/json.hpp> |
| 12 | +#include <string> |
| 13 | +#include <vector> |
| 14 | +#include <VX/vx.h> |
| 15 | + |
| 16 | +#define DEFAULT_MODEL "gpt-4o-mini" |
| 17 | +#define SERVER_URL "http://localhost:8000" |
| 18 | +#define API_KEY "hardcoded-api-key" |
| 19 | + |
| 20 | +class RemoteModelClient |
| 21 | +{ |
| 22 | +private: |
| 23 | + // Helper function for non-streaming response |
| 24 | + static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) |
| 25 | + { |
| 26 | + size_t totalSize = size * nmemb; |
| 27 | + ((std::string *)userp)->append((char *)contents, totalSize); |
| 28 | + return totalSize; |
| 29 | + } |
| 30 | + |
| 31 | +public: |
| 32 | + // kernel function (non-streaming) |
| 33 | + vx_status AiServerQuery(const std::string &input_text, std::string &output_text, const std::string &api_path) |
| 34 | + { |
| 35 | + CURL *curl = curl_easy_init(); |
| 36 | + if (!curl) |
| 37 | + return VX_FAILURE; |
| 38 | + |
| 39 | + nlohmann::json request_json = { |
| 40 | + {"model", DEFAULT_MODEL}, |
| 41 | + {"messages", {{{"role", "user"}, {"content", input_text}}}}, |
| 42 | + {"max_tokens", 100}, |
| 43 | + {"stream", false}}; |
| 44 | + |
| 45 | + std::string request_payload = request_json.dump(); |
| 46 | + std::string response_string; |
| 47 | + std::string api_url = std::string(SERVER_URL) + api_path; |
| 48 | + |
| 49 | + struct curl_slist *headers = nullptr; |
| 50 | + headers = curl_slist_append(headers, "Content-Type: application/json"); |
| 51 | + headers = curl_slist_append(headers, ("Authorization: Bearer " + std::string(API_KEY)).c_str()); |
| 52 | + |
| 53 | + curl_easy_setopt(curl, CURLOPT_URL, api_url.c_str()); |
| 54 | + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request_payload.c_str()); |
| 55 | + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); |
| 56 | + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); |
| 57 | + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_string); |
| 58 | + |
| 59 | + CURLcode res = curl_easy_perform(curl); |
| 60 | + curl_slist_free_all(headers); |
| 61 | + curl_easy_cleanup(curl); |
| 62 | + |
| 63 | + if (res != CURLE_OK) |
| 64 | + return VX_FAILURE; |
| 65 | + |
| 66 | + auto json_response = nlohmann::json::parse(response_string); |
| 67 | + output_text = json_response["choices"][0]["message"]["content"]; |
| 68 | + |
| 69 | + return VX_SUCCESS; |
| 70 | + } |
| 71 | + |
| 72 | + // kernel function (streaming) |
| 73 | + vx_status AiServerQueryStream(const std::string &input_text, std::string &output_text, const std::string &api_path) |
| 74 | + { |
| 75 | + CURL *curl = curl_easy_init(); |
| 76 | + if (!curl) |
| 77 | + return VX_FAILURE; |
| 78 | + |
| 79 | + nlohmann::json request_json = { |
| 80 | + {"model", DEFAULT_MODEL}, |
| 81 | + {"messages", {{{"role", "user"}, {"content", input_text}}}}, |
| 82 | + {"max_tokens", 100}, |
| 83 | + {"stream", true}}; |
| 84 | + |
| 85 | + std::string request_payload = request_json.dump(); |
| 86 | + std::string response_chunk; |
| 87 | + std::string api_url = std::string(SERVER_URL) + api_path; |
| 88 | + |
| 89 | + struct curl_slist *headers = nullptr; |
| 90 | + headers = curl_slist_append(headers, "Content-Type: application/json"); |
| 91 | + headers = curl_slist_append(headers, ("Authorization: Bearer " + std::string(API_KEY)).c_str()); |
| 92 | + |
| 93 | + curl_easy_setopt(curl, CURLOPT_URL, api_url.c_str()); |
| 94 | + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request_payload.c_str()); |
| 95 | + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); |
| 96 | + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); |
| 97 | + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_chunk); |
| 98 | + |
| 99 | + CURLcode res = curl_easy_perform(curl); |
| 100 | + curl_slist_free_all(headers); |
| 101 | + curl_easy_cleanup(curl); |
| 102 | + |
| 103 | + if (res != CURLE_OK) |
| 104 | + return VX_FAILURE; |
| 105 | + |
| 106 | + // Just return raw streamed response (newline-delimited JSON chunks) |
| 107 | + output_text = response_chunk; |
| 108 | + return VX_SUCCESS; |
| 109 | + } |
| 110 | +}; |
0 commit comments