Kurlyk
Loading...
Searching...
No Matches
chatgpt_request_example.cpp
Go to the documentation of this file.
1#include <kurlyk.hpp>
2#include <iostream>
3#include <fstream>
4#include <nlohmann/json.hpp> // For working with JSON (make sure to include the appropriate library)
5
6using json = nlohmann::json;
7
8// Function to load the API key and organization ID from a file
9std::pair<std::string, std::string> load_api_credentials(const std::string& filename) {
10 std::ifstream file(filename);
11 if (!file) {
12 throw std::runtime_error("Failed to open the API credentials file.");
13 }
14
15 std::string api_key, organization;
16 std::getline(file, api_key); // Read the first line as API key
17 std::getline(file, organization); // Read the second line as organization ID
18
19 if (api_key.empty() || organization.empty()) {
20 throw std::runtime_error("API key or organization ID is missing in the file.");
21 }
22
23 return {api_key, organization};
24}
25
26int main() {
27 kurlyk::init(true); // Initialize the kurlyk library for async support
28 try {
29 // Load the API key and organization ID from the file
30 auto [api_key, organization] = load_api_credentials("openai_api_key.txt");
31
32 // Set headers for the request to ChatGPT
33 kurlyk::Headers headers = {
34 {"Authorization", "Bearer " + api_key},
35 //{"OpenAI-Organization", organization},
36 {"Content-Type", "application/json"}
37 };
38
39 // Create the request body in JSON format
40 json request_body = {
41 {"model", "gpt-3.5-turbo"},
42 {"messages", {
43 {{"role", "user"}, {"content", u8"Hello, ChatGPT!"}}
44 }},
45 {"max_tokens", 50}
46 };
47
48 std::string host = "https://neuroapi.host";
49 kurlyk::HttpClient client;
50 client.set_host(host);
51 client.set_rate_limit_rpm(3);
52 client.set_retry_attempts(1, 5000);
53 client.set_verbose(true);
54
55 KURLYK_PRINT << "Request body: " << request_body.dump(4) << std::endl;
56
57 // Send the POST request with JSON body
58 auto future = client.post("/v1/chat/completions", {}, headers, request_body.dump());
59
60 auto response = future.get();
61 if (response->ready && response->status_code == 200) {
62 KURLYK_PRINT << "Response from ChatGPT: " << response->content << std::endl;
63 } else {
64 KURLYK_PRINT << "Error: " << response->status_code
65 << " - " << response->error_code.message() << std::endl;
66 KURLYK_PRINT << "Response: " << response->content << std::endl;
67 }
68
69 } catch (const std::exception& e) {
70 KURLYK_PRINT << "Error: " << e.what() << std::endl;
71 }
72
73 kurlyk::deinit(); // Deinitialize the kurlyk library
74 return 0;
75}
nlohmann::json json
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.
Definition runtime.hpp:13
void deinit()
Deinitializes the Kurlyk library, stopping the network worker and releasing resources.
Definition runtime.hpp:26
utils::CaseInsensitiveMultimap Headers
Alias for HTTP headers, providing a case-insensitive unordered multimap.
#define KURLYK_PRINT