headless_lms_models/
chatbot_configurations.rs

1use crate::prelude::*;
2
3#[derive(Clone, PartialEq, Deserialize, Serialize)]
4pub struct ChatbotConfiguration {
5    pub id: Uuid,
6    pub created_at: DateTime<Utc>,
7    pub updated_at: DateTime<Utc>,
8    pub deleted_at: Option<DateTime<Utc>>,
9    pub course_id: Uuid,
10    pub enabled_to_students: bool,
11    pub chatbot_name: String,
12    pub prompt: String,
13    pub initial_message: String,
14    pub weekly_tokens_per_user: i32,
15    pub daily_tokens_per_user: i32,
16    pub temperature: f32,
17    pub top_p: f32,
18    pub frequency_penalty: f32,
19    pub presence_penalty: f32,
20    pub response_max_tokens: i32,
21    pub use_azure_search: bool,
22    pub maintain_azure_search_index: bool,
23    pub hide_citations: bool,
24    pub use_semantic_reranking: bool,
25}
26
27pub async fn get_by_id(conn: &mut PgConnection, id: Uuid) -> ModelResult<ChatbotConfiguration> {
28    let res = sqlx::query_as!(
29        ChatbotConfiguration,
30        r#"
31SELECT * FROM chatbot_configurations
32WHERE id = $1
33        "#,
34        id
35    )
36    .fetch_one(conn)
37    .await?;
38    Ok(res)
39}
40
41pub async fn insert(
42    conn: &mut PgConnection,
43    input: ChatbotConfiguration,
44) -> ModelResult<ChatbotConfiguration> {
45    let res = sqlx::query_as!(
46        ChatbotConfiguration,
47        r#"
48INSERT INTO chatbot_configurations (
49    course_id,
50    enabled_to_students,
51    chatbot_name,
52    prompt,
53    initial_message,
54    weekly_tokens_per_user,
55    daily_tokens_per_user,
56    temperature,
57    top_p,
58    frequency_penalty,
59    presence_penalty,
60    response_max_tokens
61  )
62VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
63RETURNING *
64        "#,
65        input.course_id,
66        input.enabled_to_students,
67        input.chatbot_name,
68        input.prompt,
69        input.initial_message,
70        input.weekly_tokens_per_user,
71        input.daily_tokens_per_user,
72        input.temperature,
73        input.top_p,
74        input.frequency_penalty,
75        input.presence_penalty,
76        input.response_max_tokens
77    )
78    .fetch_one(conn)
79    .await?;
80    Ok(res)
81}
82
83pub async fn get_for_course(
84    conn: &mut PgConnection,
85    course_id: Uuid,
86) -> ModelResult<Vec<ChatbotConfiguration>> {
87    let res = sqlx::query_as!(
88        ChatbotConfiguration,
89        "
90SELECT * FROM
91chatbot_configurations
92WHERE course_id = $1
93AND deleted_at IS NULL
94",
95        course_id
96    )
97    .fetch_all(conn)
98    .await?;
99    Ok(res)
100}
101
102pub async fn get_for_azure_search_maintenance(
103    conn: &mut PgConnection,
104) -> ModelResult<Vec<ChatbotConfiguration>> {
105    let res = sqlx::query_as!(
106        ChatbotConfiguration,
107        "
108SELECT * FROM
109chatbot_configurations
110WHERE maintain_azure_search_index = true
111AND deleted_at IS NULL
112",
113    )
114    .fetch_all(conn)
115    .await?;
116    Ok(res)
117}