@@ -15,109 +15,7 @@ export class DeepSeekHandler extends OpenAiHandler {
1515 } )
1616 }
1717
18- private get baseUrl ( ) : string {
19- return this . options . deepSeekBaseUrl ?? "https://api.deepseek.com/v1"
20- }
21-
22- async * createMessage ( systemPrompt : string , messages : Anthropic . Messages . MessageParam [ ] ) : ApiStream {
23- const modelInfo = this . getModel ( ) . info
24- const modelId = this . options . apiModelId ?? deepSeekDefaultModelId
25- const isReasoner = modelId . includes ( "deepseek-reasoner" )
26-
27- const systemMessage = { role : "system" , content : systemPrompt }
28- const formattedMessages = isReasoner
29- ? convertToR1Format ( [ { role : "user" , content : systemPrompt } , ...messages ] )
30- : [ systemMessage , ...convertToOpenAiMessages ( messages ) ]
31-
32- const response = await fetch ( `${ this . baseUrl } /chat/completions` , {
33- method : "POST" ,
34- headers : {
35- "Content-Type" : "application/json" ,
36- Authorization : `Bearer ${ this . options . deepSeekApiKey } ` ,
37- } ,
38- body : JSON . stringify ( {
39- model : modelId ,
40- messages : formattedMessages ,
41- temperature : 0 ,
42- stream : true ,
43- max_tokens : modelInfo . maxTokens ,
44- } ) ,
45- } )
46-
47- if ( ! response . ok ) {
48- throw new Error ( `DeepSeek API error: ${ response . statusText } ` )
49- }
50-
51- if ( ! response . body ) {
52- throw new Error ( "No response body received from DeepSeek API" )
53- }
54-
55- const reader = response . body . getReader ( )
56- const decoder = new TextDecoder ( )
57- let buffer = ""
58-
59- try {
60- while ( true ) {
61- const { done, value } = await reader . read ( )
62- if ( done ) break
63-
64- buffer += decoder . decode ( value , { stream : true } )
65- const lines = buffer . split ( "\n" )
66- buffer = lines . pop ( ) || ""
67-
68- for ( const line of lines ) {
69- if ( line . trim ( ) === "" ) continue
70- if ( ! line . startsWith ( "data: " ) ) continue
71-
72- const data = line . slice ( 6 )
73- if ( data === "[DONE]" ) continue
74-
75- try {
76- const chunk = JSON . parse ( data )
77- // Handle regular delta format
78- const delta = chunk . choices [ 0 ] ?. delta ?? { }
79- if ( delta . type === "ui" ) {
80- yield {
81- type : "text" ,
82- text : delta . metadata ?. content || "" ,
83- metadata : delta . metadata ,
84- }
85- } else if ( delta . content ) {
86- yield {
87- type : "text" ,
88- text : delta . content ,
89- }
90- }
91-
92- if ( "reasoning_content" in delta && delta . reasoning_content ) {
93- yield {
94- type : "reasoning" ,
95- text : delta . reasoning_content ,
96- }
97- }
98-
99- if ( chunk . usage ) {
100- const usage = chunk . usage as DeepSeekUsage
101- let inputTokens = ( usage . prompt_tokens || 0 ) - ( usage . prompt_cache_hit_tokens || 0 )
102- yield {
103- type : "usage" ,
104- inputTokens : inputTokens ,
105- outputTokens : usage . completion_tokens || 0 ,
106- cacheReadTokens : usage . prompt_cache_hit_tokens || 0 ,
107- cacheWriteTokens : usage . prompt_cache_miss_tokens || 0 ,
108- }
109- }
110- } catch ( error ) {
111- console . error ( "Error parsing DeepSeek response:" , error )
112- }
113- }
114- }
115- } finally {
116- reader . releaseLock ( )
117- }
118- }
119-
120- getModel ( ) : { id : string ; info : ModelInfo } {
18+ override getModel ( ) : { id : string ; info : ModelInfo } {
12119 const modelId = this . options . apiModelId ?? deepSeekDefaultModelId
12220 const info = deepSeekModels [ modelId as keyof typeof deepSeekModels ] || deepSeekModels [ deepSeekDefaultModelId ]
12321
@@ -138,34 +36,4 @@ export class DeepSeekHandler extends OpenAiHandler {
13836 cacheReadTokens : usage ?. prompt_tokens_details ?. cached_tokens ,
13937 }
14038 }
141-
142- async completePrompt ( prompt : string ) : Promise < string > {
143- try {
144- const response = await fetch ( `${ this . baseUrl } /chat/completions` , {
145- method : "POST" ,
146- headers : {
147- "Content-Type" : "application/json" ,
148- Authorization : `Bearer ${ this . options . deepSeekApiKey } ` ,
149- } ,
150- body : JSON . stringify ( {
151- model : this . getModel ( ) . id ,
152- messages : [ { role : "user" , content : prompt } ] ,
153- temperature : 0 ,
154- stream : false ,
155- } ) ,
156- } )
157-
158- if ( ! response . ok ) {
159- throw new Error ( `DeepSeek API error: ${ response . statusText } ` )
160- }
161-
162- const data = await response . json ( )
163- return data . choices [ 0 ] ?. message ?. content || ""
164- } catch ( error ) {
165- if ( error instanceof Error ) {
166- throw new Error ( `DeepSeek completion error: ${ error . message } ` )
167- }
168- throw error
169- }
170- }
17139}
0 commit comments