rust_chatgpt/v1/
images.rs1use crate::error::ChatGptError;
2use crate::v1::{convert_form, convert_from_value, trim_value, ChatGptRequest};
3use reqwest::multipart::Form;
4use serde::{Deserialize, Serialize};
5use serde_json::{json, Value};
6
7#[derive(Debug, Deserialize, Serialize, Clone)]
8pub struct ChatGptResponseImagesGenerations {
9 pub(crate) value: Value,
10}
11
12impl ChatGptResponseImagesGenerations {
13 pub fn get_urls(&self) -> Vec<String> {
14 let mut urls = vec![];
15 if let Some(value) = self.value.get("data") {
16 if let Some(rows) = value.as_array() {
17 for row in rows {
18 if let Some(url) = row.get("url") {
19 urls.push(url.to_string());
20 }
21 }
22 }
23 }
24 urls
25 }
26 pub fn b64_jsons(&self) -> Vec<String> {
27 let mut urls = vec![];
28 if let Some(value) = self.value.get("data") {
29 if let Some(rows) = value.as_array() {
30 for row in rows {
31 if let Some(url) = row.get("b64_json") {
32 urls.push(url.to_string());
33 }
34 }
35 }
36 }
37 urls
38 }
39}
40
41#[derive(Debug, Deserialize, Serialize, Clone)]
42pub struct ChatGptRequestImagesGenerations {
43 prompt: String,
44 n: Option<isize>,
45 size: Option<String>,
46 response_format: String,
47 user: Option<String>,
48}
49
50impl ChatGptRequest for ChatGptRequestImagesGenerations {
51 fn from_value(value: Value) -> Result<Self, ChatGptError>
52 where
53 Self: Sized,
54 {
55 convert_from_value(value)
56 }
57
58 fn to_value(&self) -> Value {
59 trim_value(json!(self)).unwrap()
60 }
61}
62
63impl ChatGptRequestImagesGenerations {
64 pub fn new(prompt: &str, n: isize) -> Self {
65 Self {
66 prompt: prompt.to_string(),
67 n: Some(n),
68 size: None,
69 response_format: "url".to_string(),
70 user: None,
71 }
72 }
73 pub fn set_response_format(&mut self, response_format: &str) -> &mut Self {
74 self.response_format = response_format.to_string();
75 self
76 }
77}
78
79#[derive(Debug, Deserialize, Serialize, Clone)]
80pub struct ChatGptResponseImagesEdits {
81 pub(crate) value: Value,
82}
83
84#[derive(Debug, Deserialize, Serialize, Clone)]
85pub struct ChatGptRequestImagesEdits {
86 image: String,
87 mask: Option<String>,
88 prompt: String,
89 n: Option<f32>,
90 size: Option<String>,
91 response_format: Option<String>,
92 user: Option<String>,
93}
94
95impl ChatGptRequestImagesEdits {
96 pub fn new(image: &str, prompt: &str) -> ChatGptRequestImagesEdits {
97 ChatGptRequestImagesEdits {
98 image: image.to_string(),
99 mask: None,
100 prompt: prompt.to_string(),
101 n: None,
102 size: None,
103 response_format: None,
104 user: None,
105 }
106 }
107}
108
109impl ChatGptRequest for ChatGptRequestImagesEdits {
110 fn from_value(value: Value) -> Result<Self, ChatGptError>
111 where
112 Self: Sized,
113 {
114 convert_from_value(value)
115 }
116
117 fn to_value(&self) -> Value {
118 trim_value(json!(self)).unwrap()
119 }
120}
121
122impl From<ChatGptRequestImagesEdits> for Form {
123 fn from(val: ChatGptRequestImagesEdits) -> Self {
124 convert_form(val.to_value(), vec!["image".to_string()])
125 }
126}
127
128#[derive(Debug, Deserialize, Serialize, Clone)]
129pub struct ChatGptResponseImagesVariation {
130 pub(crate) value: Value,
131}
132
133#[derive(Debug, Deserialize, Serialize, Clone)]
134pub struct ChatGptRequestImagesVariation {
135 image: String,
136 n: Option<isize>,
137 size: Option<String>,
138 response_format: Option<String>,
139 user: Option<String>,
140}
141
142impl ChatGptRequestImagesVariation {
143 pub fn new(image: &str, n: isize) -> ChatGptRequestImagesVariation {
144 ChatGptRequestImagesVariation {
145 image: image.to_string(),
146 n: Some(n),
147 size: None,
148 response_format: None,
149 user: None,
150 }
151 }
152}
153
154impl ChatGptRequest for ChatGptRequestImagesVariation {
155 fn from_value(value: Value) -> Result<Self, ChatGptError>
156 where
157 Self: Sized,
158 {
159 convert_from_value(value)
160 }
161
162 fn to_value(&self) -> Value {
163 trim_value(json!(self)).unwrap()
164 }
165}
166
167impl From<ChatGptRequestImagesVariation> for Form {
168 fn from(val: ChatGptRequestImagesVariation) -> Self {
169 convert_form(val.to_value(), vec!["image".to_string()])
170 }
171}