|
| 1 | +#include "AIQueryProcessor.h" |
| 2 | + |
| 3 | +#include "chdb-internal.h" |
| 4 | +#include "PybindWrapper.h" |
| 5 | + |
| 6 | +#include <pybind11/pybind11.h> |
| 7 | +#include <pybind11/detail/non_limited_api.h> |
| 8 | + |
| 9 | +#if USE_CLIENT_AI |
| 10 | +#include <Client/AI/AIClientFactory.h> |
| 11 | +#include <Client/AI/AISQLGenerator.h> |
| 12 | +#endif |
| 13 | + |
| 14 | +#include <cstdlib> |
| 15 | +#include <iostream> |
| 16 | +#include <stdexcept> |
| 17 | + |
| 18 | +namespace py = pybind11; |
| 19 | + |
| 20 | +#if USE_CLIENT_AI |
| 21 | + |
| 22 | +AIQueryProcessor::AIQueryProcessor(chdb_connection * connection_, const DB::AIConfiguration & config_) |
| 23 | + : connection(connection_), ai_config(config_) |
| 24 | +{ |
| 25 | +} |
| 26 | + |
| 27 | +AIQueryProcessor::~AIQueryProcessor() = default; |
| 28 | + |
| 29 | +namespace |
| 30 | +{ |
| 31 | +void applyEnvFallback(DB::AIConfiguration & config) |
| 32 | +{ |
| 33 | + if (config.api_key.empty()) |
| 34 | + { |
| 35 | + if (const char * api_key = std::getenv("AI_API_KEY")) |
| 36 | + config.api_key = api_key; |
| 37 | + else if (const char * openai_key = std::getenv("OPENAI_API_KEY")) |
| 38 | + config.api_key = openai_key; |
| 39 | + else if (const char * anthropic_key = std::getenv("ANTHROPIC_API_KEY")) |
| 40 | + config.api_key = anthropic_key; |
| 41 | + } |
| 42 | +} |
| 43 | +} |
| 44 | + |
| 45 | +std::string AIQueryProcessor::executeQueryForAI(const std::string & query) |
| 46 | +{ |
| 47 | + chdb_result * result = chdb_query_n(*connection, query.data(), query.size(), "TSV", 3); |
| 48 | + const auto & error_msg = CHDB::chdb_result_error_string(result); |
| 49 | + if (!error_msg.empty()) |
| 50 | + { |
| 51 | + std::string msg_copy(error_msg); |
| 52 | + chdb_destroy_query_result(result); |
| 53 | + throw std::runtime_error(msg_copy); |
| 54 | + } |
| 55 | + |
| 56 | + std::string data(chdb_result_buffer(result), chdb_result_length(result)); |
| 57 | + chdb_destroy_query_result(result); |
| 58 | + return data; |
| 59 | +} |
| 60 | + |
| 61 | +void AIQueryProcessor::initializeGenerator() |
| 62 | +{ |
| 63 | + if (generator) |
| 64 | + return; |
| 65 | + |
| 66 | + // If a custom base URL is provided but provider is empty, default to OpenAI-compatible. |
| 67 | + if (ai_config.provider.empty() && !ai_config.base_url.empty()) |
| 68 | + ai_config.provider = "openai"; |
| 69 | + |
| 70 | + applyEnvFallback(ai_config); |
| 71 | + |
| 72 | + if (ai_config.api_key.empty()) |
| 73 | + throw std::runtime_error("AI SQL generator is not configured. Provide ai_api_key (or set OPENAI_API_KEY/ANTHROPIC_API_KEY) when creating the connection or session."); |
| 74 | + |
| 75 | + auto ai_result = DB::AIClientFactory::createClient(ai_config); |
| 76 | + |
| 77 | + if (ai_result.no_configuration_found || !ai_result.client.has_value()) |
| 78 | + throw std::runtime_error("AI SQL generator is not configured. Provide ai_api_key (or set OPENAI_API_KEY/ANTHROPIC_API_KEY) when creating the connection or session."); |
| 79 | + |
| 80 | + auto query_executor = [this](const std::string & query_text) { return executeQueryForAI(query_text); }; |
| 81 | + std::cerr << "[chdb] AI SQL generator using provider=" << (ai_config.provider.empty() ? "<auto>" : ai_config.provider) |
| 82 | + << ", model=" << (ai_config.model.empty() ? "<default>" : ai_config.model) |
| 83 | + << ", base_url=" << (ai_config.base_url.empty() ? "<default>" : ai_config.base_url) << std::endl; |
| 84 | + generator = std::make_unique<DB::AISQLGenerator>(ai_config, std::move(ai_result.client.value()), query_executor, std::cerr); |
| 85 | +} |
| 86 | + |
| 87 | +std::string AIQueryProcessor::generateSQLFromPrompt(const std::string & prompt) |
| 88 | +{ |
| 89 | + initializeGenerator(); |
| 90 | + |
| 91 | + if (!generator) |
| 92 | + throw std::runtime_error("AI SQL generator is not configured. Provide ai_api_key (or set OPENAI_API_KEY/ANTHROPIC_API_KEY) when creating the connection or session."); |
| 93 | + |
| 94 | + std::string sql; |
| 95 | + { |
| 96 | + py::gil_scoped_release release; |
| 97 | + sql = generator->generateSQL(prompt); |
| 98 | + } |
| 99 | + |
| 100 | + if (sql.empty()) |
| 101 | + throw std::runtime_error("AI did not return a SQL query."); |
| 102 | + |
| 103 | + return sql; |
| 104 | +} |
| 105 | + |
| 106 | +std::string AIQueryProcessor::generateSQL(const std::string & prompt) |
| 107 | +{ |
| 108 | + return generateSQLFromPrompt(prompt); |
| 109 | +} |
| 110 | + |
| 111 | +#else |
| 112 | + |
| 113 | +AIQueryProcessor::AIQueryProcessor(chdb_connection *, const DB::AIConfiguration &) : connection(nullptr) { } |
| 114 | +AIQueryProcessor::~AIQueryProcessor() = default; |
| 115 | +std::string AIQueryProcessor::executeQueryForAI(const std::string &) { return {}; } |
| 116 | +void AIQueryProcessor::initializeGenerator() { } |
| 117 | +std::string AIQueryProcessor::generateSQLFromPrompt(const std::string &) { return {}; } |
| 118 | +std::string AIQueryProcessor::generateSQL(const std::string &) { return {}; } |
| 119 | + |
| 120 | +#endif |
0 commit comments