Skip to content

Commit

Permalink
derive Deserialize anywhere Serialize is derived (#241)
Browse files Browse the repository at this point in the history
* derive Deserialize anywhere Serialize is derived

* fix some clippy lints
  • Loading branch information
kczimm committed Jul 9, 2024
1 parent 0885a26 commit d5c0c83
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 44 deletions.
8 changes: 2 additions & 6 deletions async-openai/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
VectorStores,
};

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
/// Client is a container for config, backoff and http_client
/// used to make API calls.
pub struct Client<C: Config> {
Expand All @@ -27,11 +27,7 @@ pub struct Client<C: Config> {
impl Client<OpenAIConfig> {
/// Client with default [OpenAIConfig]
pub fn new() -> Self {
Self {
http_client: reqwest::Client::new(),
config: OpenAIConfig::default(),
backoff: Default::default(),
}
Self::default()
}
}

Expand Down
12 changes: 6 additions & 6 deletions async-openai/src/types/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct AudioInput {
pub source: InputSource,
}

#[derive(Debug, Serialize, Default, Clone, Copy, PartialEq)]
#[derive(Debug, Serialize, Deserialize, Default, Clone, Copy, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum AudioResponseFormat {
#[default]
Expand All @@ -21,7 +21,7 @@ pub enum AudioResponseFormat {
Vtt,
}

#[derive(Debug, Serialize, Default, Clone, Copy, PartialEq)]
#[derive(Debug, Serialize, Deserialize, Default, Clone, Copy, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum SpeechResponseFormat {
#[default]
Expand All @@ -33,7 +33,7 @@ pub enum SpeechResponseFormat {
Wav,
}

#[derive(Debug, Default, Serialize, Clone, PartialEq)]
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum Voice {
Expand All @@ -46,7 +46,7 @@ pub enum Voice {
Shimmer,
}

#[derive(Debug, Default, Serialize, Clone, PartialEq)]
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
pub enum SpeechModel {
#[default]
#[serde(rename = "tts-1")]
Expand All @@ -57,7 +57,7 @@ pub enum SpeechModel {
Other(String),
}

#[derive(Debug, Default, Serialize, Clone, PartialEq)]
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum TimestampGranularity {
Word,
Expand Down Expand Up @@ -172,7 +172,7 @@ pub struct TranscriptionSegment {
pub no_speech_prob: f32,
}

#[derive(Clone, Default, Debug, Builder, PartialEq, Serialize)]
#[derive(Clone, Default, Debug, Builder, PartialEq, Serialize, Deserialize)]
#[builder(name = "CreateSpeechRequestArgs")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
Expand Down
4 changes: 2 additions & 2 deletions async-openai/src/types/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};

use crate::error::OpenAIError;

#[derive(Debug, Serialize, Default, Clone, Builder, PartialEq)]
#[derive(Debug, Serialize, Default, Clone, Builder, PartialEq, Deserialize)]
#[builder(name = "BatchRequestArgs")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
Expand Down Expand Up @@ -40,7 +40,7 @@ pub enum BatchEndpoint {
V1Completions,
}

#[derive(Debug, Clone, PartialEq, Serialize, Default)]
#[derive(Debug, Clone, PartialEq, Serialize, Default, Deserialize)]
pub enum BatchCompletionWindow {
#[default]
#[serde(rename = "24h")]
Expand Down
2 changes: 1 addition & 1 deletion async-openai/src/types/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::error::OpenAIError;

use super::{ChatCompletionStreamOptions, Choice, CompletionUsage, Prompt, Stop};

#[derive(Clone, Serialize, Default, Debug, Builder, PartialEq)]
#[derive(Clone, Serialize, Deserialize, Default, Debug, Builder, PartialEq)]
#[builder(name = "CreateCompletionRequestArgs")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
Expand Down
6 changes: 3 additions & 3 deletions async-openai/src/types/embedding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};

use crate::error::OpenAIError;

#[derive(Debug, Serialize, Clone, PartialEq)]
#[derive(Debug, Serialize, Clone, PartialEq, Deserialize)]
#[serde(untagged)]
pub enum EmbeddingInput {
String(String),
Expand All @@ -14,15 +14,15 @@ pub enum EmbeddingInput {
ArrayOfIntegerArray(Vec<Vec<u32>>),
}

#[derive(Debug, Serialize, Default, Clone, PartialEq)]
#[derive(Debug, Serialize, Default, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum EncodingFormat {
#[default]
Float,
Base64,
}

#[derive(Debug, Serialize, Default, Clone, Builder, PartialEq)]
#[derive(Debug, Serialize, Default, Clone, Builder, PartialEq, Deserialize)]
#[builder(name = "CreateEmbeddingRequestArgs")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
Expand Down
2 changes: 1 addition & 1 deletion async-openai/src/types/fine_tuning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct Hyperparameters {
pub n_epochs: NEpochs,
}

#[derive(Debug, Serialize, Clone, Default, Builder, PartialEq)]
#[derive(Debug, Serialize, Deserialize, Clone, Default, Builder, PartialEq)]
#[builder(name = "CreateFineTuningJobRequestArgs")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
Expand Down
14 changes: 7 additions & 7 deletions async-openai/src/types/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::error::OpenAIError;

use super::InputSource;

#[derive(Default, Debug, Serialize, Clone, Copy, PartialEq)]
#[derive(Default, Debug, Serialize, Deserialize, Clone, Copy, PartialEq)]
pub enum ImageSize {
#[serde(rename = "256x256")]
S256x256,
Expand All @@ -20,7 +20,7 @@ pub enum ImageSize {
S1024x1792,
}

#[derive(Default, Debug, Serialize, Clone, Copy, PartialEq)]
#[derive(Default, Debug, Serialize, Deserialize, Clone, Copy, PartialEq)]
pub enum DallE2ImageSize {
#[serde(rename = "256x256")]
S256x256,
Expand All @@ -31,7 +31,7 @@ pub enum DallE2ImageSize {
S1024x1024,
}

#[derive(Debug, Serialize, Default, Clone, Copy, PartialEq)]
#[derive(Debug, Serialize, Deserialize, Default, Clone, Copy, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum ResponseFormat {
#[default]
Expand All @@ -40,7 +40,7 @@ pub enum ResponseFormat {
B64Json,
}

#[derive(Debug, Serialize, Default, Clone, PartialEq)]
#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
pub enum ImageModel {
#[default]
#[serde(rename = "dall-e-2")]
Expand All @@ -51,23 +51,23 @@ pub enum ImageModel {
Other(String),
}

#[derive(Debug, Serialize, Default, Clone, PartialEq)]
#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum ImageQuality {
#[default]
Standard,
HD,
}

#[derive(Debug, Serialize, Default, Clone, PartialEq)]
#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum ImageStyle {
#[default]
Vivid,
Natural,
}

#[derive(Debug, Clone, Serialize, Default, Builder, PartialEq)]
#[derive(Debug, Clone, Serialize, Deserialize, Default, Builder, PartialEq)]
#[builder(name = "CreateImageRequestArgs")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
Expand Down
6 changes: 3 additions & 3 deletions async-openai/src/types/moderation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use serde::{Deserialize, Serialize};

use crate::error::OpenAIError;

#[derive(Debug, Serialize, Clone, PartialEq)]
#[derive(Debug, Serialize, Clone, PartialEq, Deserialize)]
#[serde(untagged)]
pub enum ModerationInput {
String(String),
StringArray(Vec<String>),
}

#[derive(Debug, Serialize, Default, Clone, Copy, PartialEq)]
#[derive(Debug, Serialize, Default, Clone, Copy, PartialEq, Deserialize)]
pub enum TextModerationModel {
#[default]
#[serde(rename = "text-moderation-latest")]
Expand All @@ -19,7 +19,7 @@ pub enum TextModerationModel {
Stable,
}

#[derive(Debug, Default, Clone, Serialize, Builder, PartialEq)]
#[derive(Debug, Default, Clone, Serialize, Builder, PartialEq, Deserialize)]
#[builder(name = "CreateModerationRequestArgs")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
Expand Down
8 changes: 4 additions & 4 deletions async-openai/src/types/vector_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::error::OpenAIError;

use super::StaticChunkingStrategy;

#[derive(Debug, Serialize, Default, Clone, Builder, PartialEq)]
#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
#[builder(name = "CreateVectorStoreRequestArgs")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
Expand Down Expand Up @@ -117,7 +117,7 @@ pub struct DeleteVectorStoreResponse {
pub deleted: bool,
}

#[derive(Debug, Serialize, Default, Clone, Builder, PartialEq)]
#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
#[builder(name = "UpdateVectorStoreRequestArgs")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
Expand Down Expand Up @@ -197,7 +197,7 @@ pub enum VectorStoreFileObjectChunkingStrategy {
},
}

#[derive(Debug, Serialize, Default, Clone, Builder, PartialEq)]
#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
#[builder(name = "CreateVectorStoreFileRequestArgs")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
Expand All @@ -216,7 +216,7 @@ pub struct DeleteVectorStoreFileResponse {
pub deleted: bool,
}

#[derive(Debug, Serialize, Default, Clone, Builder, PartialEq)]
#[derive(Debug, Serialize, Default, Clone, Builder, PartialEq, Deserialize)]
#[builder(name = "CreateVectorStoreFileBatchRequestArgs")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
Expand Down
5 changes: 1 addition & 4 deletions async-openai/src/vector_store_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ impl<'c, C: Config> VectorStoreFiles<'c, C> {

#[cfg(test)]
mod tests {
use crate::types::{
CreateFileRequest, CreateVectorStoreFileRequest, CreateVectorStoreRequest, FileInput,
FilePurpose,
};
use crate::types::{CreateFileRequest, CreateVectorStoreRequest, FileInput, FilePurpose};
use crate::Client;

#[tokio::test]
Expand Down
4 changes: 2 additions & 2 deletions examples/function-call-stream/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
if let Some(fn_call) = &chat_choice.delta.function_call {
writeln!(lock, "function_call: {:?}", fn_call).unwrap();
if let Some(name) = &fn_call.name {
fn_name = name.clone();
fn_name.clone_from(name);
}
if let Some(args) = &fn_call.arguments {
fn_args.push_str(args);
Expand Down Expand Up @@ -134,7 +134,7 @@ async fn call_fn(
}
stdout().flush()?;
}
println!("{}", "\n");
println!("\n");
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion examples/function-call/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
.create(request)
.await?
.choices
.get(0)
.first()
.unwrap()
.message
.clone();
Expand Down
4 changes: 2 additions & 2 deletions examples/tool-call-stream/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Mutex<Vec<(ChatCompletionMessageToolCall, Value)>>,
> = Arc::new(Mutex::new(Vec::new()));
if let Some(tool_calls) = chat_choice.delta.tool_calls {
for (_i, tool_call_chunk) in tool_calls.into_iter().enumerate() {
for tool_call_chunk in tool_calls.into_iter() {
let key = (chat_choice.index as i32, tool_call_chunk.index);
let states = tool_call_states.clone();
let tool_call_data = tool_call_chunk.clone();
Expand Down Expand Up @@ -228,7 +228,7 @@ async fn call_fn(name: &str, args: &str) -> Result<Value, Box<dyn std::error::Er
let unit = function_args["unit"].as_str().unwrap_or("fahrenheit");
let function = available_functions.get(name).unwrap();
let function_response = function(location, unit);
return Ok(function_response);
Ok(function_response)
}

fn get_current_weather(location: &str, unit: &str) -> serde_json::Value {
Expand Down
4 changes: 2 additions & 2 deletions examples/tool-call/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.create(request)
.await?
.choices
.get(0)
.first()
.unwrap()
.message
.clone();
Expand Down Expand Up @@ -151,7 +151,7 @@ async fn call_fn(name: &str, args: &str) -> Result<Value, Box<dyn std::error::Er
let unit = function_args["unit"].as_str().unwrap_or("fahrenheit");
let function = available_functions.get(name).unwrap();
let function_response = function(location, unit);
return Ok(function_response);
Ok(function_response)
}

fn get_current_weather(location: &str, unit: &str) -> serde_json::Value {
Expand Down

0 comments on commit d5c0c83

Please sign in to comment.