1use crate::error::ChatGptError;
2use crate::v1::{convert_from_value, trim_value, ChatGptRequest};
3use crate::ChatGptResponse;
4use serde::{Deserialize, Serialize};
5use serde_json::{json, Value};
6use std::collections::HashMap;
7
8#[derive(Debug, Deserialize, Serialize, Clone)]
9pub struct ChatGptResponseChatCompletions {
10 pub(crate) value: Value,
11}
12
13impl ChatGptResponse for ChatGptResponseChatCompletions {
14 fn to_value(&self) -> &Value {
15 &self.value
16 }
17}
18
19#[derive(Debug, Deserialize, Serialize, Clone)]
20pub struct ChatGptRequestChatCompletions {
21 model: String,
22 messages: Vec<ChatGptChatFormat>,
23 temperature: Option<f32>,
24 top_p: Option<f32>,
25 n: Option<isize>,
26 stream: Option<bool>,
27 stop: Option<Vec<String>>,
28 max_tokens: Option<isize>,
29 presence_penalty: Option<f32>,
30 frequency_penalty: Option<f32>,
31 logit_bias: Option<HashMap<String, isize>>,
32 user: Option<String>,
33}
34
35impl ChatGptRequestChatCompletions {
36 pub fn new(model: &str, messages: Vec<ChatGptChatFormat>) -> Self {
37 Self {
38 model: model.to_string(),
39 messages,
40 temperature: None,
41 top_p: None,
42 n: None,
43 stream: None,
44 stop: None,
45 max_tokens: None,
46 presence_penalty: None,
47 frequency_penalty: None,
48 logit_bias: None,
49 user: None,
50 }
51 }
52}
53
54#[derive(Debug, Deserialize, Serialize, Clone)]
55pub struct ChatGptChatFormat {
56 role: String,
57 content: String,
58}
59
60impl ChatGptChatFormat {
61 pub fn new(role: &str, content: &str) -> Self {
62 Self {
63 role: role.to_string(),
64 content: content.to_string(),
65 }
66 }
67 pub fn new_system(content: &str) -> Self {
68 ChatGptChatFormat::new("system", content)
69 }
70 pub fn new_user(content: &str) -> Self {
71 Self::new("user", content)
72 }
73 pub fn new_assistant(content: &str) -> Self {
74 Self::new("assistant", content)
75 }
76}
77
78impl ChatGptRequest for ChatGptRequestChatCompletions {
79 fn from_value(value: Value) -> Result<Self, ChatGptError>
80 where
81 Self: Sized,
82 {
83 convert_from_value(value)
84 }
85
86 fn to_value(&self) -> Value {
87 trim_value(json!(self)).unwrap()
88 }
89}