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
impl SimpleChatter
Sourcepub async fn chat<T: AsRef<str>>(&mut self, user_message: T) -> Result<String>
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(())
}Sourcepub async fn chat_json<T: AsRef<str>>(
&mut self,
user_message: T,
) -> Result<Value>
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(())
}Sourcepub fn system_prompt_mut(&mut self) -> Option<&mut String>
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§
impl Freeze for SimpleChatter
impl !RefUnwindSafe for SimpleChatter
impl Send for SimpleChatter
impl Sync for SimpleChatter
impl Unpin for SimpleChatter
impl UnsafeUnpin for SimpleChatter
impl !UnwindSafe for SimpleChatter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more