headless_lms_chatbot/
azure_skillset.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use serde_json::json;

use crate::prelude::*;

const API_VERSION: &str = "2024-07-01";

pub async fn does_skillset_exist(
    skillset_name: &str,
    app_config: &ApplicationConfiguration,
) -> anyhow::Result<bool> {
    // Retrieve Azure configurations from the application configuration
    let azure_config = app_config.azure_configuration.as_ref().ok_or_else(|| {
        anyhow::anyhow!("Azure configuration is missing from the application configuration")
    })?;

    let search_config = azure_config.search_config.as_ref().ok_or_else(|| {
        anyhow::anyhow!("Azure search configuration is missing from the Azure configuration")
    })?;

    let mut url = search_config.search_endpoint.clone();
    url.set_path(&format!("skillsets('{}')", skillset_name));
    url.set_query(Some(&format!("api-version={}", API_VERSION)));

    let response = REQWEST_CLIENT
        .get(url)
        .header("Content-Type", "application/json")
        .header("api-key", search_config.search_api_key.clone())
        .send()
        .await?;

    if response.status().is_success() {
        Ok(true)
    } else if response.status() == 404 {
        Ok(false)
    } else {
        let status = response.status();
        let error_text = response.text().await?;
        Err(anyhow::anyhow!(
            "Error checking if skillset exists. Status: {}. Error: {}",
            status,
            error_text
        ))
    }
}

pub async fn create_skillset(
    skillset_name: &str,
    target_index_name: &str,
    app_config: &ApplicationConfiguration,
) -> anyhow::Result<()> {
    let azure_config = app_config.azure_configuration.as_ref().ok_or_else(|| {
        anyhow::anyhow!("Azure configuration is missing from the application configuration")
    })?;

    let search_config = azure_config.search_config.as_ref().ok_or_else(|| {
        anyhow::anyhow!("Azure search configuration is missing from the Azure configuration")
    })?;

    let azure_openai_api_key = search_config.vectorizer_api_key.clone();

    let mut url = search_config.search_endpoint.clone();
    url.set_path(&format!("skillsets/{}", skillset_name));
    url.set_query(Some(&format!("api-version={}", API_VERSION)));

    let skillset_definition = json!({
        "name": skillset_name,
        "description": "Skillset to chunk documents and generate embeddings",
        "skills": [
            {
                "@odata.type": "#Microsoft.Skills.Text.SplitSkill",
                "name": "#1",
                "description": "Split skill to chunk documents",
                "context": "/document",
                "defaultLanguageCode": "en",
                "textSplitMode": "pages",
                "maximumPageLength": 2000,
                "pageOverlapLength": 500,
                "maximumPagesToTake": 0,
                "inputs": [
                    {
                        "name": "text",
                        "source": "/document/content",
                        "sourceContext": null,
                        "inputs": []
                    }
                ],
                "outputs": [
                    {
                        "name": "textItems",
                        "targetName": "pages"
                    }
                ]
            },
            {
                "@odata.type": "#Microsoft.Skills.Text.AzureOpenAIEmbeddingSkill",
                "name": "#2",
                "description": null,
                "context": "/document/pages/*",
                "resourceUri": search_config.vectorizer_resource_uri.clone(),
                "apiKey": azure_openai_api_key,
                "deploymentId": search_config.vectorizer_deployment_id.clone(),
                "dimensions": 1536,
                "modelName": search_config.vectorizer_model_name.clone(),
                "inputs": [
                    {
                        "name": "text",
                        "source": "/document/pages/*",
                        "sourceContext": null,
                        "inputs": []
                    }
                ],
                "outputs": [
                    {
                        "name": "embedding",
                        "targetName": "text_vector"
                    }
                ],
                "authIdentity": null
            }
        ],
        "cognitiveServices": null,
        "knowledgeStore": null,
        "indexProjections": {
            "selectors": [
                {
                    "targetIndexName": target_index_name,
                    "parentKeyFieldName": "parent_id",
                    "sourceContext": "/document/pages/*",
                    "mappings": [
                        {
                            "name": "text_vector",
                            "source": "/document/pages/*/text_vector",
                            "sourceContext": null,
                            "inputs": []
                        },
                        {
                            "name": "chunk",
                            "source": "/document/pages/*",
                            "sourceContext": null,
                            "inputs": []
                        },
                        {
                            "name": "title",
                            "source": "/document/title",
                            "sourceContext": null,
                            "inputs": []
                        },
                        {
                          "name": "url",
                          "source": "/document/url",
                          "sourceContext": null,
                          "inputs": []
                      }
                    ]
                }
            ],
            "parameters": {
                "projectionMode": "skipIndexingParentDocuments"
            }
        },
        "encryptionKey": null
    });

    let response = REQWEST_CLIENT
        .put(url)
        .header("Content-Type", "application/json")
        .header("api-key", search_config.search_api_key.clone())
        .json(&skillset_definition)
        .send()
        .await?;

    if response.status().is_success() {
        Ok(())
    } else {
        let status = response.status();
        let error_text = response.text().await?;
        Err(anyhow::anyhow!(
            "Error creating skillset. Status: {}. Error: {}",
            status,
            error_text
        ))
    }
}