You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: deploy_ai_search/README.md
+1Lines changed: 1 addition & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -24,6 +24,7 @@ The associated scripts in this portion of the repository contains pre-built scri
24
24
-`index_type text_2_sql`. This selects the `Text2SQLAISearch` sub class.
25
25
-`rebuild`. Whether to delete and rebuild the index.
26
26
-`suffix`. Optional parameter that will apply a suffix onto the deployed index and indexer. This is useful if you want deploy a test version, before overwriting the main version.
27
+
-`single_data_dictionary`. Optional parameter that controls whether you will be uploading a single data dictionary, or a data dictionary file per entity. By default, this is set to False.
Copy file name to clipboardExpand all lines: text_2_sql/README.md
+13-75Lines changed: 13 additions & 75 deletions
Original file line number
Diff line number
Diff line change
@@ -2,8 +2,6 @@
2
2
3
3
This portion of the repo contains code to implement a multi-shot approach to Text2SQL generation. This code can be integrated into a RAG application to allow the application to intelligently switch between different data sources (SQL, AI Search etc) to answer the question with the best possible information.
4
4
5
-
The implementation is written for [Semantic Kernel](https://github.com/microsoft/semantic-kernel) in Python, although it can easily be adapted for C# or another framework such as LangChain.
6
-
7
5
The sample provided works with Azure SQL Server, although it has been easily adapted to other SQL sources such as Snowflake.
8
6
9
7
**Three iterations on the approach are provided for SQL query generation. A prompt based approach and a two vector database based approaches. See Multi-Shot Approach for more details**
@@ -68,6 +66,10 @@ As the query cache is shared between users (no data is stored in the cache), a n
68
66
|**Disadvantages**| Slows down significantly as the number of entities increases. | Uses LLM to detect the best fitting entity which is slow compared to a vector approach. | AI Search adds additional cost to the solution. | Slower than other approaches for the first time a question with no similar questions in the cache is asked. |
69
67
|| Consumes a significant number of tokens as number of entities increases. | As number of entities increases, token usage will grow but at a lesser rate than Iteration 1. || AI Search adds additional cost to the solution. |
70
68
|| LLM struggled to differentiate which table to choose with the large amount of information passed. |||
### Complete Execution Time Comparison for Approaches
73
75
@@ -140,22 +142,13 @@ The top-performing product by quantity of units sold is the **Classic Vest, S**
140
142
|----------------|---------------|
141
143
| Classic Vest, S| Classic Vest |
142
144
143
-
## Provided Notebooks & Scripts
144
-
145
-
-`./rag_with_prompt_based_text_2_sql.ipynb` provides example of how to utilise the Prompt Based Text2SQL plugin to query the database.
146
-
-`./rag_with_vector_based_text_2_sql.ipynb` provides example of how to utilise the Vector Based Text2SQL plugin to query the database.
147
-
-`./rag_with_vector_based_text_2_sql_query_cache.ipynb` provides example of how to utilise the Vector Based Text2SQL plugin, alongside the query cache, to query the database.
148
-
-`./rag_with_ai_search_and_text_2_sql.ipynb` provides an example of how to use the Text2SQL and an AISearch plugin in parallel to automatically retrieve data from the most relevant source to answer the query.
149
-
- This setup is useful for a production application as the SQL Database is unlikely to be able to answer all the questions a user may ask.
150
-
-`./time_comparison_script.py` provides a utility script for performing time based comparisons between the different approaches.
151
-
152
145
## Data Dictionary
153
146
154
147
### entities.json
155
148
156
149
To power the knowledge of the LLM, a data dictionary containing all the SQL views / table metadata is used. Whilst the LLM could query the database at runtime to find out the schemas for the database, storing them in a text file reduces the overall latency of the system and allows the metadata for each table to be adjusted in a form of prompt engineering.
157
150
158
-
The data dictionary is stored in `./data_dictionary/entities.json`. Below is a sample entry for a view / table that we which to expose to the LLM. The Microsoft SQL Server [Adventure Works Database](https://learn.microsoft.com/en-us/sql/samples/adventureworks-install-configure?view=sql-server-ver16) is used as an sample.
151
+
Below is a sample entry for a view / table that we which to expose to the LLM. The Microsoft SQL Server [Adventure Works Database](https://learn.microsoft.com/en-us/sql/samples/adventureworks-install-configure?view=sql-server-ver16) is used as an sample.
159
152
160
153
```json
161
154
{
@@ -182,45 +175,14 @@ The data dictionary is stored in `./data_dictionary/entities.json`. Below is a s
182
175
}
183
176
```
184
177
185
-
#### Property Definitions
186
-
-**EntityName** is a human readable name for the entity.
187
-
-**Entity** is the actual name for the entity that is used in the SQL query.
188
-
-**Description** provides a comprehensive description of what information the entity contains.
189
-
-**Columns** contains a list of the columns exposed for querying. Each column contains:
190
-
-**Definition** a short definition of what information the column contains. Here you can add extra metadata to **prompt engineer** the LLM to select the right columns or interpret the data in the column correctly.
191
-
-**Name** is the actual column name.
192
-
-**Type** is the datatype for the column.
193
-
-**SampleValues (optional)** is a list of sample values that are in the column. This is useful for instructing the LLM of what format the data may be in.
194
-
-**AllowedValues (optional)** is a list of absolute allowed values for the column. This instructs the LLM only to use these values if filtering against this column.
195
-
196
-
A full data dictionary must be built for all the views / tables you which to expose to the LLM. The metadata provide directly influences the accuracy of the Text2SQL component.
178
+
See `./data_dictionary` for more details on how the data dictionary is structured and ways to **automatically generate it**.
197
179
198
180
## Prompt Based SQL Plugin (Iteration 2)
199
181
200
182
This approach works well for a small number of entities (tested on up to 20 entities with hundreds of columns). It performed well on the testing, with correct metadata, we achieved 100% accuracy on the test set.
201
183
202
184
Whilst a simple and high performing approach, the downside of this approach is the increase in number of tokens as the number of entities increases. Additionally, we found that the LLM started to get "confused" on which columns belong to which entities as the number of entities increased.
203
185
204
-
### prompt_based_sql_plugin.py
205
-
206
-
The `./plugins/prompt_based_sql_plugin/prompt_based_sql_plugin.py` contains 3 key methods to power the Prompt Based Text2SQL engine.
207
-
208
-
#### system_prompt()
209
-
210
-
This method takes the loaded `entities.json` file and generates a system prompt based on it. Here, the **EntityName** and **Description** are used to build a list of available entities for the LLM to select.
211
-
212
-
This is then inserted into a pre-made Text2SQL generation prompt that already contains optimised and working instructions for the LLM. This system prompt for the plugin is added to the main prompt file at runtime.
213
-
214
-
The **target_engine** is passed to the prompt, along with **engine_specific_rules** to ensure that the SQL queries generated work on the target engine.
215
-
216
-
#### get_entity_schema()
217
-
218
-
This method is called by the Semantic Kernel framework automatically, when instructed to do so by the LLM, to fetch the full schema definitions for a given entity. This returns a JSON string of the chosen entity which allows the LLM to understand the column definitions and their associated metadata. This can be called in parallel for multiple entities.
219
-
220
-
#### run_sql_query()
221
-
222
-
This method is called by the Semantic Kernel framework automatically, when instructed to do so by the LLM, to run a SQL query against the given database. It returns a JSON string containing a row wise dump of the results returned. These results are then interpreted to answer the question.
223
-
224
186
## Vector Based SQL Plugin (Iterations 3 & 4)
225
187
226
188
This approach allows the system to scale without significantly increasing the number of tokens used within the system prompt. Indexing and running an AI Search instance consumes additional cost, compared to the prompt based approach.
@@ -234,39 +196,15 @@ The following environmental variables control the behaviour of the Vector Based
234
196
-**Text2Sql__UseQueryCache** - controls whether the query cached index is checked before using the standard schema index.
235
197
-**Text2Sql__PreRunQueryCache** - controls whether the top result from the query cache index (if enabled) is pre-fetched against the data source to include the results in the prompt.
236
198
237
-
### vector_based_sql_plugin.py
238
-
239
-
The `./plugins/vector_based_sql_plugin/vector_based_sql_plugin.py` contains 3 key methods to power the Vector Based Text2SQL engine.
199
+
## Code Availability
240
200
241
-
#### Indexing
242
-
243
-
`./deploy_ai_search/text_2_sql.py` & `./deploy_ai_search/text_2_sql_query_cache.py` contains the scripts to deploy and index the data dictionary for use within the plugin. See instructions in `./deploy_ai_search/README.md`.
244
-
245
-
#### system_prompt()
246
-
247
-
This method simply returns a pre-made system prompt that contains optimised and working instructions for the LLM. This system prompt for the plugin is added to the main prompt file at runtime.
248
-
249
-
The **target_engine** is passed to the prompt, along with **engine_specific_rules** to ensure that the SQL queries generated work on the target engine.
250
-
251
-
**If the query cache is enabled, the prompt is adjusted to instruct the LLM to look at the cached data and results first, before calling `get_entity_schema()`.**
252
-
253
-
#### get_entity_schema()
254
-
255
-
This method is called by the Semantic Kernel framework automatically, when instructed to do so by the LLM, to search the AI Search instance with the given text. The LLM is able to pass the key terms from the user query, and retrieve a ranked list of the most suitable entities to answer the question.
256
-
257
-
The search text passed is vectorised against the entity level **Description** columns. A hybrid Semantic Reranking search is applied against the **EntityName**, **Entity**, **Columns/Name** fields.
258
-
259
-
#### fetch_queries_from_cache()
260
-
261
-
The vector based with query cache uses the `fetch_queries_from_cache()` method to fetch the most relevant previous query and injects it into the prompt before the initial LLM call. The use of Auto-Function Calling here is avoided to reduce the response time as the cache index will always be used first.
262
-
263
-
If the score of the top result is higher than the defined threshold, the query will be executed against the target data source and the results included in the prompt. This allows us to prompt the LLM to evaluated whether it can use these results to answer the question, **without further SQL Query generation** to speed up the process.
264
-
265
-
#### run_sql_query()
266
-
267
-
This method is called by the Semantic Kernel framework automatically, when instructed to do so by the LLM, to run a SQL query against the given database. It returns a JSON string containing a row wise dump of the results returned. These results are then interpreted to answer the question.
201
+
|| Common Text2SQL Approach | Prompt Based Multi-Shot Text2SQL Approach | Vector Based Multi-Shot Text2SQL Approach | Vector Based Multi-Shot Text2SQL Approach With Query Cache |
Additionally, if any of the cache functionality is enabled, this method will update the query cache index based on the SQL query run, and the schemas used in execution.
207
+
See the relevant directory for the code in the provided framework.
0 commit comments