pub struct ChatCompletionRequest {Show 17 fields
pub model: String,
pub messages: Vec<ChatCompletionMessage>,
pub temperature: Option<f64>,
pub top_p: Option<f64>,
pub n: Option<i64>,
pub response_format: Option<Value>,
pub stream: Option<bool>,
pub stop: Option<Vec<String>>,
pub max_tokens: Option<i64>,
pub presence_penalty: Option<f64>,
pub frequency_penalty: Option<f64>,
pub logit_bias: Option<HashMap<String, i32>>,
pub user: Option<String>,
pub seed: Option<i64>,
pub tools: Option<Vec<Tool>>,
pub parallel_tool_calls: Option<bool>,
pub tool_choice: Option<ToolChoiceType>,
}
Fields§
§model: String
§messages: Vec<ChatCompletionMessage>
§temperature: Option<f64>
§top_p: Option<f64>
§n: Option<i64>
§response_format: Option<Value>
§stream: Option<bool>
§stop: Option<Vec<String>>
§max_tokens: Option<i64>
§presence_penalty: Option<f64>
§frequency_penalty: Option<f64>
§logit_bias: Option<HashMap<String, i32>>
§user: Option<String>
§seed: Option<i64>
§tools: Option<Vec<Tool>>
§parallel_tool_calls: Option<bool>
§tool_choice: Option<ToolChoiceType>
Implementations§
Source§impl ChatCompletionRequest
impl ChatCompletionRequest
Sourcepub fn new(model: String, messages: Vec<ChatCompletionMessage>) -> Self
pub fn new(model: String, messages: Vec<ChatCompletionMessage>) -> Self
Examples found in repository?
examples/chat_completion.rs (lines 10-19)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let req = ChatCompletionRequest::new(
GPT4_O_MINI.to_string(),
vec![chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: chat_completion::Content::Text(String::from("What is bitcoin?")),
name: None,
tool_calls: None,
tool_call_id: None,
}],
);
let result = client.chat_completion(req).await?;
println!("Content: {:?}", result.choices[0].message.content);
println!("Response Headers: {:?}", result.headers);
Ok(())
}
More examples
examples/vision.rs (lines 10-34)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let req = ChatCompletionRequest::new(
GPT4_O.to_string(),
vec![chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: chat_completion::Content::ImageUrl(vec![
chat_completion::ImageUrl {
r#type: chat_completion::ContentType::text,
text: Some(String::from("What's in this image?")),
image_url: None,
},
chat_completion::ImageUrl {
r#type: chat_completion::ContentType::image_url,
text: None,
image_url: Some(chat_completion::ImageUrlType {
url: String::from(
"https://upload.wikimedia.org/wikipedia/commons/5/50/Bitcoin.png",
),
}),
},
]),
name: None,
tool_calls: None,
tool_call_id: None,
}],
);
let result = client.chat_completion(req).await?;
println!("{:?}", result.choices[0].message.content);
Ok(())
}
examples/function_call.rs (lines 32-41)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let mut properties = HashMap::new();
properties.insert(
"coin".to_string(),
Box::new(types::JSONSchemaDefine {
schema_type: Some(types::JSONSchemaType::String),
description: Some("The cryptocurrency to get the price of".to_string()),
..Default::default()
}),
);
let req = ChatCompletionRequest::new(
GPT4_O.to_string(),
vec![chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: chat_completion::Content::Text(String::from("What is the price of Ethereum?")),
name: None,
tool_calls: None,
tool_call_id: None,
}],
)
.tools(vec![chat_completion::Tool {
r#type: chat_completion::ToolType::Function,
function: types::Function {
name: String::from("get_coin_price"),
description: Some(String::from("Get the price of a cryptocurrency")),
parameters: types::FunctionParameters {
schema_type: types::JSONSchemaType::Object,
properties: Some(properties),
required: Some(vec![String::from("coin")]),
},
},
}])
.tool_choice(chat_completion::ToolChoiceType::Auto);
// debug request json
// let serialized = serde_json::to_string(&req).unwrap();
// println!("{}", serialized);
let result = client.chat_completion(req).await?;
match result.choices[0].finish_reason {
None => {
println!("No finish_reason");
println!("{:?}", result.choices[0].message.content);
}
Some(chat_completion::FinishReason::stop) => {
println!("Stop");
println!("{:?}", result.choices[0].message.content);
}
Some(chat_completion::FinishReason::length) => {
println!("Length");
}
Some(chat_completion::FinishReason::tool_calls) => {
println!("ToolCalls");
#[derive(Deserialize, Serialize)]
struct Currency {
coin: String,
}
let tool_calls = result.choices[0].message.tool_calls.as_ref().unwrap();
for tool_call in tool_calls {
let name = tool_call.function.name.clone().unwrap();
let arguments = tool_call.function.arguments.clone().unwrap();
let c: Currency = serde_json::from_str(&arguments)?;
let coin = c.coin;
if name == "get_coin_price" {
let price = get_coin_price(&coin);
println!("{} price: {}", coin, price);
}
}
}
Some(chat_completion::FinishReason::content_filter) => {
println!("ContentFilter");
}
Some(chat_completion::FinishReason::null) => {
println!("Null");
}
}
Ok(())
}
examples/function_call_role.rs (lines 32-41)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let mut properties = HashMap::new();
properties.insert(
"coin".to_string(),
Box::new(types::JSONSchemaDefine {
schema_type: Some(types::JSONSchemaType::String),
description: Some("The cryptocurrency to get the price of".to_string()),
..Default::default()
}),
);
let req = ChatCompletionRequest::new(
GPT4_O.to_string(),
vec![chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: chat_completion::Content::Text(String::from("What is the price of Ethereum?")),
name: None,
tool_calls: None,
tool_call_id: None,
}],
)
.tools(vec![chat_completion::Tool {
r#type: chat_completion::ToolType::Function,
function: types::Function {
name: String::from("get_coin_price"),
description: Some(String::from("Get the price of a cryptocurrency")),
parameters: types::FunctionParameters {
schema_type: types::JSONSchemaType::Object,
properties: Some(properties),
required: Some(vec![String::from("coin")]),
},
},
}]);
let result = client.chat_completion(req).await?;
match result.choices[0].finish_reason {
None => {
println!("No finish_reason");
println!("{:?}", result.choices[0].message.content);
}
Some(chat_completion::FinishReason::stop) => {
println!("Stop");
println!("{:?}", result.choices[0].message.content);
}
Some(chat_completion::FinishReason::length) => {
println!("Length");
}
Some(chat_completion::FinishReason::tool_calls) => {
println!("ToolCalls");
#[derive(Deserialize, Serialize)]
struct Currency {
coin: String,
}
let tool_calls = result.choices[0].message.tool_calls.as_ref().unwrap();
for tool_call in tool_calls {
let function_call = &tool_call.function;
let arguments = function_call.arguments.clone().unwrap();
let c: Currency = serde_json::from_str(&arguments)?;
let coin = c.coin;
println!("coin: {}", coin);
let price = get_coin_price(&coin);
println!("price: {}", price);
let req = ChatCompletionRequest::new(
GPT4_O.to_string(),
vec![
chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: chat_completion::Content::Text(String::from(
"What is the price of Ethereum?",
)),
name: None,
tool_calls: None,
tool_call_id: None,
},
chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::function,
content: chat_completion::Content::Text({
let price = get_coin_price(&coin);
format!("{{\"price\": {}}}", price)
}),
name: Some(String::from("get_coin_price")),
tool_calls: None,
tool_call_id: None,
},
],
);
let result = client.chat_completion(req).await?;
println!("{:?}", result.choices[0].message.content);
}
}
Some(chat_completion::FinishReason::content_filter) => {
println!("ContentFilter");
}
Some(chat_completion::FinishReason::null) => {
println!("Null");
}
}
Ok(())
}
Source§impl ChatCompletionRequest
impl ChatCompletionRequest
pub fn temperature(self, temperature: f64) -> Self
pub fn top_p(self, top_p: f64) -> Self
pub fn n(self, n: i64) -> Self
pub fn response_format(self, response_format: Value) -> Self
pub fn stream(self, stream: bool) -> Self
pub fn stop(self, stop: Vec<String>) -> Self
pub fn max_tokens(self, max_tokens: i64) -> Self
pub fn presence_penalty(self, presence_penalty: f64) -> Self
pub fn frequency_penalty(self, frequency_penalty: f64) -> Self
pub fn logit_bias(self, logit_bias: HashMap<String, i32>) -> Self
pub fn user(self, user: String) -> Self
pub fn seed(self, seed: i64) -> Self
Sourcepub fn tools(self, tools: Vec<Tool>) -> Self
pub fn tools(self, tools: Vec<Tool>) -> Self
Examples found in repository?
examples/function_call.rs (lines 42-53)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let mut properties = HashMap::new();
properties.insert(
"coin".to_string(),
Box::new(types::JSONSchemaDefine {
schema_type: Some(types::JSONSchemaType::String),
description: Some("The cryptocurrency to get the price of".to_string()),
..Default::default()
}),
);
let req = ChatCompletionRequest::new(
GPT4_O.to_string(),
vec![chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: chat_completion::Content::Text(String::from("What is the price of Ethereum?")),
name: None,
tool_calls: None,
tool_call_id: None,
}],
)
.tools(vec![chat_completion::Tool {
r#type: chat_completion::ToolType::Function,
function: types::Function {
name: String::from("get_coin_price"),
description: Some(String::from("Get the price of a cryptocurrency")),
parameters: types::FunctionParameters {
schema_type: types::JSONSchemaType::Object,
properties: Some(properties),
required: Some(vec![String::from("coin")]),
},
},
}])
.tool_choice(chat_completion::ToolChoiceType::Auto);
// debug request json
// let serialized = serde_json::to_string(&req).unwrap();
// println!("{}", serialized);
let result = client.chat_completion(req).await?;
match result.choices[0].finish_reason {
None => {
println!("No finish_reason");
println!("{:?}", result.choices[0].message.content);
}
Some(chat_completion::FinishReason::stop) => {
println!("Stop");
println!("{:?}", result.choices[0].message.content);
}
Some(chat_completion::FinishReason::length) => {
println!("Length");
}
Some(chat_completion::FinishReason::tool_calls) => {
println!("ToolCalls");
#[derive(Deserialize, Serialize)]
struct Currency {
coin: String,
}
let tool_calls = result.choices[0].message.tool_calls.as_ref().unwrap();
for tool_call in tool_calls {
let name = tool_call.function.name.clone().unwrap();
let arguments = tool_call.function.arguments.clone().unwrap();
let c: Currency = serde_json::from_str(&arguments)?;
let coin = c.coin;
if name == "get_coin_price" {
let price = get_coin_price(&coin);
println!("{} price: {}", coin, price);
}
}
}
Some(chat_completion::FinishReason::content_filter) => {
println!("ContentFilter");
}
Some(chat_completion::FinishReason::null) => {
println!("Null");
}
}
Ok(())
}
More examples
examples/function_call_role.rs (lines 42-53)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let mut properties = HashMap::new();
properties.insert(
"coin".to_string(),
Box::new(types::JSONSchemaDefine {
schema_type: Some(types::JSONSchemaType::String),
description: Some("The cryptocurrency to get the price of".to_string()),
..Default::default()
}),
);
let req = ChatCompletionRequest::new(
GPT4_O.to_string(),
vec![chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: chat_completion::Content::Text(String::from("What is the price of Ethereum?")),
name: None,
tool_calls: None,
tool_call_id: None,
}],
)
.tools(vec![chat_completion::Tool {
r#type: chat_completion::ToolType::Function,
function: types::Function {
name: String::from("get_coin_price"),
description: Some(String::from("Get the price of a cryptocurrency")),
parameters: types::FunctionParameters {
schema_type: types::JSONSchemaType::Object,
properties: Some(properties),
required: Some(vec![String::from("coin")]),
},
},
}]);
let result = client.chat_completion(req).await?;
match result.choices[0].finish_reason {
None => {
println!("No finish_reason");
println!("{:?}", result.choices[0].message.content);
}
Some(chat_completion::FinishReason::stop) => {
println!("Stop");
println!("{:?}", result.choices[0].message.content);
}
Some(chat_completion::FinishReason::length) => {
println!("Length");
}
Some(chat_completion::FinishReason::tool_calls) => {
println!("ToolCalls");
#[derive(Deserialize, Serialize)]
struct Currency {
coin: String,
}
let tool_calls = result.choices[0].message.tool_calls.as_ref().unwrap();
for tool_call in tool_calls {
let function_call = &tool_call.function;
let arguments = function_call.arguments.clone().unwrap();
let c: Currency = serde_json::from_str(&arguments)?;
let coin = c.coin;
println!("coin: {}", coin);
let price = get_coin_price(&coin);
println!("price: {}", price);
let req = ChatCompletionRequest::new(
GPT4_O.to_string(),
vec![
chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: chat_completion::Content::Text(String::from(
"What is the price of Ethereum?",
)),
name: None,
tool_calls: None,
tool_call_id: None,
},
chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::function,
content: chat_completion::Content::Text({
let price = get_coin_price(&coin);
format!("{{\"price\": {}}}", price)
}),
name: Some(String::from("get_coin_price")),
tool_calls: None,
tool_call_id: None,
},
],
);
let result = client.chat_completion(req).await?;
println!("{:?}", result.choices[0].message.content);
}
}
Some(chat_completion::FinishReason::content_filter) => {
println!("ContentFilter");
}
Some(chat_completion::FinishReason::null) => {
println!("Null");
}
}
Ok(())
}
pub fn parallel_tool_calls(self, parallel_tool_calls: bool) -> Self
Sourcepub fn tool_choice(self, tool_choice: ToolChoiceType) -> Self
pub fn tool_choice(self, tool_choice: ToolChoiceType) -> Self
Examples found in repository?
examples/function_call.rs (line 54)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = OpenAIClient::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let mut properties = HashMap::new();
properties.insert(
"coin".to_string(),
Box::new(types::JSONSchemaDefine {
schema_type: Some(types::JSONSchemaType::String),
description: Some("The cryptocurrency to get the price of".to_string()),
..Default::default()
}),
);
let req = ChatCompletionRequest::new(
GPT4_O.to_string(),
vec![chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: chat_completion::Content::Text(String::from("What is the price of Ethereum?")),
name: None,
tool_calls: None,
tool_call_id: None,
}],
)
.tools(vec![chat_completion::Tool {
r#type: chat_completion::ToolType::Function,
function: types::Function {
name: String::from("get_coin_price"),
description: Some(String::from("Get the price of a cryptocurrency")),
parameters: types::FunctionParameters {
schema_type: types::JSONSchemaType::Object,
properties: Some(properties),
required: Some(vec![String::from("coin")]),
},
},
}])
.tool_choice(chat_completion::ToolChoiceType::Auto);
// debug request json
// let serialized = serde_json::to_string(&req).unwrap();
// println!("{}", serialized);
let result = client.chat_completion(req).await?;
match result.choices[0].finish_reason {
None => {
println!("No finish_reason");
println!("{:?}", result.choices[0].message.content);
}
Some(chat_completion::FinishReason::stop) => {
println!("Stop");
println!("{:?}", result.choices[0].message.content);
}
Some(chat_completion::FinishReason::length) => {
println!("Length");
}
Some(chat_completion::FinishReason::tool_calls) => {
println!("ToolCalls");
#[derive(Deserialize, Serialize)]
struct Currency {
coin: String,
}
let tool_calls = result.choices[0].message.tool_calls.as_ref().unwrap();
for tool_call in tool_calls {
let name = tool_call.function.name.clone().unwrap();
let arguments = tool_call.function.arguments.clone().unwrap();
let c: Currency = serde_json::from_str(&arguments)?;
let coin = c.coin;
if name == "get_coin_price" {
let price = get_coin_price(&coin);
println!("{} price: {}", coin, price);
}
}
}
Some(chat_completion::FinishReason::content_filter) => {
println!("ContentFilter");
}
Some(chat_completion::FinishReason::null) => {
println!("Null");
}
}
Ok(())
}
Trait Implementations§
Source§impl Clone for ChatCompletionRequest
impl Clone for ChatCompletionRequest
Source§fn clone(&self) -> ChatCompletionRequest
fn clone(&self) -> ChatCompletionRequest
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for ChatCompletionRequest
impl Debug for ChatCompletionRequest
Source§impl<'de> Deserialize<'de> for ChatCompletionRequest
impl<'de> Deserialize<'de> for ChatCompletionRequest
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 ChatCompletionRequest
impl RefUnwindSafe for ChatCompletionRequest
impl Send for ChatCompletionRequest
impl Sync for ChatCompletionRequest
impl Unpin for ChatCompletionRequest
impl UnwindSafe for ChatCompletionRequest
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more