mirror of
https://github.com/wolfpld/tracy
synced 2026-01-19 04:52:09 +00:00
Implement separate-channel non-streamed chat completion requests.
This commit is contained in:
@@ -248,6 +248,30 @@ int TracyLlmApi::Tokenize( const std::string& text, int modelIdx )
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
nlohmann::json TracyLlmApi::SendMessage( const nlohmann::json& chat, int modelIdx )
|
||||||
|
{
|
||||||
|
assert( m_curl );
|
||||||
|
|
||||||
|
nlohmann::json req = {
|
||||||
|
{ "model", m_models[modelIdx].name },
|
||||||
|
{ "messages", chat }
|
||||||
|
};
|
||||||
|
|
||||||
|
auto data = req.dump( -1, ' ', false, nlohmann::json::error_handler_t::replace );
|
||||||
|
std::string buf;
|
||||||
|
auto res = PostRequest( m_url + "/v1/chat/completions", data, buf, true );
|
||||||
|
if( res != 200 ) return {};
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return nlohmann::json::parse( buf );
|
||||||
|
}
|
||||||
|
catch( const std::exception& )
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int64_t TracyLlmApi::GetRequest( const std::string& url, std::string& response )
|
int64_t TracyLlmApi::GetRequest( const std::string& url, std::string& response )
|
||||||
{
|
{
|
||||||
assert( m_curl );
|
assert( m_curl );
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ public:
|
|||||||
bool ChatCompletion( const nlohmann::json& req, const std::function<bool(const nlohmann::json&)>& callback, int modelIdx );
|
bool ChatCompletion( const nlohmann::json& req, const std::function<bool(const nlohmann::json&)>& callback, int modelIdx );
|
||||||
bool Embeddings( const nlohmann::json& req, nlohmann::json& response, bool separateConnection = false );
|
bool Embeddings( const nlohmann::json& req, nlohmann::json& response, bool separateConnection = false );
|
||||||
[[nodiscard]] int Tokenize( const std::string& text, int modelIdx );
|
[[nodiscard]] int Tokenize( const std::string& text, int modelIdx );
|
||||||
|
[[nodiscard]] nlohmann::json SendMessage( const nlohmann::json& chat, int modelIdx );
|
||||||
|
|
||||||
[[nodiscard]] bool IsConnected() const { return m_curl != nullptr; }
|
[[nodiscard]] bool IsConnected() const { return m_curl != nullptr; }
|
||||||
[[nodiscard]] const std::vector<LlmModel>& GetModels() const { return m_models; }
|
[[nodiscard]] const std::vector<LlmModel>& GetModels() const { return m_models; }
|
||||||
|
|||||||
Reference in New Issue
Block a user