Files
moloapp/src/vendors/retrieval/provider.h
T

61 lines
2.2 KiB
C++

#pragma once
// Shared plumbing of the provider classes: building a Gateway over one
// ApiClient per host, and small Json helpers so each provider file only holds
// what is specific to the provider.
#include <functional>
#include <memory>
#include <string>
#include <vector>
#include "clients/transport.h"
#include "llm/api_client.h"
#include "vendors/retrieval/gateway.h"
namespace molo::retrieval {
struct HostSpec {
std::string host; // name used in CallRow::host
std::string base_url; // https://..., no trailing slash
};
using TransportFactory = std::function<std::unique_ptr<clients::Transport>()>;
// One ApiClient per host. `factory` null = real HTTPS. `key` empty = look up
// base.key_env through llm::Config (empty there too = not configured).
std::shared_ptr<Gateway> make_gateway(const CallTable& table, const llm::ApiClientConfig& base,
const std::vector<HostSpec>& hosts, std::string key,
const TransportFactory& factory);
// A host name from the data plane of a database must be a bare https host.
void check_host(const std::string& vendor, const std::string& host);
class Provider {
public:
explicit Provider(std::shared_ptr<Gateway> gateway) : gw_(std::move(gateway)) {}
virtual ~Provider() = default;
Gateway& gateway() const { return *gw_; }
std::shared_ptr<Gateway> shared_gateway() const { return gw_; }
bool configured() const { return gw_->configured(); }
const CallTable& table() const { return gw_->table(); }
protected:
std::shared_ptr<Gateway> gw_;
};
// Json helpers
struct J {
static Json strings(const std::vector<std::string>& v);
static Json object() { return Json::object(); }
static std::string str(const Json& doc, const std::string& key, const std::string& fallback = "");
static double num(const Json& doc, const std::string& key, double fallback = 0);
static bool has_array(const Json& doc, const std::string& key);
static const Json* at(const Json& doc, const std::string& key);
static std::vector<float> floats(const Json& arr);
// The array at `key`, or an empty static array.
static const Json::Array& array(const Json& doc, const char* key);
};
} // namespace molo::retrieval