Skip to main content

headless_lms_models/
chatbot_configurations_models.rs

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