Skip to main content

Request

Struct Request 

Source
pub struct Request { /* private fields */ }
Expand description

一个发送至 Deepseek API 的请求对象,封装了原始请求数据。 该结构体保证请求合法

Implementations§

Source§

impl Request

Source

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?")
]);
Source

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?")
]);
Source

pub fn builder() -> Self

Source

pub fn add_message(self, message: Message) -> Self

Source

pub fn messages(self, messages: Vec<Message>) -> Self

Source

pub fn model(self, model: Model) -> Self

Source

pub fn response_format_type( self, response_format_type: ResponseFormatType, ) -> Self

Source

pub fn json(self) -> Self

Source

pub fn text(self) -> Self

Source

pub fn frequency_penalty(self, penalty: f32) -> Self

Possible values: >= -2 and <= 2 Default value: 0 介于 -2.0 和 2.0 之间的数字。如果该值为正,那么新 token 会根据其在已有文本中的出现频率受到相应的惩罚,降低模型重复相同内容的可能性。

Source

pub fn presence_penalty(self, penalty: f32) -> Self

Possible values: >= -2 and <= 2 Default value: 0 介于 -2.0 和 2.0 之间的数字。如果该值为正,那么新 token 会根据其是否已在已有文本中出现受到相应的惩罚,从而增加模型谈论新主题的可能性。

Source

pub fn max_tokens(self, max_tokens: u32) -> Self

限制一次请求中模型生成 completion 的最大 token 数。输入 token 和输出 token 的总长度受模型的上下文长度的限制。取值范围与默认值详见文档。

Source

pub fn temperature(self, temperature: f32) -> Self

Possible values: <= 2 Default value: 1 采样温度,介于 0 和 2 之间。更高的值,如 0.8,会使输出更随机,而更低的值,如 0.2,会使其更加集中和确定。 我们通常建议可以更改这个值或者更改 top_p,但不建议同时对两者进行修改。

Source

pub fn stop_vec(self, stop: Vec<String>) -> Self

Source

pub fn stop_str(self, stop: String) -> Self

Source

pub fn top_p(self, top_p: f32) -> Self

Possible values: <= 1 Default value: 1 作为调节采样温度的替代方案,模型会考虑前 top_p 概率的 token 的结果。所以 0.1 就意味着只有包括在最高 10% 概率中的 token 会被考虑。 我们通常建议修改这个值或者更改 temperature,但不建议同时对两者进行修改。

Source

pub fn add_tool(self, tool: Tool) -> Self

Source

pub fn tool_choice_type(self, tool_choice: ToolChoiceType) -> Self

Source

pub fn tool_choice_object(self, tool_choice: ToolChoiceObject) -> Self

Source

pub fn logprobs(self, top_logprobs: u32) -> Self

top_logprobs: 一个介于 0 到 20 之间的整数 N,指定每个输出位置返回输出概率 top N 的 token,且返回这些 token 的对数概率

Source

pub fn raw(&self) -> &ChatCompletionRequest

Source

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 所有权的要求。

Source

pub async fn execute_client_nostreaming( self, client: &Client, token: &str, ) -> Result<ChatCompletionResponse>

便捷方法:使用默认 base URL 执行无流式请求(接收不可变的 &Client)。

Source

pub async fn execute_baseurl_nostreaming( self, base_url: &str, token: &str, ) -> Result<ChatCompletionResponse>

在没有外部 Client 的情况下执行无流式请求(使用传入的 base_url)。

Source

pub async fn execute_nostreaming( self, token: &str, ) -> Result<ChatCompletionResponse>

使用默认 base URL 执行无流式请求(最常用的便捷方法)。

Source

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>

Source

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.

Source

pub unsafe fn from_raw_unchecked(raw: ChatCompletionRequest) -> Self

§Safety

该函数允许直接从原始请求数据创建一个 Request 对象,绕过了构建器的合法性检查。调用者必须确保提供的原始数据是合法且符合 API 要求的,否则可能导致请求失败或产生不可预期的行为。

Source

pub unsafe fn get_raw_mut(&mut self) -> &mut ChatCompletionRequest

§Safety

该函数返回对原始请求数据的可变引用,允许直接修改请求的各个字段。调用者必须确保在修改过程中保持请求数据的合法性和一致性,以避免产生无效的请求或引发错误。

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