Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Nomic Embedding API and Nomic Embed #125

Merged
merged 4 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions engine/server/web_server/web_controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ class WebController : public oatpp::web::server::api::ApiController {
if (headerValue != nullptr) {
headers[MIXEDBREADAI_KEY_HEADER] = headerValue->c_str();
}
headerValue = request->getHeader(NOMIC_KEY_HEADER);
if (headerValue != nullptr) {
headers[NOMIC_KEY_HEADER] = headerValue->c_str();
}

std::string db_path = parsedBody.GetString("path");
std::string db_name = parsedBody.GetString("name");
Expand Down Expand Up @@ -416,6 +420,10 @@ class WebController : public oatpp::web::server::api::ApiController {
if (headerValue != nullptr) {
headers[MIXEDBREADAI_KEY_HEADER] = headerValue->c_str();
}
headerValue = request->getHeader(NOMIC_KEY_HEADER);
if (headerValue != nullptr) {
headers[NOMIC_KEY_HEADER] = headerValue->c_str();
}

auto data = parsedBody.GetArray("data");
vectordb::Status insert_status = db_server->Insert(db_name, table_name, data, headers, upsert);
Expand Down Expand Up @@ -650,6 +658,10 @@ class WebController : public oatpp::web::server::api::ApiController {
if (headerValue != nullptr) {
headers[MIXEDBREADAI_KEY_HEADER] = headerValue->c_str();
}
headerValue = request->getHeader(NOMIC_KEY_HEADER);
if (headerValue != nullptr) {
headers[NOMIC_KEY_HEADER] = headerValue->c_str();
}

vectordb::Json result;
vectordb::Status search_status;
Expand Down
21 changes: 17 additions & 4 deletions engine/services/embedding_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Status EmbeddingService::denseEmbedDocuments(
std::string jinaai_key = "";
std::string voyageai_key = "";
std::string mixedbreadai_key = "";
std::string nomic_key = "";
// Inject 3rd party service key based on their model name.
if (server::CommonUtil::StartsWith(model_name, "openai/")) {
if (headers.find(OPENAI_KEY_HEADER) == headers.end()) {
Expand All @@ -83,6 +84,11 @@ Status EmbeddingService::denseEmbedDocuments(
return Status(INVALID_PAYLOAD, "Missing mixedbread ai API key.");
}
mixedbreadai_key = headers[MIXEDBREADAI_KEY_HEADER];
} else if (server::CommonUtil::StartsWith(model_name, "nomicai/")) {
if (headers.find(NOMIC_KEY_HEADER) == headers.end()) {
return Status(INVALID_PAYLOAD, "Missing Nomic API key.");
}
nomic_key = headers[NOMIC_KEY_HEADER];
}

// Constructing documents list from attr_column_container
Expand All @@ -91,7 +97,7 @@ Status EmbeddingService::denseEmbedDocuments(
// Assuming attr_column_container[idx] returns a string or can be converted to string
requestBody->documents->push_back(oatpp::String(std::get<std::string>(attr_column_container[idx]).c_str()));
}
auto response = m_client->denseEmbedDocuments("/v1/embeddings", openai_key, jinaai_key, voyageai_key, mixedbreadai_key, requestBody);
auto response = m_client->denseEmbedDocuments("/v1/embeddings", openai_key, jinaai_key, voyageai_key, mixedbreadai_key, nomic_key, requestBody);
auto responseBody = response->readBodyToString();
// std::cout << "Embedding response: " << responseBody->c_str() << std::endl;
vectordb::Json json;
Expand Down Expand Up @@ -143,6 +149,7 @@ Status EmbeddingService::denseEmbedQuery(
std::string jinaai_key = "";
std::string voyageai_key = "";
std::string mixedbreadai_key = "";
std::string nomic_key = "";
// Inject 3rd party service key based on their model name.
if (server::CommonUtil::StartsWith(model_name, "openai/")) {
if (headers.find(OPENAI_KEY_HEADER) == headers.end()) {
Expand All @@ -164,9 +171,15 @@ Status EmbeddingService::denseEmbedQuery(
return Status(INVALID_PAYLOAD, "Missing mixedbread ai API key.");
}
mixedbreadai_key = headers[MIXEDBREADAI_KEY_HEADER];
} else if (server::CommonUtil::StartsWith(model_name, "nomicai/")) {
if (headers.find(NOMIC_KEY_HEADER) == headers.end()) {
return Status(INVALID_PAYLOAD, "Missing Nomic API key.");
}
nomic_key = headers[NOMIC_KEY_HEADER];
}

auto response = m_client->denseEmbedDocuments("/v1/embeddings", openai_key, jinaai_key, voyageai_key, mixedbreadai_key, requestBody);

auto response = m_client->denseEmbedDocuments("/v1/embeddings", openai_key, jinaai_key, voyageai_key, mixedbreadai_key, nomic_key, requestBody);
auto responseBody = response->readBodyToString();
// std::cout << "Embedding response: " << responseBody->c_str() << std::endl;
vectordb::Json json;
Expand All @@ -192,8 +205,8 @@ Status EmbeddingService::denseEmbedQuery(
std::this_thread::sleep_for(std::chrono::seconds(delaySec));
std::cout << "Retry embedding the query." << std::endl;
}
return Status(INFRA_UNEXPECTED_ERROR, "Failed to embbed the query.");
return Status(INFRA_UNEXPECTED_ERROR, "Failed to embed the query.");
}

} // namespace engine
} // namespace vectordb
} // namespace vectordb
2 changes: 1 addition & 1 deletion engine/services/embedding_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class MyApiClient : public oatpp::web::client::ApiClient {
API_CLIENT_INIT(MyApiClient)

API_CALL("GET", "{path}", getEmbeddings, PATH(String, path))
API_CALL("POST", "{path}", denseEmbedDocuments, PATH(String, path), HEADER(String, openaiHeader, OPENAI_KEY_HEADER), HEADER(String, jinaaiHeader, JINAAI_KEY_HEADER), HEADER(String, voyageaiHeader, VOYAGEAI_KEY_HEADER), HEADER(String, mixedbreadaiHeader, MIXEDBREADAI_KEY_HEADER), BODY_DTO(Object<EmbeddingRequestBody>, body))
API_CALL("POST", "{path}", denseEmbedDocuments, PATH(String, path), HEADER(String, openaiHeader, OPENAI_KEY_HEADER), HEADER(String, jinaaiHeader, JINAAI_KEY_HEADER), HEADER(String, voyageaiHeader, VOYAGEAI_KEY_HEADER), HEADER(String, mixedbreadaiHeader, MIXEDBREADAI_KEY_HEADER), HEADER(String, nomicHeader, NOMIC_KEY_HEADER), BODY_DTO(Object<EmbeddingRequestBody>, body))


#include OATPP_CODEGEN_END(ApiClient) //<- End codegen
Expand Down
1 change: 1 addition & 0 deletions engine/utils/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ namespace vectordb {
constexpr const char* JINAAI_KEY_HEADER = "X-JinaAI-API-Key";
constexpr const char* VOYAGEAI_KEY_HEADER = "X-VoyageAI-API-Key";
constexpr const char* MIXEDBREADAI_KEY_HEADER = "X-MixedbreadAI-API-Key";
constexpr const char* NOMIC_KEY_HEADER = "X-NOMIC-API-Key";
} // namespace vectordb