TT Client Library 1.0.0
C++ library for Tandem Tales clients.
Loading...
Searching...
No Matches
ExternalAPI.h
1#pragma once
2
3#include <string>
4#include <vector>
5#include <optional>
6#include <nlohmann/json.hpp>
7
8#ifndef EXTERNALAPI_H
9#define EXTERNALAPI_H
10
11namespace tt
12{
17 public:
19 static constexpr std::string_view INVALID_KEY = "API-INVALID-KEY";
20
22 static constexpr std::string_view EMBED_URL = "https://openrouter.ai/api/v1/embeddings";
24 static constexpr std::string_view EMBED_MODEL = "qwen/qwen3-embedding-8b";
26 static constexpr int EMBED_TOKENS_LIMIT = 1000000;
27
29 static constexpr std::string_view COMPLETE_URL = "https://openrouter.ai/api/v1/chat/completions";
31 static constexpr std::string_view COMPLETE_MODEL = "deepseek/deepseek-v4-flash-0731";
33 static constexpr int COMPLETE_IN_TOKENS_LIMIT = 1000000;
35 static constexpr int COMPLETE_OUT_TOKENS_LIMIT = 1000000;
37 static constexpr int COMPLETE_SEED = 0;
38
43 ExternalAPI(const std::string& key);
44
47
58 std::vector<float> embed(std::string text, int dim = 1024);
59
79 std::string complete(const std::vector<std::string>& messages);
80
97 std::string complete(const std::string& message);
98
107 void setReasoning(const std::optional<std::string>& value = std::nullopt);
108
116 void setMaxT(const std::optional<int>& value = std::nullopt);
117
126 void setTemp(const std::optional<float>& value = std::nullopt);
127
136 void setTopK(const std::optional<int>& value = std::nullopt);
137
146 void setTopP(const std::optional<float>& value = std::nullopt);
147
157 void setMinP(const std::optional<float>& value = std::nullopt);
158
163 void setKey(const std::string& k);
164
170 const std::optional<std::string>& getReasoning() const;
171
177 const std::optional<int>& getMaxT() const;
178
184 const std::optional<float>& getTemp() const;
185
191 const std::optional<int>& getTopK() const;
192
198 const std::optional<float>& getTopP() const;
199
205 const std::optional<float>& getMinP() const;
206
211 const std::string& getKey() const;
212
217 const int getRemainingInTokens() const;
218
223 const int getRemainingOutTokens() const;
224
229 const int getRemainingEmbedTokens() const;
230
235 const int getInTokensUsed() const;
236
241 const int getOutTokensUsed() const;
242
247 const int getEmbedTokensUsed() const;
248
249 private:
253 void checkKey() const;
254
261 void checkInLimit(int used, int limit) const;
262
269 void checkOutLimit(int used, int limit) const;
270
278 void checkNominal(const std::string& name, const std::string& value, const std::vector<std::string>& values) const;
279
288 void checkInt(const std::string& name, int value, int min, int max);
289
298 void checkFloat(const std::string& name, float value, float min, float max);
299
301 std::string key;
302
304 int embedTokens = 0;
306 int completeInTokens = 0;
308 int completeOutTokens = 0;
309
313 std::optional<std::string> reasoning = std::nullopt;
315 std::optional<int> maxt = std::nullopt;
317 std::optional<float> temp = std::nullopt;
319 std::optional<int> topk = std::nullopt;
322 std::optional<float> topp = std::nullopt;
326 std::optional<float> minp = std::nullopt;
327 };
328}
329
330
331#endif
const std::optional< float > & getTemp() const
Gives the current temperature value.
Definition ExternalAPI.cpp:191
const int getRemainingOutTokens() const
Gives the remaining amount of out tokens for completion.
Definition ExternalAPI.cpp:221
const std::optional< float > & getTopP() const
Gives the current topp value.
Definition ExternalAPI.cpp:201
ExternalAPI(const std::string &key)
Constructs an ExternalAPI object.
Definition ExternalAPI.cpp:10
static constexpr int COMPLETE_SEED
The seed used for large language model text completion requests.
Definition ExternalAPI.h:37
void setMaxT(const std::optional< int > &value=std::nullopt)
Sets the maximum number of tokens the model may use to generate its response.
Definition ExternalAPI.cpp:145
void setTopP(const std::optional< float > &value=std::nullopt)
When choosing a next token to add to the output, the model may only choose from tokens whose probabil...
Definition ExternalAPI.cpp:162
std::vector< float > embed(std::string text, int dim=1024)
Queries the external text embedding API and returns its response as a vector of floats.
Definition ExternalAPI.cpp:21
const std::optional< int > & getMaxT() const
Gives the current maxt value.
Definition ExternalAPI.cpp:186
static constexpr std::string_view COMPLETE_URL
The URL to which large language model text completion requests will be sent.
Definition ExternalAPI.h:29
void setTemp(const std::optional< float > &value=std::nullopt)
Sets the temperature, which influences the randomness of the response.
Definition ExternalAPI.cpp:150
const std::string & getKey() const
Gives the API key.
Definition ExternalAPI.cpp:211
static constexpr int COMPLETE_IN_TOKENS_LIMIT
The maximum number of input tokens that can be used for text completion requests.
Definition ExternalAPI.h:33
void setMinP(const std::optional< float > &value=std::nullopt)
When choosing a next token to add to the output, the model may only choose from tokens whose probabil...
Definition ExternalAPI.cpp:169
const int getRemainingEmbedTokens() const
Gives the remaining amount of tokens for embed.
Definition ExternalAPI.cpp:226
void setKey(const std::string &k)
Sets the API key.
Definition ExternalAPI.cpp:176
static constexpr std::string_view INVALID_KEY
Used for checking whether a key has been set.
Definition ExternalAPI.h:19
static constexpr std::string_view COMPLETE_MODEL
The large language model to be used for text completion requests.
Definition ExternalAPI.h:31
static constexpr std::string_view EMBED_MODEL
The model to be used for large language model text embedding requests.
Definition ExternalAPI.h:24
const int getEmbedTokensUsed() const
Gives the amount of tokens used for embed.
Definition ExternalAPI.cpp:241
void setTopK(const std::optional< int > &value=std::nullopt)
When choosing a next token to add to the output, the model may only choose from the best k tokens.
Definition ExternalAPI.cpp:157
const int getInTokensUsed() const
Gives the amount of in tokens used for completion.
Definition ExternalAPI.cpp:231
const std::optional< int > & getTopK() const
Gives the current topk value.
Definition ExternalAPI.cpp:196
void setReasoning(const std::optional< std::string > &value=std::nullopt)
Sets the reasoning level used by the large language model during text completion.
Definition ExternalAPI.cpp:138
std::string complete(const std::vector< std::string > &messages)
Queries the external text completion API and returns its response as a string.
Definition ExternalAPI.cpp:60
static constexpr std::string_view EMBED_URL
The URL to which large language model text embedding requests will be sent.
Definition ExternalAPI.h:22
const int getOutTokensUsed() const
Gives the amount of out tokens used for completion.
Definition ExternalAPI.cpp:236
const std::optional< float > & getMinP() const
Gives the current minp value.
Definition ExternalAPI.cpp:206
ExternalAPI()
Constructs an ExternalAPI object.
Definition ExternalAPI.cpp:16
static constexpr int COMPLETE_OUT_TOKENS_LIMIT
The maximum number of output tokens that can be used for text completion requests.
Definition ExternalAPI.h:35
static constexpr int EMBED_TOKENS_LIMIT
The maximum number of input tokens that can be used for text embedding requests.
Definition ExternalAPI.h:26
const std::optional< std::string > & getReasoning() const
Gives the current reasoning value.
Definition ExternalAPI.cpp:181
const int getRemainingInTokens() const
Gives the remaining amount of in tokens for completion.
Definition ExternalAPI.cpp:216
Tandem Tales is a platform that enables short, paired storytelling exercises between two agents over ...
Definition Client.h:22