Kurlyk
Toggle main menu visibility
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
7
using
json
= nlohmann::json;
8
9
// Function to load the API key and organization ID from a file
10
std::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
27
int
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
}
json
nlohmann::json json
Definition
bearer_token_auth_provider_example.cpp:7
main
int main()
Definition
chatgpt_request_example.cpp:27
load_api_credentials
std::pair< std::string, std::string > load_api_credentials(const std::string &filename)
Definition
chatgpt_request_example.cpp:10
kurlyk::HttpClient
Concrete HTTP client for making requests to a specific host.
Definition
HttpClient.hpp:17
kurlyk::HttpClient::set_verbose
void set_verbose(bool verbose)
Enables or disables verbose output.
Definition
HttpClient.hpp:563
kurlyk::HttpClient::post
bool post(const std::string &path, const QueryParams &query, const Headers &headers, const std::string &content, HttpResponseCallback callback)
Sends a POST request.
Definition
HttpClient.hpp:696
kurlyk::HttpClient::set_retry_attempts
void set_retry_attempts(long retry_attempts, long retry_delay_ms)
Sets retry attempts and delay between retries for HTTP requests.
Definition
HttpClient.hpp:504
kurlyk::HttpClient::set_rate_limit_rpm
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).
Definition
HttpClient.hpp:314
kurlyk::HttpClient::set_host
void set_host(const std::string &host)
Sets the host URL for the HTTP client.
Definition
HttpClient.hpp:182
kurlyk.hpp
Main header file for the Kurlyk library, providing HTTP and WebSocket support.
kurlyk::init
void init(const bool use_async=true)
Initializes the Kurlyk library, setting up necessary managers and the network worker.
Definition
runtime.hpp:13
kurlyk::deinit
void deinit()
Deinitializes the Kurlyk library, stopping async processing or cleaning up synchronous state.
Definition
runtime.hpp:26
kurlyk::Headers
utils::CaseInsensitiveMultimap Headers
Alias for HTTP headers, providing a case-insensitive unordered multimap.
Definition
CaseInsensitiveMultimap.hpp:62
KURLYK_PRINT
#define KURLYK_PRINT
Definition
print_utils.hpp:8
examples
chatgpt_request_example.cpp
Generated by
1.17.0