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,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getProviderCredentials = getProviderCredentials;
|
|
4
|
+
exports.setProviderCredentials = setProviderCredentials;
|
|
5
|
+
exports.getCopilotCredentials = getCopilotCredentials;
|
|
6
|
+
exports.setCopilotCredentials = setCopilotCredentials;
|
|
7
|
+
exports.getAnthropicCredentials = getAnthropicCredentials;
|
|
8
|
+
exports.setAnthropicCredentials = setAnthropicCredentials;
|
|
9
|
+
exports.clearCredentials = clearCredentials;
|
|
10
|
+
const promises_1 = require("node:fs/promises");
|
|
11
|
+
const node_path_1 = require("node:path");
|
|
12
|
+
const node_os_1 = require("node:os");
|
|
13
|
+
const STORE_DIR = (0, node_path_1.join)((0, node_os_1.homedir)(), '.litellm');
|
|
14
|
+
const STORE_PATH = (0, node_path_1.join)(STORE_DIR, 'auth.json');
|
|
15
|
+
function isNotFound(err) {
|
|
16
|
+
return err?.code === 'ENOENT';
|
|
17
|
+
}
|
|
18
|
+
async function ensureDir() {
|
|
19
|
+
await (0, promises_1.mkdir)(STORE_DIR, { recursive: true });
|
|
20
|
+
}
|
|
21
|
+
async function readStore() {
|
|
22
|
+
try {
|
|
23
|
+
const data = await (0, promises_1.readFile)(STORE_PATH, 'utf-8');
|
|
24
|
+
return JSON.parse(data);
|
|
25
|
+
}
|
|
26
|
+
catch (err) {
|
|
27
|
+
if (isNotFound(err))
|
|
28
|
+
return {};
|
|
29
|
+
throw err;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async function getProviderCredentials(provider) {
|
|
33
|
+
try {
|
|
34
|
+
const store = await readStore();
|
|
35
|
+
const raw = store[provider];
|
|
36
|
+
if (!raw)
|
|
37
|
+
return null;
|
|
38
|
+
return raw;
|
|
39
|
+
}
|
|
40
|
+
catch (err) {
|
|
41
|
+
if (isNotFound(err))
|
|
42
|
+
return null;
|
|
43
|
+
throw err;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
async function setProviderCredentials(provider, creds) {
|
|
47
|
+
await ensureDir();
|
|
48
|
+
const store = await readStore();
|
|
49
|
+
store[provider] = creds;
|
|
50
|
+
await (0, promises_1.writeFile)(STORE_PATH, JSON.stringify(store, null, 2), 'utf-8');
|
|
51
|
+
}
|
|
52
|
+
// Backward-compat old single-provider format
|
|
53
|
+
async function getCopilotCredentials() {
|
|
54
|
+
const legacy = await getProviderCredentials('github-copilot');
|
|
55
|
+
if (legacy)
|
|
56
|
+
return legacy;
|
|
57
|
+
try {
|
|
58
|
+
const store = await readStore();
|
|
59
|
+
if (store.copilotToken && store.githubToken) {
|
|
60
|
+
const migrated = {
|
|
61
|
+
githubToken: store.githubToken,
|
|
62
|
+
copilotToken: store.copilotToken,
|
|
63
|
+
expiresAt: store.expiresAt,
|
|
64
|
+
enterpriseUrl: store.enterpriseUrl,
|
|
65
|
+
};
|
|
66
|
+
await setProviderCredentials('github-copilot', migrated);
|
|
67
|
+
return migrated;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
// ignore
|
|
72
|
+
}
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
async function setCopilotCredentials(creds) {
|
|
76
|
+
await setProviderCredentials('github-copilot', creds);
|
|
77
|
+
}
|
|
78
|
+
async function getAnthropicCredentials() {
|
|
79
|
+
return getProviderCredentials('anthropic');
|
|
80
|
+
}
|
|
81
|
+
async function setAnthropicCredentials(creds) {
|
|
82
|
+
await setProviderCredentials('anthropic', creds);
|
|
83
|
+
}
|
|
84
|
+
async function clearCredentials() {
|
|
85
|
+
try {
|
|
86
|
+
await (0, promises_1.writeFile)(STORE_PATH, JSON.stringify({}), 'utf-8');
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
// ignore
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
/* eslint-disable no-console, @typescript-eslint/no-unsafe-return */
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
const fs_1 = require("fs");
|
|
6
|
+
const path_1 = require("path");
|
|
7
|
+
const copilot_1 = require("../auth/copilot");
|
|
8
|
+
const anthropic_1 = require("../auth/anthropic");
|
|
9
|
+
function findPackageJson(startDir) {
|
|
10
|
+
let dir = startDir;
|
|
11
|
+
for (let i = 0; i < 10; i++) {
|
|
12
|
+
try {
|
|
13
|
+
return JSON.parse((0, fs_1.readFileSync)((0, path_1.resolve)(dir, 'package.json'), 'utf8'));
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
dir = (0, path_1.resolve)(dir, '..');
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
throw new Error('Could not find package.json');
|
|
20
|
+
}
|
|
21
|
+
const pkg = findPackageJson(__dirname);
|
|
22
|
+
const [cmd, subcmd] = process.argv.slice(2);
|
|
23
|
+
if (cmd === 'login' && subcmd === 'copilot') {
|
|
24
|
+
(0, copilot_1.login)().catch((err) => {
|
|
25
|
+
console.error('\n❌ Error:', err.message);
|
|
26
|
+
process.exit(1);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
else if (cmd === 'login' && subcmd === 'anthropic') {
|
|
30
|
+
(0, anthropic_1.loginAnthropic)().catch((err) => {
|
|
31
|
+
console.error('\n❌ Error:', err.message);
|
|
32
|
+
process.exit(1);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
else if (cmd === '--version' || cmd === '-v') {
|
|
36
|
+
console.log(pkg.version);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
console.log(`litellmTS v${pkg.version}`);
|
|
40
|
+
console.log('');
|
|
41
|
+
console.log('Uso:');
|
|
42
|
+
console.log(' litellm login copilot Iniciar sesión en GitHub Copilot');
|
|
43
|
+
console.log(' litellm login anthropic Configurar API key de Anthropic');
|
|
44
|
+
console.log(' litellm --version Mostrar versión');
|
|
45
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Handler, HandlerParams, HandlerParamsNotStreaming, HandlerParamsStreaming, Result, ResultNotStreaming, ResultStreaming } from './types';
|
|
2
|
+
import './handlers';
|
|
3
|
+
export declare const MODEL_HANDLER_MAPPINGS: Record<string, Handler>;
|
|
4
|
+
/**
|
|
5
|
+
* Send a chat completion request to the provider that matches the model prefix.
|
|
6
|
+
*
|
|
7
|
+
* Supports 45+ providers including OpenAI, Anthropic, Cohere, Gemini, Groq, Together AI, and more.
|
|
8
|
+
* Model routing is automatic based on the model name prefix (e.g. `gpt-`, `claude-`, `gemini/`).
|
|
9
|
+
*
|
|
10
|
+
* @param params - The completion parameters including model, messages, and optional stream flag
|
|
11
|
+
* @returns The completion result (non-streaming) or an async iterable (streaming)
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* // Non-streaming
|
|
15
|
+
* const res = await completion({ model: 'gpt-4', messages: [{ role: 'user', content: 'Hello' }] });
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* // Streaming
|
|
19
|
+
* const stream = await completion({ model: 'claude-3', messages: [{ role: 'user', content: 'Hi' }], stream: true });
|
|
20
|
+
* for await (const chunk of stream) { process.stdout.write(chunk.choices[0].delta.content ?? ''); }
|
|
21
|
+
*/
|
|
22
|
+
export declare function completion(params: HandlerParamsNotStreaming): Promise<ResultNotStreaming>;
|
|
23
|
+
export declare function completion(params: HandlerParamsStreaming): Promise<ResultStreaming>;
|
|
24
|
+
export declare function completion(params: HandlerParams): Promise<Result>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MODEL_HANDLER_MAPPINGS = void 0;
|
|
4
|
+
exports.completion = completion;
|
|
5
|
+
const getHandler_1 = require("./handlers/getHandler");
|
|
6
|
+
const registry_1 = require("./registry");
|
|
7
|
+
require("./handlers");
|
|
8
|
+
exports.MODEL_HANDLER_MAPPINGS = (0, registry_1.getCompletionHandlers)();
|
|
9
|
+
async function completion(params) {
|
|
10
|
+
const handler = (0, getHandler_1.getHandler)(params.model, exports.MODEL_HANDLER_MAPPINGS);
|
|
11
|
+
if (!handler) {
|
|
12
|
+
throw new Error(`Model: ${params.model} not supported. Cannot find a handler.`);
|
|
13
|
+
}
|
|
14
|
+
return handler(params);
|
|
15
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { EmbeddingParams, EmbeddingResponse } from './types';
|
|
2
|
+
import './handlers';
|
|
3
|
+
/**
|
|
4
|
+
* Generate embeddings for the given input using the provider that matches the model prefix.
|
|
5
|
+
*
|
|
6
|
+
* Supports OpenAI, Mistral, Gemini, Ollama, and 38+ OpenAI-compatible embedding providers.
|
|
7
|
+
*
|
|
8
|
+
* @param params - The embedding parameters including input text and model name
|
|
9
|
+
* @returns An embedding response with the vector data
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const res = await embedding({ model: 'text-embedding-3-small', input: 'Hello world' });
|
|
13
|
+
* console.log(res.data[0].embedding);
|
|
14
|
+
*/
|
|
15
|
+
export declare function embedding(params: EmbeddingParams): Promise<EmbeddingResponse>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.embedding = embedding;
|
|
4
|
+
const getHandler_1 = require("./handlers/getHandler");
|
|
5
|
+
const registry_1 = require("./registry");
|
|
6
|
+
require("./handlers");
|
|
7
|
+
const EMBEDDING_MODEL_HANDLER_MAPPINGS = (0, registry_1.getEmbeddingHandlers)();
|
|
8
|
+
/**
|
|
9
|
+
* Generate embeddings for the given input using the provider that matches the model prefix.
|
|
10
|
+
*
|
|
11
|
+
* Supports OpenAI, Mistral, Gemini, Ollama, and 38+ OpenAI-compatible embedding providers.
|
|
12
|
+
*
|
|
13
|
+
* @param params - The embedding parameters including input text and model name
|
|
14
|
+
* @returns An embedding response with the vector data
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* const res = await embedding({ model: 'text-embedding-3-small', input: 'Hello world' });
|
|
18
|
+
* console.log(res.data[0].embedding);
|
|
19
|
+
*/
|
|
20
|
+
async function embedding(params) {
|
|
21
|
+
const handler = (0, getHandler_1.getHandler)(params.model, EMBEDDING_MODEL_HANDLER_MAPPINGS);
|
|
22
|
+
if (!handler) {
|
|
23
|
+
throw new Error(`Model: ${params.model} not supported. Cannot find a handler.`);
|
|
24
|
+
}
|
|
25
|
+
return handler(params);
|
|
26
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AI21Handler = AI21Handler;
|
|
4
|
+
const combinePrompts_1 = require("../utils/combinePrompts");
|
|
5
|
+
const getUnixTimestamp_1 = require("../utils/getUnixTimestamp");
|
|
6
|
+
const sse_1 = require("../utils/sse");
|
|
7
|
+
const FINISH_REASON_MAP = {
|
|
8
|
+
length: 'length',
|
|
9
|
+
endoftext: 'stop',
|
|
10
|
+
};
|
|
11
|
+
function toUsage(response) {
|
|
12
|
+
const promptTokens = response.prompt.tokens.length;
|
|
13
|
+
const completionTokens = response.completions.reduce((acc, completion) => {
|
|
14
|
+
return acc + completion.data.tokens.length;
|
|
15
|
+
}, 0);
|
|
16
|
+
return {
|
|
17
|
+
prompt_tokens: promptTokens,
|
|
18
|
+
completion_tokens: completionTokens,
|
|
19
|
+
total_tokens: promptTokens + completionTokens,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function toResponse(response, model) {
|
|
23
|
+
const choices = response.completions.map((completion, i) => {
|
|
24
|
+
return {
|
|
25
|
+
finish_reason: FINISH_REASON_MAP[completion.finishReason.reason] ?? 'stop',
|
|
26
|
+
index: i,
|
|
27
|
+
message: {
|
|
28
|
+
content: completion.data.text,
|
|
29
|
+
role: 'assistant',
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
});
|
|
33
|
+
return {
|
|
34
|
+
model: model,
|
|
35
|
+
created: (0, getUnixTimestamp_1.getUnixTimestamp)(),
|
|
36
|
+
usage: toUsage(response),
|
|
37
|
+
choices: choices,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
async function getAI21Response(model, prompt, baseUrl, apiKey, stream) {
|
|
41
|
+
const body = { prompt };
|
|
42
|
+
if (stream)
|
|
43
|
+
body.stream = true;
|
|
44
|
+
return fetch(`${baseUrl}/studio/v1/${model}/complete`, {
|
|
45
|
+
method: 'POST',
|
|
46
|
+
headers: {
|
|
47
|
+
Authorization: `Bearer ${apiKey}`,
|
|
48
|
+
'Content-Type': 'application/json',
|
|
49
|
+
accept: stream ? 'text/event-stream' : 'application/json',
|
|
50
|
+
},
|
|
51
|
+
body: JSON.stringify(body),
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
async function AI21Handler(params) {
|
|
55
|
+
const baseUrl = params.baseUrl ?? 'https://api.ai21.com';
|
|
56
|
+
const apiKey = params.apiKey ?? process.env.AI21_API_KEY;
|
|
57
|
+
if (!apiKey)
|
|
58
|
+
throw new Error('AI21 requires an API key. Set AI21_API_KEY environment variable or pass apiKey in params.');
|
|
59
|
+
const model = params.model;
|
|
60
|
+
const prompt = (0, combinePrompts_1.combinePrompts)(params.messages);
|
|
61
|
+
const res = await getAI21Response(model, prompt, baseUrl, apiKey, params.stream ?? false);
|
|
62
|
+
if (!res.ok) {
|
|
63
|
+
throw new Error(`Received an error with code ${res.status} from AI21 API.`);
|
|
64
|
+
}
|
|
65
|
+
if (params.stream) {
|
|
66
|
+
return (0, sse_1.iterateSSEStream)(res, (payload) => {
|
|
67
|
+
const parsed = JSON.parse(payload);
|
|
68
|
+
return {
|
|
69
|
+
model,
|
|
70
|
+
created: (0, getUnixTimestamp_1.getUnixTimestamp)(),
|
|
71
|
+
choices: [
|
|
72
|
+
{
|
|
73
|
+
delta: { content: parsed.text ?? '', role: 'assistant' },
|
|
74
|
+
finish_reason: parsed.finishReason
|
|
75
|
+
? (FINISH_REASON_MAP[parsed.finishReason.reason] ?? 'stop')
|
|
76
|
+
: null,
|
|
77
|
+
index: 0,
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
};
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
const body = (await res.json());
|
|
84
|
+
return toResponse(body, model);
|
|
85
|
+
}
|
|
86
|
+
const registry_1 = require("../registry");
|
|
87
|
+
(0, registry_1.registerCompletionHandler)('j2-', AI21Handler);
|
|
@@ -0,0 +1,85 @@
|
|
|
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.AnthropicHandler = AnthropicHandler;
|
|
7
|
+
const sdk_1 = __importDefault(require("@anthropic-ai/sdk"));
|
|
8
|
+
const getUnixTimestamp_1 = require("../utils/getUnixTimestamp");
|
|
9
|
+
const toUsage_1 = require("../utils/toUsage");
|
|
10
|
+
const auth_1 = require("../auth");
|
|
11
|
+
function toAnthropicPrompt(messages) {
|
|
12
|
+
return messages
|
|
13
|
+
.map((msg) => {
|
|
14
|
+
const content = msg.content ?? '';
|
|
15
|
+
if (msg.role === 'assistant') {
|
|
16
|
+
return `${sdk_1.default.AI_PROMPT} ${content}`;
|
|
17
|
+
}
|
|
18
|
+
return `${sdk_1.default.HUMAN_PROMPT} ${content}`;
|
|
19
|
+
})
|
|
20
|
+
.join('') + sdk_1.default.AI_PROMPT;
|
|
21
|
+
}
|
|
22
|
+
function toFinishReson(string) {
|
|
23
|
+
if (string === 'max_tokens') {
|
|
24
|
+
return 'length';
|
|
25
|
+
}
|
|
26
|
+
return 'stop';
|
|
27
|
+
}
|
|
28
|
+
function toResponse(anthropicResponse, prompt) {
|
|
29
|
+
return {
|
|
30
|
+
model: anthropicResponse.model,
|
|
31
|
+
created: (0, getUnixTimestamp_1.getUnixTimestamp)(),
|
|
32
|
+
usage: (0, toUsage_1.toUsage)(prompt, anthropicResponse.completion),
|
|
33
|
+
choices: [
|
|
34
|
+
{
|
|
35
|
+
message: {
|
|
36
|
+
content: anthropicResponse.completion,
|
|
37
|
+
role: 'assistant',
|
|
38
|
+
},
|
|
39
|
+
finish_reason: toFinishReson(anthropicResponse.stop_reason),
|
|
40
|
+
index: 0,
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function toStreamingChunk(anthropicResponse) {
|
|
46
|
+
return {
|
|
47
|
+
model: anthropicResponse.model,
|
|
48
|
+
created: (0, getUnixTimestamp_1.getUnixTimestamp)(),
|
|
49
|
+
choices: [
|
|
50
|
+
{
|
|
51
|
+
delta: { content: anthropicResponse.completion, role: 'assistant' },
|
|
52
|
+
finish_reason: toFinishReson(anthropicResponse.stop_reason),
|
|
53
|
+
index: 0,
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
async function* toStreamingResponse(stream) {
|
|
59
|
+
for await (const chunk of stream) {
|
|
60
|
+
yield toStreamingChunk(chunk);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
async function AnthropicHandler(params) {
|
|
64
|
+
const apiKey = params.apiKey ?? process.env.ANTHROPIC_API_KEY ?? (await (0, auth_1.getAnthropicKey)());
|
|
65
|
+
const anthropic = new sdk_1.default({
|
|
66
|
+
apiKey: apiKey,
|
|
67
|
+
});
|
|
68
|
+
const prompt = toAnthropicPrompt(params.messages);
|
|
69
|
+
const anthropicParams = {
|
|
70
|
+
model: params.model,
|
|
71
|
+
max_tokens_to_sample: params.max_tokens ?? 300,
|
|
72
|
+
prompt,
|
|
73
|
+
};
|
|
74
|
+
if (params.stream) {
|
|
75
|
+
const completionStream = await anthropic.completions.create({
|
|
76
|
+
...anthropicParams,
|
|
77
|
+
stream: params.stream,
|
|
78
|
+
});
|
|
79
|
+
return toStreamingResponse(completionStream);
|
|
80
|
+
}
|
|
81
|
+
const completion = await anthropic.completions.create(anthropicParams);
|
|
82
|
+
return toResponse(completion, prompt);
|
|
83
|
+
}
|
|
84
|
+
const registry_1 = require("../registry");
|
|
85
|
+
(0, registry_1.registerCompletionHandler)('claude-', AnthropicHandler);
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CohereHandler = CohereHandler;
|
|
4
|
+
const cohere_ai_1 = require("cohere-ai");
|
|
5
|
+
const combinePrompts_1 = require("../utils/combinePrompts");
|
|
6
|
+
const getUnixTimestamp_1 = require("../utils/getUnixTimestamp");
|
|
7
|
+
const toUsage_1 = require("../utils/toUsage");
|
|
8
|
+
async function CohereHandler(params) {
|
|
9
|
+
const apiKey = params.apiKey ?? process.env.COHERE_API_KEY;
|
|
10
|
+
if (!apiKey)
|
|
11
|
+
throw new Error('Cohere requires an API key. Set COHERE_API_KEY environment variable or pass apiKey in params.');
|
|
12
|
+
const cohere = new cohere_ai_1.CohereClient({ token: apiKey });
|
|
13
|
+
const textsCombined = (0, combinePrompts_1.combinePrompts)(params.messages);
|
|
14
|
+
const config = {
|
|
15
|
+
model: params.model,
|
|
16
|
+
prompt: textsCombined,
|
|
17
|
+
max_tokens: params.max_tokens ?? 50,
|
|
18
|
+
temperature: params.temperature ?? 1,
|
|
19
|
+
};
|
|
20
|
+
if (params.stream) {
|
|
21
|
+
const stream = await cohere.generateStream({
|
|
22
|
+
model: params.model,
|
|
23
|
+
prompt: textsCombined,
|
|
24
|
+
maxTokens: params.max_tokens ?? 50,
|
|
25
|
+
temperature: params.temperature ?? 1,
|
|
26
|
+
});
|
|
27
|
+
return toRealStream(stream, params.model, textsCombined);
|
|
28
|
+
}
|
|
29
|
+
const response = await cohere.generate(config);
|
|
30
|
+
return {
|
|
31
|
+
model: params.model,
|
|
32
|
+
created: (0, getUnixTimestamp_1.getUnixTimestamp)(),
|
|
33
|
+
usage: (0, toUsage_1.toUsage)(textsCombined, response.generations[0].text),
|
|
34
|
+
choices: [
|
|
35
|
+
{
|
|
36
|
+
message: {
|
|
37
|
+
content: response.generations[0].text,
|
|
38
|
+
role: 'assistant',
|
|
39
|
+
},
|
|
40
|
+
finish_reason: 'stop',
|
|
41
|
+
index: 0,
|
|
42
|
+
},
|
|
43
|
+
],
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
async function* toRealStream(stream, model, prompt) {
|
|
47
|
+
let fullText = '';
|
|
48
|
+
for await (const event of stream) {
|
|
49
|
+
if (event.eventType === 'text-generation') {
|
|
50
|
+
fullText += event.text ?? '';
|
|
51
|
+
yield {
|
|
52
|
+
model,
|
|
53
|
+
created: (0, getUnixTimestamp_1.getUnixTimestamp)(),
|
|
54
|
+
usage: (0, toUsage_1.toUsage)(prompt, fullText),
|
|
55
|
+
choices: [
|
|
56
|
+
{
|
|
57
|
+
delta: { content: event.text, role: 'assistant' },
|
|
58
|
+
finish_reason: null,
|
|
59
|
+
index: 0,
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
else if (event.eventType === 'stream-end') {
|
|
65
|
+
yield {
|
|
66
|
+
model,
|
|
67
|
+
created: (0, getUnixTimestamp_1.getUnixTimestamp)(),
|
|
68
|
+
usage: (0, toUsage_1.toUsage)(prompt, fullText),
|
|
69
|
+
choices: [
|
|
70
|
+
{
|
|
71
|
+
delta: { content: '', role: 'assistant' },
|
|
72
|
+
finish_reason: 'stop',
|
|
73
|
+
index: 0,
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
else if (event.eventType === 'stream-error') {
|
|
79
|
+
const msg = event.message ?? 'unknown';
|
|
80
|
+
throw new Error(`Cohere stream error: ${msg}`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const registry_1 = require("../registry");
|
|
85
|
+
(0, registry_1.registerCompletionHandler)('command', CohereHandler);
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CopilotHandler = CopilotHandler;
|
|
4
|
+
const getUnixTimestamp_1 = require("../utils/getUnixTimestamp");
|
|
5
|
+
const auth_1 = require("../auth");
|
|
6
|
+
const COPILOT_API = 'https://api.githubcopilot.com';
|
|
7
|
+
const USER_AGENT = 'GitHubCopilotChat/0.35.0';
|
|
8
|
+
const EDITOR_VERSION = 'vscode/1.107.0';
|
|
9
|
+
const EDITOR_PLUGIN_VERSION = 'copilot-chat/0.35.0';
|
|
10
|
+
const COPILOT_INTEGRATION_ID = 'vscode-chat';
|
|
11
|
+
function getAuthHeaders(token) {
|
|
12
|
+
return {
|
|
13
|
+
Authorization: `Bearer ${token}`,
|
|
14
|
+
'User-Agent': USER_AGENT,
|
|
15
|
+
'Editor-Version': EDITOR_VERSION,
|
|
16
|
+
'Editor-Plugin-Version': EDITOR_PLUGIN_VERSION,
|
|
17
|
+
'Copilot-Integration-Id': COPILOT_INTEGRATION_ID,
|
|
18
|
+
'Openai-Intent': 'conversation-edits',
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function toUsage(data) {
|
|
22
|
+
if (!data.usage)
|
|
23
|
+
return undefined;
|
|
24
|
+
return {
|
|
25
|
+
prompt_tokens: data.usage.prompt_tokens ?? 0,
|
|
26
|
+
completion_tokens: data.usage.completion_tokens ?? 0,
|
|
27
|
+
total_tokens: data.usage.total_tokens ?? 0,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
async function* toStreamingResponse(response) {
|
|
31
|
+
const reader = response.body?.getReader();
|
|
32
|
+
if (!reader)
|
|
33
|
+
throw new Error('No response body');
|
|
34
|
+
const decoder = new TextDecoder();
|
|
35
|
+
let buffer = '';
|
|
36
|
+
while (true) {
|
|
37
|
+
const { done, value } = await reader.read();
|
|
38
|
+
if (done)
|
|
39
|
+
break;
|
|
40
|
+
buffer += decoder.decode(value, { stream: true });
|
|
41
|
+
const lines = buffer.split('\n');
|
|
42
|
+
buffer = lines.pop() ?? '';
|
|
43
|
+
for (const line of lines) {
|
|
44
|
+
const trimmed = line.trim();
|
|
45
|
+
if (!trimmed?.startsWith('data: '))
|
|
46
|
+
continue;
|
|
47
|
+
const payload = trimmed.slice(6);
|
|
48
|
+
if (payload === '[DONE]')
|
|
49
|
+
return;
|
|
50
|
+
try {
|
|
51
|
+
const parsed = JSON.parse(payload);
|
|
52
|
+
yield {
|
|
53
|
+
model: parsed.model,
|
|
54
|
+
created: parsed.created,
|
|
55
|
+
usage: toUsage(parsed),
|
|
56
|
+
choices: (parsed.choices ?? []).map((c) => ({
|
|
57
|
+
delta: {
|
|
58
|
+
content: c.delta?.content ?? null,
|
|
59
|
+
role: c.delta?.role ?? null,
|
|
60
|
+
},
|
|
61
|
+
index: c.index ?? 0,
|
|
62
|
+
finish_reason: c.finish_reason ?? null,
|
|
63
|
+
})),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
// skip parse errors
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async function CopilotHandler(params) {
|
|
73
|
+
const apiKey = params.apiKey ??
|
|
74
|
+
process.env.COPILOT_API_KEY ??
|
|
75
|
+
(await (0, auth_1.getValidToken)());
|
|
76
|
+
if (!apiKey) {
|
|
77
|
+
throw new Error('No se encontró token de Copilot. Ejecuta: npx litellm login copilot');
|
|
78
|
+
}
|
|
79
|
+
const baseUrl = params.baseUrl ?? COPILOT_API;
|
|
80
|
+
const model = params.model.startsWith('copilot/')
|
|
81
|
+
? params.model.slice(8)
|
|
82
|
+
: params.model;
|
|
83
|
+
const body = {
|
|
84
|
+
model,
|
|
85
|
+
messages: params.messages.map((m) => ({
|
|
86
|
+
role: m.role,
|
|
87
|
+
content: m.content,
|
|
88
|
+
})),
|
|
89
|
+
stream: params.stream ?? false,
|
|
90
|
+
};
|
|
91
|
+
if (params.temperature != null)
|
|
92
|
+
body.temperature = params.temperature;
|
|
93
|
+
if (params.top_p != null)
|
|
94
|
+
body.top_p = params.top_p;
|
|
95
|
+
if (params.max_tokens != null)
|
|
96
|
+
body.max_tokens = params.max_tokens;
|
|
97
|
+
if (params.stop != null)
|
|
98
|
+
body.stop = params.stop;
|
|
99
|
+
if (params.presence_penalty != null)
|
|
100
|
+
body.presence_penalty = params.presence_penalty;
|
|
101
|
+
if (params.n != null)
|
|
102
|
+
body.n = params.n;
|
|
103
|
+
const response = await fetch(`${baseUrl}/chat/completions`, {
|
|
104
|
+
method: 'POST',
|
|
105
|
+
headers: {
|
|
106
|
+
'Content-Type': 'application/json',
|
|
107
|
+
...getAuthHeaders(apiKey),
|
|
108
|
+
},
|
|
109
|
+
body: JSON.stringify(body),
|
|
110
|
+
});
|
|
111
|
+
if (!response.ok) {
|
|
112
|
+
const text = await response.text();
|
|
113
|
+
throw new Error(`Error de Copilot API: ${response.status} ${response.statusText}\n${text}`);
|
|
114
|
+
}
|
|
115
|
+
if (params.stream) {
|
|
116
|
+
return toStreamingResponse(response);
|
|
117
|
+
}
|
|
118
|
+
const data = (await response.json());
|
|
119
|
+
const choices = (data.choices ?? []).map((c) => ({
|
|
120
|
+
index: c.index,
|
|
121
|
+
finish_reason: c.finish_reason ?? null,
|
|
122
|
+
message: {
|
|
123
|
+
role: c.message?.role ?? 'assistant',
|
|
124
|
+
content: c.message?.content ?? null,
|
|
125
|
+
},
|
|
126
|
+
}));
|
|
127
|
+
const result = {
|
|
128
|
+
created: data.created ?? (0, getUnixTimestamp_1.getUnixTimestamp)(),
|
|
129
|
+
model: data.model,
|
|
130
|
+
usage: toUsage(data),
|
|
131
|
+
choices,
|
|
132
|
+
};
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
const registry_1 = require("../registry");
|
|
136
|
+
(0, registry_1.registerCompletionHandler)('copilot/', CopilotHandler);
|