SQL Generation and RAG for Financial Data Q&A Chatbot

q4 · Finance · 2023 · source Agentic: yes · promotional bias: med · source-backed · extracted with prompt 0.5.6 (schema v2)

Generated from extraction.json — do not edit by hand. Every claim cites verbatim source evidence; stated means the quote directly asserts the claim, implied means it supports it indirectly.

Problem

Domain: Financial-data question answering for investor relations

Q4 needed a chatbot for Investor Relations Officers that could answer questions over structured and numerical financial data, where conventional semantic RAG retrieved incorrect context because embeddings struggled with numbers, transactions, stock quotes, and dates.

Constraints:

Architecture

Q4 implemented a Bedrock-backed text-to-SQL RAG pipeline. A user question, database schema, sample rows, and instructions are sent to an LLM through SQLDatabaseChain to generate SQL. The SQL runs against the structured database to retrieve context, then a second LLM step summarizes the answer. Model selection was tuned per stage, eventually using Claude variants for SQL and Titan Text Express or Claude Instant for summarization.

Diagram confidence: explicit (topology described by the source).

flowchart LR
  investor_relations_user["Investor relations user"] --> q4_q_a_chatbot["Q4 Q&A chatbot"]
  q4_q_a_chatbot --> sql_generation_prompt["SQL generation prompt"]
  sql_generation_prompt --> sqldatabasechain["SQLDatabaseChain"]
  sqldatabasechain --> amazon_bedrock_models["Amazon Bedrock models"]
  sqldatabasechain --> structured_financial_database["Structured financial database"]
  structured_financial_database --> retrieved_context["Retrieved context"]
  retrieved_context --> summarization_prompt["Summarization prompt"]
  q4_q_a_chatbot --> summarization_prompt
  summarization_prompt --> amazon_bedrock_models
Drag to pan · scroll or use +/− to zoom · reconstructed from the source's described component topology

Components

Component Role Interacts with
Investor relations user Asks natural-language questions about financial and ownership data. Q4 Q&A chatbot
Q4 Q&A chatbot User-facing interface that orchestrates SQL generation, database retrieval, and answer summarization. Investor relations user, SQL generation prompt, Summarization prompt
SQL generation prompt Combines user question, schema, sample rows, field explanations, and instructions. Q4 Q&A chatbot, SQLDatabaseChain
SQLDatabaseChain LangChain component that orchestrates text-to-SQL conversion and database interaction. SQL generation prompt, Amazon Bedrock models, Structured financial database
Amazon Bedrock models Serverless model access layer for Claude and Titan models used across SQL generation, validation, and summarization tasks. SQLDatabaseChain, Summarization prompt
Structured financial database Holds numerical and structured ownership, transaction, stock, and customer data queried through SQL. SQLDatabaseChain, Retrieved context
Retrieved context SQL result context passed into the final summarization step. Structured financial database, Summarization prompt
Summarization prompt Combines original question, retrieved context, and instructions to generate the final user-facing answer. Q4 Q&A chatbot, Amazon Bedrock models, Retrieved context

Data flow

An IRO asks a natural-language question. The system combines the question with schema, sample rows, and instructions, then uses SQLDatabaseChain and Amazon Bedrock to generate SQL. The SQL query retrieves context from the database. The original question, retrieved context, and summarization instructions are sent back to an LLM to produce the final answer.

3 supporting quotes
“In Q4’s use case, we start with translating the customer question into SQL.”article, stated
“We now can construct the prompt by combining the user question as is, the database schema, three sample rows from the table we intend to use, and a set of instructions”article, stated
“Now that we have the relevant context, we can send the user’s original question, the context retrieved, and a set of instructions back to the LLM”article, stated

Why this architecture

Q4 moved from semantic RAG to SQL generation because numerical financial data requires precise structured retrieval. Bedrock and LangChain let the team switch models by task, generate SQL from schema-rich prompts, retrieve exact database context, and summarize answers while optimizing latency and cost.

3 supporting quotes
“the dataset was converted into embeddings, stored in a vector database, and then matched with the embeddings of the question to extract context.”article, stated
“the embeddings generated from numbers struggled with similarity ranking, and in many cases led to retrieving incorrect information.”article, stated
“The idea was to use the LLM to first generate a SQL statement from the user question, presented to the LLM in natural language.”article, stated

Models used: Anthropic Claude V2 · Claude Instant · Titan Text Express · Amazon Bedrock foundation models

Scale

Alternatives considered (roads not taken)

Conventional RAG with search, semantic search, and embeddings

Rejected because: It retrieved incorrect context for numerical and structured financial data because embeddings struggled with numbers and similarity ranking.

“Conventional RAG with semantic search was the last step before moving to SQL generation.”article, stated
“the embeddings generated from numbers struggled with similarity ranking, and in many cases led to retrieving incorrect information.”article, stated

Keeping an LLM SQL validation stage

Rejected because: After testing, generated SQL was consistently accurate enough that validation did not justify its added latency and cost.

“the cost/benefit of adding an SQL validation stage unfavorable. We ended up eliminating the SQL validation stage without negatively impacting the quality of our output”article, stated

Using Claude V2 for every LLM call

Rejected because: Smaller or more performant models could satisfy simpler SQL or summarization tasks at better latency and cost.

“Claude Instant, with the same prompt data, would produce comparable quality of SQL as Claude V2 at a much better performance and price point.”article, stated
“Titan Text Expressed also offered better price-performance, as we discussed earlier.”article, stated

