LlamaLib  v2.0.5
Cross-platform library for local LLMs
Loading...
Searching...
No Matches
LLM_client.h
Go to the documentation of this file.
1
6
7#pragma once
8
9#include <vector>
10#include <thread>
11#include <chrono>
12#include <iostream>
13#include <sstream>
14#include "LLM.h"
15#include "completion_processor.h"
16
17#if TARGET_OS_IOS || TARGET_OS_VISION
18 #include "ios_http_transport.h"
19#else
20 // increase max payload length to allow use of larger context size
21 #define CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH 1048576
22 // disable Nagle's algorithm
23 #define CPPHTTPLIB_TCP_NODELAY true
24 #include "httplib.h"
25#endif
26
31class UNDREAMAI_API LLMClient : public LLMLocal
32{
33public:
38
44 LLMClient(const std::string &url, const int port, const std::string &API_key = "", const int max_retries = 5);
45
47 ~LLMClient();
48
49
50 bool is_server_alive();
51
55 void set_SSL(const char *SSL_cert);
56
60 bool is_remote() const { return !url.empty() && port > -1; }
61
62 //=================================== LLM METHODS START ===================================//
63
67 std::string tokenize_json(const json &data) override;
68
73 std::string detokenize_json(const json &data) override;
74
79 std::string embeddings_json(const json &data) override;
80
86 std::string completion_json(const json &data, CharArrayFn callback = nullptr, bool callbackWithJSON = true) override;
87
92 std::string apply_template_json(const json &data) override;
93
98 std::string slot_json(const json &data) override;
99
102 void cancel(int id_slot) override;
103
106 int get_next_available_slot() override;
107
110 int get_slot_context_size() override;
111 //=================================== LLM METHODS END ===================================//
112
113private:
114 // Local LLM members
115 LLMProvider *llm = nullptr;
116#if TARGET_OS_IOS || TARGET_OS_VISION
117 IOSHttpTransport *transport = nullptr;
118#else
119 httplib::Client *client = nullptr;
120 httplib::SSLClient *sslClient = nullptr;
121#endif
122 bool use_ssl = false;
123
124 // Remote LLM members
125 std::string url = "";
126 int port = -1;
127 std::string API_key = "";
128 std::string SSL_cert = "";
129 int max_retries = 5;
130 std::vector<bool*> active_requests;
131
132
140 std::string post_request(const std::string &path, const json &payload, CharArrayFn callback = nullptr, bool callbackWithJSON = true);
141};
142
145
146extern "C"
147{
148 UNDREAMAI_API bool LLMClient_Is_Server_Alive(LLMClient *llm);
149
154 UNDREAMAI_API void LLMClient_Set_SSL(LLMClient *llm, const char *SSL_cert);
155
160 UNDREAMAI_API LLMClient *LLMClient_Construct(LLMProvider *llm);
161
167 UNDREAMAI_API LLMClient *LLMClient_Construct_Remote(const char *url, const int port, const char *API_key = "");
168}
169
170
Core LLM functionality interface and base classes.
Client for accessing LLM functionality locally or remotely.
Definition LLM_client.h:32
bool is_remote() const
Check if this is a remote client.
Definition LLM_client.h:60
Abstract class for local LLM operations with slot management.
Definition LLM.h:222
virtual std::string slot_json(const json &data)=0
Manage slots with HTTP response support.
virtual int get_slot_context_size()=0
Get slot context size.
virtual int get_next_available_slot()=0
Get an available processing slot.
virtual void cancel(int id_slot)=0
Cancel request.
Abstract class for LLM service providers.
Definition LLM.h:279
virtual std::string embeddings_json(const json &data)=0
Generate embeddings with HTTP response support.
virtual std::string apply_template_json(const json &data)=0
Apply a chat template to message data.
virtual std::string tokenize_json(const json &data)=0
Tokenize input (override)
virtual std::string completion_json(const json &data, CharArrayFn callback, bool callbackWithJSON)=0
Generate text completion.
virtual std::string detokenize_json(const json &data)=0
Convert tokens back to text.
LLMClient * LLMClient_Construct(LLMProvider *llm)
Construct local LLMClient (C API)
void LLMClient_Set_SSL(LLMClient *llm, const char *SSL_cert)
Set SSL certificate (C API)
LLMClient * LLMClient_Construct_Remote(const char *url, const int port, const char *API_key="")
Construct remote LLMClient (C API)