Expand description
ds-api — Rust client for DeepSeek
Quickstart
Example: simple non-streaming request
use ds_api::{ApiClient, ApiRequest};
use ds_api::raw::request::message::Message;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Set DEEPSEEK_API_KEY in your environment before running this example.
let token = std::env::var("DEEPSEEK_API_KEY")?;
let client = ApiClient::new(token);
let req = ApiRequest::deepseek_chat(vec![
Message::new(ds_api::raw::request::message::Role::User, "Hello from Rust"),
])
.max_tokens(150)
.json();
let resp = client.send(req).await?;
// Print debug representation of the response; adapt to your needs.
println!("Response: {:?}", resp);
Ok(())
}Example: DeepseekAgent with a minimal tool
use ds_api::{DeepseekAgent, tool};
use futures::StreamExt;
use serde_json::json;
struct EchoTool;
#[tool]
impl ds_api::Tool for EchoTool {
// Example tool method: echo a string back as JSON.
async fn echo(&self, input: String) -> serde_json::Value {
json!({ "echo": input })
}
}
#[tokio::main]
async fn main() {
// Ensure DEEPSEEK_API_KEY is set in your environment before running this example.
let token = std::env::var("DEEPSEEK_API_KEY").expect("DEEPSEEK_API_KEY must be set");
let agent = DeepseekAgent::new(token).add_tool(EchoTool);
// The agent returns a stream of `AgentResponse` events. When the model triggers tool calls,
// the stream yields a preview (assistant content + tool call requests) followed by the tool results.
let mut s = agent.chat("Please echo: hello");
while let Some(event) = s.next().await {
match event {
Err(e) => { eprintln!("Error: {e}"); break; }
Ok(ev) => {
if let Some(content) = &ev.content {
println!("Assistant: {}", content);
}
for tc in &ev.tool_calls {
// ToolCallEvent fields: id, name, args, result
println!("Tool call: {} -> {}", tc.name, tc.result);
}
}
}
}
}See the crate README for more examples and migration notes.
Re-exports§
pub use agent::AgentResponse;pub use agent::DeepseekAgent;pub use agent::ToolCallEvent;pub use api::ApiClient;pub use api::ApiRequest;pub use conversation::DeepseekConversation;pub use error::ApiError;pub use tool_trait::Tool;
Modules§
- agent
- Agent module (refactored)
- api
- conversation
- error
- Unified error types
- raw
- Raw API data structures
- tool_
trait