89 lines
3.3 KiB
C++
89 lines
3.3 KiB
C++
#include "openai/core/embeddings.h"
|
|||
|
|
|
||
|
|
#include <cstring>
|
||
|
|
|
||
|
|
#include "llm/error.h"
|
||
|
|
#include "openai/core/body.h"
|
||
|
|
#include "openai/core/caps.h"
|
||
|
|
#include "openai/core/check.h"
|
||
|
|
#include "security/crypto/Base64.h"
|
||
|
|
|
||
|
|
namespace molo::openai::core {
|
||
|
|
|
||
|
|
using clients::body::Json;
|
||
|
|
|
||
|
|
void EmbeddingRequest::validate() const {
|
||
|
|
Check::non_empty("model", model);
|
||
|
|
Check::require(inputs.empty() != token_inputs.empty(), "give inputs or token_inputs (exactly one)");
|
||
|
|
Check::max_count("inputs", inputs.size() + token_inputs.size(), CoreCaps::kMaxEmbeddingInputs);
|
||
|
|
for (const std::string& s : inputs) {
|
||
|
|
Check::non_empty("input", s);
|
||
|
|
Check::max_chars("input", s, CoreCaps::kMaxInputChars);
|
||
|
|
}
|
||
|
|
for (const auto& t : token_inputs) Check::require(!t.empty(), "token input is empty");
|
||
|
|
Check::in_set_or_empty("encoding_format", encoding_format, {"float", "base64"});
|
||
|
|
if (dimensions) Check::range("dimensions", *dimensions, 1, static_cast<double>(CoreCaps::kMaxEmbeddingDimensions));
|
||
|
|
}
|
||
|
|
|
||
|
|
Json EmbeddingRequest::to_json() const {
|
||
|
|
validate();
|
||
|
|
Body b(Call::kEmbeddingCreate);
|
||
|
|
b.put_text("model", model);
|
||
|
|
if (!inputs.empty()) {
|
||
|
|
if (inputs.size() == 1) b.put("input", Json(inputs[0]));
|
||
|
|
else b.put_strings("input", inputs);
|
||
|
|
} else {
|
||
|
|
Json outer = Json::array();
|
||
|
|
for (const auto& t : token_inputs) {
|
||
|
|
Json inner = Json::array();
|
||
|
|
for (int v : t) inner.push(Json(v));
|
||
|
|
outer.push(inner);
|
||
|
|
}
|
||
|
|
b.put("input", token_inputs.size() == 1 ? outer.as_array()[0] : outer);
|
||
|
|
}
|
||
|
|
b.put_text("encoding_format", encoding_format);
|
||
|
|
b.put_int("dimensions", dimensions);
|
||
|
|
b.put_text("user", user);
|
||
|
|
b.merge_extra(extra);
|
||
|
|
return b.build();
|
||
|
|
}
|
||
|
|
|
||
|
|
EmbeddingResult EmbeddingsService::parse(const Json& doc) {
|
||
|
|
EmbeddingResult r;
|
||
|
|
r.raw = doc;
|
||
|
|
r.model = Doc::str(doc, "model");
|
||
|
|
if (const Json* u = Doc::obj(doc, "usage")) {
|
||
|
|
r.prompt_tokens = static_cast<int>(Doc::num(*u, "prompt_tokens"));
|
||
|
|
r.total_tokens = static_cast<int>(Doc::num(*u, "total_tokens"));
|
||
|
|
}
|
||
|
|
const Json* data = Doc::arr(doc, "data");
|
||
|
|
if (!data) throw llm::ApiError("openai", 502, "BAD_RESPONSE", "embedding list has no data");
|
||
|
|
r.vectors.resize(data->as_array().size());
|
||
|
|
for (const Json& e : data->as_array()) {
|
||
|
|
long index = Doc::num(e, "index", -1);
|
||
|
|
if (index < 0 || static_cast<std::size_t>(index) >= r.vectors.size())
|
||
|
|
throw llm::ApiError("openai", 502, "BAD_RESPONSE", "embedding index out of range");
|
||
|
|
const Json* emb = e.find("embedding");
|
||
|
|
std::vector<float>& out = r.vectors[static_cast<std::size_t>(index)];
|
||
|
|
if (emb && emb->is_array()) {
|
||
|
|
for (const Json& v : emb->as_array()) out.push_back(static_cast<float>(v.as_number()));
|
||
|
|
} else if (emb && emb->is_string()) {
|
||
|
|
std::string raw;
|
||
|
|
if (!security::crypto::Base64::decode(emb->as_string(), raw) || raw.size() % 4 != 0)
|
||
|
|
throw llm::ApiError("openai", 502, "BAD_RESPONSE", "bad base64 embedding");
|
||
|
|
out.resize(raw.size() / 4);
|
||
|
|
std::memcpy(out.data(), raw.data(), raw.size()); // little-endian float32 on the wire
|
||
|
|
} else {
|
||
|
|
throw llm::ApiError("openai", 502, "BAD_RESPONSE", "embedding has no vector");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return r;
|
||
|
|
}
|
||
|
|
|
||
|
|
EmbeddingResult EmbeddingsService::create(const EmbeddingRequest& request) {
|
||
|
|
Json body = request.to_json();
|
||
|
|
return parse(json_call(Call::kEmbeddingCreate, {}, &body));
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace molo::openai::core
|