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_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 `AgentEvent` items. Each variant represents
53    // a distinct event: assistant text, a tool call request, or a tool result.
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(AgentEvent::Token(text)) => println!("Assistant: {}", text),
59            Ok(AgentEvent::ToolCall(c)) => println!("Tool call: {}({})", c.name, c.args),
60            Ok(AgentEvent::ToolResult(r)) => println!("Result: {} -> {}", r.name, r.result),
61        }
62    }
63}
64```
65
66See the crate README for more examples and migration notes.
67*/
68
69pub mod agent;
70pub mod api;
71pub mod conversation;
72pub mod error;
73pub mod raw; // raw types remain accessible via `ds_api::raw` but are not the primary public API
74pub mod tool_trait;
75
76pub use agent::{AgentEvent, DeepseekAgent, ToolCallInfo, ToolCallResult};
77pub use api::{ApiClient, ApiRequest};
78pub use conversation::{Conversation, LlmSummarizer, SlidingWindowSummarizer};
79pub use error::ApiError;
80pub use tool_trait::Tool;
81
82pub use ds_api_macros::tool;