@@ -30,29 +30,7 @@ export class AnthropicHandler implements ApiHandler, SingleCompletionHandler {
3030 async * createMessage ( systemPrompt : string , messages : Anthropic . Messages . MessageParam [ ] ) : ApiStream {
3131 let stream : AnthropicStream < Anthropic . Messages . RawMessageStreamEvent >
3232 const cacheControl : CacheControlEphemeral = { type : "ephemeral" }
33- let { id : modelId , info : modelInfo } = this . getModel ( )
34- const maxTokens = this . options . modelMaxTokens || modelInfo . maxTokens || 8192
35- let temperature = this . options . modelTemperature ?? ANTHROPIC_DEFAULT_TEMPERATURE
36- let thinking : BetaThinkingConfigParam | undefined = undefined
37-
38- // Anthropic "Thinking" models require a temperature of 1.0.
39- if ( modelId === "claude-3-7-sonnet-20250219:thinking" ) {
40- // The `:thinking` variant is a virtual identifier for the
41- // `claude-3-7-sonnet-20250219` model with a thinking budget.
42- // We can handle this more elegantly in the future.
43- modelId = "claude-3-7-sonnet-20250219"
44-
45- // Clamp the thinking budget to be at most 80% of max tokens and at
46- // least 1024 tokens.
47- const maxBudgetTokens = Math . floor ( maxTokens * 0.8 )
48- const budgetTokens = Math . max (
49- Math . min ( this . options . anthropicThinking ?? maxBudgetTokens , maxBudgetTokens ) ,
50- 1024 ,
51- )
52-
53- thinking = { type : "enabled" , budget_tokens : budgetTokens }
54- temperature = 1.0
55- }
33+ let { id : modelId , temperature, maxTokens, thinking } = this . getModel ( )
5634
5735 switch ( modelId ) {
5836 case "claude-3-7-sonnet-20250219" :
@@ -202,40 +180,62 @@ export class AnthropicHandler implements ApiHandler, SingleCompletionHandler {
202180 }
203181 }
204182
205- getModel ( ) : { id : AnthropicModelId ; info : ModelInfo } {
183+ getModel ( ) {
206184 const modelId = this . options . apiModelId
185+ let temperature = this . options . modelTemperature ?? ANTHROPIC_DEFAULT_TEMPERATURE
186+ let thinking : BetaThinkingConfigParam | undefined = undefined
207187
208188 if ( modelId && modelId in anthropicModels ) {
209- const id = modelId as AnthropicModelId
210- return { id, info : anthropicModels [ id ] }
211- }
189+ let id = modelId as AnthropicModelId
190+ const info : ModelInfo = anthropicModels [ id ]
212191
213- return { id : anthropicDefaultModelId , info : anthropicModels [ anthropicDefaultModelId ] }
214- }
192+ // The `:thinking` variant is a virtual identifier for the
193+ // `claude-3-7-sonnet-20250219` model with a thinking budget.
194+ // We can handle this more elegantly in the future.
195+ if ( id === "claude-3-7-sonnet-20250219:thinking" ) {
196+ id = "claude-3-7-sonnet-20250219"
197+ }
215198
216- async completePrompt ( prompt : string ) : Promise < string > {
217- try {
218- const response = await this . client . messages . create ( {
219- model : this . getModel ( ) . id ,
220- max_tokens : this . getModel ( ) . info . maxTokens || 8192 ,
221- temperature : this . options . modelTemperature ?? ANTHROPIC_DEFAULT_TEMPERATURE ,
222- messages : [ { role : "user" , content : prompt } ] ,
223- stream : false ,
224- } )
199+ const maxTokens = this . options . modelMaxTokens || info . maxTokens || 8192
225200
226- const content = response . content [ 0 ]
201+ if ( info . thinking ) {
202+ // Anthropic "Thinking" models require a temperature of 1.0.
203+ temperature = 1.0
227204
228- if ( content . type === "text" ) {
229- return content . text
230- }
205+ // Clamp the thinking budget to be at most 80% of max tokens and at
206+ // least 1024 tokens.
207+ const maxBudgetTokens = Math . floor ( maxTokens * 0.8 )
208+ const budgetTokens = Math . max (
209+ Math . min ( this . options . anthropicThinking ?? maxBudgetTokens , maxBudgetTokens ) ,
210+ 1024 ,
211+ )
231212
232- return ""
233- } catch ( error ) {
234- if ( error instanceof Error ) {
235- throw new Error ( `Anthropic completion error: ${ error . message } ` )
213+ thinking = { type : "enabled" , budget_tokens : budgetTokens }
236214 }
237215
238- throw error
216+ return { id , info , temperature , maxTokens , thinking }
239217 }
218+
219+ const id = anthropicDefaultModelId
220+ const info : ModelInfo = anthropicModels [ id ]
221+ const maxTokens = this . options . modelMaxTokens || info . maxTokens || 8192
222+
223+ return { id, info, temperature, maxTokens, thinking }
224+ }
225+
226+ async completePrompt ( prompt : string ) {
227+ let { id : modelId , temperature, maxTokens, thinking } = this . getModel ( )
228+
229+ const message = await this . client . messages . create ( {
230+ model : modelId ,
231+ max_tokens : maxTokens ,
232+ temperature,
233+ thinking,
234+ messages : [ { role : "user" , content : prompt } ] ,
235+ stream : false ,
236+ } )
237+
238+ const content = message . content . find ( ( { type } ) => type === "text" )
239+ return content ?. type === "text" ? content . text : ""
240240 }
241241}
0 commit comments