openai_req/edit/
mod.rs

1use serde::{Serialize,Deserialize};
2use crate::{JsonRequest, Usage};
3use async_trait::async_trait;
4
5
6///text edit request as defined by https://platform.openai.com/docs/api-reference/edits
7///
8/// # Usage example
9///```
10///    use openai_req::edit::EditRequest;
11///    use openai_req::JsonRequest;
12///
13///    let instruction = "correct spelling";
14///    let text = "quick blck fox jupms over lazy dog";
15///    let request = EditRequest::new_text(instruction).set_input(text);
16///    let response = request.run(&client).await?;
17/// ```
18///
19#[derive(Clone, Serialize, Deserialize, Debug)]
20pub struct EditRequest {
21    model: String,
22    #[serde(skip_serializing_if = "Option::is_none")]
23    input: Option<String>,
24    instruction: String,
25    #[serde(skip_serializing_if = "Option::is_none")]
26    n: Option<u16>,
27    #[serde(skip_serializing_if = "Option::is_none")]
28    temperature: Option<f32>,
29    #[serde(skip_serializing_if = "Option::is_none")]
30    top_p: Option<f32>,
31}
32
33#[async_trait(?Send)]
34impl JsonRequest<EditResponse> for EditRequest {
35    const ENDPOINT: &'static str = "/edits";
36}
37
38impl EditRequest {
39
40    pub fn new_text(instruction: &str) -> Self {
41        Self {
42            model: "text-davinci-edit-001".to_string(),
43            input: None,
44            instruction: instruction.to_string(),
45            n: None,
46            temperature: None,
47            top_p: None,
48        }
49    }
50
51    pub fn new_code(instruction: &str) -> Self {
52        Self {
53            model: "code-davinci-edit-001".to_string(),
54            input: None,
55            instruction: instruction.to_string(),
56            n: None,
57            temperature: None,
58            top_p: None,
59        }
60    }
61
62    pub fn with_model(model: &str, instruction: &str) -> Self {
63        Self {
64            model: model.to_string(),
65            input: None,
66            instruction: instruction.to_string(),
67            n: None,
68            temperature: None,
69            top_p: None,
70        }
71    }
72
73    pub fn set_input(mut self, input: &str) -> Self {
74        self.input = Some(input.to_string());
75        self
76    }
77
78    pub fn set_n(mut self, n: u16) -> Self {
79        self.n = Some(n);
80        self
81    }
82
83    pub fn set_temperature(mut self, temperature: f32) -> Self {
84        self.temperature = Some(temperature);
85        self
86    }
87
88    pub fn set_top_p(mut self, top_p: f32) -> Self {
89        self.top_p = Some(top_p);
90        self
91    }
92}
93
94#[derive(Clone, Serialize, Deserialize, Debug)]
95pub struct EditChoice {
96    pub text: String,
97    pub index: i64,
98}
99
100#[derive(Clone, Serialize, Deserialize, Debug)]
101pub struct EditResponse {
102    pub object: String,
103    pub created: i64,
104    pub choices: Vec<EditChoice>,
105    pub usage: Usage,
106}
107