Skip to main content

ds_api/
lib.rs

1/*!
2ds-api — Rust client for DeepSeek
3
4Quickstart
5
6Example: simple non-streaming request
7```no_run
8use ds_api::{ApiClient, ApiRequest};
9use ds_api::raw::request::message::Message;
10
11#[tokio::main]
12async fn main() -> Result<(), Box<dyn std::error::Error>> {
13    // Set DEEPSEEK_API_KEY in your environment before running this example.
14    let token = std::env::var("DEEPSEEK_API_KEY")?;
15    let client = ApiClient::new(token);
16
17    let req = ApiRequest::deepseek_chat(vec![
18        Message::new(ds_api::raw::request::message::Role::User, "Hello from Rust"),
19    ])
20    .max_tokens(150)
21    .json();
22
23    let resp = client.send(req).await?;
24    // Print debug representation of the response; adapt to your needs.
25    println!("Response: {:?}", resp);
26    Ok(())
27}
28```
29
30Example: DeepseekAgent with a minimal tool
31```no_run
32use ds_api::{AgentEvent, DeepseekAgent, tool};
33use futures::StreamExt;
34use serde::Serialize;
35
36struct EchoTool;
37
38#[derive(Serialize)]
39struct EchoResponse {
40    echo: String,
41}
42
43#[tool]
44impl ds_api::Tool for EchoTool {
45    /// Echo the input back.
46    /// input: the string to echo
47    async fn echo(&self, input: String) -> EchoResponse {
48        EchoResponse { echo: input }
49    }
50}
51
52#[tokio::main]
53async fn main() {
54    // Ensure DEEPSEEK_API_KEY is set in your environment before running this example.
55    let token = std::env::var("DEEPSEEK_API_KEY").expect("DEEPSEEK_API_KEY must be set");
56    let agent = DeepseekAgent::new(token).add_tool(EchoTool);
57
58    // The agent returns a stream of `AgentEvent` items. Each variant represents
59    // a distinct event: assistant text, a tool call request, or a tool result.
60    let mut s = agent.chat("Please echo: hello");
61    while let Some(event) = s.next().await {
62        match event {
63            Err(e) => { eprintln!("Error: {e}"); break; }
64            Ok(AgentEvent::Token(text)) => println!("Assistant: {}", text),
65            Ok(AgentEvent::ToolCall(c)) => println!("Tool call: {}({})", c.name, c.args),
66            Ok(AgentEvent::ToolResult(r)) => println!("Result: {} -> {}", r.name, r.result),
67        }
68    }
69}
70```
71
72See the crate README for more examples and migration notes.
73*/
74
75pub mod agent;
76pub mod api;
77pub mod conversation;
78pub mod error;
79pub mod raw; // raw types remain accessible via `ds_api::raw` but are not the primary public API
80pub mod tool_trait;
81
82pub use agent::{AgentEvent, DeepseekAgent, ToolCallInfo, ToolCallResult};
83pub use api::{ApiClient, ApiRequest};
84pub use conversation::{Conversation, LlmSummarizer, SlidingWindowSummarizer};
85pub use error::ApiError;
86
87pub use tool_trait::Tool;
88
89pub use ds_api_macros::tool;