embeddings.hpp 1019 B

12345678910111213141516171819202122232425
  1. #pragma once
  2. #include <string>
  3. #include <vector>
  4. #include <nlohmann/json.hpp>
  5. namespace svapi {
  6. std::vector<float> parseEmbeddingResponse(const nlohmann::json& body);
  7. // Split an OpenAI-compatible base URL into the origin (scheme://host[:port] —
  8. // httplib::Client ignores any path) and a path prefix to prepend to
  9. // "/v1/embeddings". e.g. "https://api.openai.com" -> {"https://api.openai.com", ""};
  10. // "https://openrouter.ai/api" -> {"https://openrouter.ai", "/api"}.
  11. struct EmbeddingEndpoint { std::string origin; std::string path; };
  12. EmbeddingEndpoint splitEmbeddingBase(const std::string& apiBase);
  13. class EmbeddingClient {
  14. public:
  15. EmbeddingClient(std::string apiBase, std::string apiKey,
  16. uint32_t connectTimeoutSec = 10, uint32_t readTimeoutSec = 60,
  17. uint32_t poolSize = 4);
  18. std::vector<float> embed(const std::string& model, const std::string& text);
  19. private:
  20. std::string apiBase_, apiKey_;
  21. uint32_t connectTimeoutSec_, readTimeoutSec_, poolSize_;
  22. };
  23. }