Skip to content

Commit 328d120

Browse files
committed
Add support for beta header to enable extended token output
1 parent e86e154 commit 328d120

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

crates/misanthropy/src/lib.rs

+36-35
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ pub const ANTHROPIC_API_VERSION: &str = "2023-06-01";
2525

2626
const DEFAULT_API_DOMAIN: &str = "api.anthropic.com";
2727

28+
/// Beta header value for increased output tokens
29+
pub const ANTHROPIC_BETA_HEADER_VALUE: &str = "max-tokens-3-5-sonnet-2024-07-15";
30+
2831
mod error;
2932

3033
pub use error::*;
@@ -793,6 +796,7 @@ impl Message {
793796
pub struct Anthropic {
794797
api_key: String,
795798
base_url: String,
799+
use_beta: bool,
796800
}
797801

798802
impl Anthropic {
@@ -802,9 +806,17 @@ impl Anthropic {
802806
Self {
803807
api_key,
804808
base_url: format!("https://{}", DEFAULT_API_DOMAIN),
809+
use_beta: true,
805810
}
806811
}
807812

813+
/// Enables or disables the beta feature for increased output tokens.
814+
/// See: https://docs.anthropic.com/en/release-notes/api#july-15th-2024
815+
pub fn with_beta(mut self, use_beta: bool) -> Self {
816+
self.use_beta = use_beta;
817+
self
818+
}
819+
808820
/// Creates an Anthropic client using the API key from the environment.
809821
/// Reads the key from the ANTHROPIC_API_KEY environment variable.
810822
pub fn from_env() -> Result<Self> {
@@ -822,6 +834,26 @@ impl Anthropic {
822834
}
823835
}
824836

837+
/// Creates the headers for API requests, including the beta header if enabled.
838+
fn create_headers(&self) -> Result<HeaderMap> {
839+
let mut headers = HeaderMap::new();
840+
headers.insert("x-api-key", HeaderValue::from_str(&self.api_key)?);
841+
headers.insert(
842+
"anthropic-version",
843+
HeaderValue::from_static(ANTHROPIC_API_VERSION),
844+
);
845+
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
846+
847+
if self.use_beta {
848+
headers.insert(
849+
"anthropic-beta",
850+
HeaderValue::from_static(ANTHROPIC_BETA_HEADER_VALUE),
851+
);
852+
}
853+
854+
Ok(headers)
855+
}
856+
825857
/// Sends a message request to the Anthropic API and returns a streaming response.
826858
/// Allows processing of incremental updates as they arrive from the API.
827859
///
@@ -832,20 +864,10 @@ impl Anthropic {
832864
"Streaming requests must have stream set to true".to_string(),
833865
));
834866
}
835-
let mut headers = HeaderMap::new();
836-
headers.insert("x-api-key", HeaderValue::from_str(&self.api_key)?);
837-
headers.insert(
838-
"anthropic-version",
839-
HeaderValue::from_static(ANTHROPIC_API_VERSION),
840-
);
841-
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
842-
843-
let url = format!("{}/v1/messages", self.base_url);
844-
845867
let event_source = EventSource::new(
846868
reqwest::Client::new()
847-
.post(&url)
848-
.headers(headers)
869+
.post(format!("{}/v1/messages", self.base_url))
870+
.headers(self.create_headers()?)
849871
.json(&request),
850872
)
851873
.map_err(|e| Error::EventSourceError(e.to_string()))?;
@@ -857,41 +879,20 @@ impl Anthropic {
857879
/// Uses client defaults for model and max_tokens if not specified in the request.
858880
pub async fn messages(&self, request: &MessagesRequest) -> Result<MessagesResponse> {
859881
let client = reqwest::Client::new();
860-
861-
let mut headers = HeaderMap::new();
862-
headers.insert("x-api-key", HeaderValue::from_str(&self.api_key)?);
863-
headers.insert(
864-
"anthropic-version",
865-
HeaderValue::from_static(ANTHROPIC_API_VERSION),
866-
);
867-
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
868-
let url = format!("{}/v1/messages", self.base_url);
869-
trace!("Full request:");
870-
trace!("URL: {}", url);
871-
trace!("Headers: {:#?}", headers);
872-
trace!("Body: {:#?}", request);
873-
874882
let response = client
875-
.post(url)
876-
.headers(headers)
883+
.post(format!("{}/v1/messages", self.base_url))
884+
.headers(self.create_headers()?)
877885
.json(&request)
878886
.send()
879887
.await?;
880888

881889
let status = response.status();
882890

883-
// Debug print the full response, including status and headers
884-
trace!("Full response:");
885-
trace!("Status: {}", status);
886-
trace!("Headers: {:#?}", response.headers());
887-
888891
if status.is_success() {
889892
let messages_response: MessagesResponse = response.json().await?;
890-
trace!("Body: {:#?}", messages_response);
891893
Ok(messages_response)
892894
} else {
893895
let error_response: ApiErrorResponse = response.json().await?;
894-
trace!("Error: {:#?}", error_response);
895896
Err(error_response.into())
896897
}
897898
}

0 commit comments

Comments
 (0)