Class SqlDatabaseContentRetriever
java.lang.Object
dev.langchain4j.experimental.rag.content.retriever.sql.SqlDatabaseContentRetriever
- All Implemented Interfaces:
ContentRetriever
WARNING! Although fun and exciting, this class is dangerous to use! Do not ever use this in production!
The database user must have very limited READ-ONLY permissions!
Although the generated SQL is somewhat validated (to ensure that the SQL is a SELECT statement) using JSqlParser,
this class does not guarantee that the SQL will be harmless. Use it at your own risk!
Using the
Optionally,
The default prompt template is not highly optimized, so it is advised to experiment with it and see what works best for your use case.
Using the
DataSource
and the ChatLanguageModel
, this ContentRetriever
attempts to generate and execute SQL queries for given natural language queries.
Optionally,
sqlDialect
, databaseStructure
, promptTemplate
, and maxRetries
can be specified
to customize the behavior. See the javadoc of the constructor for more details.
Most methods can be overridden to customize the behavior further.
The default prompt template is not highly optimized, so it is advised to experiment with it and see what works best for your use case.
-
Constructor Summary
ConstructorDescriptionSqlDatabaseContentRetriever
(DataSource dataSource, String sqlDialect, String databaseStructure, PromptTemplate promptTemplate, ChatLanguageModel chatLanguageModel, Integer maxRetries) Creates an instance of aSqlDatabaseContentRetriever
. -
Method Summary
Modifier and TypeMethodDescriptionprotected String
protected Prompt
protected String
protected String
generateSqlQuery
(Query naturalLanguageQuery, String previousSqlQuery, String previousErrorMessage) static String
getSqlDialect
(DataSource dataSource) protected boolean
protected void
-
Constructor Details
-
SqlDatabaseContentRetriever
public SqlDatabaseContentRetriever(DataSource dataSource, String sqlDialect, String databaseStructure, PromptTemplate promptTemplate, ChatLanguageModel chatLanguageModel, Integer maxRetries) Creates an instance of aSqlDatabaseContentRetriever
.- Parameters:
dataSource
- TheDataSource
to be used for executing SQL queries. This is a mandatory parameter. WARNING! The database user must have very limited READ-ONLY permissions!sqlDialect
- The SQL dialect, which will be provided to the LLM in theSystemMessage
. The LLM should know the specific SQL dialect in order to generate valid SQL queries. Example: "MySQL", "PostgreSQL", etc. This is an optional parameter. If not specified, it will be determined from theDataSource
.databaseStructure
- The structure of the database, which will be provided to the LLM in theSystemMessage
. The LLM should be familiar with available tables, columns, relationships, etc. in order to generate valid SQL queries. It is best to specify the complete "CREATE TABLE ..." DDL statement for each table. Example (shortened): "CREATE TABLE customers(\n id INT PRIMARY KEY,\n name VARCHAR(50), ...);\n CREATE TABLE products(...);\n ..." This is an optional parameter. If not specified, it will be generated from theDataSource
. WARNING! In this case, all tables will be visible to the LLM!promptTemplate
- ThePromptTemplate
to be used for creating aSystemMessage
. This is an optional parameter. Default:DEFAULT_PROMPT_TEMPLATE
.chatLanguageModel
- TheChatLanguageModel
to be used for generating SQL queries. This is a mandatory parameter.maxRetries
- The maximum number of retries to perform if the database cannot execute the generated SQL query. An error message will be sent back to the LLM to try correcting the query. This is an optional parameter. Default: 1.
-
-
Method Details
-
getSqlDialect
-
retrieve
Description copied from interface:ContentRetriever
Retrieves relevantContent
s using a givenQuery
. TheContent
s are sorted by relevance, with the most relevantContent
s appearing at the beginning of the returnedList<Content>
.- Specified by:
retrieve
in interfaceContentRetriever
- Parameters:
naturalLanguageQuery
- TheQuery
to use for retrieval.- Returns:
- A list of retrieved
Content
s.
-
generateSqlQuery
-
createSystemPrompt
-
clean
-
validate
-
isSelect
-
execute
- Throws:
SQLException
-