Skip to content

Commit

Permalink
change the meta prompt and add the number of the expected responses t…
Browse files Browse the repository at this point in the history
…o it, and adapt the all functions to this change.
  • Loading branch information
dorbanianas committed Dec 25, 2024
1 parent f96ec77 commit 59d64ec
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/functions/aggregate/llm_first_or_last/implementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ nlohmann::json LlmFirstOrLast::Evaluate(nlohmann::json& tuples) {

do {
accumulated_tuples_tokens = Tiktoken::GetNumTokens(batch_tuples.dump());
accumulated_tuples_tokens +=
Tiktoken::GetNumTokens(PromptManager::ConstructNumTuples(static_cast<int>(tuples.size())));
accumulated_tuples_tokens +=
Tiktoken::GetNumTokens(PromptManager::ConstructInputTuplesHeader(tuples[start_index]));
while (accumulated_tuples_tokens < static_cast<unsigned int>(available_tokens) &&
Expand Down
2 changes: 2 additions & 0 deletions src/functions/aggregate/llm_reduce/implementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ nlohmann::json LlmReduce::ReduceLoop(const std::vector<nlohmann::json>& tuples,

do {
accumulated_tuples_tokens = Tiktoken::GetNumTokens(batch_tuples.dump());
accumulated_tuples_tokens +=
Tiktoken::GetNumTokens(PromptManager::ConstructNumTuples(static_cast<int>(tuples.size())));
accumulated_tuples_tokens +=
Tiktoken::GetNumTokens(PromptManager::ConstructInputTuplesHeader(tuples[start_index]));
while (accumulated_tuples_tokens < static_cast<unsigned int>(available_tokens) &&
Expand Down
2 changes: 2 additions & 0 deletions src/functions/aggregate/llm_rerank/implementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ nlohmann::json LlmRerank::SlidingWindow(nlohmann::json& tuples) {
next_tuples.clear();
batch_size = half_batch;
accumulated_rows_tokens = Tiktoken::GetNumTokens(window_tuples.dump());
accumulated_rows_tokens +=
Tiktoken::GetNumTokens(PromptManager::ConstructNumTuples(static_cast<int>(tuples.size())));
accumulated_rows_tokens +=
Tiktoken::GetNumTokens(PromptManager::ConstructInputTuplesHeader(tuples[start_index]));
while (available_tokens - accumulated_rows_tokens > 0 && start_index >= 0) {
Expand Down
2 changes: 2 additions & 0 deletions src/functions/scalar/scalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ nlohmann::json ScalarFunctionBase::BatchAndComplete(const std::vector<nlohmann::
int start_index = 0;

do {
accumulated_tuples_tokens +=
Tiktoken::GetNumTokens(PromptManager::ConstructNumTuples(static_cast<int>(tuples.size())));
accumulated_tuples_tokens +=
Tiktoken::GetNumTokens(PromptManager::ConstructInputTuplesHeader(tuples[start_index]));
while (accumulated_tuples_tokens < static_cast<unsigned int>(available_tokens) &&
Expand Down
10 changes: 6 additions & 4 deletions src/include/flockmtl/prompt_manager/prompt_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@ class PromptManager {

static PromptDetails CreatePromptDetails(const nlohmann::json& prompt_details_json);

static std::string ConstructMarkdownHeader(const nlohmann::json& tuple);
static std::string ConstructNumTuples(int num_tuples);

static std::string ConstructMarkdownSingleTuple(const nlohmann::json& tuple);
static std::string ConstructInputTuplesHeader(const nlohmann::json& tuple);

static std::string ConstructMarkdownArrayTuples(const nlohmann::json& tuples);
static std::string ConstructSingleInputTuple(const nlohmann::json& tuple);

static std::string ConstructInputTuples(const nlohmann::json& tuples);

template <typename FunctionType>
static std::string Render(const std::string& user_prompt, const nlohmann::json& tuples, FunctionType option) {
auto prompt = PromptManager::GetTemplate(option);
auto markdown_tuples = PromptManager::ConstructMarkdownArrayTuples(tuples);
auto markdown_tuples = PromptManager::ConstructInputTuples(tuples);
prompt = PromptManager::ReplaceSection(prompt, PromptSection::USER_PROMPT, user_prompt);
prompt = PromptManager::ReplaceSection(prompt, PromptSection::TUPLES, markdown_tuples);
return prompt;
Expand Down
25 changes: 13 additions & 12 deletions src/include/flockmtl/prompt_manager/repository.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ enum class AggregateFunctionType { REDUCE, REDUCE_JSON, FIRST, LAST, RERANK };
enum class ScalarFunctionType { COMPLETE_JSON, COMPLETE, FILTER };

constexpr auto META_PROMPT =
"You are a semantic analysis tool for DBMS. The tool will analyze each tuple in the provided data and respond to "
"user requests based on this context.\n\nUser Prompt:\n\n- {{USER_PROMPT}}\n\nTuples "
"You are FlockMTL a semantic analysis tool for DBMS. You will analyze each tuple in the provided data and respond "
"to "
"the user prompt.\n\nUser Prompt:\n\n- {{USER_PROMPT}}\n\nTuples "
"Table:\n\n{{TUPLES}}\n\nInstructions:\n\n{{INSTRUCTIONS}}\n\nExpected Response Format:\n\n{{RESPONSE_FORMAT}}";

class INSTRUCTIONS {
public:
static constexpr auto SCALAR_FUNCTION =
"- The response should be directly relevant to each tuple without additional formatting, purely answering the "
"prompt as if each tuple were a standalone entity.\n- Use clear, context-relevant language to generate a "
"user prompt as if each tuple were a standalone entity.\n- Use clear, context-relevant language to generate a "
"meaningful and concise answer for each tuple.";
static constexpr auto AGGREGATE_FUNCTION =
"- For each tuple in the provided data, evaluate the relevant attribute(s) based on the user prompt.\n- After "
Expand All @@ -36,18 +37,18 @@ class RESPONSE_FORMAT {
public:
// Scalar Functions
static constexpr auto COMPLETE_JSON =
"The system should interpret database tuples and provide a response to the user's prompt for each tuple in a "
"JSON format that contains the necessary columns for the answer.\n\nThe tool should respond in JSON format as "
"follows:\n\n```json\n{\t\"tuples\": [\n\t\t{<response 1>},\n\t\t{<response 2>},\n\t\t...\n\t\t{<response "
"n>}\n\t]\n}\n```";
"You should return the responses to the user's prompt for each tuple in a "
"JSON format that contains the necessary columns for the answer.\n\nThe tool should respond in JSON format:\n\n"
"```json\n{\"tuples\": [{<response>},{<response>}, ..., {<response>}]}\n```";
static constexpr auto COMPLETE =
"The system should interpret database tuples and provide a response to the user's prompt for each tuple in "
"plain text.\n\tThe tool should respond in JSON format as follows:\n\n```json\n{\"tuples\": [\"<response 1>\", "
"\"<response 2>\", ... , \"<response n>\"]}";
"You should return the responses to the user's prompt for each tuple in plain text. Ensure no tuple is "
"missed.\n"
"Respond in the following JSON format:\n\n"
"```json\n{\"tuples\": [\"<response>\", \"<response>\", ..., \"<response>\"]}\n```";
static constexpr auto FILTER =
"The system should interpret database tuples and provide a response to the user's prompt for each tuple in a "
"You should return the responses to the user's prompt for each tuple in a "
"BOOL format that would be true/false.\n\tThe tool should respond in JSON format as "
"follows:\n\n```json\n{\"tuples\": [<bool response 1>, <bool response 2>, ... , <bool response n>]}";
"follows:\n\n```json\n{\"tuples\": [<bool_response>, <bool_response>, ... , <bool_response>]}\n```";

// Aggregate Functions
static constexpr auto REDUCE =
Expand Down
5 changes: 5 additions & 0 deletions src/prompt_manager/prompt_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ std::string PromptManager::ConstructSingleInputTuple(const nlohmann::json& tuple
return tuple_str;
}

std::string PromptManager::ConstructNumTuples(const int num_tuples) {
return "- The Number of Tuples to Generate Responses for: " + std::to_string(num_tuples) + "\n\n";
}

std::string PromptManager::ConstructInputTuples(const nlohmann::json& tuples) {
auto tuples_str = std::string("");
tuples_str += PromptManager::ConstructNumTuples(static_cast<int>(tuples.size()));
tuples_str += PromptManager::ConstructInputTuplesHeader(tuples[0]);
for (const auto& tuple : tuples) {
tuples_str += PromptManager::ConstructSingleInputTuple(tuple);
Expand Down

0 comments on commit 59d64ec

Please sign in to comment.