/** * DeepSeek — Chat Completions at https://api.deepseek.com. * https://api-docs.deepseek.com/api/create-chat-completion/ * https://api-docs.deepseek.com/guides/thinking_mode/ * * The wire format is OpenAI-compatible, so this reuses the compatible adapter's engine with * the differences DeepSeek documents: * - Thinking is ON by default. `thinking: {type: "disabled"}` turns it off; * `reasoning_effort` accepts none / low / high / max. * - In thinking mode `tool_choice: "required"` and named tool choices are rejected with a * 400. Structured generation keeps thinking on (answer keys are noticeably more accurate * with it) and asks for the tool call in the prompt with `tool_choice: "auto"`. Only when * the effort is set to "none" is thinking off, and then the tool choice is forced. * - When tools are present, the `reasoning_content` of every earlier assistant turn must be * sent back, or the API answers 400. It is kept in `providerData` and replayed. * - Usage reports `prompt_cache_hit_tokens`; cache hits are billed at a much lower rate. * - `/models` lists context window, output limit, input modalities and effort levels. */ import { AIError, type ChatRequest, type DiscoveredModel, type ModelCapabilities, type ProviderAdapter, type ProviderConfig, type TestResult } from '../types.ts'; import { isChatModelId, nearestEffort, num, runTest, str, TEST_TIMEOUT_MS, withCatalogDefaults } from './common.ts'; import { isRecord, requestJson, trimBaseUrl } from './http.ts'; import { streamChatCompletions, type CompatOptions } from './openaiCompatible.ts'; export const DEEPSEEK_BASE_URL = 'https://api.deepseek.com'; const LABEL = 'DeepSeek'; const EFFORTS = ['low', 'high', 'max'] as const; function base(cfg: ProviderConfig): string { return trimBaseUrl(cfg.baseUrl?.trim() || DEEPSEEK_BASE_URL); } /** "none" turns thinking off; a model marked as not reasoning is treated the same way. */ function effortOf(req: ChatRequest): ChatRequest['reasoningEffort'] { return req.capabilities?.reasoning === false ? 'none' : req.reasoningEffort; } function options(cfg: ProviderConfig): CompatOptions { if (!cfg.apiKey) throw new AIError('not_configured', `Add your DeepSeek API key for "${cfg.name}" in Settings.`); return { kind: 'deepseek', label: cfg.name || LABEL, // DeepSeek serves the same API at the root and under /v1; the explicit /v1 keeps the // compatible engine from guessing. baseUrl: `${base(cfg)}/v1`, apiKey: cfg.apiKey, headers: cfg.headers, replayReasoning: true, forcedToolChoice: (req) => effortOf(req) === 'none', adjustBody: (body, req) => { const effort = effortOf(req); delete body.reasoning_effort; if (effort === 'none' || body.tool_choice === 'required' || isRecord(body.tool_choice)) { // Non-thinking mode (also the only mode that accepts a forced tool choice). body.thinking = { type: 'disabled' }; } else { body.thinking = { type: 'enabled' }; if (effort) body.reasoning_effort = nearestEffort(effort, EFFORTS); // Sampling parameters have no effect in thinking mode; leave them out. delete body.temperature; } }, }; } async function listModels(cfg: ProviderConfig, signal?: AbortSignal, timeoutMs?: number): Promise { const o = options(cfg); const res = await requestJson(`${base(cfg)}/models`, { provider: o.label, secrets: [o.apiKey, ...Object.values(o.headers ?? {})], headers: { ...o.headers, Authorization: `Bearer ${o.apiKey}` }, signal, firstByteTimeoutMs: timeoutMs, totalTimeoutMs: timeoutMs, }); const list = isRecord(res) && Array.isArray(res.data) ? res.data : []; const found: DiscoveredModel[] = []; for (const m of list) { if (!isRecord(m)) continue; const id = str(m.id); if (!id || !isChatModelId(id)) continue; const model: DiscoveredModel = { id }; if (str(m.name) && str(m.name) !== id) model.label = str(m.name); if (num(m.context_window)) model.contextWindow = num(m.context_window); if (num(m.max_output_tokens)) model.maxOutput = num(m.max_output_tokens); const caps: Partial = { tools: true, pdf: false }; if (Array.isArray(m.input_modalities)) caps.vision = m.input_modalities.includes('image'); if (isRecord(m.effort)) caps.reasoning = true; model.capabilities = caps; found.push(model); } return withCatalogDefaults('deepseek', found); } async function test(cfg: ProviderConfig): Promise { return runTest(cfg.name || LABEL, async () => ({ modelCount: (await listModels(cfg, undefined, TEST_TIMEOUT_MS)).length })); } export const deepseekAdapter: ProviderAdapter = { kind: 'deepseek', meta: { label: 'DeepSeek', defaultBaseUrl: DEEPSEEK_BASE_URL, requiresApiKey: true, requiresBaseUrl: false, supportsDiscovery: true, docsUrl: 'https://platform.deepseek.com/api_keys', keyHint: 'Starts with sk-', }, stream: (cfg, req) => streamChatCompletions(options(cfg), req), listModels: (cfg, signal) => listModels(cfg, signal), test, };