Skip to content

Commit 864bd18

Browse files
committed
feat: add thinking budget support for Anthropic requests
1 parent 33933a2 commit 864bd18

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

llm_client/src/clients/anthropic.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,13 @@ enum ContentBlockDeltaType {
227227
},
228228
}
229229

230+
#[derive(serde::Serialize, Debug, Clone)]
231+
struct AnthropicThinking {
232+
#[serde(rename = "type")]
233+
thinking_type: String,
234+
budget_tokens: usize,
235+
}
236+
230237
#[derive(serde::Serialize, Debug, Clone)]
231238
struct AnthropicRequest {
232239
system: Vec<AnthropicMessageContent>,
@@ -238,6 +245,8 @@ struct AnthropicRequest {
238245
stream: bool,
239246
#[serde(skip_serializing_if = "Option::is_none")]
240247
max_tokens: Option<usize>,
248+
#[serde(skip_serializing_if = "Option::is_none")]
249+
thinking: Option<AnthropicThinking>,
241250
model: String,
242251
}
243252

@@ -262,6 +271,12 @@ impl AnthropicRequest {
262271
}
263272
}
264273
};
274+
let thinking = completion_request
275+
.thinking_budget()
276+
.map(|budget| AnthropicThinking {
277+
thinking_type: "enabled".to_owned(),
278+
budget_tokens: budget,
279+
});
265280
let messages = completion_request.messages();
266281
// grab the tools over here ONLY from the system message
267282
let tools = messages
@@ -346,6 +361,7 @@ impl AnthropicRequest {
346361
tools,
347362
stream: true,
348363
max_tokens,
364+
thinking,
349365
model: model_str,
350366
}
351367
}
@@ -356,6 +372,12 @@ impl AnthropicRequest {
356372
) -> Self {
357373
let temperature = completion_request.temperature();
358374
let max_tokens = completion_request.get_max_tokens();
375+
let thinking = completion_request
376+
.thinking_budget()
377+
.map(|budget| AnthropicThinking {
378+
thinking_type: "enabled".to_owned(),
379+
budget_tokens: budget,
380+
});
359381
let messages = vec![AnthropicMessage::new(
360382
"user".to_owned(),
361383
completion_request.prompt().to_owned(),
@@ -367,6 +389,7 @@ impl AnthropicRequest {
367389
tools: vec![],
368390
stream: true,
369391
max_tokens,
392+
thinking,
370393
model: model_str,
371394
}
372395
}

llm_client/src/clients/types.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ pub struct LLMClientCompletionRequest {
693693
frequency_penalty: Option<f32>,
694694
stop_words: Option<Vec<String>>,
695695
max_tokens: Option<usize>,
696+
thinking_budget: Option<usize>,
696697
}
697698

698699
#[derive(Clone)]
@@ -703,6 +704,7 @@ pub struct LLMClientCompletionStringRequest {
703704
frequency_penalty: Option<f32>,
704705
stop_words: Option<Vec<String>>,
705706
max_tokens: Option<usize>,
707+
thinking_budget: Option<usize>,
706708
}
707709

708710
impl LLMClientCompletionStringRequest {
@@ -719,6 +721,7 @@ impl LLMClientCompletionStringRequest {
719721
frequency_penalty,
720722
stop_words: None,
721723
max_tokens: None,
724+
thinking_budget: None,
722725
}
723726
}
724727

@@ -755,6 +758,15 @@ impl LLMClientCompletionStringRequest {
755758
pub fn get_max_tokens(&self) -> Option<usize> {
756759
self.max_tokens
757760
}
761+
762+
pub fn set_thinking_budget(mut self, thinking_budget: usize) -> Self {
763+
self.thinking_budget = Some(thinking_budget);
764+
self
765+
}
766+
767+
pub fn thinking_budget(&self) -> Option<usize> {
768+
self.thinking_budget
769+
}
758770
}
759771

760772
impl LLMClientCompletionRequest {
@@ -771,6 +783,7 @@ impl LLMClientCompletionRequest {
771783
frequency_penalty,
772784
stop_words: None,
773785
max_tokens: None,
786+
thinking_budget: None,
774787
}
775788
}
776789

@@ -859,6 +872,15 @@ impl LLMClientCompletionRequest {
859872
pub fn get_max_tokens(&self) -> Option<usize> {
860873
self.max_tokens
861874
}
875+
876+
pub fn set_thinking_budget(mut self, thinking_budget: usize) -> Self {
877+
self.thinking_budget = Some(thinking_budget);
878+
self
879+
}
880+
881+
pub fn thinking_budget(&self) -> Option<usize> {
882+
self.thinking_budget
883+
}
862884
}
863885

864886
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]

0 commit comments

Comments
 (0)