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(ev) = s.next().await {
56        if let Some(content) = &ev.content {
57            println!("Assistant: {}", content);
58        }
59        for tc in &ev.tool_calls {
60            // ToolCallEvent fields: id, name, args, result
61            println!("Tool call: {} -> {}", tc.name, tc.result);
62        }
63    }
64}
65```
66
67See the crate README for more examples and migration notes.
68*/
69
70pub mod agent;
71pub mod api;
72pub mod conversation;
73pub mod error;
74pub mod raw; // raw types remain accessible via `ds_api::raw` but are not the primary public API
75pub mod tool_trait;
76
77// Legacy convenience chat modules `NormalChatter` and `SimpleChatter` were removed
78// during the refactor. Use the new `ApiRequest` / `ApiClient` /
79// `DeepseekConversation` / `DeepseekAgent` APIs instead.
80pub use agent::{AgentResponse, DeepseekAgent, ToolCallEvent};
81pub use api::{ApiClient, ApiRequest};
82pub use conversation::DeepseekConversation;
83pub use tool_trait::Tool;
84
85pub use ds_api_macros::tool;