Spaces:
Runtime error
Runtime error
File size: 2,768 Bytes
077865a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | import { BaseProvider, providerHttpError } from './base.js';
import { flattenMessageContent } from '../lib/content.js';
const API_BASE = 'https://api.cohere.ai/compatibility/v1';
export class CohereProvider extends BaseProvider {
platform = 'cohere';
name = 'Cohere';
async chatCompletion(apiKey, messages, modelId, options) {
const body = {
model: modelId,
messages: flattenMessageContent(messages),
temperature: options?.temperature,
max_tokens: options?.max_tokens,
top_p: options?.top_p,
tools: options?.tools,
tool_choice: options?.tool_choice,
};
const res = await this.fetchWithTimeout(`${API_BASE}/chat/completions`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
});
if (!res.ok) {
const err = await res.json().catch(() => ({}));
throw providerHttpError(res, `Cohere API error ${res.status}: ${err.error?.message ?? res.statusText}`);
}
const data = await res.json();
data._routed_via = { platform: 'cohere', model: modelId };
return data;
}
async *streamChatCompletion(apiKey, messages, modelId, options) {
const body = {
model: modelId,
messages: flattenMessageContent(messages),
temperature: options?.temperature,
max_tokens: options?.max_tokens,
top_p: options?.top_p,
tools: options?.tools,
tool_choice: options?.tool_choice,
stream: true,
};
const res = await this.fetchWithTimeout(`${API_BASE}/chat/completions`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
});
if (!res.ok) {
const err = await res.json().catch(() => ({}));
throw providerHttpError(res, `Cohere API error ${res.status}: ${err.error?.message ?? res.statusText}`);
}
yield* this.readSseStream(res);
}
async validateKey(apiKey) {
// Transport errors propagate — health.ts marks status='error' without
// counting toward auto-disable. Only confirmed 401/403 disables a key.
const res = await this.fetchWithTimeout(`${API_BASE}/models`, {
method: 'GET',
headers: { 'Authorization': `Bearer ${apiKey}` },
}, 10000);
return res.status !== 401 && res.status !== 403;
}
}
//# sourceMappingURL=cohere.js.map |