@@ -25,6 +25,9 @@ pub const ANTHROPIC_API_VERSION: &str = "2023-06-01";
25
25
26
26
const DEFAULT_API_DOMAIN : & str = "api.anthropic.com" ;
27
27
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
+
28
31
mod error;
29
32
30
33
pub use error:: * ;
@@ -793,6 +796,7 @@ impl Message {
793
796
pub struct Anthropic {
794
797
api_key : String ,
795
798
base_url : String ,
799
+ use_beta : bool ,
796
800
}
797
801
798
802
impl Anthropic {
@@ -802,9 +806,17 @@ impl Anthropic {
802
806
Self {
803
807
api_key,
804
808
base_url : format ! ( "https://{}" , DEFAULT_API_DOMAIN ) ,
809
+ use_beta : true ,
805
810
}
806
811
}
807
812
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
+
808
820
/// Creates an Anthropic client using the API key from the environment.
809
821
/// Reads the key from the ANTHROPIC_API_KEY environment variable.
810
822
pub fn from_env ( ) -> Result < Self > {
@@ -822,6 +834,26 @@ impl Anthropic {
822
834
}
823
835
}
824
836
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
+
825
857
/// Sends a message request to the Anthropic API and returns a streaming response.
826
858
/// Allows processing of incremental updates as they arrive from the API.
827
859
///
@@ -832,20 +864,10 @@ impl Anthropic {
832
864
"Streaming requests must have stream set to true" . to_string ( ) ,
833
865
) ) ;
834
866
}
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
-
845
867
let event_source = EventSource :: new (
846
868
reqwest:: Client :: new ( )
847
- . post ( & url )
848
- . headers ( headers )
869
+ . post ( format ! ( "{}/v1/messages" , self . base_url ) )
870
+ . headers ( self . create_headers ( ) ? )
849
871
. json ( & request) ,
850
872
)
851
873
. map_err ( |e| Error :: EventSourceError ( e. to_string ( ) ) ) ?;
@@ -857,41 +879,20 @@ impl Anthropic {
857
879
/// Uses client defaults for model and max_tokens if not specified in the request.
858
880
pub async fn messages ( & self , request : & MessagesRequest ) -> Result < MessagesResponse > {
859
881
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
-
874
882
let response = client
875
- . post ( url )
876
- . headers ( headers )
883
+ . post ( format ! ( "{}/v1/messages" , self . base_url ) )
884
+ . headers ( self . create_headers ( ) ? )
877
885
. json ( & request)
878
886
. send ( )
879
887
. await ?;
880
888
881
889
let status = response. status ( ) ;
882
890
883
- // Debug print the full response, including status and headers
884
- trace ! ( "Full response:" ) ;
885
- trace ! ( "Status: {}" , status) ;
886
- trace ! ( "Headers: {:#?}" , response. headers( ) ) ;
887
-
888
891
if status. is_success ( ) {
889
892
let messages_response: MessagesResponse = response. json ( ) . await ?;
890
- trace ! ( "Body: {:#?}" , messages_response) ;
891
893
Ok ( messages_response)
892
894
} else {
893
895
let error_response: ApiErrorResponse = response. json ( ) . await ?;
894
- trace ! ( "Error: {:#?}" , error_response) ;
895
896
Err ( error_response. into ( ) )
896
897
}
897
898
}
0 commit comments