Skip to content

Instantly share code, notes, and snippets.

@hagope
Created June 23, 2024 21:58
Show Gist options
  • Save hagope/1f424320db87316ce4576d0b168320a0 to your computer and use it in GitHub Desktop.
Save hagope/1f424320db87316ce4576d0b168320a0 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <curl/curl.h>
#include <jsoncpp/json/json.h>
/**
flash.cpp: a c++ program to run Gemini Flash in a flash
$ sudo apt-get update
$ sudo apt-get install libcurl4-openssl-dev libjsoncpp-dev -y
$ g++ flash.cpp -o flash -lcurl -ljsoncpp
$ ./flash "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://generativelanguage.googleapis.com/v1/models/gemini-1.5-flash:generateContent?key=" + std::string(getenv("GEMINI_API_KEY"));
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");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
// Set the POST data
std::string postData = R"({"contents":[{"parts":[{"text":")" + prompt + R"("}]}]})";
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["candidates"][0]["content"]["parts"][0]["text"].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