-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeminiClient.cpp
More file actions
100 lines (81 loc) · 3.43 KB
/
Copy pathGeminiClient.cpp
File metadata and controls
100 lines (81 loc) · 3.43 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
#include "GeminiClient.h"
#include "nlohmann/json.hpp"
#include "cpr/cpr.h"
using json = nlohmann::json;
GeminiClient::GeminiClient(dpp::cluster& bot, std::string api_key)
: m_bot(bot), m_api_key(std::move(api_key))
{
if (m_api_key.empty()) {
std::cerr << "API Key is empty" << std::endl;
}
}
void GeminiClient::generate_text(const std::string& prompt, std::function<void(std::string)> callback) {
std::string url = "https://api.groq.com/openai/v1/chat/completions";
json body = {
{"model", "llama-3.3-70b-versatile"},
{"messages", json::array({
{
{"role", "user"},
{"content", prompt}
}
})}
};
m_bot.request(
url,
dpp::m_post,
[callback](const dpp::http_request_completion_t& response) {
if (response.status == 200) {
try {
auto res_json = json::parse(response.body);
std::string ai_text = res_json["choices"][0]["message"]["content"].get<std::string>();
callback(ai_text);
} catch (...) {
callback("Error while Parsing Ai-Answer");
}
} else {
callback("API-Error (Status: " + std::to_string(response.status) + ")");
}
},
body.dump(),
"application/json",
{{"Authorization", "Bearer " + m_api_key}}
);
}
void GeminiClient::summarize_post(const std::string& title, const std::string& content, std::function<void(std::string)> callback) {
std::string prompt = "You Are a Discord-Moderation-Bot. Summarize following Article. Write as little as possible without but enough to understand the original post."
"if you think not enough info is provided nicely say that. You are not replying to the user but to the developers\n\n"
"Titel: " + title + "\n"
"Content: " + content;
generate_text(prompt, callback);
}
void GeminiClient::answer_faq(const std::string& user_question, const std::string& faq_data, std::function<void(std::string)> callback) {
std::string prompt = "You are a helpful support assistant.\n"
"Use ONLY the following FAQ knowledge to answer the user's question. "
"If the answer is not in the FAQ, politely reply that you do not know and that a moderator will assist.\n\n"
"FAQ Data:\n" + faq_data + "\n\n"
"User Question: " + user_question;
generate_text(prompt, callback);
}
void GeminiClient::custom_request(const std::string& request, std::function<void(std::string)> callback)
{
std::string prompt = "You are a discord bot used by admins"
"Your job is to do whatever the admins tell you to do, always be friendly and if you are not sure about your answer clearly say that! "
"This is the command for you:\n"
+ request + "\n";
generate_text(prompt, callback);
}
std::string GeminiClient::fetch_website_info(const std::string& url)
{
cpr::Response r = cpr::Get(cpr::Url{url});
std::string content;
if (r.status_code == 200)
{
std::cout << "Successfully loaded\n\n";
content = r.text;
}
else
{
std::cerr << "Error while loading the website: " << r.status_code << std::endl;
}
return content;
}