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

Add the batching to the llm_embedding #54

Merged
merged 1 commit into from
Nov 12, 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
8 changes: 4 additions & 4 deletions src/core/functions/scalar/llm_embedding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ static void LlmEmbeddingScalarFunction(DataChunk &args, ExpressionState &state,
auto model_details_json = CoreScalarParsers::Struct2Json(args.data[1], 1)[0];
auto model_details = ModelManager::CreateModelDetails(con, model_details_json);

auto embeddings = nlohmann::json::array();
vector<string> prepared_inputs;
for (auto &row : inputs) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prepared_inputs is not a clear name, let's find a better name, you can also provide the input size for prepared_inputs in its ctor.

std::string concat_input;
for (auto &item : row.items()) {
concat_input += item.value().get<std::string>() + " ";
}

auto element_embedding = ModelManager::CallEmbedding(concat_input, model_details);
embeddings.push_back(element_embedding);
prepared_inputs.push_back(concat_input);
}

auto embeddings = ModelManager::CallEmbedding(prepared_inputs, model_details);

for (size_t index = 0; index < embeddings.size(); index++) {
vector<Value> embedding;
for (auto &value : embeddings[index]) {
Expand Down
28 changes: 17 additions & 11 deletions src/core/model_manager/model_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,15 @@ nlohmann::json ModelManager::CallComplete(const std::string &prompt, const Model
}
}

nlohmann::json ModelManager::OpenAICallEmbedding(const std::string &input, const ModelDetails &model_details) {
nlohmann::json ModelManager::OpenAICallEmbedding(const vector<string> &inputs, const ModelDetails &model_details) {
// Get API key from the environment variable
auto key = openai::OpenAI::get_openai_api_key();
openai::start(key);

// Create a JSON request payload with the provided parameters
nlohmann::json request_payload = {
{"model", model_details.model},
{"input", input},
{"input", inputs},
};

// Make a request to the OpenAI API
Expand All @@ -230,12 +230,15 @@ nlohmann::json ModelManager::OpenAICallEmbedding(const std::string &input, const
// Add error handling code here
}

auto embedding = completion["data"][0]["embedding"];
auto embeddings = nlohmann::json::array();
for (auto &item : completion["data"]) {
embeddings.push_back(item["embedding"]);
}

return embedding;
return embeddings;
}

nlohmann::json ModelManager::AzureCallEmbedding(const std::string &input, const ModelDetails &model_details) {
nlohmann::json ModelManager::AzureCallEmbedding(const vector<string> &inputs, const ModelDetails &model_details) {
// Get API key from the environment variable
auto api_key = AzureModelManager::get_azure_api_key();
auto resource_name = AzureModelManager::get_azure_resource_name();
Expand All @@ -247,7 +250,7 @@ nlohmann::json ModelManager::AzureCallEmbedding(const std::string &input, const
// Create a JSON request payload with the provided parameters
nlohmann::json request_payload = {
{"model", model_details.model},
{"input", input},
{"input", inputs},
};

// Make a request to the Azure API
Expand All @@ -261,12 +264,15 @@ nlohmann::json ModelManager::AzureCallEmbedding(const std::string &input, const
// Add error handling code here
}

auto embedding = completion["data"][0]["embedding"];
auto embeddings = nlohmann::json::array();
for (auto &item : completion["data"]) {
embeddings.push_back(item["embedding"]);
}

return embedding;
return embeddings;
}

nlohmann::json ModelManager::CallEmbedding(const std::string &input, const ModelDetails &model_details) {
nlohmann::json ModelManager::CallEmbedding(const vector<string> &inputs, const ModelDetails &model_details) {

// Check if the provided model is in the list of supported models
if (supported_embedding_models.find(model_details.model) == supported_embedding_models.end()) {
Expand All @@ -286,9 +292,9 @@ nlohmann::json ModelManager::CallEmbedding(const std::string &input, const Model

if (model_details.provider_name == "openai" || model_details.provider_name == "default" ||
model_details.provider_name.empty()) {
return OpenAICallEmbedding(input, model_details);
return OpenAICallEmbedding(inputs, model_details);
} else {
return AzureCallEmbedding(input, model_details);
return AzureCallEmbedding(inputs, model_details);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/include/flockmtl/core/model_manager/model_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct ModelManager {
static nlohmann::json CallComplete(const std::string &prompt, const ModelDetails &model_details,
const bool json_response = true);

static nlohmann::json CallEmbedding(const std::string &input, const ModelDetails &model_details);
static nlohmann::json CallEmbedding(const vector<string> &inputs, const ModelDetails &model_details);

private:
static std::pair<std::string, int32_t> GetQueriedModel(Connection &con, const std::string &model_name,
Expand All @@ -34,9 +34,9 @@ struct ModelManager {
static nlohmann::json AzureCallComplete(const std::string &prompt, const ModelDetails &model_details,
const bool json_response);

static nlohmann::json OpenAICallEmbedding(const std::string &input, const ModelDetails &model_details);
static nlohmann::json OpenAICallEmbedding(const vector<string> &inputs, const ModelDetails &model_details);

static nlohmann::json AzureCallEmbedding(const std::string &input, const ModelDetails &model_details);
static nlohmann::json AzureCallEmbedding(const vector<string> &inputs, const ModelDetails &model_details);
};

} // namespace core
Expand Down