1use crate::prelude::*;
2use utoipa::ToSchema;
3
4#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Copy, Type, ToSchema)]
5#[sqlx(type_name = "model_type", rename_all = "kebab-case")]
6pub enum ModelType {
7 GPTThinking,
8 GPTNonThinking,
9 GPTHardThinking,
10 Mistral,
11}
12
13#[derive(Clone, PartialEq, Deserialize, Serialize, ToSchema)]
14pub struct ChatbotConfigurationModel {
15 pub id: Uuid,
16 pub created_at: DateTime<Utc>,
17 pub updated_at: DateTime<Utc>,
18 pub deleted_at: Option<DateTime<Utc>>,
19 pub model: String,
20 pub model_type: ModelType,
21 pub default_model: bool,
22 pub context_size: i32,
23}
24
25#[derive(Clone, PartialEq, Deserialize, Serialize)]
26pub struct NewChatbotConfigurationModel {
27 pub id: Uuid,
28 pub model: String,
29 pub model_type: ModelType,
30 pub default_model: bool,
31 pub context_size: i32,
32}
33
34pub async fn get_by_id(
35 conn: &mut PgConnection,
36 id: Uuid,
37) -> ModelResult<ChatbotConfigurationModel> {
38 let res = sqlx::query_as!(
39 ChatbotConfigurationModel,
40 r#"
41SELECT
42 id,
43 created_at,
44 updated_at,
45 deleted_at,
46 model,
47 model_type as "model_type: ModelType",
48 default_model,
49 context_size
50FROM chatbot_configurations_models
51WHERE id = $1
52AND deleted_at IS NULL
53 "#,
54 id
55 )
56 .fetch_one(conn)
57 .await?;
58 Ok(res)
59}
60
61pub async fn get_all(conn: &mut PgConnection) -> ModelResult<Vec<ChatbotConfigurationModel>> {
62 let res = sqlx::query_as!(
63 ChatbotConfigurationModel,
64 r#"
65SELECT
66 id,
67 created_at,
68 updated_at,
69 deleted_at,
70 model,
71 model_type as "model_type: ModelType",
72 default_model,
73 context_size
74FROM chatbot_configurations_models
75WHERE deleted_at IS NULL
76 "#,
77 )
78 .fetch_all(conn)
79 .await?;
80 Ok(res)
81}
82
83pub async fn get_default(conn: &mut PgConnection) -> ModelResult<ChatbotConfigurationModel> {
84 let res = sqlx::query_as!(
85 ChatbotConfigurationModel,
86 r#"
87SELECT
88 id,
89 created_at,
90 updated_at,
91 deleted_at,
92 model,
93 model_type as "model_type: ModelType",
94 default_model,
95 context_size
96FROM chatbot_configurations_models
97WHERE default_model = true
98AND deleted_at IS NULL
99 "#,
100 )
101 .fetch_one(conn)
102 .await?;
103 Ok(res)
104}
105
106pub async fn get_by_chatbot_configuration_id(
107 conn: &mut PgConnection,
108 chatbotconf_id: Uuid,
109) -> ModelResult<ChatbotConfigurationModel> {
110 let res = sqlx::query_as!(
111 ChatbotConfigurationModel,
112 r#"
113SELECT id,
114 created_at,
115 updated_at,
116 deleted_at,
117 model,
118 model_type AS "model_type: ModelType",
119 default_model,
120 context_size
121FROM chatbot_configurations_models
122WHERE id = (
123 SELECT model_id
124 FROM chatbot_configurations
125 WHERE id = $1
126 AND deleted_at IS NULL
127 )
128 AND deleted_at IS NULL
129 "#,
130 chatbotconf_id,
131 )
132 .fetch_one(conn)
133 .await?;
134 Ok(res)
135}
136
137pub async fn insert(
138 conn: &mut PgConnection,
139 input: NewChatbotConfigurationModel,
140) -> ModelResult<ChatbotConfigurationModel> {
141 let res = sqlx::query_as!(
142 ChatbotConfigurationModel,
143 r#"
144INSERT INTO chatbot_configurations_models (id, model, model_type, default_model, context_size) VALUES ($1, $2, $3, $4, $5) RETURNING
145 id,
146 created_at,
147 updated_at,
148 deleted_at,
149 model,
150 model_type as "model_type: ModelType",
151 default_model,
152 context_size
153 "#,
154 input.id,
155 input.model,
156 input.model_type as ModelType,
157 input.default_model,
158 input.context_size,
159 )
160 .fetch_one(conn)
161 .await?;
162 Ok(res)
163}