4#include <nlohmann/json.hpp>
6using json = nlohmann::json;
10 std::ifstream file(filename);
12 throw std::runtime_error(
"Failed to open the API credentials file.");
15 std::string api_key, organization;
16 std::getline(file, api_key);
17 std::getline(file, organization);
19 if (api_key.empty() || organization.empty()) {
20 throw std::runtime_error(
"API key or organization ID is missing in the file.");
23 return {api_key, organization};
34 {
"Authorization",
"Bearer " + api_key},
36 {
"Content-Type",
"application/json"}
41 {
"model",
"gpt-3.5-turbo"},
43 {{
"role",
"user"}, {
"content", u8
"Hello, ChatGPT!"}}
48 std::string host =
"https://neuroapi.host";
55 KURLYK_PRINT <<
"Request body: " << request_body.dump(4) << std::endl;
58 auto future = client.
post(
"/v1/chat/completions", {}, headers, request_body.dump());
60 auto response = future.get();
61 if (response->ready && response->status_code == 200) {
62 KURLYK_PRINT <<
"Response from ChatGPT: " << response->content << std::endl;
65 <<
" - " << response->error_code.message() << std::endl;
66 KURLYK_PRINT <<
"Response: " << response->content << std::endl;
69 }
catch (
const std::exception& e) {
std::pair< std::string, std::string > load_api_credentials(const std::string &filename)
A client class for making HTTP requests to a specific host.
void set_verbose(bool verbose)
Enables or disables verbose output.
bool post(const std::string &path, const QueryParams &query, const Headers &headers, const std::string &content, HttpResponseCallback callback)
Sends a POST request.
void set_retry_attempts(long retry_attempts, long retry_delay_ms)
Sets retry attempts and delay between retries for HTTP requests.
void set_host(const std::string &host)
Sets the host URL for the HTTP client.
void set_rate_limit_rpm(long requests_per_minute, RateLimitType type=RateLimitType::RL_GENERAL)
Sets the rate limit based on requests per minute (RPM).
Main header file for the Kurlyk library, providing HTTP and WebSocket support.
void init(const bool use_async=true)
Initializes the Kurlyk library, setting up necessary managers and the network worker.
void deinit()
Deinitializes the Kurlyk library, stopping the network worker and releasing resources.
utils::CaseInsensitiveMultimap Headers
Alias for HTTP headers, providing a case-insensitive unordered multimap.