-
Notifications
You must be signed in to change notification settings - Fork 15
/
chatgpt.hpp
186 lines (167 loc) · 4.69 KB
/
chatgpt.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <iostream>
#include <Windows.h>
#include <wininet.h>
#include <vector>
#include <optional>
#include <string>
#include <sstream>
#include <map>
#include <memory>
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"wininet.lib")
#include "rest.h"
#include "jsonxx.h"
struct CHATGPT_RESULT
{
jsonxx::Object o;
std::string t;
std::vector<char> data;
};
std::vector<char> Fetch(const char* TheLink)
{
std::vector<char> aaaa;
// Create thread that will show download progress
DWORD Size;
unsigned long bfs = 1000;
TCHAR ss[1000];
DWORD TotalTransferred = 0;
int err = 1;
HINTERNET hI = 0, hRead = 0;
hI = InternetOpen(L"ChatGPT-API", INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0);
if (hI)
{
hRead = InternetOpenUrlA(hI, TheLink, 0, 0, INTERNET_FLAG_NO_CACHE_WRITE, 0);
if (hRead)
{
if (!HttpQueryInfo(hRead, HTTP_QUERY_CONTENT_LENGTH, ss, &bfs, 0))
Size = (DWORD)-1;
else
Size = _wtoi(ss);
for (;;)
{
DWORD n;
char Buff[100010] = { 0 };
memset(Buff, 0, 100010);
BOOL F = InternetReadFile(hRead, Buff, 100000, &n);
if (F == false)
{
err = 2;
break;
}
if (n == 0)
{
// End of file !
err = 0;
break;
}
TotalTransferred += n;
//Write to File !
//char xx = Buff[n];
size_t olds = aaaa.size();
aaaa.resize(olds + n);
memcpy(aaaa.data() + olds, Buff, n);
int NewPos = 0;
if (Size != -1)
NewPos = (100 * TotalTransferred) / Size;
}
InternetCloseHandle(hRead);
}
InternetCloseHandle(hI);
}
return aaaa;
}
class CHATGPT_API
{
std::string tok;
std::string model = "text-davinci-003";
public:
CHATGPT_API(const char* api_key)
{
tok = api_key;
}
void SetModel(const char* m)
{
model = m;
}
std::wstring Bearer()
{
wchar_t auth[200] = {};
swprintf_s(auth, 200, L"Authorization: Bearer %S", tok.c_str());
return auth;
}
std::optional<CHATGPT_RESULT> Image(const char* prompt,int wi = 1024,int he = 1024)
{
std::vector<char> data(10000);
sprintf_s(data.data(), 10000, R"({
"prompt" : "%s",
"n" : 1,
"size" : "%ix%i"
})", prompt, wi,he);
data.resize(strlen(data.data()));
RESTAPI::REST r;
r.Connect(L"api.openai.com", true, 0, 0, 0, 0);
std::initializer_list<std::wstring> hdrs = {
Bearer(),
L"Content-Type: application/json",
};
auto hi = r.RequestWithBuffer(L"/v1/images/generations", L"POST", hdrs, data.data(), data.size());
std::vector<char> out;
r.ReadToMemory(hi, out);
out.resize(out.size() + 1);
try
{
jsonxx::Object o;
o.parse(out.data());
CHATGPT_RESULT r;
r.o = o;
auto& data = o.get<jsonxx::Array>("data");
auto& data0 = data.get<jsonxx::Object>(0);
r.t = data0.get<jsonxx::String>("url");
r.data = Fetch(r.t.c_str());
return r;
}
catch (...)
{
}
return {};
}
std::optional<CHATGPT_RESULT> Text(const char* prompt, int Temperature = 0, int max_tokens = 100)
{
std::vector<char> data(10000);
sprintf_s(data.data(), 10000, R"({
"model": "%s",
"prompt" : "%s",
"temperature" : %i,
"max_tokens" : %i,
"top_p" : 1,
"frequency_penalty" : 0.2,
"presence_penalty" : 0
})", model.c_str(), prompt, Temperature, max_tokens);
data.resize(strlen(data.data()));
RESTAPI::REST r;
r.Connect(L"api.openai.com", true, 0, 0, 0, 0);
std::initializer_list<std::wstring> hdrs = {
Bearer(),
L"Content-Type: application/json",
};
auto hi = r.RequestWithBuffer(L"/v1/completions", L"POST", hdrs, data.data(), data.size());
std::vector<char> out;
r.ReadToMemory(hi, out);
out.resize(out.size() + 1);
try
{
jsonxx::Object o;
o.parse(out.data());
CHATGPT_RESULT r;
r.o = o;
auto& choices = o.get<jsonxx::Array>("choices");
auto& choice0 = choices.get<jsonxx::Object>(0);
r.t = choice0.get<jsonxx::String>("text");
return r;
}
catch (...)
{
}
return {};
}
};