A text-to-sql intelligent agent coordination system and method for perceiving potential user intent in a multi-round context
By introducing modules such as a memory bank, a suggestion strategy pool, and a strategy recommender into the Text-to-SQL system, the optimal strategy is dynamically selected, which solves the problem of changing user intent in multi-turn interactions and improves the accuracy and stability of SQL generation.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- DEPPSHEN (XIAMEN) DATA TECH CO LTD
- Filing Date
- 2026-05-08
- Publication Date
- 2026-06-05
Smart Images

Figure CN122152877A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the technical fields of natural language processing and big data models, and in particular to a Text-to-SQL intelligent agent collaborative system and method for perceiving potential user intentions in multi-turn contexts. Background Technology
[0002] Text-to-SQL technology supports the automatic translation of user-input natural language queries into structured query statements (SQL), lowering the barrier for non-expert users to access structured databases. In recent years, with the rapid development of Large Language Models (LLMs), LLM-based Text-to-SQL technology has made significant progress. Existing technologies typically involve concatenating the user's natural language query with database schema information and inputting it into the LLM. Through thought chain prompts, the model is guided to generate the corresponding SQL query. This method is suitable for single-round queries and demonstrates good performance in one-time translation.
[0003] However, existing technologies have significant limitations in multi-turn interaction scenarios. First, most methods only use historical dialogues as a simple contextual concatenation input model, failing to model the evolution of potential user intent during multiple interactions. When a user adjusts their questioning style due to unsatisfactory initial query results, the system struggles to capture this change in intent, leading to generated SQL statements that deviate from the user's actual needs. Second, existing solutions typically employ statically configured prompt strategy templates. Even when introducing various thought chain forms, they lack a dynamic strategy selection mechanism adapted to the current context, making flexible matching difficult and resulting in translated SQL statements that fail to meet the user's true needs. Furthermore, they lack the ability to accumulate experience. While existing methods introduce feedback mechanisms for error correction, the feedback information is often only used for localized error correction in a single query, failing to learn the user's personalized preferences from historical interactions, thus limiting performance improvements in long-term use.
[0004] Therefore, how to dynamically adapt the prompt strategy according to changes in intent, and how to continuously learn and optimize from historical interactions, have become problems that Text-to-SQL technology urgently needs to solve. Summary of the Invention
[0005] The purpose of this invention is to provide a Text-to-SQL intelligent agent collaborative system and method that can perceive the user's potential intent in a multi-turn context. The technical problem to be solved is to provide a strategy selection that matches the user's intent before SQL generation, so as to constrain the generation of SQL query statements.
[0006] To achieve the above objectives, the solution of the present invention is: a Text-to-SQL intelligent agent collaborative system that perceives the potential intent of users in multi-turn contexts, including a memory, a prompting policy pool, a policy recommender, an SQL generator, an SQL refiner, and a policy optimizer; A system memory is used to store system operation data. The prompt policy pool is used to store prompt policies; The strategy recommender receives user information from the user side and strategy information from the suggestion strategy pool. Based on the user information and strategy information, it identifies the user's potential intent in the current round and matches the optimal strategy from the suggestion strategy pool according to the potential intent to constrain the generation of SQL query statements. The SQL generator generates corresponding SQL query statements based on the user's natural language query in the current round, the system operation data stored in the memory, and the optimal strategy extracted by the strategy recommender. SQL refiner, used to refine and update SQL query statements; The strategy optimizer updates the hint policies in the hint policy pool based on the adjustment information from the SQL refiner.
[0007] Furthermore, the strategy recommender processes user information and strategy information through a dual-branch learning architecture, extracting long-term user preference features and current semantic demand features respectively. Based on the extracted features, it outputs decision information for strategy selection, which is the matching score or intent type information of each prompt strategy. Based on the output decision information, it selects the optimal strategy from the prompt strategy pool to constrain the generation of SQL query statements.
[0008] Furthermore, the user information includes at least the user's multi-turn history of dialogue, current natural language query, and user ID, and the policy information includes at least the policy ID and policy text; The dual-branch learning architecture includes an ID latent representation branch and a semantic latent representation branch; The ID latent representation branch is used to convert the user ID and the policy ID into a fixed-dimensional ID embedding vector through a learnable embedding mapping, and after processing by the first cross-attention mechanism, extract the user's long-term preference for the policy and output the user-side representation vector. The semantic latent representation branch uses a pre-trained text embedding model to vectorize the multi-turn historical dialogue, current natural language query, and policy text, and processes them through a second cross-attention mechanism to extract the matching features between the user's current semantic needs and the policy content, and outputs the policy-side representation vector. The strategy recommender calculates the matching score of each prompt strategy based on the user-side representation vector and the strategy-side representation vector through the inner product, and outputs the prompt strategy with the highest score as the optimal strategy. When there is no historical dialogue, the default prompt strategy is selected as the optimal strategy.
[0009] Furthermore, positive and negative policy samples are constructed based on the execution results of SQL query statements. The prompt policies corresponding to SQL statements that are executed successfully and meet the correctness criteria are used as positive policy samples, and the prompt policies corresponding to SQL statements that fail to meet the correctness criteria are used as negative policy samples. The user-side representation vector, positive policy samples, and negative policy samples are trained in pairs and optimized using the BPR loss function to make the matching score of positive policy samples higher than that of negative policy samples. ; in, The BPR ranking loss is used to train the policy recommender, ensuring that the scores of positive policy samples are higher than those of negative policy samples. For training user set, For the current context, For users and context The set of positive policies, For positive policy samples, Negative policy samples For positive policy prediction scores, The predicted score is for the negative strategy; The difference between positive and negative strategy scores; : Sigmoid function; Logarithmic loss term: the smaller the score difference, the greater the penalty. Double summation : The total accumulated loss for users and their sample pairs.
[0010] Furthermore, the intent type information is either a discrete intent category identifier or a vector representation of the confidence probability of different intent categories.
[0011] Furthermore, the suggestion strategy pool includes a variety of different suggestion strategies, each corresponding to a different thought chain type, which is used to constrain the reasoning path of the SQL generator to guide the SQL generator to use different reasoning strategies to generate SQL query statements. The suggestion strategies include at least three thought chain types: no thought chain, problem-breaking thought chain, and query plan-mimicking thought chain.
[0012] Furthermore, the SQL generator combines the optimal strategy, the user's natural language query, and database schema information to generate the SQL query statement through a large language model.
[0013] Furthermore, it also includes a database, which is used to store the target data tables and their related data involved in the user query. The database engine parses and executes the SQL query statements output by the SQL generator and returns the corresponding query results or execution exception feedback information. The SQL refiner is used to input the execution exception information and execution result returned by the database, the natural language query of the current round of the user, and the database schema information into the large language model when an exception occurs in the SQL query statement during the database execution phase, or when the user is not satisfied with the execution result. It generates a modified SQL query statement to adjust and update the SQL query statement, and iterates until the user is satisfied.
[0014] Furthermore, the strategy optimizer is used to obtain the SQL query statement generation log, system feedback information, and user feedback information. The system feedback information is from the database execution phase, including execution results or exception information, and the user feedback information is information provided by the user during the usage process.
[0015] The present invention also provides a method for a Text-to-SQL intelligent agent collaborative system that applies the above-described method for perceiving potential user intent in multi-turn contexts, the method comprising: S1, based on the user's multi-turn historical dialogue information, retrieve contextual information related to the user's current natural language query from the memory bank; S2, based on the historical dialogue information, perceive the current user intent and recommend the optimal matching strategy; S3. Based on the optimal strategy recommended in step S2, the user's current natural language query, and context information, generate an SQL query statement; S4, execute the SQL query statement and obtain the execution result or execution exception information; S5. When there is an anomaly in the execution result or the user is not satisfied with the execution result, adjust the SQL query statement and record the adjustment process information. The adjustment process information and the prompting strategy are concatenated and input into the large language model to optimize the prompting strategies in the prompting strategy pool, generate new prompting strategies, and update the prompting strategy pool for use in the subsequent SQL query generation process.
[0016] After adopting the above solution, the beneficial effects of the present invention are as follows: (1) Add strategy selection before SQL generation, and constrain the reasoning path of SQL generation through the optimal strategy, so that the SQL generator can generate SQL statements that meet the user's needs at one time with a higher probability. This is different from the existing technology of generating multiple SQL statements and then evaluating and filtering them. It significantly reduces the number of calls to the large language model and reduces the overhead. (2) Accurately capture the user's potential intent. The semantic potential representation branch uses a pre-trained text embedding model to vectorize the context and policy text. The ID potential representation branch uses a learnable embedding mapping to map the user identifier and policy identifier into fixed-dimensional vectors. It accurately models the user's current semantic needs, can grasp the evolution trend of the user's intent in multi-turn interactions and adapt to it, and improve the accuracy of intent understanding in multi-turn dialogue scenarios. (3) Dynamically select the optimal prompting strategy, use cross-attention mechanism to fuse dual-branch features, generate user intent type information that represents the user's potential intent, and match the strategy based on the user intent type information, dynamically and adaptively select the optimal strategy, and can automatically adjust the prompting strategy according to different users and different scenarios to avoid production deviations caused by strategy rigidity. (4) This invention achieves dynamic selection and continuous optimization of prompting strategies without the need for parameter fine-tuning of large language models, avoiding the high cost and deployment complexity of traditional fine-tuning methods, effectively improving the accuracy, stability and robustness of SQL generation in complex multi-round interaction scenarios, and significantly improving the structural integrity, semantic consistency and execution success rate of generated SQL statements, thereby enhancing the practicality of the Text-to-SQL system and the user's interactive experience. Attached Figure Description
[0017] Figure 1 This is a schematic diagram of the architecture of the Text-to-SQL intelligent agent collaborative system according to Embodiment 1 of the present invention.
[0018] Figure 2 This is a schematic diagram of the intent-aware strategy recommender of Embodiment 1 of the present invention.
[0019] Figure 3 This is a flowchart illustrating the present invention. Detailed Implementation
[0020] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0021] For ease of explanation, a prompt policy in the prompt policy pool is represented as a policy. p ,Strategy p The corresponding text is represented as T p Each strategy p With a unique identifier, strategy p The identifier is represented as ID p User ID is represented as ID u The user question in the current round is represented as q t The sequence of user questions in the most recent k rounds is denoted as { q t-k , ..., q t-1}
[0022] Example 1 In one exemplary embodiment, such as Figures 1 - 3 As shown, a Text-to-SQL intelligent agent collaborative system that perceives the user's potential intent in a multi-turn context is provided, including a memory, a prompting policy pool, a policy recommender, an SQL generator, an SQL refiner, a policy optimizer, and a database.
[0023] (i) A database memory is used to store system operation data. This data includes: user's multi-turn historical dialogues, SQL query statements generated in each turn, database schema information, system feedback information, SQL refiner logs, and user feedback. System feedback information originates from the database execution phase and includes execution results or exception information returned by the database. Exception information includes, but is not limited to: syntax errors, non-existent table columns, type mismatches, and empty results. When a user's question for the current turn is received... q t When the context is retrieved, the memory retrieves and returns the context related to the question, including at least the current question and the most recent k rounds of questions, and may also include the corresponding SQL query statement and schema summary, providing input for subsequent strategy recommendation and SQL query statement generation. Preferably, if there is no historical dialogue in the current session, the context identifier returned by the memory is null, which triggers the strategy recommender to recommend the default prompt strategy.
[0024] (ii) Hint strategy pool, used to store hint strategies. The hint strategy pool includes a variety of different hint strategies. Different hint strategies are for various Text-to-SQL scenarios. Different hint strategies correspond to different thought chain types and are used to constrain the reasoning path of the SQL generator to guide the SQL generator to use different reasoning strategies to generate SQL query statements. The hint strategy includes at least three thought chain types, namely no thought chain, problem splitting thought chain, and query plan imitation thought chain. The hint strategy pool can be dynamically updated. Optionally, each policy in the policy pool should include: Policy ID ( ID p ), strategy text ( T p The system includes a version number, applicable tags, and historical performance statistics. The version number indicates the iterative version of the same strategy, such as V1, V2, V3, etc. The applicable tags describe the application scenarios suitable for the suggestion strategy, such as multi-table joins and aggregation statistics, to assist the strategy recommender in filtering. The historical performance statistics record the performance data of the suggestion strategy in historical operation, such as execution success rate, result accuracy rate, average number of iterations, user satisfaction rate, etc., as the basis for the performance of the suggestion strategy to support the ranking of suggestion strategy recommendations and strategy updates.
[0025] (iii) Strategy recommender, which receives user information from the user side and strategy information of the prompt strategy pool. Based on the user information and strategy information, it identifies the user's potential intention in the current round and matches the optimal strategy from the prompt strategy pool according to the potential intention to constrain the generation of SQL query statements.
[0026] The user information includes at least the user's multi-turn history of dialogue, current natural language query, and user ID, and the policy information includes at least the policy ID and policy text.
[0027] The strategy recommender processes user information and strategy information through a dual-branch learning architecture, extracting long-term user preference features and current semantic demand features respectively. Based on the extracted features, it outputs decision information for strategy selection, which is the matching score or intent type information of each prompt strategy. Based on the output decision information, it selects the optimal strategy from the prompt strategy pool to constrain the generation of SQL query statements.
[0028] The matching score is used to characterize the degree of matching between the user's potential intent in the current round and each prompting strategy.
[0029] The intent type information can be a discrete intent category identifier or a vector representation of the confidence probability of different intent categories.
[0030] The dual-branch learning architecture includes an ID latent representation branch and a semantic latent representation branch; The ID latent representation branch is used to convert the user ID and policy ID into a fixed-dimensional ID embedding vector through a learnable embedding mapping, and after processing by the first cross-attention mechanism, extract the user's long-term preference for the policy and output the user-side representation vector. The semantic latent representation branch uses a pre-trained text embedding model to vectorize the multi-turn historical dialogue, current natural language query, and policy text, and processes them through a second cross-attention mechanism to extract the matching features between the user's current semantic needs and the policy text content, and outputs the policy-side representation vector. The strategy recommender calculates the matching score of each prompt strategy based on the user-side representation vector and the strategy-side representation vector through the inner product, and uses BPR loss for optimization training. It outputs the prompt strategy with the highest score as the optimal strategy. When there is no historical dialogue, the default prompt strategy is selected as the optimal strategy.
[0031] The following provides a detailed explanation of the specific structure and processing flow of the strategy recommender; 1. In this specific embodiment, the input to the strategy recommender includes at least the following two types of information: (1) ID input: This includes user ID input and policy ID input, specifically represented as follows: User ID (User Identifier): ID u ; Policy identifier (policy ID): ID p ; (2) Semantic input: This includes text input from the user's context and text input from the policy side. The text input from the user's context includes the user's multi-turn history dialogue and the current natural language query. The text input from the policy side is the policy text, and the specific symbols are as follows: Questions from users in the current round: q t ; Recent k-round user questions: { q t-k , ..., q t-1}; The above concatenations form a context sequence: ctx t = [ q t-k , ..., q t-1 , q t ]; Strategy text: Tp 2. Two-branch representation learning: ID latent representation and semantic latent representation; In this specific embodiment, to avoid the problems of difficulty in modeling intent drift and unstable policy selection caused by relying solely on context concatenation, representation learning is divided into two complementary branches: 2.1, The ID latent representation branch is used to obtain users' long-term preferences, which can be interpreted as user habits; ID potential representation branches ID u and ID p As input, an initial vector is obtained through embedding mapping: xu id = EmbedU( ID u ), xp id = EmbedP( ID p ); Where EmbedU is the user ID embedding mapping function, used to embed discrete user identifiers. ID u Mapped into a dense vector of fixed dimensions, representing long-term user preferences or habitual characteristics; EmbedP is a policy ID embedding mapping function used to embed discrete policy identifiers. ID p Mapped into a fixed-dimensional dense vector, representing the identity and learnable features of the policy; xu id This represents a vector of long-term user preferences based on their ID. xp id Represents an ID-based policy vector; To make the influence of user preferences on policy selection interpretable, this embodiment introduces cross-attention, enabling user vectors to align with the differences between different policy vectors, thereby outputting a user-side representation. .
[0032] 2.2 Semantic latent representation branches are used to obtain the user's current needs, which can be interpreted as how the user currently wants to ask or search; The semantic latent representation branch input is ctx t and T p Each is encoded as a sequence representation: H ctx = EncCtx( ctx t ), Hp = EncP( T p); in, H ctx For context sequence, Hp The strategy text represents a sequence; EncCtx is a context encoder used to encode multi-turn dialogue context. ctx t = [ q t-k , ..., q t-1 , q t Encoded as a contextual semantic representation (sequence / vector) to characterize the current query context; EncP is a policy text encoder used to translate policy prompt text. T p Encode it as a policy semantic representation (sequence / vector) to characterize the reasoning semantic features of the policy; Similarly, cross-attention is introduced to allow the policy text to focus on the parts of the context relevant to its reasoning perspective, outputting the final policy-side representation. .
[0033] 3. A two-branch cross-attention mechanism, including a first cross-attention mechanism and a second cross-attention mechanism, wherein, 3.1, First cross-attention mechanism: ID-aware cross-attention (user-policy alignment); Using the user ID vector as the query and the policy ID vector as the key / value pair, the user-side output is obtained: ; ; in, This represents the intensity of the user's attention to or preference for this policy at the ID level. : User-side input vector, the initial representation obtained by embedding the user ID; : Input vector on the policy ID side, the initial representation obtained by embedding the policy ID; Query projection matrix, which maps user vectors to the query space; The key projection matrix maps the policy ID vector to the key space. Value projection matrix maps the policy ID vector to the value space; The feature dimension corresponding to the scaling factor; (·): Normalization function, which transforms the relevance score into an attention weight distribution; ID branch attention weight, representing the intensity of user attention to the ID feature of the candidate strategy; • : Weighted summation / matrix multiplication operation, aggregating values using attention weights; Output projection matrix, which maps the attention aggregation result to the end-user representation; : The final representation vector on the user side; Matrix transpose operation; : Scaling term, to prevent the dot product from becoming too large and causing softmax saturation; Optionally, when the candidate policy set is large, multiple p can be computed in parallel. Output the distribution of user preferences for different strategies.
[0034] 3.2, Second Cross-Attention Mechanism: Semantic Cross-Attention (Policy Text-Context Alignment); Using the policy text as the query and the context as the key / value pair, we obtain the policy-side output: ; ; Subsequently The result is obtained by pooling / aggregating, for example, by taking the [CLS] bit or by average pooling: ; in, The row / column correspondence is as follows: the matching strength between a certain instruction in the strategy text and a certain round of questions or certain key phrases in the context; a certain instruction may be: decompose steps, find the table first, make constraints first, etc. : The semantic representation sequence after policy text encoding; : Sequence of semantic representations encoded from the dialogue context; The query projection matrix maps the semantic representation of the strategy to the query space. The key projection matrix maps the contextual semantic representation to the key space; The value projection matrix maps the contextual semantic representation to the value space. Semantic branch attention weights represent the intensity of attention the policy text pays to each position in the context. : The semantic representation of the strategy after incorporating contextual information.
[0035] 4. Matching scoring and strategy selection; In obtaining user-side representation Representation of the strategy side Therefore, this embodiment uses the inner product as the matching scoring function: ; Final strategy selection: ; and output System prompting strategy for SQL generators; in, The set of suggestion policies in the suggestion policy pool. For the current user identifier, For users With strategy The predicted matching score or preference score, To select the independent variable that maximizes the objective value; The optimal strategy that is ultimately selected.
[0036] 5. Training Sample Construction: The execution effect of the generated SQL query statement is used as the supervision signal, that is, the SQL execution success is a good strategy, and the SQL execution failure is a bad strategy. In order to enable the strategy recommender to automatically learn to select strategies that are more likely to produce good strategies, this embodiment transforms the strategy selection quality in a single dialogue into pairwise supervision: Positive Strategy In the same ( ID u , ctx t Under this ) strategy, the generated SQL is made to satisfy the correctness judgment. negative strategy In the same ( ID u , ctx t This strategy can lead to SQL execution errors or failure to meet correctness criteria.
[0037] Optionally, negative policy Samples can be taken from the set of policies that users have not used or the set of historically failed policies. Specifically, Usually with Pair sampling is used to form training triples. , , ).
[0038] 6. Loss function and optimization objective; This embodiment uses the standard BPR loss as the main loss function, ensuring that the score for positive policies is higher than that for negative policies. ; in: For the Sigmoid function, by minimizing This can enable the model to learn that if the user and the context are the same, the score of the correct policy should be significantly higher than that of the wrong policy, thereby improving the stability and accuracy of policy selection during the inference phase. Among them, L BPR BPR ranking loss is used to train the policy recommender so that the scores of positive policy samples are higher than the scores of negative policy samples. For training user set, For the current context, For users and context The set of positive policies, For positive policy samples, Negative policy samples For positive policy prediction scores, The predicted score is for the negative strategy; The difference between positive and negative strategy scores; The Sigmoid function maps the score difference to (0,1); Logarithmic loss term: the smaller the score difference, the greater the penalty. Double summation : The total accumulated loss for users and their sample pairs.
[0039] The loss function is used to update the following parameters: ID latent representation parameters, bi-branch cross-attention parameters, and related parameters used for aggregation and scoring.
[0040] Further focus on Figure 2 The structure and processing of the strategy recommender are explained in detail. The structure is divided into three functional areas: user and prompt strategy hybrid representation, dual cross-attention intent perception and pairwise learning. The left input end receives user information and strategy information at the same time. The user information includes at least the user's multi-turn history dialogue, the current natural language query and user ID. The strategy information includes at least the strategy ID and strategy text.
[0041] In the representation layer, the policy recommender constructs an ID latent representation branch and a semantic latent representation branch, respectively. The ID latent representation branch is used to map user IDs and policy IDs into dense vectors corresponding to discrete identifiers, while the text latent representation branch is used to encode multi-round query contexts and policy texts into semantic vectors.
[0042] At the intent-aware layer, the policy recommender employs a two-way cross-attention mechanism for alignment modeling. One of these mechanisms is a user-policy intent recognition cross-attention based on ID awareness, which outputs a user-side representation vector. The other approach is a text-aware user-policy intent recognition cross-attention method, which outputs a policy-side representation vector. Both represent the matching relationship between the current user and the candidate strategy in the current context.
[0043] In the training layer, a pairwise learning approach is used to construct positive and negative policy samples to obtain the positive policy representation. With negative policy representation And based on the loss function L = f ( , , Optimize the strategy to make the scores of positive strategy samples higher than those of negative strategy samples, thereby improving the accuracy and stability of strategy recommendations.
[0044] In the diagram, the flame icon indicates that the corresponding module parameters are involved in training updates, while the snowflake icon indicates that the corresponding module parameters are frozen. This enables a collaborative training method between the trainable recommender and the frozen base encoder, completing policy matching learning without fine-tuning the backbone of the large language model.
[0045] During the inference phase, the policy recommender scores the suggestion policies in the suggestion policy pool based on the matching ability obtained during training and outputs the optimal policy for subsequent SQL generator to call.
[0046] In other embodiments, the strategy recommender takes the user ID, historical questions, current question, and suggestion strategies in the suggestion strategy pool as input, and determines the most suitable suggestion strategy for the current query by calculating the cosine similarity between the current user feature vector and the feature vectors of each suggestion strategy, and recommends the most suitable suggestion strategy for the current query.
[0047] (iv) The SQL generator generates corresponding SQL query statements based on the user's natural language query in the current round, the system operation data stored in the memory, and the optimal strategy extracted by the strategy recommender. Specifically, the SQL generator receives the context output by the memory and the strategy text output by the strategy recommender. As input prompts, SQL query statements are generated by calling a large language model. SQLt .
[0048] Preferably, the SQL generator can provide recommended strategy text during the construction process. Context ctx t Database schema information is assembled in a preset order, taking into account the reasoning perspective and structural form of the constraint generation process, thereby improving the integrity of the SQL structure and constraint coverage. Specifically, The reasoning path and structure of the constraint generation process, such as decomposition first, planning first, or direct generation.
[0049] (v) Database, used to store the target data tables and their related data involved in user queries. The database engine parses and executes the SQL query statements output by the SQL generator and returns the corresponding query results or execution exception feedback information. Specifically, the database stores the target business data tables and related data, such as table structure, fields, primary and foreign keys, execution results, etc. Exception information includes syntax errors, table columns not found, type mismatch, empty results, etc. The database and memory work together. The memory first outputs the context, and then the database completes the query and database execution. The database schema information is specifically the structural description information of the database, such as database name, table name, field name, primary and foreign keys, constraints, etc.
[0050] (vi) SQL Refiner, used to adjust and re-execute the current SQL query statement when an exception occurs during the database execution phase. It inputs the execution exception information and execution results returned by the database, the natural language query of the current round of the user, and the database schema information into the large language model to generate a modified SQL query statement, so as to adjust and update the SQL query statement, and iterate until the user is satisfied, so as to improve the execution success rate and result satisfaction. When the database returns an execution exception, or the user is dissatisfied with the execution result, the process of adjusting and re-executing the current SQL includes at least the following steps: Step 1: Receive information, including the current SQL, database exception information or execution results, the current question and context; Step 2: Generate adjusted SQL based on the large language model; Step 3: Resubmit the database operation and obtain feedback; Step 4: If the problem persists, continue iterative adjustments, up to n iterations. If the user continues to request adjustments, the user-driven adjustment rounds can be disregarded by the aforementioned n-times limit.
[0051] (vii) The strategy optimizer updates the suggestion strategies in the suggestion strategy pool based on the adjustment information of the SQL refiner. The strategy optimizer is used to obtain the generation log of SQL query statements, system feedback information, and user feedback information. The system feedback information is the database execution result information, including execution results or exception information. The user feedback information is the information provided by the user during use, including dissatisfaction, requests for further adjustment, etc. Specifically, the strategy optimizer receives the adjustment process records of the SQL refiner in the current round, such as: the original SQL, error information, adjusted SQL, summary of adjustment reasons, summary of user feedback, etc., and adjusts the currently selected strategy accordingly. T p* Optimize the policy, generate updated policy text, and write it back to the policy pool to implement policy iteration.
[0052] Preferably, the update method of the policy optimizer is as follows: taking the adjustment process record and the current policy text as input, a new policy with implicit intention preferences is generated through a large language model to reduce repeated errors in similar scenarios.
[0053] In this specific embodiment, the Text-to-SQL intelligent agent collaborative system includes several modules: a memory, a prompting strategy pool, a strategy recommender, an SQL generator, an SQL refiner, a strategy optimizer, and a database. Each module works collaboratively around a closed-loop process of user questioning → strategy selection → SQL generation → database execution → adjustment of erroneous SQL → strategy update, in order to realize the perception of the user's potential analysis intent and the dynamic recommendation and continuous optimization of prompting strategies in multi-round interaction scenarios.
[0054] This invention also provides a method for a Text-to-SQL intelligent agent collaborative system that perceives potential user intent in multi-turn contexts, combined with... Figure 3 As shown, the method includes the following steps: S1, based on the user's multi-turn historical dialogue information, retrieves contextual information related to the user's current natural language query from the memory; specifically, it first receives the user's question for the current turn, then performs a contextual retrieval from the memory, and returns the context. ctx t = [ q t-k , ..., q t-1 , q t If there is no history of dialogue, an empty context identifier is returned; S2, based on the historical dialogue information, perceive the current user intent and recommend the optimal matching strategy; specifically, score the matching of each prompt strategy and calculate it through the strategy recommender. and and obtain the matching score. ; S3, based on the optimal strategy recommended in step S2, the user's current natural language query, and context information, generate an SQL query statement; specifically, first select... Corresponding strategy text If there is no history of conversations, the default prompting strategy will be selected directly, and then the SQL generator will be used to generate ( ctx t , The prompt is created by combining the database schema information (LLM), and the SQL statement is generated by entering LLM. SQL t ; S4, execute the SQL query statement and obtain the execution result or execution exception information; specifically, first execute through the database. SQL t It returns the query result if the execution was successful, or provides feedback on any execution errors. S5. When there is an abnormality in the execution result or the user is not satisfied with the execution result, the SQL query statement is adjusted and the adjustment process information is recorded; the adjustment process information is concatenated with the prompting strategy and input into the large language model to optimize the prompting strategy in the prompting strategy pool, generate a new prompting strategy, update the prompting strategy pool, and use it for the subsequent SQL query generation process. Specifically, the SQL refiner is used for adjustments. If an error message is returned or the user is dissatisfied, the SQL refiner is triggered to iterate until successful or the maximum number of iterations n is reached. The adjustment process is then recorded and sent to the prompting strategy optimizer to generate an update strategy. Write back to the hint policy pool.
Claims
1. A Text-to-SQL intelligent agent collaborative system that perceives potential user intent in multi-turn contexts, characterized in that: It includes a policy memory, a policy suggestion pool, a policy recommender, an SQL generator, an SQL refiner, and a policy optimizer; A system memory is used to store system operation data. The prompt policy pool is used to store prompt policies; The strategy recommender receives user information from the user side and strategy information from the suggestion strategy pool. Based on the user information and strategy information, it identifies the user's potential intent in the current round and matches the optimal strategy from the suggestion strategy pool according to the potential intent to constrain the generation of SQL query statements. The SQL generator generates corresponding SQL query statements based on the user's natural language query in the current round, the system operation data stored in the memory, and the optimal strategy extracted by the strategy recommender. SQL refiner, used to refine and update SQL query statements; The strategy optimizer updates the hint policies in the hint policy pool based on the adjustment information from the SQL refiner.
2. The Text-to-SQL intelligent agent collaborative system for perceiving potential user intent in multi-turn contexts as described in claim 1, characterized in that: The strategy recommender processes user information and strategy information through a dual-branch learning architecture, extracting long-term user preference features and current semantic demand features respectively. Based on the extracted features, it outputs decision information for strategy selection, which is the matching score or intent type information of each prompt strategy. Based on the output decision information, it selects the optimal strategy from the prompt strategy pool to constrain the generation of SQL query statements.
3. The Text-to-SQL intelligent agent collaborative system for perceiving potential user intent in multi-turn contexts as described in claim 2, characterized in that: The user information includes at least the user's multi-turn history of dialogue, current natural language query, and user ID; the policy information includes at least the policy ID and policy text. The dual-branch learning architecture includes an ID latent representation branch and a semantic latent representation branch; The ID latent representation branch is used to convert the user ID and policy ID into a fixed-dimensional ID embedding vector through a learnable embedding mapping, and after processing by the first cross-attention mechanism, extract the user's long-term preference for the policy and output the user-side representation vector. The semantic latent representation branch uses a pre-trained text embedding model to vectorize the multi-turn historical dialogue, current natural language query, and policy text, and processes them through a second cross-attention mechanism to extract the matching features between the user's current semantic needs and the policy text content, and outputs the policy-side representation vector. The strategy recommender calculates the matching score of each prompt strategy based on the user-side representation vector and the strategy-side representation vector through the inner product, and outputs the prompt strategy with the highest score as the optimal strategy. When there is no historical dialogue, the default prompt strategy is selected as the optimal strategy.
4. The Text-to-SQL intelligent agent collaborative system for perceiving potential user intent in multi-turn contexts as described in claim 3, characterized in that: Positive and negative policy samples are constructed based on the execution results of SQL query statements. The prompt policies corresponding to SQL statements that are executed successfully and meet the correctness criteria are used as positive policy samples, and the prompt policies corresponding to SQL statements that fail to meet the correctness criteria are used as negative policy samples. The user-side representation vector, positive policy samples, and negative policy samples are trained in pairs and optimized using the BPR loss function to make the matching score of positive policy samples higher than that of negative policy samples. ; in, The BPR ranking loss is used to train the policy recommender, ensuring that the scores of positive policy samples are higher than those of negative policy samples. For training user set, For the current context, For users and context The set of positive policies, For positive policy samples, Negative policy samples For positive policy prediction scores, The predicted score is for the negative strategy; The difference between positive and negative strategy scores; : Sigmoid function; Logarithmic loss term: the smaller the score difference, the greater the penalty. Double summation : The total accumulated loss for users and their sample pairs.
5. The Text-to-SQL intelligent agent collaborative system for perceiving potential user intent in multi-turn contexts as described in claim 2, characterized in that: The intent type information is either a discrete intent category identifier or a vector representation of the confidence probability of different intent categories.
6. The Text-to-SQL intelligent agent collaborative system for perceiving potential user intent in multi-turn contexts as described in claim 1, characterized in that: The suggestion strategy pool includes a variety of different suggestion strategies, each corresponding to a different thought chain type. These strategies constrain the reasoning path of the SQL generator, guiding it to use different reasoning strategies to generate SQL query statements. The suggestion strategies include at least three thought chain types: no thought chain, problem-splitting thought chain, and query plan imitation thought chain.
7. The Text-to-SQL intelligent agent collaborative system for perceiving potential user intent in multi-turn contexts as described in claim 1, characterized in that: The SQL generator combines the optimal strategy, the user's natural language query, and database schema information to generate the SQL query statement through a large language model.
8. The Text-to-SQL intelligent agent collaborative system for perceiving potential user intent in multi-turn contexts as described in claim 1, characterized in that: It also includes a database, which stores the target data tables and related data involved in the user query. The database engine parses and executes the SQL query statements output by the SQL generator and returns the corresponding query results or feedback information of execution exceptions. The SQL refiner is used to input the execution exception information and execution result returned by the database, the natural language query of the current round of the user, and the database schema information into the large language model when an exception occurs in the SQL query statement during the database execution phase, or when the user is not satisfied with the execution result. It generates a modified SQL query statement to adjust and update the SQL query statement, and iterates until the user is satisfied.
9. The Text-to-SQL intelligent agent collaborative system for perceiving potential user intent in multi-turn contexts as described in claim 8, characterized in that: The strategy optimizer is used to obtain the SQL query statement generation log, system feedback information, and user feedback information. The system feedback information is the database execution result information, including execution results or exception information. The user feedback information is the information provided by the user during the usage process.
10. A method for a Text-to-SQL intelligent agent collaborative system that perceives potential user intent in a multi-turn context, as described in claim 1, characterized in that: The method includes: S1, based on the user's multi-turn historical dialogue information, retrieve contextual information related to the user's current natural language query from the memory bank; S2, based on the historical dialogue information, perceive the current user intent and recommend the optimal matching strategy; S3. Based on the optimal strategy recommended in step S2, the user's current natural language query, and context information, generate an SQL query statement; S4, execute the SQL query statement and obtain the execution result or execution exception information; S5. When there is an anomaly in the execution result or the user is not satisfied with the execution result, adjust the SQL query statement and record the adjustment process information. The adjustment process information and the prompting strategy are concatenated and input into the large language model to optimize the prompting strategies in the prompting strategy pool, generate new prompting strategies, and update the prompting strategy pool for use in the subsequent SQL query generation process.