-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommands.cpp
More file actions
executable file
·132 lines (116 loc) · 4.26 KB
/
commands.cpp
File metadata and controls
executable file
·132 lines (116 loc) · 4.26 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
130
131
132
#include "commands.hpp"
#include "comfy_client.hpp"
#include "musicgen_client.hpp"
#include "config.hpp"
void command_handler::registerCommand(const std::string &name,const std::string& description, std::function<std::string(const std::string &)> func, bool postProcess)
{
m_commands[name] = {description, postProcess, func};
}
std::pair<std::string,std::string> command_handler::executeCommand(const std::string &input, bool& llmPostProcessing)
{
std::regex commandRegex("\\[(\\w+)\\](.*)\\[/\\1\\](.*)");
std::smatch match;
if (std::regex_search(input, match, commandRegex))
{
std::string commandName = match[1].str();
std::string args = match[2].str();
auto it = m_commands.find(commandName);
if (it != m_commands.end())
{
LOG("command_handler calling {}({})", it->first, args);
llmPostProcessing = it->second.llmPostProcessing;
return std::make_pair(it->first, it->second.fn(args));
}
else
{
ERRLOG("Command not found: {}", commandName);
}
}
else
{
ERRLOG("Invalid command format");
}
return {};
}
std::vector<std::pair<std::string, std::string>> command_handler::listCommands()
{
std::vector<std::pair<std::string, std::string>> commandsInfo;
for (const auto& [name, data] : m_commands)
{
commandsInfo.emplace_back(name, data.description);
}
return commandsInfo;
}
std::string helper_functions::getCoordinates(const std::string &city)
{
//to avoid city, country errors
std::string firstWord = city.substr(0, city.find(" "));
std::string url = "https://geocoding-api.open-meteo.com/v1/search?name=" + urlEncode(firstWord) + "&count=2&language=en&format=json";
LOG("getCoordinates {}", url);
std::string response = makeHttpRequest(url, "", "GET");
if(!response.empty())
{
try
{
json j = json::parse(response);
if (j.empty())
{
ERRLOG("No location found for the given city.");
return std::string();
}
// j[0]["lat"];
// j[0]["lon"];
return j["results"][0].dump();
}
catch (json::parse_error& e)
{
ERRLOG("Error: {}", e.what());
return std::string();
}
}
return std::string();
}
std::string helper_functions::getWeather(const std::string &city)
{
const static std::string errorMessage = "Unexpected error";
auto strCoords = getCoordinates(city);
// LOG("coords: {}", strCoords);
if(strCoords.length() < 1)
{
return errorMessage;
}
try
{
json coordsData = json::parse(strCoords);
double lat, lon;
int reportedTemp;
std::string reportedCity, reportedCountry, reportedUnits;
reportedCountry = coordsData["country"];
reportedCity = coordsData["name"];
lat = coordsData["latitude"];
lon = coordsData["longitude"];
std::string url = "https://api.open-meteo.com/v1/forecast?latitude=" + std::to_string(lat) + "&longitude=" + std::to_string(lon) + "¤t=temperature_2m,wind_speed_10m&hourly=temperature_2m,relative_humidity_2m,wind_speed_10m";
std::string response = makeHttpRequest(url, "", "GET");
// LOG("weather: {}", response);
json j = json::parse(response);
reportedTemp = j["current"]["temperature_2m"].get<int>();
reportedUnits = j["current_units"]["temperature_2m"];
std::string formattedReturn = "Current Temperature in " + reportedCity + ", " + reportedCountry + " is " + std::to_string(reportedTemp) + reportedUnits;
return formattedReturn;
}
catch (json::parse_error& e)
{
ERRLOG("Error: {}", e.what());
}
return std::string();
}
std::string helper_functions::generateImage(const std::string &prompt)
{
auto handler = std::make_unique<comfy_client>(g_config->getValue<std::string>("comfy.base_url"), g_config->getValue<std::string>("comfy.fs_location"));
return handler->processSingleTxt2Img(prompt);
}
std::string helper_functions::generateMusic(const std::string &prompt)
{
auto handler = std::make_unique<musicgen_client>();
return handler->processSingleTxtToAudio(prompt, 12);
}