Patterns

rag-retrieval (retrieval architecture)

Q4 used SQL retrieval as the grounding step in a RAG-style chatbot, replacing vector retrieval with structured database context extraction.

“In this post, we demonstrated that for numerical and structured datasets, using SQL to extract the context used for augmentation can lead to more favorable results. We also demonstrated that frameworks like LangChain can minimize the coding effort.”article, stated

tool-use-orchestration (orchestration)

SQLDatabaseChain orchestrated SQL generation and database interaction between natural-language questions, schemas, LLMs, and structured data.

“we use SQLDatabaseChain in the SQL generation, simplifying and orchestrating interactions between the database and the LLM.”article, stated

model-routing (serving)

The team selected different Bedrock models for query generation, validation, and summarization depending on accuracy, latency, and cost requirements.

“we can choose the model that is most optimal for a specific task, be it SQL generation, validation, or summarization.”article, stated

prompt-chaining (prompt workflow)

The system chains SQL generation, optional validation, database retrieval, and final answer summarization as distinct LLM/context steps.

“The generated query is then run against the database to fetch the relevant context. The context is finally used to augment the input prompt for a summarization step.”article, stated

structured-output-contract (output control)

Q4 instructed the model to output clean SQL only and used schema/sample rows/field explanations to constrain generated queries.

“We now can construct the prompt by combining the user question as is, the database schema, three sample rows from the table we intend to use, and a set of instructions to generate the required SQL in clean SQL format without comments or additions.”article, stated

Tradeoffs

Use SQL generation for retrieval.

Chose Text-to-SQL over the structured financial database over Semantic vector retrieval over numerical data.

Rationale: SQL gave higher recall and more domain-bounded context for numerical datasets where embeddings retrieved incorrect information.

“Q4’s hypothesis was that in order to get higher recall for the retrieval step, specifically for the numerical dataset, they needed to first generate SQL from the user question.”article, stated

Select models per task.

Chose Claude V2 for harder query generation and smaller models for simpler or summarization tasks over One large model for every LLM call.

Rationale: Bedrock made it easy to change model names, allowing Q4 to balance accuracy, performance, and cost by task.

“provided we have the right context and instructions, Claude Instant, with the same prompt data, would produce comparable quality of SQL as Claude V2 at a much better performance and price point.”article, stated
“For more sophisticated input, Claude V2 was necessary to produce the desired accuracy.”article, stated

Eliminate SQL validation after testing.

Chose Skip the validation stage over Always run native or LLM-based SQL validation.

Rationale: The generated SQL was consistently accurate enough that removing validation improved latency and cost without hurting output quality.

“it became clear that the quality of the SQL generated was consistently accurate in a way that made the cost/benefit of adding an SQL validation stage unfavorable.”article, stated

Gotchas (what bit them)

Vector retrieval can fail on numerical financial data.

Root cause: Embeddings generated from numbers struggled with similarity ranking and retrieved incorrect information.

Resolution: Generate SQL over a structured dataset and use the SQL result as the context for the final answer.

“Considering the nature of Q4’s dataset, which is mostly financial data consisting of numbers, financial transactions, stock quotes, and dates”article, stated
“the embeddings generated from numbers struggled with similarity ranking, and in many cases led to retrieving incorrect information.”article, stated

SQL generation needs schema context, not just the user question.

Root cause: The LLM must understand the database structure, sample rows, and field meanings to generate accurate SQL.

Resolution: Include schema, sample rows, and human-readable field explanations in the prompt.

“the prompt needed to include the database schema, a few sample data rows, and human-readable field explanations for the fields that are not easy to comprehend.”article, stated

Validation stages can add unnecessary latency and cost.

Root cause: If generated SQL is already consistently accurate, a second LLM/database validation trip may not add enough value.

Resolution: Q4 removed the SQL validation stage after testing showed no negative impact on output quality.

“Knowing that, we were able to skip this validation step and save on latency and cost.”article, stated
“We ended up eliminating the SQL validation stage without negatively impacting the quality of our output and shaved off the SQL validation round trip time.”article, stated

Evaluation

Q4 evaluated retrieval approach quality during proof-of-concept experiments, compared model suitability by task, tested SQL quality, and used latency/cost tradeoffs to decide whether to retain validation and which models to use.

Metrics: Target query response time: seconds. · Final end-to-end latency: single-digit seconds. · Prompt context: two to five rows sufficient, with three rows used. · SQL generation was consistently accurate enough to remove validation.

3 supporting quotes
“After some testing, we noticed that, provided we have the right context and instructions, Claude Instant, with the same prompt data, would produce comparable quality of SQL”article, stated
“the quality of the SQL generated was consistently accurate in a way that made the cost/benefit of adding an SQL validation stage unfavorable.”article, stated
“The end-to-end latency of the overall solution came within what was defined as acceptable for the use case—single-digit seconds.”article, stated

Outcomes

The final proof-of-concept used structured ownership data and achieved single-digit-second end-to-end latency with schema-plus-three-row prompts.

3 supporting quotes
“After we had addressed all the challenges identified during the proof of concept, we had fulfilled all the solution requirements.”article, stated
“This stands true for simple tasks that required only a WHERE clause to filter the data, and also with more complex tasks that required context-based aggregations”article, stated
“With this level of flexibility, Q4 was able to choose the most performant LLM for each LLM call based on the nature of the task”article, stated