pub struct Request { /* private fields */ }Expand description
一个发送至 Deepseek API 的请求对象,封装了原始请求数据。 该结构体保证请求合法
Implementations§
Source§impl Request
impl Request
Sourcepub fn basic_query(messages: Vec<Message>) -> Self
pub fn basic_query(messages: Vec<Message>) -> Self
创建一个基本的聊天请求,使用 DeepseekChat 模型。
参数 messages 是一个消息列表,表示对话的上下文。
example:
use ds_api::request::message::Role;
use ds_api::request::Message;
use ds_api::request::Request;
let request = Request::basic_query(vec![
Message::new(Role::User, "What is the capital of France?")
]);Sourcepub fn basic_query_reasoner(messages: Vec<Message>) -> Self
pub fn basic_query_reasoner(messages: Vec<Message>) -> Self
创建一个基本的聊天请求,使用 DeepseekReasoner 模型。
参数 messages 是一个消息列表,表示对话的上下文。
example:
use ds_api::request::message::Role;
use ds_api::request::Message;
use ds_api::request::Request;
let request = Request::basic_query_reasoner(vec![
Message::new(Role::User, "What is the capital of France?")
]);pub fn builder() -> Self
pub fn add_message(self, message: Message) -> Self
pub fn messages(self, messages: Vec<Message>) -> Self
pub fn model(self, model: Model) -> Self
pub fn response_format_type( self, response_format_type: ResponseFormatType, ) -> Self
pub fn json(self) -> Self
pub fn text(self) -> Self
Sourcepub fn frequency_penalty(self, penalty: f32) -> Self
pub fn frequency_penalty(self, penalty: f32) -> Self
Possible values: >= -2 and <= 2 Default value: 0 介于 -2.0 和 2.0 之间的数字。如果该值为正,那么新 token 会根据其在已有文本中的出现频率受到相应的惩罚,降低模型重复相同内容的可能性。
Sourcepub fn presence_penalty(self, penalty: f32) -> Self
pub fn presence_penalty(self, penalty: f32) -> Self
Possible values: >= -2 and <= 2 Default value: 0 介于 -2.0 和 2.0 之间的数字。如果该值为正,那么新 token 会根据其是否已在已有文本中出现受到相应的惩罚,从而增加模型谈论新主题的可能性。
Sourcepub fn max_tokens(self, max_tokens: u32) -> Self
pub fn max_tokens(self, max_tokens: u32) -> Self
限制一次请求中模型生成 completion 的最大 token 数。输入 token 和输出 token 的总长度受模型的上下文长度的限制。取值范围与默认值详见文档。
Sourcepub fn temperature(self, temperature: f32) -> Self
pub fn temperature(self, temperature: f32) -> Self
Possible values: <= 2 Default value: 1 采样温度,介于 0 和 2 之间。更高的值,如 0.8,会使输出更随机,而更低的值,如 0.2,会使其更加集中和确定。 我们通常建议可以更改这个值或者更改 top_p,但不建议同时对两者进行修改。
pub fn stop_vec(self, stop: Vec<String>) -> Self
pub fn stop_str(self, stop: String) -> Self
Sourcepub fn top_p(self, top_p: f32) -> Self
pub fn top_p(self, top_p: f32) -> Self
Possible values: <= 1 Default value: 1 作为调节采样温度的替代方案,模型会考虑前 top_p 概率的 token 的结果。所以 0.1 就意味着只有包括在最高 10% 概率中的 token 会被考虑。 我们通常建议修改这个值或者更改 temperature,但不建议同时对两者进行修改。
pub fn add_tool(self, tool: Tool) -> Self
pub fn tool_choice_type(self, tool_choice: ToolChoiceType) -> Self
pub fn tool_choice_object(self, tool_choice: ToolChoiceObject) -> Self
Sourcepub fn logprobs(self, top_logprobs: u32) -> Self
pub fn logprobs(self, top_logprobs: u32) -> Self
top_logprobs: 一个介于 0 到 20 之间的整数 N,指定每个输出位置返回输出概率 top N 的 token,且返回这些 token 的对数概率
pub fn raw(&self) -> &ChatCompletionRequest
Sourcepub async fn execute_client_baseurl_nostreaming(
self,
client: &Client,
base_url: &str,
token: &str,
) -> Result<ChatCompletionResponse>
pub async fn execute_client_baseurl_nostreaming( self, client: &Client, base_url: &str, token: &str, ) -> Result<ChatCompletionResponse>
执行无流式(non-streaming)请求,使用指定的 base_url(会自动追加 /chat/completions path)。
接收一个不可变的 &reqwest::Client,避免对外部 client 所有权的要求。
Sourcepub async fn execute_client_nostreaming(
self,
client: &Client,
token: &str,
) -> Result<ChatCompletionResponse>
pub async fn execute_client_nostreaming( self, client: &Client, token: &str, ) -> Result<ChatCompletionResponse>
便捷方法:使用默认 base URL 执行无流式请求(接收不可变的 &Client)。
Sourcepub async fn execute_baseurl_nostreaming(
self,
base_url: &str,
token: &str,
) -> Result<ChatCompletionResponse>
pub async fn execute_baseurl_nostreaming( self, base_url: &str, token: &str, ) -> Result<ChatCompletionResponse>
在没有外部 Client 的情况下执行无流式请求(使用传入的 base_url)。
Sourcepub async fn execute_nostreaming(
self,
token: &str,
) -> Result<ChatCompletionResponse>
pub async fn execute_nostreaming( self, token: &str, ) -> Result<ChatCompletionResponse>
使用默认 base URL 执行无流式请求(最常用的便捷方法)。
Sourcepub async fn execute_client_streaming_baseurl(
self,
client: &Client,
base_url: &str,
token: &str,
) -> Result<impl Stream<Item = Result<ChatCompletionChunk, ApiError>>>
pub async fn execute_client_streaming_baseurl( self, client: &Client, base_url: &str, token: &str, ) -> Result<impl Stream<Item = Result<ChatCompletionChunk, ApiError>>>
执行流式(SSE)响应请求。使用 DEFAULT_API_BASE 构建 URL,并对流中可能出现的解析错误进行更加明确的映射。
返回一个 Stream,每个 Item 是 Result<ChatCompletionChunk, ApiError>。
Execute a streaming request (SSE) using a custom base_url.
返回一个 Stream,每个 Item 是 Result<ChatCompletionChunk, ApiError>。
Sourcepub async fn execute_client_streaming(
self,
client: &Client,
token: &str,
) -> Result<impl Stream<Item = Result<ChatCompletionChunk, ApiError>>>
pub async fn execute_client_streaming( self, client: &Client, token: &str, ) -> Result<impl Stream<Item = Result<ChatCompletionChunk, ApiError>>>
Execute a streaming request (SSE) using the default API base URL.
Convenience wrapper that delegates to execute_client_streaming_baseurl.
Sourcepub unsafe fn from_raw_unchecked(raw: ChatCompletionRequest) -> Self
pub unsafe fn from_raw_unchecked(raw: ChatCompletionRequest) -> Self
§Safety
该函数允许直接从原始请求数据创建一个 Request 对象,绕过了构建器的合法性检查。调用者必须确保提供的原始数据是合法且符合 API 要求的,否则可能导致请求失败或产生不可预期的行为。
Sourcepub unsafe fn get_raw_mut(&mut self) -> &mut ChatCompletionRequest
pub unsafe fn get_raw_mut(&mut self) -> &mut ChatCompletionRequest
§Safety
该函数返回对原始请求数据的可变引用,允许直接修改请求的各个字段。调用者必须确保在修改过程中保持请求数据的合法性和一致性,以避免产生无效的请求或引发错误。