Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions adi_function_app/GETTING_STARTED.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Getting Started with Document Intelligence Function App

To get started, perform the following steps:

1. Setup Azure OpenAI in your subscription with **gpt-4o-mini** & an embedding model, an Python Function App, AI Search and a storage account.
2. Clone this repository and deploy the AI Search rag documents indexes from `deploy_ai_search`.
3. Run `uv sync` within the adi_function_app directory to install dependencies.
4. Configure the environment variables of the function app based on the provided sample
5. Package your Azure Function and upload to your Function App
6. Upload a document for indexing or send a direct HTTP request to the Azure Function.
3 changes: 3 additions & 0 deletions adi_function_app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ The properties returned from the ADI Custom Skill and Chunking are then used to
- Keyphrase extraction
- Vectorisation

> [!NOTE]
> See `GETTING_STARTED.md` for a step by step guide of how to use the accelerator.

## Sample Output

Using the [Phi-3 Technical Report: A Highly Capable Language Model Locally on Your Phone](https://arxiv.org/pdf/2404.14219) as an example, the following output can be obtained for page 7:
Expand Down
11 changes: 11 additions & 0 deletions text_2_sql/GETTING_STARTED.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Getting Started with Agentic Text2SQL Component

To get started, perform the following steps:

1. Setup Azure OpenAI in your subscription with **gpt-4o-mini** & an embedding model, alongside a SQL Server sample database, AI Search and a storage account.
2. Clone this repository and deploy the AI Search text2sql indexes from `deploy_ai_search`.
3. Run `uv sync` within the text_2_sql directory to install dependencies.
4. Configure the .env file based on the provided sample
5. Generate a data dictionary for your target server using the instructions in `data_dictionary`.
6. Upload these data dictionaries to the relevant contains in your storage account. Wait for them to be automatically indexed.
7. Navigate to `autogen` directory to view the AutoGen implementation. Follow the steps in `Iteration 5 - Agentic Vector Based Text2SQL.ipynb` to get started.
113 changes: 24 additions & 89 deletions text_2_sql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ This portion of the repo contains code to implement a multi-shot approach to Tex

The sample provided works with Azure SQL Server, although it has been easily adapted to other SQL sources such as Snowflake.

**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**
> [!NOTE]
>
> - Previous versions of this approach have now been moved to `previous_iterations/semantic_kernel`. These will not be updated.

## High Level Workflow

The following diagram shows a workflow for how the Text2SQL plugin would be incorporated into a RAG application. Using the plugins available, alongside the [Function Calling](https://platform.openai.com/docs/guides/function-calling) capabilities of LLMs, the LLM can do [Chain of Thought](https://learn.microsoft.com/en-us/dotnet/ai/conceptual/chain-of-thought-prompting) reasoning to determine the steps needed to answer the question. This allows the LLM to recognise intent and therefore pick appropriate data sources based on the intent of the question.

![High level workflow for a plugin driven RAG application](../images/Plugin%20Based%20RAG%20Flow.png "High Level Workflow")

> [!NOTE]
> See `GETTING_STARTED.md` for a step by step guide of how to use the accelerator.

## Why Text2SQL instead of indexing the database contents?

Generating SQL queries and executing them to provide context for the RAG application provided several benefits in the use case this was designed for.
Expand All @@ -35,13 +40,7 @@ To solve these issues, a Multi-Shot approach is developed. Below is the iteratio

![Comparison between a common Text2SQL approach and a Multi-Shot Text2SQL approach.](./images/Text2SQL%20Approaches.png "Multi Shot SQL Approaches")

Three different iterations are presented and code provided for:
- **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.
- **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.
- **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.
- **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.

All approaches limit the number of tokens used and avoids filling the prompt with confusing schema information.
Our approach has evolved as the system has matured into an multi-agent approach that brings improved reasoning, speed and instruction following capabilities. With separation into agents, different agents can focus on one task only, and provide a better overall flow and response quality.

Using Auto-Function calling capabilities, the LLM is able to retrieve from the plugin the full schema information for the views / tables that it considers useful for answering the question. Once retrieved, the full SQL query can then be generated. The schemas for multiple views / tables can be retrieved to allow the LLM to perform joins and other complex queries.

Expand All @@ -61,6 +60,10 @@ As the query cache is shared between users (no data is stored in the cache), a n

![Vector Based with Query Cache Logical Flow.](./images/Agentic%20Text2SQL%20Query%20Cache.png "Agentic Vector Based with Query Cache Logical Flow")

#### Parallel execution

After the first agent has rewritten and decomposed the user input, we execute each of the individual questions in parallel for the quickest time to generate an answer.

### Caching Strategy

The cache strategy implementation is a simple way to prove that the system works. You can adopt several different strategies for cache population. Below are some of the strategies that could be used:
Expand All @@ -70,53 +73,12 @@ The cache strategy implementation is a simple way to prove that the system works
- **Positive Indication System:** Only update the cache when a user positively reacts to a question e.g. a thumbs up from the UI or doesn't ask a follow up question.
- **Always update:** Always add all questions into the cache when they are asked. The sample code in the repository currently implements this approach, but this could lead to poor SQL queries reaching the cache. One of the other caching strategies would be better production version.

### Comparison of Iterations
| | Common Text2SQL Approach | Prompt Based Multi-Shot Text2SQL Approach | Vector Based Multi-Shot Text2SQL Approach | Vector Based Multi-Shot Text2SQL Approach With Query Cache | Agentic Vector Based Multi-Shot Text2SQL Approach With Query Cache |
|-|-|-|-|-|-|
|**Advantages** | Fast for a limited number of entities. | Significant reduction in token usage. | Significant reduction in token usage. | Significant reduction in token usage.
| | | | Scales well to multiple entities. | Scales well to multiple entities. | Scales well to multiple entities with small agents. |
| | | | Uses a vector approach to detect the best fitting entity which is faster than using an LLM. Matching is offloaded to AI Search. | Uses a vector approach to detect the best fitting entity which is faster than using an LLM. Matching is offloaded to AI Search. | Uses a vector approach to detect the best fitting entity which is faster than using an LLM. Matching is offloaded to AI Search. |
| | | | | Significantly faster to answer similar questions as best fitting entity detection is skipped. Observed tests resulted in almost half the time for final output compared to the previous iteration. | Significantly faster to answer similar questions as best fitting entity detection is skipped. Observed tests resulted in almost half the time for final output compared to the previous iteration. |
| | | | | Significantly faster execution time for known questions. Total execution time can be reduced by skipping the query generation step. | Significantly faster execution time for known questions. Total execution time can be reduced by skipping the query generation step. |
| | | | | | Instruction following and accuracy is improved by decomposing the task into smaller tasks. |
| | | | | | Handles query decomposition for complex questions. |
|**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. | Slower than other approaches for the first time a question with no similar questions in the cache is asked. |
| | 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. | AI Search and multiple agents adds additional cost to the solution. |
| | LLM struggled to differentiate which table to choose with the large amount of information passed. | | | |
|**Code Availability**| | | | |
| Semantic Kernel | Yes :heavy_check_mark: | Yes :heavy_check_mark: | Yes :heavy_check_mark: | Yes :heavy_check_mark: | |
| LangChain | | | | | |
| AutoGen | | | | | Yes :heavy_check_mark: |

### Complete Execution Time Comparison for Approaches

To compare the different in complete execution time, the following questions were tested 25 times each for 4 different approaches.

Approaches:
- Prompt-based Multi-Shot (Iteration 2)
- Vector-Based Multi-Shot (Iteration 3)
- Vector-Based Multi-Shot with Query Cache (Iteration 4)
- Vector-Based Multi-shot with Pre Run Query Cache (Iteration 4)

Questions:
- What is the total revenue in June 2008?
- Give me the total number of orders in 2008?
- Which country did had the highest number of orders in June 2008?

The graph below shows the response times for the experimentation on a Known Question Set (i.e. the cache has already been populated with the query mapping by the LLM). gpt-4o was used as the completion LLM for this experiment. The response time is the complete execution time including:

- Prompt Preparation
- Question Understanding
- Cache Index Requests _(if applicable)_
- SQL Query Execution
- Interpretation and generation of answer in the correct format

![Response Time Distribution](./images/Known%20Question%20Response%20Time.png "Response Time Distribution By Approach")

The vector-based cache approaches consistently outperform those that just use a Prompt-Based or Vector-Based approach by a significant margin. Given that it is highly likely the same Text2SQL questions will be repeated often, storing the question-sql mapping leads to **significant performance increases** that are beneficial, despite the initial additional latency (between 1 - 2 seconds from testing) when a question is asked the first time.

## Sample Output

> [!NOTE]
>
> - Full payloads for input / outputs can be found in `text_2_sql_core/src/text_2_sql_core/payloads/interaction_payloads.py`.

### What is the top performing product by quantity of units sold?

#### SQL Query Generated
Expand All @@ -130,14 +92,12 @@ The vector-based cache approaches consistently outperform those that just use a
"answer": "The top-performing product by quantity of units sold is the **Classic Vest, S** from the **Classic Vest** product model, with a total of 87 units sold [1][2].",
"sources": [
{
"title": "Sales Order Detail",
"chunk": "| ProductID | TotalUnitsSold |\n|-----------|----------------|\n| 864 | 87 |\n",
"reference": "SELECT TOP 1 ProductID, SUM(OrderQty) AS TotalUnitsSold FROM SalesLT.SalesOrderDetail GROUP BY ProductID ORDER BY TotalUnitsSold DESC;"
"sql_rows": "| ProductID | TotalUnitsSold |\n|-----------|----------------|\n| 864 | 87 |\n",
"sql_query": "SELECT TOP 1 ProductID, SUM(OrderQty) AS TotalUnitsSold FROM SalesLT.SalesOrderDetail GROUP BY ProductID ORDER BY TotalUnitsSold DESC;"
},
{
"title": "Product and Description",
"chunk": "| Name | ProductModel |\n|----------------|---------------|\n| Classic Vest, S| Classic Vest |\n",
"reference": "SELECT Name, ProductModel FROM SalesLT.vProductAndDescription WHERE ProductID = 864;"
"sql_rows": "| Name | ProductModel |\n|----------------|---------------|\n| Classic Vest, S| Classic Vest |\n",
"sql_query": "SELECT Name, ProductModel FROM SalesLT.vProductAndDescription WHERE ProductID = 864;"
}
]
}
Expand All @@ -159,6 +119,10 @@ The top-performing product by quantity of units sold is the **Classic Vest, S**
|----------------|---------------|
| Classic Vest, S| Classic Vest |

## Disambiguation Requests

If the LLM is unable to understand or answer the question asked, it can ask the user follow up questions with a DisambiguationRequest. In cases where multiple columns may be the correct one, or that there user may be referring to several different filter values, the LLM can produce a series of options for the end user to select from.

## Data Dictionary

### entities.json
Expand Down Expand Up @@ -233,28 +197,9 @@ Below is a sample entry for a view / table that we which to expose to the LLM. T

See `./data_dictionary` for more details on how the data dictionary is structured and ways to **automatically generate it**.

## Prompt Based SQL Plugin (Iteration 2)

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.

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.

## Vector Based SQL Plugin (Iterations 3 & 4)

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.

If the query cache is enabled, we used a vector search to find the similar previously asked questions and the queries / schemas they map to. In the case of a high probability of a match, the results can be pre-run with the stored query and passed to the LLM alongside the query. If the results can answer the question, query generation can be skipped all together, speeding up the total execution time.

In the case of an unknown question, there is a minor increase in latency but the query index cache could be pre-populated before it is released to users with common questions.

The following environmental variables control the behaviour of the Vector Based Text2SQL generation:

- **Text2Sql__UseQueryCache** - controls whether the query cached index is checked before using the standard schema index.
- **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.

## Agentic Vector Based Approach (Iteration 5)

This approach builds on the the Vector Based SQL Plugin approach, but adds a agentic approach to the solution.
This approach builds on the the Vector Based SQL Plugin approach that was previously developed, but adds a agentic approach to the solution.

This agentic system contains the following agents:

Expand All @@ -267,16 +212,6 @@ This agentic system contains the following agents:

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.

## Code Availability

| | Common Text2SQL Approach | Prompt Based Multi-Shot Text2SQL Approach | Vector Based Multi-Shot Text2SQL Approach | Vector Based Multi-Shot Text2SQL Approach With Query Cache | Agentic Vector Based Multi-Shot Text2SQL Approach With Query Cache |
|-|-|-|-|-|-|
| Semantic Kernel | Yes :heavy_check_mark: | Yes :heavy_check_mark: | Yes :heavy_check_mark: | Yes :heavy_check_mark: | |
| LangChain | | | | | |
| AutoGen | | | | | Yes :heavy_check_mark: |

See the relevant directory for the code in the provided framework.

## Tips for good Text2SQL performance.

- Pre-assemble views to avoid the LLM having to make complex joins between multiple tables
Expand Down
3 changes: 1 addition & 2 deletions text_2_sql/autogen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ The system produces standardized JSON output through the Answer and Sources Agen
"sources": [
{
"sql_query": "The SQL query used",
"sql_rows": ["Array of result rows"],
"markdown_table": "Formatted markdown table of results"
"sql_rows": ["Array of result rows"]
}
]
}
Expand Down
Binary file modified text_2_sql/images/Agentic Text2SQL Query Cache.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified text_2_sql/images/Text2SQL Approaches.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading