LlamaLib  v2.0.2
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 //=================================== LLM METHODS END ===================================//
108
109private:
110 // Local LLM members
111 LLMProvider *llm = nullptr;
112#if TARGET_OS_IOS || TARGET_OS_VISION
113 IOSHttpTransport *transport = nullptr;
114#else
115 httplib::Client *client = nullptr;
116 httplib::SSLClient *sslClient = nullptr;
117#endif
118 bool use_ssl = false;
119
120 // Remote LLM members
121 std::string url = "";
122 int port = -1;
123 std::string API_key = "";
124 std::string SSL_cert = "";
125 int max_retries = 5;
126 std::vector<bool*> active_requests;
127
128
136 std::string post_request(const std::string &path, const json &payload, CharArrayFn callback = nullptr, bool callbackWithJSON = true);
137};
138
141
142extern "C"
143{
144 UNDREAMAI_API bool LLMClient_Is_Server_Alive(LLMClient *llm);
145
150 UNDREAMAI_API void LLMClient_Set_SSL(LLMClient *llm, const char *SSL_cert);
151
156 UNDREAMAI_API LLMClient *LLMClient_Construct(LLMProvider *llm);
157
163 UNDREAMAI_API LLMClient *LLMClient_Construct_Remote(const char *url, const int port, const char *API_key = "");
164}
165
166
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_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:275
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)