litellmts-core 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -0
- package/README.md +266 -0
- package/dist/auth/anthropic.d.ts +7 -0
- package/dist/auth/anthropic.js +83 -0
- package/dist/auth/copilot.d.ts +9 -0
- package/dist/auth/copilot.js +138 -0
- package/dist/auth/index.d.ts +6 -0
- package/dist/auth/index.js +14 -0
- package/dist/auth/refresh.d.ts +2 -0
- package/dist/auth/refresh.js +54 -0
- package/dist/auth/store.d.ts +19 -0
- package/dist/auth/store.js +91 -0
- package/dist/bin/litellm.d.ts +2 -0
- package/dist/bin/litellm.js +45 -0
- package/dist/completion.d.ts +24 -0
- package/dist/completion.js +15 -0
- package/dist/embedding.d.ts +15 -0
- package/dist/embedding.js +26 -0
- package/dist/handlers/ai21.d.ts +2 -0
- package/dist/handlers/ai21.js +87 -0
- package/dist/handlers/anthropic.d.ts +2 -0
- package/dist/handlers/anthropic.js +85 -0
- package/dist/handlers/cohere.d.ts +2 -0
- package/dist/handlers/cohere.js +85 -0
- package/dist/handlers/copilot.d.ts +2 -0
- package/dist/handlers/copilot.js +136 -0
- package/dist/handlers/deepinfra.d.ts +2 -0
- package/dist/handlers/deepinfra.js +56 -0
- package/dist/handlers/gemini.d.ts +2 -0
- package/dist/handlers/gemini.js +102 -0
- package/dist/handlers/geminiEmbedding.d.ts +2 -0
- package/dist/handlers/geminiEmbedding.js +29 -0
- package/dist/handlers/getHandler.d.ts +11 -0
- package/dist/handlers/getHandler.js +24 -0
- package/dist/handlers/index.d.ts +16 -0
- package/dist/handlers/index.js +18 -0
- package/dist/handlers/mistral.d.ts +2 -0
- package/dist/handlers/mistral.js +56 -0
- package/dist/handlers/mistralEmbedding.d.ts +2 -0
- package/dist/handlers/mistralEmbedding.js +31 -0
- package/dist/handlers/ollama.d.ts +2 -0
- package/dist/handlers/ollama.js +89 -0
- package/dist/handlers/ollamaEmbedding.d.ts +2 -0
- package/dist/handlers/ollamaEmbedding.js +36 -0
- package/dist/handlers/openai.d.ts +2 -0
- package/dist/handlers/openai.js +76 -0
- package/dist/handlers/openaiEmbedding.d.ts +2 -0
- package/dist/handlers/openaiEmbedding.js +18 -0
- package/dist/handlers/openaiLike.d.ts +3 -0
- package/dist/handlers/openaiLike.js +22 -0
- package/dist/handlers/openaiLikeEmbedding.d.ts +3 -0
- package/dist/handlers/openaiLikeEmbedding.js +22 -0
- package/dist/handlers/replicate.d.ts +2 -0
- package/dist/handlers/replicate.js +98 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +19 -0
- package/dist/mappings/openaiLike.d.ts +6 -0
- package/dist/mappings/openaiLike.js +190 -0
- package/dist/registry.d.ts +15 -0
- package/dist/registry.js +30 -0
- package/dist/types.d.ts +104 -0
- package/dist/types.js +2 -0
- package/dist/utils/combinePrompts.d.ts +10 -0
- package/dist/utils/combinePrompts.js +19 -0
- package/dist/utils/encoders.d.ts +2 -0
- package/dist/utils/encoders.js +9 -0
- package/dist/utils/getUnixTimestamp.d.ts +1 -0
- package/dist/utils/getUnixTimestamp.js +6 -0
- package/dist/utils/sse.d.ts +12 -0
- package/dist/utils/sse.js +41 -0
- package/dist/utils/toUsage.d.ts +12 -0
- package/dist/utils/toUsage.js +35 -0
- package/package.json +63 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ReplicateHandler = ReplicateHandler;
|
|
7
|
+
const replicate_1 = __importDefault(require("replicate"));
|
|
8
|
+
const eventsource_1 = __importDefault(require("eventsource"));
|
|
9
|
+
const combinePrompts_1 = require("../utils/combinePrompts");
|
|
10
|
+
const toUsage_1 = require("../utils/toUsage");
|
|
11
|
+
const getUnixTimestamp_1 = require("../utils/getUnixTimestamp");
|
|
12
|
+
async function sleep(time) {
|
|
13
|
+
return new Promise((res) => {
|
|
14
|
+
setTimeout(() => {
|
|
15
|
+
res({});
|
|
16
|
+
}, time);
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
async function handleNonStreamingPrediction(prompt, prediction, replicate) {
|
|
20
|
+
const pred = await replicate.wait(prediction, {});
|
|
21
|
+
const output = pred.output.reduce((acc, curr) => (acc += curr), '');
|
|
22
|
+
return {
|
|
23
|
+
usage: (0, toUsage_1.toUsage)(prompt, output),
|
|
24
|
+
created: (0, getUnixTimestamp_1.getUnixTimestamp)(),
|
|
25
|
+
choices: [
|
|
26
|
+
{
|
|
27
|
+
message: {
|
|
28
|
+
role: 'assistant',
|
|
29
|
+
content: output,
|
|
30
|
+
},
|
|
31
|
+
finish_reason: 'stop',
|
|
32
|
+
index: 0,
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
async function* handleStreamingPrediction(prompt, prediction) {
|
|
38
|
+
if (!prediction?.urls?.stream) {
|
|
39
|
+
throw new Error('Prediction does not support streaming');
|
|
40
|
+
}
|
|
41
|
+
const source = new eventsource_1.default(prediction.urls.stream, {
|
|
42
|
+
withCredentials: true,
|
|
43
|
+
});
|
|
44
|
+
let results = [];
|
|
45
|
+
let done = false;
|
|
46
|
+
let resolve;
|
|
47
|
+
let promise = new Promise((r) => (resolve = r));
|
|
48
|
+
source.addEventListener('output', (e) => {
|
|
49
|
+
results.push(e.data);
|
|
50
|
+
resolve({});
|
|
51
|
+
promise = new Promise((r) => (resolve = r));
|
|
52
|
+
});
|
|
53
|
+
source.addEventListener('done', () => {
|
|
54
|
+
done = true;
|
|
55
|
+
source.close();
|
|
56
|
+
});
|
|
57
|
+
while (!done) {
|
|
58
|
+
await promise;
|
|
59
|
+
await sleep(500);
|
|
60
|
+
const combined = results.reduce((acc, curr) => (acc += curr), '');
|
|
61
|
+
yield {
|
|
62
|
+
created: (0, getUnixTimestamp_1.getUnixTimestamp)(),
|
|
63
|
+
usage: (0, toUsage_1.toUsage)(prompt, combined),
|
|
64
|
+
choices: [
|
|
65
|
+
{
|
|
66
|
+
delta: {
|
|
67
|
+
content: combined,
|
|
68
|
+
role: 'assistant',
|
|
69
|
+
},
|
|
70
|
+
index: 0,
|
|
71
|
+
finish_reason: 'stop',
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
};
|
|
75
|
+
results = [];
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
async function ReplicateHandler(params) {
|
|
79
|
+
const apiKey = params.apiKey ?? process.env.REPLICATE_API_KEY;
|
|
80
|
+
const replicate = new replicate_1.default({
|
|
81
|
+
auth: apiKey,
|
|
82
|
+
});
|
|
83
|
+
const model = params.model.split('replicate/')[1];
|
|
84
|
+
const version = model.split(':')[1];
|
|
85
|
+
const prompt = (0, combinePrompts_1.combinePrompts)(params.messages);
|
|
86
|
+
const prediction = await replicate.predictions.create({
|
|
87
|
+
version: version,
|
|
88
|
+
input: {
|
|
89
|
+
prompt,
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
if (params.stream) {
|
|
93
|
+
return handleStreamingPrediction(prompt, prediction);
|
|
94
|
+
}
|
|
95
|
+
return handleNonStreamingPrediction(prompt, prediction, replicate);
|
|
96
|
+
}
|
|
97
|
+
const registry_1 = require("../registry");
|
|
98
|
+
(0, registry_1.registerCompletionHandler)('replicate/', ReplicateHandler);
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @litellmts/core — Unified API client for 45+ LLM providers.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* import { completion } from '@litellmts/core';
|
|
6
|
+
* const res = await completion({ model: 'gpt-4', messages: [{ role: 'user', content: 'Hello' }] });
|
|
7
|
+
*/
|
|
8
|
+
export { completion } from './completion';
|
|
9
|
+
export { embedding } from './embedding';
|
|
10
|
+
export { login, loginAnthropic, getValidToken, getAnthropicKey } from './auth';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAnthropicKey = exports.getValidToken = exports.loginAnthropic = exports.login = exports.embedding = exports.completion = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* @litellmts/core — Unified API client for 45+ LLM providers.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* import { completion } from '@litellmts/core';
|
|
9
|
+
* const res = await completion({ model: 'gpt-4', messages: [{ role: 'user', content: 'Hello' }] });
|
|
10
|
+
*/
|
|
11
|
+
var completion_1 = require("./completion");
|
|
12
|
+
Object.defineProperty(exports, "completion", { enumerable: true, get: function () { return completion_1.completion; } });
|
|
13
|
+
var embedding_1 = require("./embedding");
|
|
14
|
+
Object.defineProperty(exports, "embedding", { enumerable: true, get: function () { return embedding_1.embedding; } });
|
|
15
|
+
var auth_1 = require("./auth");
|
|
16
|
+
Object.defineProperty(exports, "login", { enumerable: true, get: function () { return auth_1.login; } });
|
|
17
|
+
Object.defineProperty(exports, "loginAnthropic", { enumerable: true, get: function () { return auth_1.loginAnthropic; } });
|
|
18
|
+
Object.defineProperty(exports, "getValidToken", { enumerable: true, get: function () { return auth_1.getValidToken; } });
|
|
19
|
+
Object.defineProperty(exports, "getAnthropicKey", { enumerable: true, get: function () { return auth_1.getAnthropicKey; } });
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OPENAI_LIKE_MAPPINGS = void 0;
|
|
4
|
+
exports.OPENAI_LIKE_MAPPINGS = {
|
|
5
|
+
'groq/': {
|
|
6
|
+
name: 'Groq',
|
|
7
|
+
baseUrl: 'https://api.groq.com/openai/v1',
|
|
8
|
+
apiKeyEnv: 'GROQ_API_KEY',
|
|
9
|
+
},
|
|
10
|
+
'deepseek/': {
|
|
11
|
+
name: 'DeepSeek',
|
|
12
|
+
baseUrl: 'https://api.deepseek.com/beta',
|
|
13
|
+
apiKeyEnv: 'DEEPSEEK_API_KEY',
|
|
14
|
+
},
|
|
15
|
+
'perplexity/': {
|
|
16
|
+
name: 'Perplexity',
|
|
17
|
+
baseUrl: 'https://api.perplexity.ai',
|
|
18
|
+
apiKeyEnv: 'PERPLEXITY_API_KEY',
|
|
19
|
+
},
|
|
20
|
+
'xai/': {
|
|
21
|
+
name: 'X AI',
|
|
22
|
+
baseUrl: 'https://api.x.ai/v1',
|
|
23
|
+
apiKeyEnv: 'XAI_API_KEY',
|
|
24
|
+
},
|
|
25
|
+
'openrouter/': {
|
|
26
|
+
name: 'OpenRouter',
|
|
27
|
+
baseUrl: 'https://openrouter.ai/api/v1',
|
|
28
|
+
apiKeyEnv: 'OPENROUTER_API_KEY',
|
|
29
|
+
},
|
|
30
|
+
'together/': {
|
|
31
|
+
name: 'Together AI',
|
|
32
|
+
baseUrl: 'https://api.together.xyz/v1',
|
|
33
|
+
apiKeyEnv: 'TOGETHER_API_KEY',
|
|
34
|
+
},
|
|
35
|
+
'fireworks/': {
|
|
36
|
+
name: 'Fireworks AI',
|
|
37
|
+
baseUrl: 'https://api.fireworks.ai/inference/v1',
|
|
38
|
+
apiKeyEnv: 'FIREWORKS_API_KEY',
|
|
39
|
+
},
|
|
40
|
+
'cerebras/': {
|
|
41
|
+
name: 'Cerebras',
|
|
42
|
+
baseUrl: 'https://api.cerebras.ai/v1',
|
|
43
|
+
apiKeyEnv: 'CEREBRAS_API_KEY',
|
|
44
|
+
},
|
|
45
|
+
'sambanova/': {
|
|
46
|
+
name: 'SambaNova',
|
|
47
|
+
baseUrl: 'https://api.sambanova.ai/v1',
|
|
48
|
+
apiKeyEnv: 'SAMBANOVA_API_KEY',
|
|
49
|
+
},
|
|
50
|
+
'nebius/': {
|
|
51
|
+
name: 'Nebius AI',
|
|
52
|
+
baseUrl: 'https://api.studio.nebius.ai/v1',
|
|
53
|
+
apiKeyEnv: 'NEBIUS_API_KEY',
|
|
54
|
+
},
|
|
55
|
+
'hyperbolic/': {
|
|
56
|
+
name: 'Hyperbolic',
|
|
57
|
+
baseUrl: 'https://api.hyperbolic.xyz/v1',
|
|
58
|
+
apiKeyEnv: 'HYPERBOLIC_API_KEY',
|
|
59
|
+
},
|
|
60
|
+
'novita/': {
|
|
61
|
+
name: 'Novita AI',
|
|
62
|
+
baseUrl: 'https://api.novita.ai/v3/openai',
|
|
63
|
+
apiKeyEnv: 'NOVITA_API_KEY',
|
|
64
|
+
},
|
|
65
|
+
'github/': {
|
|
66
|
+
name: 'GitHub Models',
|
|
67
|
+
baseUrl: 'https://models.inference.ai.azure.com',
|
|
68
|
+
apiKeyEnv: 'GITHUB_TOKEN',
|
|
69
|
+
},
|
|
70
|
+
'anyscale/': {
|
|
71
|
+
name: 'Anyscale',
|
|
72
|
+
baseUrl: 'https://api.endpoints.anyscale.com/v1',
|
|
73
|
+
apiKeyEnv: 'ANYSCALE_API_KEY',
|
|
74
|
+
},
|
|
75
|
+
'nvidia_nim/': {
|
|
76
|
+
name: 'NVIDIA NIM',
|
|
77
|
+
baseUrl: 'https://integrate.api.nvidia.com/v1',
|
|
78
|
+
apiKeyEnv: 'NVIDIA_API_KEY',
|
|
79
|
+
},
|
|
80
|
+
'ai21/': {
|
|
81
|
+
name: 'AI21 Labs',
|
|
82
|
+
baseUrl: 'https://api.ai21.com/studio/v1',
|
|
83
|
+
apiKeyEnv: 'AI21_API_KEY',
|
|
84
|
+
},
|
|
85
|
+
'codestral/': {
|
|
86
|
+
name: 'Codestral',
|
|
87
|
+
baseUrl: 'https://codestral.mistral.ai/v1',
|
|
88
|
+
apiKeyEnv: 'CODESTRAL_API_KEY',
|
|
89
|
+
},
|
|
90
|
+
'moonshot/': {
|
|
91
|
+
name: 'Moonshot',
|
|
92
|
+
baseUrl: 'https://api.moonshot.ai/v1',
|
|
93
|
+
apiKeyEnv: 'MOONSHOT_API_KEY',
|
|
94
|
+
},
|
|
95
|
+
'dashscope/': {
|
|
96
|
+
name: 'DashScope',
|
|
97
|
+
baseUrl: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
|
|
98
|
+
apiKeyEnv: 'DASHSCOPE_API_KEY',
|
|
99
|
+
},
|
|
100
|
+
'meta_llama/': {
|
|
101
|
+
name: 'Meta Llama',
|
|
102
|
+
baseUrl: 'https://api.llama.com/compat/v1',
|
|
103
|
+
apiKeyEnv: 'LLAMA_API_KEY',
|
|
104
|
+
},
|
|
105
|
+
'featherless/': {
|
|
106
|
+
name: 'Featherless AI',
|
|
107
|
+
baseUrl: 'https://api.featherless.ai/v1',
|
|
108
|
+
apiKeyEnv: 'FEATHERLESS_API_KEY',
|
|
109
|
+
},
|
|
110
|
+
'nscale/': {
|
|
111
|
+
name: 'Nscale',
|
|
112
|
+
baseUrl: 'https://inference.api.nscale.com/v1',
|
|
113
|
+
apiKeyEnv: 'NSCALE_API_KEY',
|
|
114
|
+
},
|
|
115
|
+
'inception/': {
|
|
116
|
+
name: 'Inception Labs',
|
|
117
|
+
baseUrl: 'https://api.inceptionlabs.ai/v1',
|
|
118
|
+
apiKeyEnv: 'INCEPTION_API_KEY',
|
|
119
|
+
},
|
|
120
|
+
'morph/': {
|
|
121
|
+
name: 'Morph LLM',
|
|
122
|
+
baseUrl: 'https://api.morphllm.com/v1',
|
|
123
|
+
apiKeyEnv: 'MORPH_API_KEY',
|
|
124
|
+
},
|
|
125
|
+
'lambda/': {
|
|
126
|
+
name: 'Lambda AI',
|
|
127
|
+
baseUrl: 'https://api.lambda.ai/v1',
|
|
128
|
+
apiKeyEnv: 'LAMBDA_API_KEY',
|
|
129
|
+
},
|
|
130
|
+
'aiml/': {
|
|
131
|
+
name: 'AIML API',
|
|
132
|
+
baseUrl: 'https://api.aimlapi.com/v1',
|
|
133
|
+
apiKeyEnv: 'AIML_API_KEY',
|
|
134
|
+
},
|
|
135
|
+
'wandb/': {
|
|
136
|
+
name: 'Weights & Biases',
|
|
137
|
+
baseUrl: 'https://api.inference.wandb.ai/v1',
|
|
138
|
+
apiKeyEnv: 'WANDB_API_KEY',
|
|
139
|
+
},
|
|
140
|
+
'volcengine/': {
|
|
141
|
+
name: 'Volcengine',
|
|
142
|
+
baseUrl: 'https://ark.cn-beijing.volces.com/api/v3',
|
|
143
|
+
apiKeyEnv: 'VOLCENGINE_API_KEY',
|
|
144
|
+
},
|
|
145
|
+
'galadriel/': {
|
|
146
|
+
name: 'Galadriel',
|
|
147
|
+
baseUrl: 'https://api.galadriel.com/v1',
|
|
148
|
+
apiKeyEnv: 'GALADRIEL_API_KEY',
|
|
149
|
+
},
|
|
150
|
+
'empower/': {
|
|
151
|
+
name: 'Empower',
|
|
152
|
+
baseUrl: 'https://app.empower.dev/api/v1',
|
|
153
|
+
apiKeyEnv: 'EMPOWER_API_KEY',
|
|
154
|
+
},
|
|
155
|
+
'friendliai/': {
|
|
156
|
+
name: 'Friendli AI',
|
|
157
|
+
baseUrl: 'https://api.friendli.ai/serverless/v1',
|
|
158
|
+
apiKeyEnv: 'FRIENDLI_API_KEY',
|
|
159
|
+
},
|
|
160
|
+
'helicone/': {
|
|
161
|
+
name: 'Helicone',
|
|
162
|
+
baseUrl: 'https://ai-gateway.helicone.ai',
|
|
163
|
+
apiKeyEnv: 'HELICONE_API_KEY',
|
|
164
|
+
},
|
|
165
|
+
'vercel_ai/': {
|
|
166
|
+
name: 'Vercel AI Gateway',
|
|
167
|
+
baseUrl: 'https://ai-gateway.vercel.sh/v1',
|
|
168
|
+
apiKeyEnv: 'VERCEL_AI_GATEWAY_API_KEY',
|
|
169
|
+
},
|
|
170
|
+
'clarifai/': {
|
|
171
|
+
name: 'Clarifai',
|
|
172
|
+
baseUrl: 'https://api.clarifai.com/v2/ext/openai/v1',
|
|
173
|
+
apiKeyEnv: 'CLARIFAI_API_KEY',
|
|
174
|
+
},
|
|
175
|
+
'baseten/': {
|
|
176
|
+
name: 'Baseten',
|
|
177
|
+
baseUrl: 'https://inference.baseten.co/v1',
|
|
178
|
+
apiKeyEnv: 'BASETEN_API_KEY',
|
|
179
|
+
},
|
|
180
|
+
'publicai/': {
|
|
181
|
+
name: 'PublicAI',
|
|
182
|
+
baseUrl: 'https://api.publicai.co/v1',
|
|
183
|
+
apiKeyEnv: 'PUBLICAI_API_KEY',
|
|
184
|
+
},
|
|
185
|
+
'venice/': {
|
|
186
|
+
name: 'Venice AI',
|
|
187
|
+
baseUrl: 'https://api.venice.ai/api/v1',
|
|
188
|
+
apiKeyEnv: 'VENICE_API_KEY',
|
|
189
|
+
},
|
|
190
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { EmbeddingHandler, Handler } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Register a completion handler for a model prefix.
|
|
4
|
+
* Handlers self-register when their module is imported.
|
|
5
|
+
*/
|
|
6
|
+
export declare function registerCompletionHandler(prefix: string, handler: Handler): void;
|
|
7
|
+
/**
|
|
8
|
+
* Register an embedding handler for a model prefix.
|
|
9
|
+
* Handlers self-register when their module is imported.
|
|
10
|
+
*/
|
|
11
|
+
export declare function registerEmbeddingHandler(prefix: string, handler: EmbeddingHandler): void;
|
|
12
|
+
/** @internal Get the completion handler registry. */
|
|
13
|
+
export declare function getCompletionHandlers(): Record<string, Handler>;
|
|
14
|
+
/** @internal Get the embedding handler registry. */
|
|
15
|
+
export declare function getEmbeddingHandlers(): Record<string, EmbeddingHandler>;
|
package/dist/registry.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.registerCompletionHandler = registerCompletionHandler;
|
|
4
|
+
exports.registerEmbeddingHandler = registerEmbeddingHandler;
|
|
5
|
+
exports.getCompletionHandlers = getCompletionHandlers;
|
|
6
|
+
exports.getEmbeddingHandlers = getEmbeddingHandlers;
|
|
7
|
+
const completionHandlers = {};
|
|
8
|
+
const embeddingHandlers = {};
|
|
9
|
+
/**
|
|
10
|
+
* Register a completion handler for a model prefix.
|
|
11
|
+
* Handlers self-register when their module is imported.
|
|
12
|
+
*/
|
|
13
|
+
function registerCompletionHandler(prefix, handler) {
|
|
14
|
+
completionHandlers[prefix] = handler;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Register an embedding handler for a model prefix.
|
|
18
|
+
* Handlers self-register when their module is imported.
|
|
19
|
+
*/
|
|
20
|
+
function registerEmbeddingHandler(prefix, handler) {
|
|
21
|
+
embeddingHandlers[prefix] = handler;
|
|
22
|
+
}
|
|
23
|
+
/** @internal Get the completion handler registry. */
|
|
24
|
+
function getCompletionHandlers() {
|
|
25
|
+
return completionHandlers;
|
|
26
|
+
}
|
|
27
|
+
/** @internal Get the embedding handler registry. */
|
|
28
|
+
function getEmbeddingHandlers() {
|
|
29
|
+
return embeddingHandlers;
|
|
30
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
export type Role = 'system' | 'user' | 'assistant' | 'function' | 'tool';
|
|
2
|
+
/** A single message in a chat conversation. */
|
|
3
|
+
export interface Message {
|
|
4
|
+
role: Role;
|
|
5
|
+
content: string | null;
|
|
6
|
+
name?: string;
|
|
7
|
+
tool_call_id?: string;
|
|
8
|
+
tool_calls?: {
|
|
9
|
+
id: string;
|
|
10
|
+
type: 'function';
|
|
11
|
+
function: {
|
|
12
|
+
name: string;
|
|
13
|
+
arguments: string;
|
|
14
|
+
};
|
|
15
|
+
}[];
|
|
16
|
+
}
|
|
17
|
+
export type FinishReason = 'stop' | 'length' | 'function_call' | 'content_filter' | 'tool_calls';
|
|
18
|
+
export interface ConsistentResponseChoice {
|
|
19
|
+
finish_reason: FinishReason | null;
|
|
20
|
+
index: number;
|
|
21
|
+
message: {
|
|
22
|
+
role: string | null | undefined;
|
|
23
|
+
content: string | null | undefined;
|
|
24
|
+
function_call?: {
|
|
25
|
+
arguments: string;
|
|
26
|
+
name: string;
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export interface ConsistentResponseStreamingChoice extends Omit<ConsistentResponseChoice, 'message'> {
|
|
31
|
+
delta: Omit<ConsistentResponseChoice['message'], 'function_call'> & {
|
|
32
|
+
function_call?: {
|
|
33
|
+
arguments?: string;
|
|
34
|
+
name?: string;
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export interface ConsistentResponseUsage {
|
|
39
|
+
prompt_tokens: number;
|
|
40
|
+
completion_tokens: number;
|
|
41
|
+
total_tokens: number;
|
|
42
|
+
}
|
|
43
|
+
/** Normalized completion response shared across all providers. */
|
|
44
|
+
export interface ConsistentResponse {
|
|
45
|
+
choices: ConsistentResponseChoice[];
|
|
46
|
+
model?: string;
|
|
47
|
+
created?: number;
|
|
48
|
+
usage?: ConsistentResponseUsage;
|
|
49
|
+
}
|
|
50
|
+
export type ResultNotStreaming = ConsistentResponse;
|
|
51
|
+
export interface StreamingChunk extends Omit<ConsistentResponse, 'choices'> {
|
|
52
|
+
choices: ConsistentResponseStreamingChoice[];
|
|
53
|
+
}
|
|
54
|
+
export type ResultStreaming = AsyncIterable<StreamingChunk>;
|
|
55
|
+
export type Result = ResultNotStreaming | ResultStreaming;
|
|
56
|
+
/** Parameters accepted by all completion handlers. */
|
|
57
|
+
export interface HandlerParamsBase {
|
|
58
|
+
model: string;
|
|
59
|
+
messages: Message[];
|
|
60
|
+
stream?: boolean | null;
|
|
61
|
+
baseUrl?: string;
|
|
62
|
+
temperature?: number | null;
|
|
63
|
+
top_p?: number | null;
|
|
64
|
+
stop?: string | null | string[];
|
|
65
|
+
presence_penalty?: number | null;
|
|
66
|
+
n?: number | null;
|
|
67
|
+
max_tokens?: number | null;
|
|
68
|
+
apiKey?: string;
|
|
69
|
+
functions?: {
|
|
70
|
+
name: string;
|
|
71
|
+
description?: string;
|
|
72
|
+
parameters?: Record<string, unknown>;
|
|
73
|
+
}[];
|
|
74
|
+
function_call?: 'none' | 'auto' | {
|
|
75
|
+
name: string;
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
export interface HandlerParamsStreaming extends HandlerParamsBase {
|
|
79
|
+
stream?: true;
|
|
80
|
+
}
|
|
81
|
+
export interface HandlerParamsNotStreaming extends HandlerParamsBase {
|
|
82
|
+
stream?: false;
|
|
83
|
+
}
|
|
84
|
+
export type HandlerParams = HandlerParamsStreaming | HandlerParamsNotStreaming;
|
|
85
|
+
/** Parameters for embedding requests. */
|
|
86
|
+
export interface EmbeddingParams {
|
|
87
|
+
input: string | string[];
|
|
88
|
+
model: string;
|
|
89
|
+
apiKey?: string;
|
|
90
|
+
baseUrl?: string;
|
|
91
|
+
}
|
|
92
|
+
/** A single embedding vector and its index. */
|
|
93
|
+
export interface EmbeddingObject {
|
|
94
|
+
embedding: number[];
|
|
95
|
+
index: number;
|
|
96
|
+
}
|
|
97
|
+
/** Normalized embedding response shared across all providers. */
|
|
98
|
+
export interface EmbeddingResponse {
|
|
99
|
+
usage?: Pick<import('openai/resources').CompletionUsage, 'prompt_tokens' | 'total_tokens'>;
|
|
100
|
+
model: string;
|
|
101
|
+
data: EmbeddingObject[];
|
|
102
|
+
}
|
|
103
|
+
export type Handler = (params: HandlerParams) => Promise<Result>;
|
|
104
|
+
export type EmbeddingHandler = (params: EmbeddingParams) => Promise<EmbeddingResponse>;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Message } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Combine an array of chat messages into a single prompt string with role labels.
|
|
4
|
+
*
|
|
5
|
+
* Used by providers with legacy string-based prompt APIs (Cohere, AI21, Replicate, Ollama).
|
|
6
|
+
*
|
|
7
|
+
* @param messages - The messages to combine
|
|
8
|
+
* @returns A single prompt string with format `"Human: ...\n\nAssistant: ..."`
|
|
9
|
+
*/
|
|
10
|
+
export declare function combinePrompts(messages: Message[]): string;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.combinePrompts = combinePrompts;
|
|
4
|
+
/**
|
|
5
|
+
* Combine an array of chat messages into a single prompt string with role labels.
|
|
6
|
+
*
|
|
7
|
+
* Used by providers with legacy string-based prompt APIs (Cohere, AI21, Replicate, Ollama).
|
|
8
|
+
*
|
|
9
|
+
* @param messages - The messages to combine
|
|
10
|
+
* @returns A single prompt string with format `"Human: ...\n\nAssistant: ..."`
|
|
11
|
+
*/
|
|
12
|
+
function combinePrompts(messages) {
|
|
13
|
+
return messages
|
|
14
|
+
.map((msg) => {
|
|
15
|
+
const roleLabel = msg.role === 'assistant' ? 'Assistant' : msg.role === 'system' ? 'System' : 'Human';
|
|
16
|
+
return `${roleLabel}: ${msg.content ?? ''}`;
|
|
17
|
+
})
|
|
18
|
+
.join('\n\n');
|
|
19
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getEncoder = getEncoder;
|
|
4
|
+
const js_tiktoken_1 = require("js-tiktoken");
|
|
5
|
+
let _encoder = null;
|
|
6
|
+
function getEncoder() {
|
|
7
|
+
_encoder ??= (0, js_tiktoken_1.getEncoding)('cl100k_base');
|
|
8
|
+
return _encoder;
|
|
9
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getUnixTimestamp(): number;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse an SSE (Server-Sent Events) response stream into typed chunks.
|
|
3
|
+
*
|
|
4
|
+
* Reads `response.body` as a ReadableStream, splits on `\n`, filters for
|
|
5
|
+
* `data:` lines, parses each payload, and yields typed results.
|
|
6
|
+
* Stops iteration when a `doneToken` payload is encountered (default `[DONE]`).
|
|
7
|
+
*
|
|
8
|
+
* @param response - The HTTP response with an SSE body stream
|
|
9
|
+
* @param parseChunk - A function to parse each `data:` payload string into type T
|
|
10
|
+
* @param doneToken - The payload value that signals end-of-stream
|
|
11
|
+
*/
|
|
12
|
+
export declare function iterateSSEStream<T>(response: Response, parseChunk: (data: string) => T, doneToken?: string): AsyncIterable<T>;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.iterateSSEStream = iterateSSEStream;
|
|
4
|
+
/**
|
|
5
|
+
* Parse an SSE (Server-Sent Events) response stream into typed chunks.
|
|
6
|
+
*
|
|
7
|
+
* Reads `response.body` as a ReadableStream, splits on `\n`, filters for
|
|
8
|
+
* `data:` lines, parses each payload, and yields typed results.
|
|
9
|
+
* Stops iteration when a `doneToken` payload is encountered (default `[DONE]`).
|
|
10
|
+
*
|
|
11
|
+
* @param response - The HTTP response with an SSE body stream
|
|
12
|
+
* @param parseChunk - A function to parse each `data:` payload string into type T
|
|
13
|
+
* @param doneToken - The payload value that signals end-of-stream
|
|
14
|
+
*/
|
|
15
|
+
async function* iterateSSEStream(response, parseChunk, doneToken = '[DONE]') {
|
|
16
|
+
const reader = response.body?.getReader();
|
|
17
|
+
if (!reader)
|
|
18
|
+
throw new Error('Response body is not readable');
|
|
19
|
+
let done = false;
|
|
20
|
+
let buffer = '';
|
|
21
|
+
while (!done) {
|
|
22
|
+
const next = await reader.read();
|
|
23
|
+
if (next.value) {
|
|
24
|
+
buffer += new TextDecoder().decode(next.value, { stream: true });
|
|
25
|
+
const lines = buffer.split('\n');
|
|
26
|
+
buffer = lines.pop() ?? '';
|
|
27
|
+
for (const line of lines) {
|
|
28
|
+
const trimmed = line.trim();
|
|
29
|
+
if (!trimmed?.startsWith('data: '))
|
|
30
|
+
continue;
|
|
31
|
+
const payload = trimmed.slice(6);
|
|
32
|
+
if (payload === doneToken) {
|
|
33
|
+
done = true;
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
yield parseChunk(payload);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
done ||= next.done;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { EmbeddingResponse } from '../types';
|
|
2
|
+
import { ConsistentResponseUsage } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* Count the number of tokens in a text string using cl100k_base encoding.
|
|
5
|
+
*/
|
|
6
|
+
export declare function countTokens(text: string): number;
|
|
7
|
+
/**
|
|
8
|
+
* Build a usage object from prompt and completion text.
|
|
9
|
+
* Returns `undefined` when there is no completion text.
|
|
10
|
+
*/
|
|
11
|
+
export declare function toUsage(prompt: string, completion: string | undefined): ConsistentResponseUsage | undefined;
|
|
12
|
+
export declare function toEmbeddingUsage(prompt: string): EmbeddingResponse['usage'];
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.countTokens = countTokens;
|
|
4
|
+
exports.toUsage = toUsage;
|
|
5
|
+
exports.toEmbeddingUsage = toEmbeddingUsage;
|
|
6
|
+
const encoders_1 = require("./encoders");
|
|
7
|
+
/**
|
|
8
|
+
* Count the number of tokens in a text string using cl100k_base encoding.
|
|
9
|
+
*/
|
|
10
|
+
function countTokens(text) {
|
|
11
|
+
return (0, encoders_1.getEncoder)().encode(text).length;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Build a usage object from prompt and completion text.
|
|
15
|
+
* Returns `undefined` when there is no completion text.
|
|
16
|
+
*/
|
|
17
|
+
function toUsage(prompt, completion) {
|
|
18
|
+
if (!completion) {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
const promptTokens = countTokens(prompt);
|
|
22
|
+
const completionTokens = countTokens(completion);
|
|
23
|
+
return {
|
|
24
|
+
prompt_tokens: promptTokens,
|
|
25
|
+
completion_tokens: completionTokens,
|
|
26
|
+
total_tokens: promptTokens + completionTokens,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function toEmbeddingUsage(prompt) {
|
|
30
|
+
const promptTokens = (0, encoders_1.getEncoder)().encode(prompt);
|
|
31
|
+
return {
|
|
32
|
+
prompt_tokens: promptTokens.length,
|
|
33
|
+
total_tokens: promptTokens.length,
|
|
34
|
+
};
|
|
35
|
+
}
|