-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaichatbot.cpp
208 lines (174 loc) · 5.62 KB
/
aichatbot.cpp
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <curl/curl.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <nlohmann/json.hpp>
#include <openssl/sha.h>
#include <sstream>
#include <string>
#include <unordered_map>
using namespace std;
using json = nlohmann::json;
string hashPassword(const string &password) {
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256((unsigned char *)password.c_str(), password.size(), hash);
stringstream ss;
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
ss << hex << setw(2) << setfill('0') << (int)hash[i];
}
return ss.str();
}
unordered_map<string, string> loadCredentials() {
unordered_map<string, string> credentials;
ifstream file("users.txt");
string username, password;
while (file >> username >> password) {
credentials[username] = password;
}
return credentials;
}
void saveCredentials(const unordered_map<string, string> &credentials) {
ofstream file("users.txt");
for (const auto &pair : credentials) {
file << pair.first << " " << pair.second << "\n";
}
}
bool authenticateUser(const string &username, const string &password,
const unordered_map<string, string> &credentials) {
auto it = credentials.find(username);
if (it != credentials.end() && it->second == hashPassword(password)) {
return true;
}
return false;
}
size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) {
((string *)userp)->append((char *)contents, size * nmemb);
return size * nmemb;
}
bool SendRequest(const string &prompt, string &response, const string &apiKey) {
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (curl) {
string url = "https://generativelanguage.googleapis.com/v1beta/models/"
"gemini-1.5-flash:generateContent?key=" +
apiKey;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(
curl, CURLOPT_HTTPHEADER,
curl_slist_append(nullptr, "Content-Type: application/json"));
json request_body = {
{"contents",
json::array({{{"parts", json::array({{{"text", prompt}}})}}})}};
string request_str = request_body.dump();
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request_str.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, request_str.length());
response.clear();
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << endl;
return false;
}
long http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
if (http_code != 200) {
cerr << "HTTP Error: " << http_code << endl;
cout << "Response Body: " << response << endl;
return false;
}
curl_easy_cleanup(curl);
return true;
} else {
cerr << "curl_easy_init() failed" << endl;
return false;
}
}
int main() {
unordered_map<string, string> credentials = loadCredentials();
cout << "\nWelcome to AI Chatbot!\n";
cout << "1. Login\n2. Register\nChoose an option: ";
int choice;
string username, password;
cin >> choice;
cin.ignore();
bool authenticated = false;
if (choice == 1) {
cout << "Enter username: ";
cin >> username;
cout << "Enter password: ";
cin >> password;
cout << endl;
if (authenticateUser(username, password, credentials)) {
authenticated = true;
cout << "Login successful!\n";
} else {
cout << "Invalid username or password.\n";
}
} else if (choice == 2) {
cout << "Choose a username: ";
cin >> username;
cout << "Choose a password: ";
cin >> password;
cout << endl;
if (credentials.find(username) == credentials.end()) {
credentials[username] = hashPassword(password);
saveCredentials(credentials);
authenticated = true;
cout << "Registration successful!\n";
} else {
cout << "Username already exists.\n";
}
} else {
cout << "Invalid option.\n";
}
if (!authenticated)
return 0;
// Load chat history
string history_file = "history_" + username + ".txt";
ifstream history_in(history_file);
string line;
cout << "\nYour previous conversations:\n" << endl;
while (getline(history_in, line)) {
cout << line << endl;
}
history_in.close();
string apiKey = "YOUR_API_KEY_HERE";
string user_input;
cout << "Start chatting! Type 'exit' to quit.\n";
ofstream history_out(history_file, ios::app);
cin.ignore();
while (true) {
cout << "You: ";
getline(cin, user_input);
if (user_input == "exit") {
break;
}
history_out << "You: " << user_input << endl;
string chatbot_response;
if (SendRequest(user_input, chatbot_response, apiKey)) {
try {
json response_json = json::parse(chatbot_response);
if (response_json.contains("candidates") &&
!response_json["candidates"].empty()) {
string generated_text =
response_json["candidates"][0]["content"]["parts"][0]["text"];
if (!generated_text.empty()) {
cout << "Bot: " << generated_text << endl;
history_out << "Bot: " << generated_text << endl;
} else {
cerr << "Error: Generated text is empty or null" << endl;
}
} else {
cerr << "Error: No candidates or invalid response structure" << endl;
}
} catch (const exception &e) {
cerr << "Error parsing JSON response: " << e.what() << endl;
}
}
}
cout << "Bot: Have a nice day, GoodBye🥳!\n" << endl;
return 0;
}