Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Which API does it use for DeepSeek? #12

Open
PierrunoYT opened this issue Feb 3, 2025 · 2 comments
Open

Which API does it use for DeepSeek? #12

PierrunoYT opened this issue Feb 3, 2025 · 2 comments

Comments

@PierrunoYT
Copy link

Which API does it use for DeepSeek?

@tedin7
Copy link

tedin7 commented Feb 3, 2025

The DeepSeek API is used in the deepclaude repository, specifically in the src/clients/deepseek.rs file. The API URL DEEPSEEK_API_URL is used for sending requests and handling responses from the DeepSeek service.

For more details, you can view the implementation

let headers = self.build_headers(Some(&config.headers))?;
let request = self.build_request(messages, false, config);
let response = self
.client
.post(DEEPSEEK_API_URL)
.headers(headers)
.json(&request)
.send()
.await
.map_err(|e| ApiError::DeepSeekError {
message: format!("Request failed: {}", e),
type_: "request_failed".to_string(),
param: None,
code: None
})?;
if !response.status().is_success() {
let error = response
.text()
.await
.unwrap_or_else(|_| "Unknown error".to_string());
return Err(ApiError::DeepSeekError {
message: error,
type_: "api_error".to_string(),
param: None,
code: None
});
}
response
.json::<DeepSeekResponse>()
.await
.map_err(|e| ApiError::DeepSeekError {
message: format!("Failed to parse response: {}", e),
type_: "parse_error".to_string(),
param: None,
code: None
})
}
/// Sends a streaming chat request to the DeepSeek API.
///
/// Returns a stream that yields chunks of the model's response as they arrive.
///
/// # Arguments
///
/// * `messages` - Vector of messages for the conversation
/// * `config` - Configuration options for the request
///
/// # Returns
///
/// * `Pin<Box<dyn Stream<Item = Result<StreamResponse>> + Send>>` - A stream of response chunks
///
/// # Errors
///
/// The stream may yield `ApiError::DeepSeekError` if:
/// - The API request fails
/// - Stream processing encounters an error
/// - Response chunks cannot be parsed
pub fn chat_stream(
&self,
messages: Vec<Message>,
config: &ApiConfig,
) -> Pin<Box<dyn Stream<Item = Result<StreamResponse>> + Send>> {
let headers = match self.build_headers(Some(&config.headers)) {
Ok(h) => h,
Err(e) => return Box::pin(futures::stream::once(async move { Err(e) })),
};
let request = self.build_request(messages, true, config);
let client = self.client.clone();
Box::pin(async_stream::try_stream! {
let mut stream = client
.post(DEEPSEEK_API_URL)
.headers(headers)
.json(&request)
.send()
.await
.map_err(|e| ApiError::DeepSeekError {
message: format!("Request failed: {}", e),
type_: "request_failed".to_string(),
param: None,
code: None
})?
.bytes_stream();
let mut data = String::new();
while let Some(chunk) = stream.next().await {
let chunk = chunk.map_err(|e| ApiError::DeepSeekError {
message: format!("Stream error: {}", e),
type_: "stream_error".to_string(),
param: None,
code: None
})?;
data.push_str(&String::from_utf8_lossy(&chunk));
let mut start = 0;
while let Some(end) = data[start..].find("\n\n") {
let end = start + end;
let line = &data[start..end].trim();
.

@PierrunoYT
Copy link
Author

The DeepSeek API is used in the deepclaude repository, specifically in the src/clients/deepseek.rs file. The API URL DEEPSEEK_API_URL is used for sending requests and handling responses from the DeepSeek service.

For more details, you can view the implementation

deepclaude/src/clients/deepseek.rs

Lines 298 to 400 in a02c9ea
let headers = self.build_headers(Some(&config.headers))?;
let request = self.build_request(messages, false, config);

     let response = self 
         .client 
         .post(DEEPSEEK_API_URL) 
         .headers(headers) 
         .json(&request) 
         .send() 
         .await 
         .map_err(|e| ApiError::DeepSeekError {  
             message: format!("Request failed: {}", e), 
             type_: "request_failed".to_string(), 
             param: None, 
             code: None 
         })?; 

     if !response.status().is_success() { 
         let error = response 
             .text() 
             .await 
             .unwrap_or_else(|_| "Unknown error".to_string()); 
         return Err(ApiError::DeepSeekError {  
             message: error, 
             type_: "api_error".to_string(), 
             param: None, 
             code: None 
         }); 
     } 

     response 
         .json::<DeepSeekResponse>() 
         .await 
         .map_err(|e| ApiError::DeepSeekError {  
             message: format!("Failed to parse response: {}", e), 
             type_: "parse_error".to_string(), 
             param: None, 
             code: None 
         }) 
 } 

 /// Sends a streaming chat request to the DeepSeek API. 
 /// 
 /// Returns a stream that yields chunks of the model's response as they arrive. 
 /// 
 /// # Arguments 
 /// 
 /// * `messages` - Vector of messages for the conversation 
 /// * `config` - Configuration options for the request 
 /// 
 /// # Returns 
 /// 
 /// * `Pin<Box<dyn Stream<Item = Result<StreamResponse>> + Send>>` - A stream of response chunks 
 /// 
 /// # Errors 
 /// 
 /// The stream may yield `ApiError::DeepSeekError` if: 
 /// - The API request fails 
 /// - Stream processing encounters an error 
 /// - Response chunks cannot be parsed 
 pub fn chat_stream( 
     &self, 
     messages: Vec<Message>, 
     config: &ApiConfig, 
 ) -> Pin<Box<dyn Stream<Item = Result<StreamResponse>> + Send>> { 
     let headers = match self.build_headers(Some(&config.headers)) { 
         Ok(h) => h, 
         Err(e) => return Box::pin(futures::stream::once(async move { Err(e) })), 
     }; 

     let request = self.build_request(messages, true, config); 
     let client = self.client.clone(); 

     Box::pin(async_stream::try_stream! { 
         let mut stream = client 
             .post(DEEPSEEK_API_URL) 
             .headers(headers) 
             .json(&request) 
             .send() 
             .await 
             .map_err(|e| ApiError::DeepSeekError {  
                 message: format!("Request failed: {}", e), 
                 type_: "request_failed".to_string(), 
                 param: None, 
                 code: None 
             })? 
             .bytes_stream(); 

         let mut data = String::new(); 
          
         while let Some(chunk) = stream.next().await { 
             let chunk = chunk.map_err(|e| ApiError::DeepSeekError {  
                 message: format!("Stream error: {}", e), 
                 type_: "stream_error".to_string(), 
                 param: None, 
                 code: None 
             })?; 
             data.push_str(&String::from_utf8_lossy(&chunk)); 

             let mut start = 0; 
             while let Some(end) = data[start..].find("\n\n") { 
                 let end = start + end; 
                 let line = &data[start..end].trim(); 

.

Thank you. How can I make sure that Sonnet 3.5 is giving me the answer at the end. Do you know how it works?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants