1+ use crate :: adapters:: llm:: { LLMAdapter , LLMRequest , LLMResponse , ModelConfig , Usage } ;
12use anyhow:: { Context , Result } ;
23use async_trait:: async_trait;
34use reqwest:: { Client , StatusCode } ;
45use serde:: { Deserialize , Serialize } ;
56use std:: time:: Duration ;
67use tokio:: time:: sleep;
7- use crate :: adapters:: llm:: { LLMAdapter , LLMRequest , LLMResponse , ModelConfig , Usage } ;
88
99pub struct AnthropicAdapter {
1010 client : Client ,
@@ -53,14 +53,16 @@ impl AnthropicAdapter {
5353 let api_key = config. api_key . clone ( )
5454 . or_else ( || std:: env:: var ( "ANTHROPIC_API_KEY" ) . ok ( ) )
5555 . context ( "Anthropic API key not found. Set ANTHROPIC_API_KEY environment variable or provide in config" ) ?;
56-
57- let base_url = config. base_url . clone ( )
56+
57+ let base_url = config
58+ . base_url
59+ . clone ( )
5860 . unwrap_or_else ( || "https://api.anthropic.com/v1" . to_string ( ) ) ;
59-
61+
6062 let client = Client :: builder ( )
6163 . timeout ( std:: time:: Duration :: from_secs ( 60 ) )
6264 . build ( ) ?;
63-
65+
6466 Ok ( Self {
6567 client,
6668 config,
@@ -109,38 +111,40 @@ impl AnthropicAdapter {
109111#[ async_trait]
110112impl LLMAdapter for AnthropicAdapter {
111113 async fn complete ( & self , request : LLMRequest ) -> Result < LLMResponse > {
112- let messages = vec ! [
113- Message {
114- role: "user" . to_string( ) ,
115- content: request. user_prompt,
116- } ,
117- ] ;
118-
114+ let messages = vec ! [ Message {
115+ role: "user" . to_string( ) ,
116+ content: request. user_prompt,
117+ } ] ;
118+
119119 let anthropic_request = AnthropicRequest {
120120 model : self . config . model_name . clone ( ) ,
121121 messages,
122122 max_tokens : request. max_tokens . unwrap_or ( self . config . max_tokens ) ,
123123 temperature : request. temperature . unwrap_or ( self . config . temperature ) ,
124124 system : request. system_prompt ,
125125 } ;
126-
126+
127127 let url = format ! ( "{}/messages" , self . base_url) ;
128- let response = self . send_with_retry ( || {
129- self . client
130- . post ( & url)
131- . header ( "x-api-key" , & self . api_key )
132- . header ( "anthropic-version" , "2023-06-01" )
133- . header ( "anthropic-beta" , "messages-2023-12-15" )
134- . header ( "Content-Type" , "application/json" )
135- . json ( & anthropic_request)
136- } )
137- . await
138- . context ( "Failed to send request to Anthropic" ) ?;
139-
140- let anthropic_response: AnthropicResponse = response. json ( ) . await
128+ let response = self
129+ . send_with_retry ( || {
130+ self . client
131+ . post ( & url)
132+ . header ( "x-api-key" , & self . api_key )
133+ . header ( "anthropic-version" , "2023-06-01" )
134+ . header ( "anthropic-beta" , "messages-2023-12-15" )
135+ . header ( "Content-Type" , "application/json" )
136+ . json ( & anthropic_request)
137+ } )
138+ . await
139+ . context ( "Failed to send request to Anthropic" ) ?;
140+
141+ let anthropic_response: AnthropicResponse = response
142+ . json ( )
143+ . await
141144 . context ( "Failed to parse Anthropic response" ) ?;
142-
143- let content = anthropic_response. content
145+
146+ let content = anthropic_response
147+ . content
144148 . first ( )
145149 . map ( |c| {
146150 // Verify it's a text content type
@@ -151,18 +155,19 @@ impl LLMAdapter for AnthropicAdapter {
151155 }
152156 } )
153157 . unwrap_or_default ( ) ;
154-
158+
155159 Ok ( LLMResponse {
156160 content,
157161 model : anthropic_response. model ,
158162 usage : Some ( Usage {
159163 prompt_tokens : anthropic_response. usage . input_tokens ,
160164 completion_tokens : anthropic_response. usage . output_tokens ,
161- total_tokens : anthropic_response. usage . input_tokens + anthropic_response. usage . output_tokens ,
165+ total_tokens : anthropic_response. usage . input_tokens
166+ + anthropic_response. usage . output_tokens ,
162167 } ) ,
163168 } )
164169 }
165-
170+
166171 fn _model_name ( & self ) -> & str {
167172 & self . config . model_name
168173 }
0 commit comments