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: text_2_sql/README.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,7 +39,7 @@ Three different iterations are presented and code provided for:
39
39
-**Iteration 2:** Injection of a brief description of the available entities is injected into the prompt. This limits the number of tokens used and avoids filling the prompt with confusing schema information.
40
40
-**Iteration 3:** Indexing the entity definitions in a vector database, such as AI Search, and querying it to retrieve the most relevant entities for the key terms from the query.
41
41
-**Iteration 4:** Keeping an index of commonly asked questions and which schema / SQL query they resolve to - this index is generated by the LLM when it encounters a question that has not been previously asked. Additionally, indexing the entity definitions in a vector database, such as AI Search _(same as Iteration 3)_. First querying this index to see if a similar SQL query can be obtained _(if high probability of exact SQL query match, the results can be pre-fetched)_. If not, falling back to the schema index, and querying it to retrieve the most relevant entities for the key terms from the query.
42
-
-**Iteration 5:** Moves the Iteration 4 approach into a multi-agent approach for improved reasoning and query generation. With separation into agents, different agents can focus on one task only, and provide a better overall flow and response quality.
42
+
-**Iteration 5:** Moves the Iteration 4 approach into a multi-agent approach for improved reasoning and query generation. With separation into agents, different agents can focus on one task only, and provide a better overall flow and response quality. See more details below.
43
43
44
44
All approaches limit the number of tokens used and avoids filling the prompt with confusing schema information.
Copy file name to clipboardExpand all lines: text_2_sql/autogen/README.md
+59Lines changed: 59 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,3 +17,62 @@ As the query cache is shared between users (no data is stored in the cache), a n
17
17

18
18
19
19
## Provided Notebooks & Scripts
20
+
21
+
-`./agentic_text_2_sql.ipynb` provides example of how to utilise the Agentic Vector Based Text2SQL approach to query the database. The query cache plugin will be enabled or disabled depending on the environmental parameters.
22
+
23
+
## Agents
24
+
25
+
This approach builds on the the Vector Based SQL Plugin approach, but adds a agentic approach to the solution.
26
+
27
+
This agentic system contains the following agents:
28
+
29
+
-**Query Cache Agent:** Responsible for checking the cache for previously asked questions.
30
+
-**Query Decomposition Agent:** Responsible for decomposing complex questions, into sub questions that can be answered with SQL.
31
+
-**Schema Selection Agent:** Responsible for extracting key terms from the question and checking the index store for the queries.
32
+
-**SQL Query Generation Agent:** Responsible for using the previously extracted schemas and generated SQL queries to answer the question. This agent can request more schemas if needed. This agent will run the query.
33
+
-**SQL Query Verification Agent:** Responsible for verifying that the SQL query and results question will answer the question.
34
+
-**Answer Generation Agent:** Responsible for taking the database results and generating the final answer for the user.
35
+
36
+
The combination of this agent allows the system to answer complex questions, whilst staying under the token limits when including the database schemas. The query cache ensures that previously asked questions, can be answered quickly to avoid degrading user experience.
37
+
38
+
All agents can be found in `/agents/`.
39
+
40
+
## agentic_text_2_sql.py
41
+
42
+
This is the main entry point for the agentic system. In here, the `Selector Group Chat` is configured with the termination conditions to orchestrate the agents within the system.
43
+
44
+
A customer transition selector is used to automatically transition between agents dependent on the last one that was used. In some cases, this choice is delegated to an LLM to decide on the most appropriate action. This mixed approach allows for speed when needed (e.g. always calling Query Cache Agent first), but will allow the system to react dynamically to the events.
45
+
46
+
## Utils
47
+
48
+
### ai-search.py
49
+
50
+
This util file contains helper functions for interacting with AI Search.
51
+
52
+
### llm_agent_creator.py
53
+
54
+
This util file creates the agents in the AutoGen framework based on the configuration files.
55
+
56
+
### models.py
57
+
58
+
This util file creates the model connections to Azure OpenAI for the agents.
59
+
60
+
### sql.py
61
+
62
+
#### get_entity_schema()
63
+
64
+
This method is called by the AutoGen 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.
65
+
66
+
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.
67
+
68
+
#### fetch_queries_from_cache()
69
+
70
+
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.
71
+
72
+
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.
73
+
74
+
#### run_sql_query()
75
+
76
+
This method is called by the AutoGen 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.
77
+
78
+
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.
Copy file name to clipboardExpand all lines: text_2_sql/semantic_kernel/README.md
+4-1Lines changed: 4 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,6 +22,10 @@ As the query cache is shared between users (no data is stored in the cache), a n
22
22
- 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.
23
23
-`./time_comparison_script.py` provides a utility script for performing time based comparisons between the different approaches.
24
24
25
+
### ai-search.py
26
+
27
+
This util file contains helper functions for interacting with AI Search.
28
+
25
29
## Plugins
26
30
27
31
### prompt_based_sql_plugin.py
@@ -48,7 +52,6 @@ This method is called by the Semantic Kernel framework automatically, when instr
48
52
49
53
The `./plugins/vector_based_sql_plugin/vector_based_sql_plugin.py` contains 3 key methods to power the Vector Based Text2SQL engine.
50
54
51
-
52
55
#### system_prompt()
53
56
54
57
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.
0 commit comments