Skip to content

Commit c2792a1

Browse files
committed
fix: model list json.
1 parent a4bd7aa commit c2792a1

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

src/api-types.hpp

+31
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,25 @@ struct ChatCompletion {
8282
}
8383
};
8484

85+
struct Model {
86+
std::string id;
87+
std::string object;
88+
long long created;
89+
std::string owned_by;
90+
91+
Model() : id(), object(), created(0), owned_by() {}
92+
Model(const std::string &id_) : id(id_), object("model"), created(0), owned_by("user") {}
93+
};
94+
95+
struct ModelList {
96+
std::string object;
97+
std::vector<Model> data;
98+
ModelList(): object("list") {}
99+
ModelList(const Model &model_) : object("list") {
100+
data.push_back(model_);
101+
}
102+
};
103+
85104
struct InferenceParams {
86105
std::vector<ChatMessage> messages;
87106
int max_tokens;
@@ -132,6 +151,18 @@ void to_json(json& j, const ChatCompletion& completion) {
132151
{"choices", completion.choices}};
133152
}
134153

154+
void to_json(json& j, const Model& model) {
155+
j = json{{"id", model.id},
156+
{"object", model.object},
157+
{"created", model.created},
158+
{"owned_by", model.owned_by}};
159+
}
160+
161+
void to_json(json& j, const ModelList& models) {
162+
j = json{{"object", models.object},
163+
{"data", models.data}};
164+
}
165+
135166
std::vector<ChatMessage> parseChatMessages(json &json){
136167
std::vector<ChatMessage> messages;
137168
messages.reserve(json.size());

src/dllama-api.cpp

+5-7
Original file line numberDiff line numberDiff line change
@@ -495,15 +495,13 @@ void handleCompletionsRequest(HttpRequest& request, ApiServer *api) {
495495

496496
void handleModelsRequest(HttpRequest& request, const char* modelPath) {
497497
std::string path(modelPath);
498-
499498
size_t pos = path.find_last_of("/\\");
500-
std::string modelName = (pos == std::string::npos) ? path : path.substr(pos+1);
499+
std::string modelName = (pos == std::string::npos) ? path : path.substr(pos + 1);
501500

502-
request.writeJson(
503-
"{ \"object\": \"list\","
504-
"\"data\": ["
505-
"{ \"id\": \"" + modelName + "\", \"object\": \"model\", \"created\": 0, \"owned_by\": \"user\" }"
506-
"] }");
501+
Model model(modelName);
502+
ModelList list(model);
503+
std::string response = ((json)list).dump();
504+
request.writeJson(response);
507505
}
508506

509507
static void server(AppInferenceContext *context) {

0 commit comments

Comments
 (0)