Skip to content

Instantly share code, notes, and snippets.

@hagope
Created June 23, 2024 22:33
Show Gist options
  • Save hagope/b36dd5e6fbadbf24cf4ec803fe1b7a66 to your computer and use it in GitHub Desktop.
Save hagope/b36dd5e6fbadbf24cf4ec803fe1b7a66 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <curl/curl.h>
#include <jsoncpp/json/json.h>
/**
groq.cpp: a c++ program to run groq commands
$ sudo apt-get update
$ sudo apt-get install libcurl4-openssl-dev libjsoncpp-dev -y
$ g++ groq.cpp -o groq -lcurl -ljsoncpp
$ ./groq "capital of france"
**/
// Function to write the response data to a string
static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
int main(int argc, char* argv[]) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <prompt>" << std::endl;
return 1;
}
std::string prompt = argv[1];
CURL* curl;
CURLcode res;
std::string readBuffer;
// Initialize a CURL session
curl = curl_easy_init();
if (curl) {
// Set the URL for the POST request
std::string url = "https://api.groq.com/openai/v1/chat/completions";
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
// Set the Content-Type header
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
std::string auth_header = "Authorization: Bearer " + std::string(std::getenv("GROQ_API_KEY"));
headers = curl_slist_append(headers, auth_header.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
// Set the POST data
std::string postData = R"({"messages": [{"role": "user", "content": ")" + prompt +R"("}], "model": "llama3-70b-8192"})";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
// Set the callback function to capture the response
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
// Perform the request, res will get the return code
res = curl_easy_perform(curl);
// Check for errors
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
} else {
// Parse the JSON response
Json::CharReaderBuilder readerBuilder;
Json::Value root;
std::string errs;
std::istringstream s(readBuffer);
if (Json::parseFromStream(readerBuilder, s, &root, &errs)) {
// Print the desired part of the JSON response
std::cout << root["choices"][0]["message"]["content"].asString() << std::endl;
} else {
std::cerr << "Failed to parse JSON: " << errs << std::endl;
}
}
// Clean up
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment