Skip to content
Draft
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
7 changes: 7 additions & 0 deletions ydb/core/base/validation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#ifndef NDEBUG
#define Y_DEBUG_VERIFY Y_ABORT_UNLESS
#else
#define Y_DEBUG_VERIFY Y_VERIFY
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

#include "yql_http_header.h"

#include <yql/essentials/providers/common/proto/gateways_config.pb.h>

#include <yql/essentials/public/issue/yql_issue.h>

#include <contrib/libs/curl/include/curl/curl.h>

#include <library/cpp/containers/stack_vector/stack_vec.h>
#include <library/cpp/monlib/dynamic_counters/counters.h>
#include <library/cpp/retry/retry_policy.h>

#include <contrib/libs/curl/include/curl/curl.h>

#include <atomic>
#include <variant>
#include <functional>

namespace NYql {

class THttpGatewayConfig;

class IHTTPGateway {
public:
using TPtr = std::shared_ptr<IHTTPGateway>;
Expand Down Expand Up @@ -138,4 +138,4 @@ class IHTTPGateway {
const TString& awsSigV4 = {});
};

}
} // namespace NYql
6 changes: 6 additions & 0 deletions ydb/public/lib/ydb_cli/commands/ya.make
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ SRCS(
topic_write_scenario.cpp
topic_readwrite_scenario.cpp
ydb_admin.cpp
ydb_ai.cpp
ydb_benchmark.cpp
ydb_bridge.cpp
ydb_cluster.cpp
Expand Down Expand Up @@ -57,6 +58,10 @@ PEERDIR(
ydb/public/lib/ydb_cli/commands/sdk_core_access
ydb/public/lib/ydb_cli/commands/topic_workload
ydb/public/lib/ydb_cli/commands/transfer_workload
ydb/public/lib/ydb_cli/commands/ydb_ai
ydb/public/lib/ydb_cli/commands/ydb_ai/common
ydb/public/lib/ydb_cli/commands/ydb_ai/models
ydb/public/lib/ydb_cli/commands/ydb_ai/tools
ydb/public/lib/ydb_cli/commands/ydb_discovery
ydb/public/lib/ydb_cli/common
ydb/public/lib/ydb_cli/dump
Expand Down Expand Up @@ -92,5 +97,6 @@ RECURSE(
sdk_core_access
topic_workload
transfer_workload
ydb_ai
ydb_discovery
)
146 changes: 146 additions & 0 deletions ydb/public/lib/ydb_cli/commands/ydb_ai.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#include "ydb_ai.h"

#include <ydb/public/lib/ydb_cli/commands/ydb_ai/common/json_utils.h>
#include <ydb/public/lib/ydb_cli/commands/ydb_ai/line_reader.h>
#include <ydb/public/lib/ydb_cli/commands/ydb_ai/models/model_anthropic.h>
#include <ydb/public/lib/ydb_cli/commands/ydb_ai/models/model_openai.h>
#include <ydb/public/lib/ydb_cli/commands/ydb_ai/tools/exec_query_tool.h>
#include <ydb/public/lib/ydb_cli/commands/ydb_ai/tools/list_directory_tool.h>

#include <util/string/strip.h>
#include <util/system/env.h>

/*

FEATURES-TODO:

- Streamable model response printing
- Streamable results printing
- Adjusting errors, progress and response printing
- Approving before tool use
- Integration into common interactive mode
- Think about helps
- Think about robust
- Provide system promt
- Somehow render markdown

*/

namespace NYdb::NConsoleClient {

namespace {

void PrintExitMessage() {
Cout << "\nBye" << Endl;
}

} // anonymous namespace

TCommandAi::TCommandAi()
: TBase("ai", {}, "AI-TODO: KIKIMR-24198 -- description")
{}

void TCommandAi::Config(TConfig& config) {
TBase::Config(config);
config.Opts->SetTitle("AI-TODO: KIKIMR-24198 -- title");
config.Opts->SetFreeArgsNum(0);
}

int TCommandAi::Run(TConfig& config) {
Cout << "AI-TODO: KIKIMR-24198 -- welcome message" << Endl;

// AI-TODO: KIKIMR-24202 - robust file creation
NAi::TLineReader lineReader("ydb-ai> ", (TFsPath(HomeDir) / ".ydb-ai/history").GetPath());

// DeepSeek
// const auto model = NAi::CreateOpenAiModel({
// .BaseUrl = "https://api.eliza.yandex.net/raw/internal/deepseek", // AI-TODO: KIKIMR-24214 -- configure it
// .ModelId = "deepseek-0324", // AI-TODO: KIKIMR-24214 -- configure it
// .ApiKey = GetEnv("MODEL_TOKEN"), // AI-TODO: KIKIMR-24214 -- configure it
// }, config);

// Claude 3.5 haiku
// const auto model = NAi::CreateAnthropicModel({
// .BaseUrl = "https://api.eliza.yandex.net/anthropic", // AI-TODO: KIKIMR-24214 -- configure it
// .ModelId = "claude-3-5-haiku-20241022",
// .ApiKey = GetEnv("MODEL_TOKEN"), // AI-TODO: KIKIMR-24214 -- configure it
// }, config);

// YandexGPT Pro
const auto model = NAi::CreateOpenAiModel({
.BaseUrl = "https://api.eliza.yandex.net/internal/zeliboba/32b_aligned_quantized_202506/generative", // AI-TODO: KIKIMR-24214 -- configure it
.ApiKey = GetEnv("MODEL_TOKEN"), // AI-TODO: KIKIMR-24214 -- configure it
}, config);

std::unordered_map<TString, NAi::ITool::TPtr> tools = {
{"execute_query", NAi::CreateExecQueryTool(config)},
{"list_directory", NAi::CreateListDirectoryTool(config)},
};
for (const auto& [name, tool] : tools) {
model->RegisterTool(name, tool->GetParametersSchema(), tool->GetDescription());
}

// AI-TODO: there is strange highlighting of brackets
std::vector<NAi::IModel::TMessage> messages;
while (const auto& maybeLine = lineReader.ReadLine()) {
const auto& input = *maybeLine;
if (input.empty()) {
continue;
}

if (IsIn({"quit", "exit"}, to_lower(input))) {
PrintExitMessage();
return EXIT_SUCCESS;
}

// AI-TODO: limit interaction number
messages.emplace_back(NAi::IModel::TUserMessage{.Text = input});
while (!messages.empty()) {
// AI-TODO: progress visualization
const auto output = model->HandleMessages(messages);
messages.clear();

if (!output.Text && output.ToolCalls.empty()) {
// AI-TODO: proper answer format
Cout << "Model answer is empty(" << Endl;
break;
}

if (output.Text) {
// AI-TODO: proper answer format
Cout << "Model answer:\n" << output.Text << Endl;
}

for (const auto& toolCall : output.ToolCalls) {
const auto it = tools.find(toolCall.Name);
if (it == tools.end()) {
// AI-TODO: proper wrong tool handling
Cout << "Unsupported tool: " << toolCall.Name << Endl;
return EXIT_FAILURE;
}

// AI-TODO: proper tool call printing
Cout << "Calling tool: " << toolCall.Name << " with params:\n" << NAi::FormatJsonValue(toolCall.Parameters) << Endl;

// AI-TODO: add approving
const auto& result = it->second->Execute(toolCall.Parameters);
if (!result.IsSuccess) {
// AI-TODO: proper error handling
Cout << result.Text << Endl;
}
// AI-TODO: show progress

messages.push_back(NAi::IModel::TToolResponse{
.Text = result.Text,
.ToolCallId = toolCall.Id,
.IsSuccess = result.IsSuccess,
});
}
}
}

PrintExitMessage();
return EXIT_SUCCESS;
}

} // namespace NYdb::NConsoleClient
18 changes: 18 additions & 0 deletions ydb/public/lib/ydb_cli/commands/ydb_ai.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "ydb_command.h"

namespace NYdb::NConsoleClient {

class TCommandAi final : public TYdbCommand {
using TBase = TYdbCommand;

public:
TCommandAi();

void Config(TConfig& config) final;

int Run(TConfig& config) final;
};

} // namespace NYdb::NConsoleClient
Loading
Loading