pub struct RequestBody {Show 19 fields
pub messages: Vec<Message>,
pub model: String,
pub frequency_penalty: Option<f32>,
pub presence_penalty: Option<f32>,
pub max_tokens: Option<u32>,
pub response_format: Option<ResponseFormat>,
pub seed: Option<i64>,
pub n: Option<u32>,
pub stop: Option<StopKeywords>,
pub stream: bool,
pub stream_options: Option<StreamOptions>,
pub temperature: Option<f32>,
pub top_p: Option<f32>,
pub tools: Option<Vec<Tools>>,
pub tool_choice: Option<ToolChoice>,
pub logprobs: Option<bool>,
pub top_logprobs: Option<u32>,
pub extra_body: Option<ExtraBody>,
pub extra_body_map: Option<HashMap<String, String>>,
}
Fields§
§messages: Vec<Message>
§model: String
§frequency_penalty: Option<f32>
frequency_penalty
must be between -2.0 and 2.0
presence_penalty: Option<f32>
presence_penalty
must be between -2.0 and 2.0
max_tokens: Option<u32>
max_tokens
must be greater than 1
response_format: Option<ResponseFormat>
§seed: Option<i64>
§n: Option<u32>
The number of responses to generate.
stop: Option<StopKeywords>
stop keywords
stream: bool
Although it is optional, you should explicitly designate it for an expected response.
stream_options: Option<StreamOptions>
§temperature: Option<f32>
§top_p: Option<f32>
§tools: Option<Vec<Tools>>
§tool_choice: Option<ToolChoice>
§logprobs: Option<bool>
§top_logprobs: Option<u32>
§extra_body: Option<ExtraBody>
Other request bodies that are not in standard OpenAI API.
extra_body_map: Option<HashMap<String, String>>
Other request bodies that are not in standard OpenAI API and not included in the ExtraBody struct.
Implementations§
Source§impl RequestBody
impl RequestBody
pub async fn get_response(&self, url: &str, key: &str) -> Result<String>
Sourcepub async fn stream_response(
&self,
url: &str,
api_key: &str,
) -> Result<BoxStream<'static, Result<String, Error>>, Error>
pub async fn stream_response( &self, url: &str, api_key: &str, ) -> Result<BoxStream<'static, Result<String, Error>>, Error>
Getting stream response. You must ensure self.stream is true, or otherwise it will panic.
§Example
use std::sync::LazyLock;
use futures_util::StreamExt;
use openai_interface::chat::request::{Message, RequestBody};
const DEEPSEEK_API_KEY: LazyLock<&str> =
LazyLock::new(|| include_str!("../.././keys/deepseek_domestic_key").trim());
const DEEPSEEK_CHAT_URL: &'static str = "https://api.deepseek.com/chat/completions";
const DEEPSEEK_MODEL: &'static str = "deepseek-chat";
#[tokio::main]
async fn main() {
let request = RequestBody {
messages: vec![
Message::System {
content: "This is a request of test purpose. Reply briefly".to_string(),
name: None,
},
Message::User {
content: "What's your name?".to_string(),
name: None,
},
],
model: DEEPSEEK_MODEL.to_string(),
stream: true,
..Default::default()
};
let mut response = request
.stream_response(DEEPSEEK_CHAT_URL, *DEEPSEEK_API_KEY)
.await
.unwrap();
while let Some(chunk) = response.next().await {
println!("{}", chunk.unwrap());
}
}
Trait Implementations§
Source§impl Debug for RequestBody
impl Debug for RequestBody
Source§impl Default for RequestBody
impl Default for RequestBody
Source§impl<'de> Deserialize<'de> for RequestBody
impl<'de> Deserialize<'de> for RequestBody
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for RequestBody
impl RefUnwindSafe for RequestBody
impl Send for RequestBody
impl Sync for RequestBody
impl Unpin for RequestBody
impl UnwindSafe for RequestBody
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