-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlyssa_API.cpp
More file actions
148 lines (122 loc) · 4.85 KB
/
Alyssa_API.cpp
File metadata and controls
148 lines (122 loc) · 4.85 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <httplib.h>
#include <json.hpp>
#include <CoreLLM.hpp>
#include <iostream>
using json = nlohmann::json;
int main() {
httplib::Server svr;
CoreIntegration core;
std::string model_path = "models/gemma-3-4b-it-q4_0.gguf";
if (fs::exists(model_path)) {
std::cout << "Inicializando modelo automaticamente: " << model_path << std::endl;
if (!core.initialize(model_path)) {
std::cerr << "ERRO: Falha na inicialização automática do modelo!" << std::endl;
return -1;
}
std::cout << " Modelo inicializado com sucesso!" << std::endl;
} else {
std::cout << "⚠️ Modelo não encontrado em: " << model_path << std::endl;
std::cout << "ℹ️ Use o endpoint /initialize para carregar um modelo" << std::endl;
}
// Health check
svr.Get("/health", [&core](const httplib::Request&, httplib::Response& res) {
json response = {
{"status", "healthy"},
{"initialized", true}
};
res.set_content(response.dump(), "application/json");
});
// Think endpoint padrão
svr.Post("/think", [&core](const httplib::Request& req, httplib::Response& res) {
try {
auto body = json::parse(req.body);
if (!body.contains("input")) {
json error = {{"success", false}, {"error", "Missing 'input' field"}};
res.set_content(error.dump(), "application/json");
return;
}
std::string input = body["input"];
std::string expert_id = body.value("expert_id", "");
std::string result;
result = core.think_with_fusion_ttsless(input);
json response = {
{"success", true},
{"output", result},
{"expert_used", expert_id}
};
res.set_content(response.dump(), "application/json");
}
catch (const std::exception& e) {
json error = {
{"success", false},
{"error", e.what()}
};
res.set_content(error.dump(), "application/json");
}
});
// NOVO ENDPOINT: Think com fusão sem TTS
svr.Post("/think/fusion", [&core](const httplib::Request& req, httplib::Response& res) {
try {
auto body = json::parse(req.body);
if (!body.contains("input")) {
json error = {{"success", false}, {"error", "Missing 'input' field"}};
res.set_content(error.dump(), "application/json");
return;
}
std::string input = body["input"];
// Usa o novo método sem TTS
std::string result = core.think_with_fusion_ttsless(input);
json response = {
{"success", true},
{"output", result},
{"method", "weighted_fusion_ttsless"}
};
res.set_content(response.dump(), "application/json");
}
catch (const std::exception& e) {
json error = {
{"success", false},
{"error", e.what()}
};
res.set_content(error.dump(), "application/json");
}
});
// Initialize endpoint
svr.Post("/initialize", [&core](const httplib::Request& req, httplib::Response& res) {
try {
auto body = json::parse(req.body);
if (!body.contains("base_model_path")) {
json error = {{"success", false}, {"error", "Missing 'base_model_path' field"}};
res.set_content(error.dump(), "application/json");
return;
}
std::string model_path = body["base_model_path"];
// Verifica se o arquivo existe
if (!fs::exists(model_path)) {
json error = {
{"success", false},
{"error", "Model file not found: " + model_path}
};
res.set_content(error.dump(), "application/json");
return;
}
std::cout << "🔄 Inicializando modelo: " << model_path << std::endl;
bool success = core.initialize(model_path);
json response = {
{"success", success},
{"message", success ? "Model initialized successfully" : "Initialization failed"},
{"model_path", model_path}
};
res.set_content(response.dump(), "application/json");
} catch (const std::exception& e) {
json error = {
{"success", false},
{"error", e.what()}
};
res.set_content(error.dump(), "application/json");
}
});
std::cout << "Server running on http://localhost:8181" << std::endl;
svr.listen("0.0.0.0", 8181);
return 0;
}