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::{DeepseekAgent, tool};
33use futures::StreamExt;
34use serde_json::json;
35
36struct EchoTool;
37
38#[tool]
39impl ds_api::Tool for EchoTool {
40    // Example tool method: echo a string back as JSON.
41    async fn echo(&self, input: String) -> serde_json::Value {
42        json!({ "echo": input })
43    }
44}
45
46#[tokio::main]
47async fn main() {
48    // Ensure DEEPSEEK_API_KEY is set in your environment before running this example.
49    let token = std::env::var("DEEPSEEK_API_KEY").expect("DEEPSEEK_API_KEY must be set");
50    let agent = DeepseekAgent::new(token).add_tool(EchoTool);
51
52    // The agent returns a stream of `AgentResponse` events. When the model triggers tool calls,
53    // the stream yields a preview (assistant content + tool call requests) followed by the tool results.
54    let mut s = agent.chat("Please echo: hello");
55    while let Some(event) = s.next().await {
56        match event {
57            Err(e) => { eprintln!("Error: {e}"); break; }
58            Ok(ev) => {
59                if let Some(content) = &ev.content {
60                    println!("Assistant: {}", content);
61                }
62                for tc in &ev.tool_calls {
63                    // ToolCallEvent fields: id, name, args, result
64                    println!("Tool call: {} -> {}", tc.name, tc.result);
65                }
66            }
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
82// Legacy convenience chat modules `NormalChatter` and `SimpleChatter` were removed
83// during the refactor. Use the new `ApiRequest` / `ApiClient` /
84// `DeepseekConversation` / `DeepseekAgent` APIs instead.
85pub use agent::{AgentResponse, DeepseekAgent, ToolCallEvent};
86pub use api::{ApiClient, ApiRequest};
87pub use conversation::DeepseekConversation;
88pub use error::ApiError;
89pub use tool_trait::Tool;
90
91pub use ds_api_macros::tool;