
Introduction of this Spring AI course.
Learn how to create a simple Spring AI application to interact with OpenAI. This simple application provides a REST API to receive user input, send the input to OpenAI GPT model, and sends back OpenAI's response to the user.
Basic text to SQL implementation using handwritten database metadata.
Code and demo of basic text to SQL implementation
Extract database metadata using JDBC API. Metadata is serialized into JSON string, then included in the prompt sent to LLM. Use a Spring AI advisor to update the system text of prompts.
Some columns in a database table only have a few unique values. Exact values of these columns must be used in SQL statements. These low cardinality values are included in the metadata.
Introduction to embedding models and vector stores.
Use Chroma as the vector store.
Use an offline process to extract database metadata, convert into Spring AI documents, and save to a vector store.
Use LLM to generate summary of database tables.
Collect history SQL statements and include them in the prompt sent to LLM.
Generate summary of SQL statements using LLM.
Different approaches to reduce the size of prompt content sent to LLM.
Table metadata stored in Postgres
Specify the JSON schema of query result, use LLM to generate SQL statement matching the schema
Use LLM to re-select tables from a list of tables selected from vector store.
Use a Spring AI function to execute SQL statements generated by LLM.
Use a Spring AI function to validate SQL statements generated by LLM.
Build web UI using Next.js and assistant-ui.
Prepare the application for deploying as a serverless function
Use Spring Cloud Function to deploy the metadata indexer and Text-to-SQL application as serverless functions.
Code of functions for metadata indexer and Text-to-SQL application.
Use AWS CDK to deploy serverless functions to AWS Lambda.
Demo to use AWS CDK to deploy serverless functions.
Build a Text-to-SQL MCP server with resources, prompt templates, tools and completions.
MCP sever code
MCP server demo using MCP Inspector
Changes to text-to-sql implementation:
- Use pgvector instead of Chroma
- Load environment variables from an env file when deploying to AWS Lambda
- DeepSeek integration
Table metadata store, context size and new sample database
Update to Spring 1.0.0 release version
Spring AI 1.1 updates
Building AI applications is very popular these days. For Java developers, the best choice for building AI applications is using Spring AI. To learn how to use Spring AI to build AI applications, we need to have a concrete example. Text to SQL, is a typical usage of using AI to improve productivity. By using text to SQL, non-technical people use natural language to describe database query requirements. These queries are sent to LLM. LLM can generate SQL statements to answer user queries. LLM can also use tools to execute SQL statements, and return the query results to the user. Text to SQL is a good example of AI applications.
In this course, we will use Spring AI to create a text to SQL application. After learning this course, you will know:
How to use ChatClient to send requests to LLM and receive responses.
How to extract database metadata and include them in the prompt sent to LLM.
How to use Spring AI advisors to intercept ChatClient requests to process requests and responses.
How to use embedding model and vector store to implement semantic search of database metadata.
How to use LLM to generate summary of database tables and SQL statements.
How to use LLM to re-select tables automatically.
How to allow user to manually re-select tables using message history.
How to execute and validate SQL statements using functions.
How to deployment metadata indexer and Text-to-SQL application as serverless functions on AWS Lambda.
How to store table metadata in a database.
How to create a Text-to-SQL MCP server using Spring AI and MCP Java SDK.
This course covers all major aspects of Spring AI, including ChatClient, advisors, embedding models, vector stores, chat memory and function calling.
What you have learned in this course can help you build other AI applications using Spring AI.
The code has been updated to Spring AI 1.1 release version.
You can also get a free copy of the PDF book.
Source code: This course provides full source code of the text to SQL application. The source code can be downloaded from resource of 5th lecture. You can also send me your GitHub account email to access the private GitHub repository.