YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Utils.cpp
Go to the documentation of this file.
1#include "Utils.h"
2
3std::string extractContent(const std::string& jsonResponse) {
4 try {
5 json parsedJson = json::parse(jsonResponse);
6 return parsedJson["choices"][0]["message"]["content"].get<std::string>();
7 } catch (const std::exception& e) {
8 std::cerr << "Error in parsing JSON: " << e.what() << std::endl;
9 return "";
10 }
11}
12
13size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) {
14 ((std::string*)userp)->append((char*)contents, size * nmemb);
15 return size * nmemb;
16}
17
18std::string sendPostRequest(const std::string& jsonData) {
19 CURL *curl;
20 CURLcode res;
21 std::string responseString;
22
23 curl_global_init(CURL_GLOBAL_ALL);
24 curl = curl_easy_init();
25
26 if (curl) {
27 const char* url = "http://localhost:8080/v1/chat/completions";
28 std::cout << "Sending request to: " << url << std::endl;
29 std::cout << "JSON data: " << jsonData << std::endl;
30
31 curl_easy_setopt(curl, CURLOPT_URL, url);
32 curl_easy_setopt(curl, CURLOPT_POST, 1);
33 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonData.c_str());
34
35 struct curl_slist *headers = NULL;
36 headers = curl_slist_append(headers, "Content-Type: application/json");
37 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
38
39 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
40 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseString);
41
42 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); // DEBUG mode
43
44 res = curl_easy_perform(curl);
45
46 if (res != CURLE_OK) {
47 std::cerr << "Error in the request: " << curl_easy_strerror(res) << std::endl;
48 }
49
50 long response_code;
51 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
52 std::cout << "HTTP response code: " << response_code << std::endl;
53
54 curl_easy_cleanup(curl);
55 curl_global_cleanup();
56 }
57
58 return responseString;
59}
60
61void startLlama2Server(const std::string &model_path, int m_context, int m_gpulayers) {
62 pid_t pid = fork();
63
64 if (pid == 0) { // Child process
65 prctl(PR_SET_PDEATHSIG, SIGKILL); // Auto-kill if parent exits
66
67 // Prepare arguments for execvp
68 std::string exe_path = "llama-server";
69 std::string arg_m = "-m";
70 std::string arg_c = "-c";
71 std::string arg_ngl = "-ngl";
72 std::string arg_context = std::to_string(m_context);
73 std::string arg_nglvalue = std::to_string(m_gpulayers);
74
75 char *args[] = {
76 (char *)exe_path.c_str(),
77 (char *)arg_m.c_str(),
78 (char *)model_path.c_str(),
79 (char *)arg_c.c_str(),
80 (char *)arg_context.c_str(),
81 (char *)arg_ngl.c_str(),
82 (char *)arg_nglvalue.c_str(),
83 nullptr
84 };
85
86 execvp(args[0], args);
87 std::cerr << "Error: Failed to start llama2-server!" << std::endl;
88 std::exit(1);
89 } else if (pid > 0) { // Parent process
90 std::cout << "llama2-server started in background with PID: " << pid << std::endl;
91 } else {
92 std::cerr << "Error: fork() failed!" << std::endl;
93 }
94}
std::string sendPostRequest(const std::string &jsonData)
Definition Utils.cpp:18
void startLlama2Server(const std::string &model_path, int m_context, int m_gpulayers)
Definition Utils.cpp:61
std::string extractContent(const std::string &jsonResponse)
Definition Utils.cpp:3
size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
Definition Utils.cpp:13
nlohmann::json json
Definition Utils.h:15