use derive_builder::Builder;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use crate::{
api_resources::{Choices, TokenUsage},
Client, Result,
};
#[skip_serializing_none]
#[derive(Builder, Debug, Default, Deserialize, Serialize)]
#[builder(default, setter(into, strip_option))]
pub struct EditParam {
model: String,
instruction: String,
input: Option<String>,
n: Option<u32>,
temperature: Option<f32>,
top_p: Option<f32>,
}
impl EditParamBuilder {
pub fn new(model: impl Into<String>, instruction: impl Into<String>) -> Self {
Self {
model: Some(model.into()),
instruction: Some(instruction.into()),
..Self::default()
}
}
}
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct Edit {
pub object: String,
pub created: u64,
pub choices: Vec<Choices>,
pub usage: Option<TokenUsage>,
}
pub async fn create(client: &Client, param: &EditParam) -> Result<Edit> {
client.create_edit(param).await
}
impl Client {
async fn create_edit(&self, param: &EditParam) -> Result<Edit> {
self.post::<EditParam, Edit>("edits", Some(param)).await
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_edit() {
let param: EditParam = serde_json::from_str(
r#"
{
"model": "text-davinci-edit-001",
"input": "What day of the wek is it?",
"instruction": "Fix the spelling mistakes"
}
"#,
)
.unwrap();
let resp: Edit = serde_json::from_str(
r#"
{
"object": "edit",
"created": 1589478378,
"choices": [
{
"text": "What day of the week is it?",
"index": 0
}
],
"usage": {
"prompt_tokens": 25,
"completion_tokens": 32,
"total_tokens": 57
}
}
"#,
)
.unwrap();
assert_eq!(param.model, "text-davinci-edit-001");
assert_eq!(param.n, None);
assert_eq!(resp.object, "edit");
assert_eq!(resp.choices.len(), 1);
}
}