pub struct NormalChatter {
pub token: String,
pub client: Client,
}Expand description
Fields§
§token: StringDeepSeek API 访问令牌
client: ClientHTTP 客户端实例
Implementations§
Source§impl NormalChatter
impl NormalChatter
Sourcepub async fn chat<T: AsRef<str>>(
&mut self,
user_message: T,
history: &mut impl History,
) -> Result<String>
pub async fn chat<T: AsRef<str>>( &mut self, user_message: T, history: &mut impl History, ) -> Result<String>
发送聊天消息并获取文本响应
这个方法会将用户消息添加到历史记录中,发送请求到 DeepSeek API, 然后将助手的响应也添加到历史记录中,最后返回响应文本。
§参数
user_message- 用户消息内容history- 实现了Historytrait 的历史记录管理器
§返回
返回助手的响应文本,如果发生错误则返回错误信息。
§示例
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(())
}Sourcepub async fn chat_json<T: AsRef<str>>(
&mut self,
user_message: T,
history: &mut impl History,
) -> Result<Value>
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- 实现了Historytrait 的历史记录管理器
§返回
返回解析后的 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§
impl Freeze for NormalChatter
impl !RefUnwindSafe for NormalChatter
impl Send for NormalChatter
impl Sync for NormalChatter
impl Unpin for NormalChatter
impl UnsafeUnpin for NormalChatter
impl !UnwindSafe for NormalChatter
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