|
1 | | -import { Anthropic } from "@anthropic-ai/sdk" |
2 | | -import OpenAI from "openai" |
3 | | -import { ApiHandlerOptions, ModelInfo, deepSeekModels, deepSeekDefaultModelId } from "../../shared/api" |
4 | | -import { ApiHandler } from "../index" |
5 | | -import { ApiStream } from "../transform/stream" |
6 | | - |
7 | | -export class DeepSeekHandler implements ApiHandler { |
8 | | - private options: ApiHandlerOptions |
9 | | - private client: OpenAI |
10 | | - |
11 | | - constructor(options: ApiHandlerOptions) { |
12 | | - this.options = options |
13 | | - if (!options.deepSeekApiKey) { |
14 | | - throw new Error("DeepSeek API key is required. Please provide it in the settings.") |
15 | | - } |
16 | | - this.client = new OpenAI({ |
17 | | - baseURL: this.options.deepSeekBaseUrl ?? "https://api.deepseek.com/v1", |
18 | | - apiKey: this.options.deepSeekApiKey, |
19 | | - }) |
20 | | - } |
21 | | - |
22 | | - async *createMessage(systemPrompt: string, messages: Anthropic.Messages.MessageParam[]): ApiStream { |
23 | | - const modelInfo = deepSeekModels[this.options.deepSeekModelId as keyof typeof deepSeekModels] || deepSeekModels[deepSeekDefaultModelId] |
24 | | - |
25 | | - // Format all messages |
26 | | - const messagesToInclude: OpenAI.Chat.ChatCompletionMessageParam[] = [ |
27 | | - { role: 'system' as const, content: systemPrompt } |
28 | | - ] |
29 | | - |
30 | | - // Add the rest of the messages |
31 | | - for (const msg of messages) { |
32 | | - let messageContent = "" |
33 | | - if (typeof msg.content === "string") { |
34 | | - messageContent = msg.content |
35 | | - } else if (Array.isArray(msg.content)) { |
36 | | - messageContent = msg.content.reduce((acc, part) => { |
37 | | - if (part.type === "text") { |
38 | | - return acc + part.text |
39 | | - } |
40 | | - return acc |
41 | | - }, "") |
42 | | - } |
43 | | - |
44 | | - messagesToInclude.push({ |
45 | | - role: msg.role === 'user' ? 'user' as const : 'assistant' as const, |
46 | | - content: messageContent |
47 | | - }) |
48 | | - } |
49 | | - |
50 | | - const requestOptions: OpenAI.Chat.ChatCompletionCreateParamsStreaming = { |
51 | | - model: this.options.deepSeekModelId ?? "deepseek-chat", |
52 | | - messages: messagesToInclude, |
53 | | - temperature: 0, |
54 | | - stream: true, |
55 | | - max_tokens: modelInfo.maxTokens, |
56 | | - } |
57 | | - |
58 | | - if (this.options.includeStreamOptions ?? true) { |
59 | | - requestOptions.stream_options = { include_usage: true } |
60 | | - } |
61 | | - |
62 | | - let totalInputTokens = 0; |
63 | | - let totalOutputTokens = 0; |
64 | | - |
65 | | - try { |
66 | | - const stream = await this.client.chat.completions.create(requestOptions) |
67 | | - for await (const chunk of stream) { |
68 | | - const delta = chunk.choices[0]?.delta |
69 | | - if (delta?.content) { |
70 | | - yield { |
71 | | - type: "text", |
72 | | - text: delta.content, |
73 | | - } |
74 | | - } |
75 | | - if (chunk.usage) { |
76 | | - yield { |
77 | | - type: "usage", |
78 | | - inputTokens: chunk.usage.prompt_tokens || 0, |
79 | | - outputTokens: chunk.usage.completion_tokens || 0, |
80 | | - } |
81 | | - } |
82 | | - } |
83 | | - } catch (error) { |
84 | | - console.error("DeepSeek API Error:", error) |
85 | | - throw error |
86 | | - } |
87 | | - } |
88 | | - |
89 | | - getModel(): { id: string; info: ModelInfo } { |
90 | | - const modelId = this.options.deepSeekModelId ?? deepSeekDefaultModelId |
91 | | - return { |
92 | | - id: modelId, |
93 | | - info: deepSeekModels[modelId as keyof typeof deepSeekModels] || deepSeekModels[deepSeekDefaultModelId], |
94 | | - } |
95 | | - } |
| 1 | +import { OpenAiHandler } from "./openai" |
| 2 | +import { ApiHandlerOptions, ModelInfo } from "../../shared/api" |
| 3 | +import { deepSeekModels, deepSeekDefaultModelId } from "../../shared/api" |
| 4 | + |
| 5 | +export class DeepSeekHandler extends OpenAiHandler { |
| 6 | + constructor(options: ApiHandlerOptions) { |
| 7 | + if (!options.deepSeekApiKey) { |
| 8 | + throw new Error("DeepSeek API key is required. Please provide it in the settings.") |
| 9 | + } |
| 10 | + super({ |
| 11 | + ...options, |
| 12 | + openAiApiKey: options.deepSeekApiKey, |
| 13 | + openAiModelId: options.deepSeekModelId ?? deepSeekDefaultModelId, |
| 14 | + openAiBaseUrl: options.deepSeekBaseUrl ?? "https://api.deepseek.com/v1", |
| 15 | + includeMaxTokens: true |
| 16 | + }) |
| 17 | + } |
| 18 | + |
| 19 | + override getModel(): { id: string; info: ModelInfo } { |
| 20 | + const modelId = this.options.deepSeekModelId ?? deepSeekDefaultModelId |
| 21 | + return { |
| 22 | + id: modelId, |
| 23 | + info: deepSeekModels[modelId as keyof typeof deepSeekModels] || deepSeekModels[deepSeekDefaultModelId] |
| 24 | + } |
| 25 | + } |
96 | 26 | } |
0 commit comments