Skip to main content

Crate ds_api

Crate ds_api 

Source
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::{AgentEvent, 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 `AgentEvent` items. Each variant represents
    // a distinct event: assistant text, a tool call request, or a tool result.
    let mut s = agent.chat("Please echo: hello");
    while let Some(event) = s.next().await {
        match event {
            Err(e) => { eprintln!("Error: {e}"); break; }
            Ok(AgentEvent::Token(text)) => println!("Assistant: {}", text),
            Ok(AgentEvent::ToolCall(c)) => println!("Tool call: {}({})", c.name, c.args),
            Ok(AgentEvent::ToolResult(r)) => println!("Result: {} -> {}", r.name, r.result),
        }
    }
}

See the crate README for more examples and migration notes.

Re-exports§

pub use agent::AgentEvent;
pub use agent::DeepseekAgent;
pub use agent::ToolCallInfo;
pub use agent::ToolCallResult;
pub use api::ApiClient;
pub use api::ApiRequest;
pub use conversation::Conversation;
pub use conversation::LlmSummarizer;
pub use conversation::SlidingWindowSummarizer;
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

Attribute Macros§

tool