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