Skip to content

Commit

Permalink
fix: model list json.
Browse files Browse the repository at this point in the history
  • Loading branch information
b4rtaz committed Feb 16, 2025
1 parent a4bd7aa commit c2792a1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
31 changes: 31 additions & 0 deletions src/api-types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,25 @@ struct ChatCompletion {
}
};

struct Model {
std::string id;
std::string object;
long long created;
std::string owned_by;

Model() : id(), object(), created(0), owned_by() {}
Model(const std::string &id_) : id(id_), object("model"), created(0), owned_by("user") {}
};

struct ModelList {
std::string object;
std::vector<Model> data;
ModelList(): object("list") {}
ModelList(const Model &model_) : object("list") {
data.push_back(model_);
}
};

struct InferenceParams {
std::vector<ChatMessage> messages;
int max_tokens;
Expand Down Expand Up @@ -132,6 +151,18 @@ void to_json(json& j, const ChatCompletion& completion) {
{"choices", completion.choices}};
}

void to_json(json& j, const Model& model) {
j = json{{"id", model.id},
{"object", model.object},
{"created", model.created},
{"owned_by", model.owned_by}};
}

void to_json(json& j, const ModelList& models) {
j = json{{"object", models.object},
{"data", models.data}};
}

std::vector<ChatMessage> parseChatMessages(json &json){
std::vector<ChatMessage> messages;
messages.reserve(json.size());
Expand Down
12 changes: 5 additions & 7 deletions src/dllama-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,15 +495,13 @@ void handleCompletionsRequest(HttpRequest& request, ApiServer *api) {

void handleModelsRequest(HttpRequest& request, const char* modelPath) {
std::string path(modelPath);

size_t pos = path.find_last_of("/\\");
std::string modelName = (pos == std::string::npos) ? path : path.substr(pos+1);
std::string modelName = (pos == std::string::npos) ? path : path.substr(pos + 1);

request.writeJson(
"{ \"object\": \"list\","
"\"data\": ["
"{ \"id\": \"" + modelName + "\", \"object\": \"model\", \"created\": 0, \"owned_by\": \"user\" }"
"] }");
Model model(modelName);
ModelList list(model);
std::string response = ((json)list).dump();
request.writeJson(response);
}

static void server(AppInferenceContext *context) {
Expand Down

0 comments on commit c2792a1

Please sign in to comment.