forked from RooCodeInc/Roo-Code
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdeepseek.ts
More file actions
166 lines (145 loc) · 4.73 KB
/
deepseek.ts
File metadata and controls
166 lines (145 loc) · 4.73 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { Anthropic } from "@anthropic-ai/sdk"
import { ApiHandlerOptions, ModelInfo, deepSeekModels, deepSeekDefaultModelId } from "../../shared/api"
import { ApiHandler, SingleCompletionHandler } from "../index"
import { convertToR1Format } from "../transform/r1-format"
import { convertToOpenAiMessages } from "../transform/openai-format"
import { ApiStream } from "../transform/stream"
interface DeepSeekUsage {
prompt_tokens: number
completion_tokens: number
prompt_cache_miss_tokens?: number
prompt_cache_hit_tokens?: number
}
export class DeepSeekHandler implements ApiHandler, SingleCompletionHandler {
private options: ApiHandlerOptions
constructor(options: ApiHandlerOptions) {
if (!options.deepSeekApiKey) {
throw new Error("DeepSeek API key is required. Please provide it in the settings.")
}
this.options = options
}
private get baseUrl(): string {
return this.options.deepSeekBaseUrl ?? "https://api.deepseek.com/v1"
}
async *createMessage(systemPrompt: string, messages: Anthropic.Messages.MessageParam[]): ApiStream {
const modelInfo = this.getModel().info
const modelId = this.options.apiModelId ?? deepSeekDefaultModelId
const isReasoner = modelId.includes("deepseek-reasoner")
const systemMessage = { role: "system", content: systemPrompt }
const formattedMessages = isReasoner
? convertToR1Format([{ role: "user", content: systemPrompt }, ...messages])
: [systemMessage, ...convertToOpenAiMessages(messages)]
const response = await fetch(`${this.baseUrl}/chat/completions`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.options.deepSeekApiKey}`,
"creator-mode": String(this.options.creatorMode),
},
body: JSON.stringify({
model: modelId,
messages: formattedMessages,
temperature: 0,
stream: true,
max_tokens: modelInfo.maxTokens,
}),
})
if (!response.ok) {
throw new Error(`DeepSeek API error: ${response.statusText}`)
}
if (!response.body) {
throw new Error("No response body received from DeepSeek API")
}
const reader = response.body.getReader()
const decoder = new TextDecoder()
let buffer = ""
try {
while (true) {
const { done, value } = await reader.read()
if (done) break
buffer += decoder.decode(value, { stream: true })
const lines = buffer.split("\n")
buffer = lines.pop() || ""
for (const line of lines) {
if (line.trim() === "") continue
if (!line.startsWith("data: ")) continue
const data = line.slice(6)
if (data === "[DONE]") continue
try {
const chunk = JSON.parse(data)
// Handle regular delta format
const delta = chunk.choices[0]?.delta ?? {}
if (delta.type === "ui") {
yield {
type: "text",
text: delta.metadata?.content || "",
metadata: delta.metadata,
}
} else if (delta.content) {
yield {
type: "text",
text: delta.content,
}
}
if ("reasoning_content" in delta && delta.reasoning_content) {
yield {
type: "reasoning",
text: delta.reasoning_content,
}
}
if (chunk.usage) {
const usage = chunk.usage as DeepSeekUsage
let inputTokens = (usage.prompt_tokens || 0) - (usage.prompt_cache_hit_tokens || 0)
yield {
type: "usage",
inputTokens: inputTokens,
outputTokens: usage.completion_tokens || 0,
cacheReadTokens: usage.prompt_cache_hit_tokens || 0,
cacheWriteTokens: usage.prompt_cache_miss_tokens || 0,
}
}
} catch (error) {
console.error("Error parsing DeepSeek response:", error)
}
}
}
} finally {
reader.releaseLock()
}
}
getModel(): { id: string; info: ModelInfo } {
const modelId = this.options.apiModelId ?? deepSeekDefaultModelId
return {
id: modelId,
info: deepSeekModels[modelId as keyof typeof deepSeekModels] || deepSeekModels[deepSeekDefaultModelId],
}
}
async completePrompt(prompt: string): Promise<string> {
try {
const response = await fetch(`${this.baseUrl}/chat/completions`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.options.deepSeekApiKey}`,
"creator-mode": String(this.options.creatorMode),
},
body: JSON.stringify({
model: this.getModel().id,
messages: [{ role: "user", content: prompt }],
temperature: 0,
stream: false,
}),
})
if (!response.ok) {
throw new Error(`DeepSeek API error: ${response.statusText}`)
}
const data = await response.json()
return data.choices[0]?.message?.content || ""
} catch (error) {
if (error instanceof Error) {
throw new Error(`DeepSeek completion error: ${error.message}`)
}
throw error
}
}
}