|
1 |
| -# Multi-Shot Text2SQL Component - Semantic Kernel |
2 |
| - |
3 |
| -The implementation is written for [Semantic Kernel](https://github.com/microsoft/semantic-kernel) in Python, although it can easily be adapted for C#. |
4 |
| - |
5 |
| -**The provided Semantic Kernel code implements Iterations 2, 3 & 4.** |
6 |
| - |
7 |
| -## Full Logical Flow for Vector Based Approach |
8 |
| - |
9 |
| -The following diagram shows the logical flow within the Vector Based plugin. In an ideal scenario, the questions will follow the _Pre-Fetched Cache Results Path** which leads to the quickest answer generation. In cases where the question is not known, the plugin will fall back the other paths accordingly and generate the SQL query using the LLMs. |
10 |
| - |
11 |
| -As the query cache is shared between users (no data is stored in the cache), a new user can benefit from the pre-mapped question and schema resolution in the index. There are multiple possible strategies for updating the query cache, see the possible options in the Text2SQL README. |
12 |
| - |
13 |
| -**Database results were deliberately not stored within the cache. Storing them would have removed one of the key benefits of the Text2SQL plugin, the ability to get near-real time information inside a RAG application. Instead, the query is stored so that the most-recent results can be obtained quickly. Additionally, this retains the ability to apply Row or Column Level Security.** |
14 |
| - |
15 |
| - |
16 |
| - |
17 |
| -## Provided Notebooks & Scripts |
18 |
| - |
19 |
| -- `./Iteration 2 - Prompt Based Text2SQL.ipynb` provides example of how to utilise the Prompt Based Text2SQL plugin to query the database. |
20 |
| -- `./Iterations 3 & 4 - Vector Based Text2SQL.ipynb` provides example of how to utilise the Vector Based Text2SQL plugin to query the database. The query cache plugin will be enabled or disabled depending on the environmental parameters. |
21 |
| -- `./time_comparison_script.py` provides a utility script for performing time based comparisons between the different approaches. |
22 |
| - |
23 |
| -### ai-search.py |
24 |
| - |
25 |
| -This util file contains helper functions for interacting with AI Search. |
26 |
| - |
27 |
| -## Plugins |
28 |
| - |
29 |
| -### prompt_based_sql_plugin.py |
30 |
| - |
31 |
| -The `./plugins/prompt_based_sql_plugin/prompt_based_sql_plugin.py` contains 3 key methods to power the Prompt Based Text2SQL engine. |
32 |
| - |
33 |
| -#### system_prompt() |
34 |
| - |
35 |
| -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. |
36 |
| - |
37 |
| -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. |
38 |
| - |
39 |
| -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. |
40 |
| - |
41 |
| -#### get_entity_schema() |
42 |
| - |
43 |
| -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. |
44 |
| - |
45 |
| -#### run_sql_query() |
46 |
| - |
47 |
| -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. |
48 |
| - |
49 |
| -### vector_based_sql_plugin.py |
50 |
| - |
51 |
| -The `./plugins/vector_based_sql_plugin/vector_based_sql_plugin.py` contains 3 key methods to power the Vector Based Text2SQL engine. |
52 |
| - |
53 |
| -#### system_prompt() |
54 |
| - |
55 |
| -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. |
56 |
| - |
57 |
| -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. |
58 |
| - |
59 |
| -**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()`.** |
60 |
| - |
61 |
| -#### get_entity_schema() |
62 |
| - |
63 |
| -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. |
64 |
| - |
65 |
| -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. |
66 |
| - |
67 |
| -#### fetch_queries_from_cache() |
68 |
| - |
69 |
| -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. |
70 |
| - |
71 |
| -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. |
72 |
| - |
73 |
| -#### run_sql_query() |
74 |
| - |
75 |
| -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. |
76 |
| - |
77 |
| -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. |
| 1 | +Coming soon. Refer to Autogen version for now |
0 commit comments