Skip to main content

SimpleChatter

Struct SimpleChatter 

Source
pub struct SimpleChatter {
    pub history: Vec<Message>,
    pub chatter: NormalChatter,
}
Expand description

简化的聊天客户端,内置历史记录管理

这个结构体提供了简单易用的聊天接口,自动管理对话历史记录, 用户无需关心历史记录的存储和传递。

§字段

  • history: 对话历史记录,第一条消息是系统提示词
  • chatter: 底层的聊天客户端实例

§设计说明

  • 历史记录中的第一条消息是系统提示词(System Prompt)
  • 后续消息是用户(User)和助手(Assistant)的对话内容
  • 每次聊天都会自动更新历史记录
  • 历史记录会不断增长,需要注意上下文长度限制

§使用建议

对于简单的聊天应用,推荐使用 SimpleChatter,因为它提供了最简单易用的接口。 对于需要自定义历史记录管理的复杂应用,请使用 NormalChatter

Fields§

§history: Vec<Message>

对话历史记录

包含系统提示词和所有用户与助手的对话消息。 第一条消息通常是系统提示词。

§chatter: NormalChatter

底层的聊天客户端实例

用于实际发送请求和处理响应。

Implementations§

Source§

impl SimpleChatter

Source

pub fn new(token: String, system_prompt: String) -> Self

创建一个新的 SimpleChatter 实例

§参数
  • token - DeepSeek API 访问令牌
  • system_prompt - 系统提示词,用于定义助手的行为和角色
§示例
use ds_api::SimpleChatter;

let token = "your_deepseek_api_token".to_string();
let system_prompt = "You are a helpful assistant.".to_string();
let chatter = SimpleChatter::new(token, system_prompt);
Source

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

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

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

§参数
  • user_message - 用户消息内容
§返回

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

§示例
use ds_api::SimpleChatter;

#[tokio::main]
async fn main() -> ds_api::error::Result<()> {
    let token = "your_token".to_string();
    let system_prompt = "You are a helpful assistant.".to_string();
    let mut chatter = SimpleChatter::new(token, system_prompt);

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

    Ok(())
}
Source

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

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

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

§参数
  • user_message - 用户消息内容
§返回

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

§注意事项

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

§示例
use ds_api::SimpleChatter;
use serde_json::Value;

#[tokio::main]
async fn main() -> ds_api::error::Result<()> {
    let token = "your_token".to_string();
    let system_prompt = "You are a helpful assistant that responds in JSON format.".to_string();
    let mut chatter = SimpleChatter::new(token, system_prompt);

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

    Ok(())
}
Source

pub fn system_prompt_mut(&mut self) -> Option<&mut String>

获取系统提示词的可变引用

这个方法允许在运行时修改系统提示词。

§返回

返回 Option<&mut String>:当 history 为空或系统提示词不可用时返回 None

§示例
use ds_api::SimpleChatter;

#[tokio::main]
async fn main() -> ds_api::error::Result<()> {
    let token = "your_token".to_string();
    let system_prompt = "You are a helpful assistant.".to_string();
    let mut chatter = SimpleChatter::new(token, system_prompt);

    // 修改系统提示词(安全用法)
    if let Some(prompt) = chatter.system_prompt_mut() {
        *prompt = "You are a sarcastic assistant.".to_string();
    }

    let response = chatter.chat("What is the weather like?").await?;
    println!("Assistant: {}", 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