-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNetwork.cpp
More file actions
129 lines (103 loc) · 3.57 KB
/
Copy pathNetwork.cpp
File metadata and controls
129 lines (103 loc) · 3.57 KB
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
#include <curl/curl.h>
#include <string>
#include <nlohmann/json.hpp>
#include <iostream>
#include <fstream>
#include "Network.h"
using json = nlohmann::json;
size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
size_t totalSize = size * nmemb;
std::string* str = (std::string*)userp;
str->append((char*)contents, totalSize);
return totalSize;
}
std::string Network::BuildBody(const std::string& Model, const std::string& ImageId,
const std::string& SystemPrompt, const std::string& UserPrompt)
{
json j;
j["model"] = Model;
j["messages"] = json::array();
json message;
message["role"] = "system";
message["content"] = SystemPrompt;
j["messages"].push_back(std::move(message));
json message1;
message1["role"] = "system";
message1["content"] = std::format("fileid://{}", ImageId);
j["messages"].push_back(std::move(message1));
json message2;
message2["role"] = "user";
message2["content"] = UserPrompt;
j["messages"].push_back(std::move(message2));
return j.dump();
}
void Network::Request(const std::string& ApiKey, const std::string& Body, std::string& OutBuffer)
{
std::string BaseURL("https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions");
CURL* curl;
CURLcode res;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, BaseURL.c_str());
// set write callback
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &OutBuffer);
// set headers
struct curl_slist* headers = nullptr;
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, std::format("Authorization: Bearer {}", ApiKey).c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
// set data
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Body.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, Body.size());
// send request
res = curl_easy_perform(curl);
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
}
// upload image to server then get the image url
void Network::UploadImage(const std::string& ImagePath, const std::string& ApiKey, std::string& OutBuffer)
{
std::string UploadImageURL = "https://dashscope.aliyuncs.com/compatible-mode/v1/files";
CURL* curl;
CURLcode res;
curl = curl_easy_init();
if (curl) {
// set base url
curl_easy_setopt(curl, CURLOPT_URL, UploadImageURL.c_str());
// set headers
std::string ApiKeyHeader = std::format("Authorization: Bearer {}", ApiKey);
struct curl_slist* headers = nullptr;
headers = curl_slist_append(headers, ApiKeyHeader.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
// set form-data
curl_mime* mime;
curl_mimepart* part;
mime = curl_mime_init(curl);
// add file
part = curl_mime_addpart(mime);
curl_mime_name(part, "file");
curl_mime_filedata(part, ImagePath.c_str());
// add purpose
part = curl_mime_addpart(mime);
curl_mime_name(part, "purpose");
curl_mime_data(part, "file-extract", CURL_ZERO_TERMINATED);
// set form-data to curl
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
// set write callback
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &OutBuffer);
// send request
res = curl_easy_perform(curl);
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
// cleanup
curl_mime_free(mime);
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
std::cout << "Upload Image finished" << std::endl;
}
}