-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpRequestGroq.cpp
62 lines (51 loc) · 1.92 KB
/
httpRequestGroq.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <string>
#include <curl/curl.h>
#include <nlohmann/json.hpp>
#include "httpRequestGroq.h"
using json = nlohmann::json;
size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* userp) {
size_t totalSize = size * nmemb;
userp->append(static_cast<char*>(contents), totalSize);
return totalSize;
}
void RequestGroq::sendRequest(const std::string& prompt, std::string& response) {
CURL* curl;
CURLcode res;
std::string readBuffer;
std::string jsonData = R"({
"model": "llama-3.3-70b-versatile",
"messages": [{
"role": "user",
"content": ")" + prompt + R"("
}]
})";
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://api.groq.com/openai/v1/chat/completions");
struct curl_slist* headers = nullptr;
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "Authorization: Bearer YourTokenHere");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonData.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
std::cerr << "Erro na requisicaoo: " << curl_easy_strerror(res) << std::endl;
}
else {
try {
auto parsed = json::parse(readBuffer);
response = parsed["choices"][0]["message"]["content"];
}
catch (const std::exception& e) {
std::cerr << "Erro ao processar JSON: " << e.what() << std::endl;
}
}
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
}
curl_global_cleanup();
}