litellmts-core 2.0.0 → 2.1.1
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/dist/handlers/ollama.js +21 -13
- package/dist/models/registry.js +48 -4
- package/package.json +1 -1
package/dist/handlers/ollama.js
CHANGED
|
@@ -8,10 +8,10 @@ function toStreamingChunk(ollamaResponse, model, prompt) {
|
|
|
8
8
|
return {
|
|
9
9
|
model: model,
|
|
10
10
|
created: (0, getUnixTimestamp_1.getUnixTimestamp)(),
|
|
11
|
-
usage: (0, toUsage_1.toUsage)(prompt, ollamaResponse.
|
|
11
|
+
usage: (0, toUsage_1.toUsage)(prompt, ollamaResponse.message.content),
|
|
12
12
|
choices: [
|
|
13
13
|
{
|
|
14
|
-
delta: { content: ollamaResponse.
|
|
14
|
+
delta: { content: ollamaResponse.message.content, role: 'assistant' },
|
|
15
15
|
finish_reason: 'stop',
|
|
16
16
|
index: 0,
|
|
17
17
|
},
|
|
@@ -53,15 +53,19 @@ async function* iterateResponse(response, model, prompt) {
|
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
|
-
async function getOllamaResponse(model, prompt, baseUrl) {
|
|
57
|
-
|
|
56
|
+
async function getOllamaResponse(model, prompt, baseUrl, stream, apiKey) {
|
|
57
|
+
const headers = {
|
|
58
|
+
'Content-Type': 'application/json',
|
|
59
|
+
};
|
|
60
|
+
if (apiKey)
|
|
61
|
+
headers.Authorization = `Bearer ${apiKey}`;
|
|
62
|
+
return fetch(`${baseUrl}/api/chat`, {
|
|
58
63
|
method: 'POST',
|
|
59
|
-
headers
|
|
60
|
-
'Content-Type': 'application/json',
|
|
61
|
-
},
|
|
64
|
+
headers,
|
|
62
65
|
body: JSON.stringify({
|
|
63
66
|
model,
|
|
64
|
-
prompt,
|
|
67
|
+
messages: [{ role: 'user', content: prompt }],
|
|
68
|
+
stream,
|
|
65
69
|
}),
|
|
66
70
|
});
|
|
67
71
|
}
|
|
@@ -71,7 +75,7 @@ async function OllamaHandler(params) {
|
|
|
71
75
|
? params.model.slice(7)
|
|
72
76
|
: params.model;
|
|
73
77
|
const prompt = (0, combinePrompts_1.combinePrompts)(params.messages);
|
|
74
|
-
const res = await getOllamaResponse(model, prompt, baseUrl);
|
|
78
|
+
const res = await getOllamaResponse(model, prompt, baseUrl, !!params.stream, params.apiKey);
|
|
75
79
|
if (!res.ok) {
|
|
76
80
|
throw new Error(`Received an error with code ${res.status} from Ollama API.`);
|
|
77
81
|
}
|
|
@@ -88,12 +92,16 @@ async function OllamaHandler(params) {
|
|
|
88
92
|
return toResponse(message, model, prompt);
|
|
89
93
|
}
|
|
90
94
|
const registry_1 = require("../models/registry");
|
|
91
|
-
(0, registry_1.registerModelProvider)('ollama', async () => {
|
|
92
|
-
const
|
|
95
|
+
(0, registry_1.registerModelProvider)('ollama', async ({ baseUrl, apiKey } = {}) => {
|
|
96
|
+
const url = baseUrl ?? 'http://127.0.0.1:11434';
|
|
97
|
+
const headers = {};
|
|
98
|
+
if (apiKey)
|
|
99
|
+
headers.Authorization = `Bearer ${apiKey}`;
|
|
100
|
+
const res = await fetch(`${url.replace(/\/+$/, '')}/api/tags`, { headers });
|
|
93
101
|
if (!res.ok)
|
|
94
102
|
return [];
|
|
95
|
-
const { models } = await res.json();
|
|
96
|
-
return (models ?? []).map((m) => ({ id: m.name, provider: 'ollama' }));
|
|
103
|
+
const { models } = (await res.json());
|
|
104
|
+
return (models ?? []).map((m) => ({ id: m.name.replace(/-cloud$/, ''), provider: 'ollama' }));
|
|
97
105
|
});
|
|
98
106
|
const registry_2 = require("../registry");
|
|
99
107
|
(0, registry_2.registerCompletionHandler)('ollama/', OllamaHandler);
|
package/dist/models/registry.js
CHANGED
|
@@ -10,14 +10,58 @@ const CACHE_TTL = 5 * 60 * 1000;
|
|
|
10
10
|
function registerModelProvider(provider, fetcher) {
|
|
11
11
|
fetchers.set(provider, fetcher);
|
|
12
12
|
}
|
|
13
|
+
async function tryOllamaTags(baseUrl, provider, apiKey) {
|
|
14
|
+
try {
|
|
15
|
+
const headers = {};
|
|
16
|
+
if (apiKey)
|
|
17
|
+
headers.Authorization = `Bearer ${apiKey}`;
|
|
18
|
+
const res = await fetch(`${baseUrl}/api/tags`, { headers });
|
|
19
|
+
if (!res.ok)
|
|
20
|
+
return null;
|
|
21
|
+
const { models } = (await res.json());
|
|
22
|
+
if (!Array.isArray(models))
|
|
23
|
+
return null;
|
|
24
|
+
return models.map((m) => ({ id: m.name.replace(/-cloud$/, ''), provider }));
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
async function tryOpenAIModels(baseUrl, provider, apiKey) {
|
|
31
|
+
try {
|
|
32
|
+
const headers = {};
|
|
33
|
+
if (apiKey)
|
|
34
|
+
headers.Authorization = `Bearer ${apiKey}`;
|
|
35
|
+
const res = await fetch(`${baseUrl}/models`, { headers });
|
|
36
|
+
if (!res.ok)
|
|
37
|
+
return null;
|
|
38
|
+
const { data } = (await res.json());
|
|
39
|
+
if (!Array.isArray(data))
|
|
40
|
+
return null;
|
|
41
|
+
return data.map((m) => ({ id: m.id, provider }));
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
13
47
|
async function listModels(provider, opts) {
|
|
14
48
|
const cached = cache.get(provider);
|
|
15
49
|
if (cached && cached.expires > Date.now())
|
|
16
50
|
return cached.data;
|
|
17
|
-
|
|
18
|
-
if (
|
|
19
|
-
|
|
20
|
-
|
|
51
|
+
let data = null;
|
|
52
|
+
if (opts?.baseUrl) {
|
|
53
|
+
const baseUrl = opts.baseUrl.replace(/\/+$/, '');
|
|
54
|
+
data = await tryOllamaTags(baseUrl, provider, opts.apiKey);
|
|
55
|
+
if (!data || data.length === 0) {
|
|
56
|
+
data = await tryOpenAIModels(baseUrl, provider, opts.apiKey);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (!data || data.length === 0) {
|
|
60
|
+
const fetcher = fetchers.get(provider);
|
|
61
|
+
if (!fetcher)
|
|
62
|
+
throw new Error(`Provider '${provider}' not found.`);
|
|
63
|
+
data = await fetcher(opts);
|
|
64
|
+
}
|
|
21
65
|
cache.set(provider, { data, expires: Date.now() + CACHE_TTL });
|
|
22
66
|
return data;
|
|
23
67
|
}
|