Skip to main content

NormalChatter

Struct NormalChatter 

Source
pub struct NormalChatter {
    pub token: String,
    pub client: Client,
}
Expand description

支持自定义历史记录管理的聊天客户端

这个结构体提供了与 DeepSeek API 交互的基本功能,同时允许用户 通过实现 History trait 来自定义历史记录的存储和管理方式。

§字段

  • token: DeepSeek API 访问令牌
  • client: HTTP 客户端,用于发送请求

§注意事项

  • 历史记录中的第一条消息通常是系统提示词(System Prompt)
  • 后续消息是用户(User)或助手(Assistant)的对话内容
  • 需要手动管理上下文长度,避免超过模型限制

Fields§

§token: String

DeepSeek API 访问令牌

§client: Client

HTTP 客户端实例

Implementations§

Source§

impl NormalChatter

Source

pub fn new(token: String) -> Self

创建一个新的 NormalChatter 实例

§参数
  • token - DeepSeek API 访问令牌
§示例
use ds_api::NormalChatter;

let token = "your_deepseek_api_token".to_string();
let chatter = NormalChatter::new(token);
Source

pub async fn chat<T: AsRef<str>>( &mut self, user_message: T, history: &mut impl History, ) -> Result<String>

发送聊天消息并获取文本响应

这个方法会将用户消息添加到历史记录中,发送请求到 DeepSeek API, 然后将助手的响应也添加到历史记录中,最后返回响应文本。

§参数
  • user_message - 用户消息内容
  • history - 实现了 History trait 的历史记录管理器
§返回

返回助手的响应文本,如果发生错误则返回错误信息。

§示例
use ds_api::{NormalChatter, History, Message, Role};

#[tokio::main]
async fn main() -> ds_api::error::Result<()> {
    let token = "your_token".to_string();
    let mut chatter = NormalChatter::new(token);
    let mut history: Vec<Message> = vec![];

    let response = chatter.chat("Hello, world!", &mut history).await?;
    println!("Assistant: {}", response);

    Ok(())
}
Source

pub async fn chat_json<T: AsRef<str>>( &mut self, user_message: T, history: &mut impl History, ) -> Result<Value>

发送聊天消息并获取 JSON 格式的响应

这个方法与 NormalChatter::chat 类似,但会启用 JSON 响应模式,并返回解析后的 JSON 值。

§参数
  • user_message - 用户消息内容
  • history - 实现了 History trait 的历史记录管理器
§返回

返回解析后的 JSON 值,如果发生错误则返回错误信息。

§注意事项

使用此方法前,确保在系统提示词中指示模型返回 JSON 格式的响应。

§示例
use ds_api::{NormalChatter, History, Message, Role};
use serde_json::Value;

#[tokio::main]
async fn main() -> ds_api::error::Result<()> {
    let token = "your_token".to_string();
    let mut chatter = NormalChatter::new(token);
    let mut history: Vec<Message> = vec![
        Message::new(Role::System, "You are a helpful assistant that responds in JSON format.")
    ];

    let json_response = chatter.chat_json("Give me information about Paris", &mut history).await?;
    println!("JSON response: {}", serde_json::to_string_pretty(&json_response)?);

    Ok(())
}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more