Skip to content

Commit 361b3b0

Browse files
committed
Add more documentation
1 parent 62c7272 commit 361b3b0

File tree

10 files changed

+68
-6
lines changed

10 files changed

+68
-6
lines changed

text_2_sql/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Three different iterations are presented and code provided for:
3939
- **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.
4040
- **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.
4141
- **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.
4343

4444
All approaches limit the number of tokens used and avoids filling the prompt with confusing schema information.
4545

text_2_sql/autogen/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,62 @@ As the query cache is shared between users (no data is stored in the cache), a n
1717
![Vector Based with Query Cache Logical Flow.](../images/Agentic%20Text2SQL%20Query%20Cache.png "Agentic Vector Based with Query Cache Logical Flow")
1818

1919
## 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.

text_2_sql/autogen/agents/__init__.py

Whitespace-only changes.

text_2_sql/autogen/custom_agents/sql_query_cache_agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from autogen_agentchat.base import Response
77
from autogen_agentchat.messages import AgentMessage, ChatMessage, TextMessage
88
from autogen_core.base import CancellationToken
9-
from utils.sql_utils import fetch_queries_from_cache
9+
from utils.sql import fetch_queries_from_cache
1010
import json
1111
import logging
1212

File renamed without changes.

text_2_sql/autogen/utils/llm_agent_creator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import yaml
44
from autogen_core.components.tools import FunctionTool
55
from autogen_agentchat.agents import AssistantAgent
6-
from utils.sql_utils import (
6+
from utils.sql import (
77
query_execution,
88
get_entity_schemas,
99
)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import os
55
import aioodbc
66
from typing import Annotated
7-
from utils.ai_search_utils import run_ai_search_query
7+
from text_2_sql.autogen.utils.ai_search import run_ai_search_query
88
import json
99
import asyncio
1010

text_2_sql/semantic_kernel/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ As the query cache is shared between users (no data is stored in the cache), a n
2222
- 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.
2323
- `./time_comparison_script.py` provides a utility script for performing time based comparisons between the different approaches.
2424

25+
### ai-search.py
26+
27+
This util file contains helper functions for interacting with AI Search.
28+
2529
## Plugins
2630

2731
### prompt_based_sql_plugin.py
@@ -48,7 +52,6 @@ This method is called by the Semantic Kernel framework automatically, when instr
4852

4953
The `./plugins/vector_based_sql_plugin/vector_based_sql_plugin.py` contains 3 key methods to power the Vector Based Text2SQL engine.
5054

51-
5255
#### system_prompt()
5356

5457
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.

text_2_sql/semantic_kernel/plugins/vector_based_sql_plugin/vector_based_sql_plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import os
66
import json
77
import logging
8-
from utils.ai_search_utils import (
8+
from utils.ai_search import (
99
add_entry_to_index,
1010
run_ai_search_query,
1111
)
File renamed without changes.

0 commit comments

Comments
 (0)