Financial complex query statement generation method based on search algorithm and action space decomposition
By employing a search algorithm and action space decomposition method, and utilizing the Monte Carlo tree search algorithm to dynamically evaluate and update path rewards, the cascading error and global optimality problems in existing technologies are solved, thereby improving the accuracy and robustness of generating complex financial query statements.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-02-06
- Publication Date
- 2026-05-15
AI Technical Summary
Existing few-shot learning and thought chain methods are prone to cascading errors and cannot guarantee global optimality when generating complex financial queries, resulting in low accuracy and poor robustness of the generated queries.
This paper adopts a search algorithm and action space decomposition method, using the Monte Carlo Tree Search (MCTS) algorithm. By addressing the technical problems in existing technologies, the process of generating complex query statements is divided into a multi-step decision problem. The Monte Carlo Tree Search (MCTS) algorithm transforms the query into a multi-step decision path planning problem. Through four steps—selection, expansion, simulation, and backpropagation—path rewards are dynamically evaluated and updated to ensure the accuracy and robustness of the generated query statements.
It effectively avoids cascading errors, improves the accuracy and robustness of generating complex query statements, ensures that the generated query statements are syntactically and parameter-free, discovers better function combination logic, and improves query quality.
Smart Images

Figure CN122045235A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, specifically to a method for generating complex financial query statements based on search algorithms and action space decomposition. Background Technology
[0002] In scenarios such as financial investment, risk control, and market analysis, user questions often implicitly contain complex query statements related to specific database fields. For example, a user might request: "Which stocks have a 5-day moving average greater than their 10-day moving average and are ranked in the top ten in terms of price?" The 5-day and 10-day moving averages need to be calculated based on daily stock prices. This type of question requires the system not only to understand the semantics of natural language but also to accurately translate it into a computer-executable database query statement. The requirement that the 5-day moving average be greater than the 10-day moving average is related to a time window; large models directly generating such queries have a high probability of errors, or they might use illusory 5-day and 10-day moving average fields that don't exist.
[0003] Current background techniques typically employ few-shot learning and CoT (CoT) methods. Few-shot learning guides the model to generate query statements by showing examples, leveraging the contextual learning capabilities of language models. CoT, on the other hand, induces the model to gradually generate intermediate inference steps, attempting to improve logical accuracy in linear reasoning, but this method is still an extension of the single-step generation mode.
[0004] The accuracy of generating function combinations step-by-step using a small number of samples for learning is low. Using a thought chain approach to generate function combinations yields slightly higher accuracy, but this method has two core problems: Cascading errors are its main drawback. The chain of reasoning method relies on a linear and non-retrogressive generation method. Once the model makes an error in the early steps of the reasoning chain (e.g., choosing the wrong atomic function or confusing the filtering conditions), the error will propagate and accumulate in the final generated combination of functions, leading to overall logical fallacies.
[0005] The second problem is that it cannot guarantee global optimality. In the huge search space where every step may go wrong, the thought chain lacks global planning ability and cannot select the optimal and logically accurate sequence from all possible sequence of steps to generate the final function combination. Its answer is often a suboptimal solution limited to local experience. Summary of the Invention
[0006] The purpose of this invention is to provide a method for generating complex financial queries based on search algorithms and action space decomposition, aiming to overcome the problems of cascading errors and the inability to guarantee global optimality in the processing of complex queries by the thought chain technique. This method remodels the process of generating complex financial queries from natural language queries as a multi-step decision-making path search problem and introduces Monte Carlo Tree Search (MCTS) for global planning. In this way, the invention effectively avoids the accumulation of linear errors in the generation process and ensures that the final generated complex query has the highest execution accuracy and logical robustness through simulation execution and verification mechanisms.
[0007] To address the aforementioned technical problems, this invention provides a method for generating complex financial query statements based on search algorithms and action space decomposition, comprising the following steps: Obtain user query data; Based on user query data, a set of candidate atomic functions is selected from the atomic function library; Construct a function composition based on the set of candidate atomic functions; The function combinations are validated to obtain the optimal function combination, which is then used as the query statement.
[0008] Preferably, based on user query data, a set of candidate atomic functions is selected from the atomic function library, specifically including the following steps: Based on the upper-level confidence bound formula, a set of candidate atomic functions is selected from the atomic function library according to the user query data.
[0009] Preferably, based on the upper-level confidence bound formula, preliminary candidate atomic functions are selected from the atomic function library according to the user query data, specifically including the following steps: Using user query data as the root node, the most promising child node is selected from the state tree of the atomic function library for exploration to determine the set of candidate atomic functions.
[0010] Preferably, the formula for the upper confidence boundary is: in: W(v) is the overall evaluation value of node v; v is the current node; W(v) is the cumulative reward of node v; N(v) is the number of visits to node v; N(p) is the number of visits to parent node p; C is a constant used to control the trade-off between exploration and exploitation.
[0011] Preferably, the method further includes the following steps: Apply untried actions from the action space to the selected child nodes to generate new state nodes; each untried action is executed N times.
[0012] Preferably, the candidate atomic function set includes query atomic functions and filter atomic functions.
[0013] Preferably, constructing a function combination based on the candidate atomic function set specifically includes the following steps: Determine the required content based on the query atomic function; Determine the filtering conditions based on the atomic function being screened; Determine the function combination based on the required content and filtering conditions.
[0014] Preferably, the required content includes core fields or data sources.
[0015] Preferably, the function combination is validated to obtain the optimal function combination, which specifically includes the following steps: Simulate and verify the function composition; if the syntax is correct and the logic is equivalent, a high reward will be given. Update high rewards upwards along the path; After multiple rounds of MCTS iterations, the evaluation function is combined to calculate the reward value; The function combination with the highest reward value is selected as the optimal function combination.
[0016] Preferably, the reward value is calculated as follows: At the currently selected action, multiple function combinations are generated through N independent samplings and executed to obtain the execution result set S={s1,s2,…,sN}; the formula for calculating the reward value r is as follows: Where: S is the set of execution results; count(s) is the number of times result s appears in set S; It represents the frequency of the most frequently occurring result; N is the total number of samples. After obtaining the reward value r, perform backpropagation to update the state of all nodes on the path from the root node to the current leaf node: Cumulative reward score update: W(v) = W(v) + r; Access count updated: N = N + 1.
[0017] Compared with the prior art, the beneficial effects of the present invention are as follows: This invention reduces the difficulty of generating complex query statements by decomposing the action space and effectively avoids cascading errors common in single-step generation models by leveraging the global planning capabilities of Monte Carlo Tree Search. Compared with background techniques, experimental data shows that this method improves the accuracy and robustness of generating complex query statements to a certain extent. Furthermore, the addition of simulated execution and consistency checks ensures that the generated function combinations are syntactically and parameterically correct. In addition, the exploration mechanism of MCTS enables this method to discover better and more efficient function combination logic than direct generation, thus improving query quality. Attached Figure Description
[0018] The specific embodiments of the present invention will be further described in detail below with reference to the accompanying drawings.
[0019] Figure 1 The diagram illustrates the core iterative process of the MCTS algorithm in each iteration (selection, expansion, simulation, and backpropagation), and how the reward mechanism guides the search direction.
[0020] Figure 2 The diagram illustrates how the present invention decomposes a complex generation task into seven sequentially executed key actions, which serve as the basis for state transitions in the MCTS search, thereby reducing the solution space and ensuring the structured nature of the generation process. Detailed Implementation
[0021] Numerous specific details are set forth in the following description to provide a full understanding of the invention. However, the invention can be practiced in many other ways different from those described herein, and those skilled in the art can make similar extensions without departing from the spirit of the invention. Therefore, the invention is not limited to the specific embodiments disclosed below.
[0022] The terminology used in one or more embodiments of this specification is for the purpose of describing particular embodiments only and is not intended to be limiting of the one or more embodiments of this specification. The singular forms “a,” “described,” and “the” as used in one or more embodiments of this specification and the appended claims are also intended to include the plural forms unless the context clearly indicates otherwise. It should also be understood that the term “and / or” as used in one or more embodiments of this specification refers to and includes any or all possible combinations of one or more associated listed items.
[0023] It should be understood that although the terms first, second, etc., may be used to describe various information in one or more embodiments of this specification, such information should not be limited to these terms. These terms are only used to distinguish information of the same type from one another. For example, first may also be referred to as second without departing from the scope of one or more embodiments of this specification, and similarly, second may also be referred to as first. Depending on the context, the word "if" as used herein may be interpreted as "when," "when," or "in response to a determination."
[0024] The present invention will now be described in further detail with reference to the accompanying drawings: This invention provides a method for generating complex financial query statements based on search algorithms and action space decomposition; The core solution of this invention is to achieve path planning for the optimal combination of atomic functions based on the Monte Carlo Tree Search (MCTS) algorithm.
[0025] First, this invention uses a sequence of atomic function combinations to replace the direct generation of complex SQL query statements. Since query statements directly generated from a large model are prone to phantom fields and cannot be guaranteed to execute, we use a method of generating function combinations from a large model. Each atomic function is manually written or rigorously verified to ensure logical correctness and avoid phantom fields. The final function combination can deterministically generate an executable database query statement or SQL.
[0026] Secondly, the complex query generation task is decomposed into a finite, deterministic action space, encompassing the entire decision-making process from identification, filtering, and combination to completion. This process is then transformed into a state tree of a multi-step decision-making process, with the root node representing the user query and the leaf nodes representing complete function combinations. In the MCTS search iteration, the optimal function combination path is continuously explored and evaluated through four steps: selection, expansion, simulation, and backpropagation. During the simulation phase, the generated function combination fragments or complete combinations are simulated or validated for consistency. Rewards are given based on the validation results, used to evaluate the quality of the path and guide subsequent search directions. Finally, the path with the most visits or the highest average reward in the search tree is selected as the optimal function combination sequence for output.
[0027] Specifically, the following steps are included: 1) Decomposition of motion space This invention decomposes the query statement generation process into the following key actions, which serve as the basis for node state transitions in MCTS search: Action 1: Candidate Atomic Function Filtering: Based on the user's query information, the most relevant and consistent set of candidate atomic functions is filtered out by repeatedly calling the large model from the complete atomic function library.
[0028] Action 2: Query atomic functions: used to determine the core fields or data sources required in the final query.
[0029] Action 3: Filtering atomic functions: used to apply filtering conditions and determine how the conditions are combined, such as using union or intersect.
[0030] Action 4: Nestable functions: Used to handle complex logic that requires subqueries or data dependencies, taking one filter result as input to another function.
[0031] Action 5: Atomic Function Combination: Responsible for determining the calling order and logical connection relationship between multiple atomic functions in order to search for the optimal combination.
[0032] Action 6: Function Combination Correction: Perform logical fine-tuning or error checking on the function combination fragments under the current search path to adapt to the MCTS backtracking mechanism.
[0033] Action 7: End: This indicates that the function combination is complete and triggers the final simulation verification process.
[0034] To reduce the size of the solution space, it is stipulated that these actions must be executed in the order of actions one through seven, and each action can only be executed once. That is, if action three is selected the first time, then the next expansion can only select action five, and cannot select action two.
[0035] 2) Search algorithm and verification mechanism This invention employs the MCTS algorithm, whose core advantages lie in its backtracking capability and simulation verification: Selection: Based on the Upper Confidence Bound (UCT) formula, the most promising child node is selected from the state tree for exploration, balancing the utilization of explored paths with the attempt of unexplored paths. The UCT formula is as follows: in: W(v) is the overall evaluation value of node v; v is the current node; W(v) is the cumulative reward of node v; N(v) is the number of visits to node v; N(p) is the number of visits to parent node p; C is a constant used to control the trade-off between exploration and exploitation.
[0036] Extension: Apply an untried action from the action space to the selected node to generate a new state node. Each untried action can be executed N times (e.g., N=3 times), which increases the diversity of reasoning.
[0037] Simulation and Rewards: Starting from a new node, execute a fast random policy until the end action is reached, forming a complete function combination.
[0038] Backpropagation: At the terminal node, we evaluate the predicted function combinations using a self-supervised reward function. We identify the action that ultimately produces the function combination (action five or action six), and then sample the function combinations multiple times at a fixed temperature to ensure diversity. The reward value is calculated based on the consistency score of the sampling and predicted function combinations. This reward value is updated layer by layer up the search path to all traversed parent nodes, thereby accurately evaluating the quality of each intermediate step and guiding the selection in the next round.
[0039] For example, at the currently selected action, multiple function combinations are generated through N independent samplings and executed, resulting in an execution result set S={s1,s2,…,sN}. The formula for calculating the reward value r is as follows: Where: S is the set of execution results; count(s) is the number of times result s appears in set S; It is the frequency of the result that appears most frequently (the mode); N is the total number of samples. After obtaining the consistency score *r*, backpropagation is performed to update the state of all nodes on the path from the root node *v* to the current leaf node *v*: Cumulative reward score update: *W(v)* = *W(v)* + *r*. The consistency score obtained from this self-supervised verification is added to the original total reward of the node. Visit count update: *N* = *N* + 1. In this way, the updated statistics will be substituted into the UCT calculation for the next search, making paths with higher consistency results more likely to be prioritized in the future.
[0040] The final function combination selection aims to obtain the optimal function combination from multiple generated combinations. We execute all predicted function combinations and select the one with the highest consistency as the final prediction.
[0041] The action space decomposition and multi-step decision modeling of this invention decouples the task of generating complex financial queries into seven operable and traceable actions. Through this structured decomposition, the inference of LLM is transformed from a linear CoT pattern to a tree-like search planning pattern, effectively reducing the impact of cascading errors.
[0042] The introduction of Monte Carlo Tree Search (MCTS) in this invention creatively applies the MCTS algorithm to the generation of domain code / function combinations, enabling it to perform global planning and search, thereby stably converging to the globally optimal sequence of function combinations in a huge combinatorial solution space.
[0043] The embedded simulation execution reward mechanism of this invention embeds syntax verification and consistency evaluation in the simulation phase of MCTS. Through real-time feedback reward values, the generated intermediate results are dynamically evaluated to ensure that high rewards are only obtained when the syntax and parameters are accurate and the results of multiple executions are consistent.
[0044] To better illustrate the technical effects of the present invention, the present invention provides the following specific embodiments to illustrate the above technical process: Example 1: A method for generating complex financial query statements based on search algorithms and action space decomposition, comprising the following steps: User query: Find the stock codes and closing prices of the top 10 stocks in descending order of closing price from the set of "leading stocks" with a current closing price greater than 50 yuan.
[0045] Assuming an atomic function library: QueryByPrice(condition): Filter stocks based on price conditions.
[0046] GetLeadingStocks(): Retrieves a collection of all "leading stocks".
[0047] Intersect(set1,set2): Finds the intersection of two sets of stocks.
[0048] SortAndLimit(set,sort_field,order,limit): Sorts the set and limits the number of elements in it.
[0049] SelectFields(set,fields): Selects the fields to be output.
[0050] Monte Carlo Tree Search (MCTS) process simulation: First iteration: Exploring the path Selection: Starting from the root node (user query), select action one according to the UCT formula.
[0051] Extension (Action 1 - Candidate Function Filtering): LLM analyzes the query to determine the required set of core atomic functions: Current status: Selected function: {QueryByPrice,GetLeadingStocks,Intersect,SortAndLimit,SelectFields} Simulation: Start from the current state and execute a fast random policy until completion: Action 2 (Querying Atomic Functions): Select QueryByPrice Action 3 (Filtering Atomic Functions): Determine the condition > 50 and GetLeadingStocks(), with the connection method being Intersect. Action 4 (Nested Functions): Skip (This example does not require complex subquery dependencies) Action 5 (Atomic Function Combination - Generating Complete Combinations): Action 7 (End): Simulate execution and verify consistency of the generated combination. Reward: If the syntax is verified to be correct and the logic consistent, a high reward (e.g., +1.0) is given.
[0052] Backpropagation: Update the reward +1.0 up the path to all parent nodes.
[0053] Second iteration: Utilizing high-reward paths Selection: Starting from the root node, according to the UCT formula (the number of utilization items increases due to higher rewards), select the path with the highest reward in the previous round (i.e., action one).
[0054] Extend (Action 2 - Query Atomic Function): Select another branch of Action 2 (if any), for example, select GetLeadingStocks as the query starting point.
[0055] Current status: Core query function: GetLeadingStocks Simulation: Execute a fast random policy from the current state: Action 3 (Filtering Atomic Functions): Define the conditions QueryByPrice('>50') and GetLeadingStocks(), with the connection method being Intersect. Action 5 (Atomic Function Combination - Generating Complete Combinations): Action 7 (End): Simulate execution and verification. Reward: If the syntax is verified to be correct and the logic is equivalent, a high reward is given (e.g., +1.0).
[0056] Backpropagation: Update the reward by +1.0 up the path.
[0057] Final combination selection: After multiple rounds of MCTS iterations, the system evaluates all function combinations generated through action five or action six. Since the combinations generated in these two rounds are logically equivalent and both achieve high consistency scores, they are considered part of the globally optimal solution set. The system selects the combination with the highest consistency (e.g., the combination generated in the first round) as the final prediction output. Optimal function combination sequence: In the several embodiments provided by this invention, it should be understood that the disclosed apparatus and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative. For instance, the division of modules, units, or units is merely a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units, modules, or components may be combined or integrated into another device, or some features may be ignored or not executed.
[0058] The units may or may not be physically separate. The components shown as units can be one or more physical units, meaning they can be located in one place or distributed in multiple different locations. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0059] Furthermore, the functional units in the various embodiments of the present invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0060] In particular, according to embodiments disclosed in this invention, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments of this disclosure include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via a communication component, and / or installed from a removable medium. When the computer program is executed by a central processing unit (CPU), it performs the functions defined in the methods of this invention. It should be noted that the computer-readable medium described above in this invention can be a computer-readable signal medium or a computer-readable storage medium, or any combination of the two. The computer-readable storage medium can be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof.
[0061] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of the present invention. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in the block diagrams and / or flowcharts, and combinations of blocks in the block diagrams and / or flowcharts, can be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.
[0062] The above description is merely a specific embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any variations or substitutions within the technical scope disclosed in the present invention should be covered within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the scope of the claims.
Claims
1. A method for generating complex financial query statements based on search algorithms and action space decomposition, characterized in that, Includes the following steps: Obtain user query data; Based on user query data, a set of candidate atomic functions is selected from the atomic function library; Construct a function composition based on the set of candidate atomic functions; The function combinations are validated to obtain the optimal function combination, which is then used as the query statement.
2. The method for generating complex financial query statements based on search algorithms and action space decomposition according to claim 1, characterized in that, Based on user query data, a set of candidate atomic functions is selected from the atomic function library, specifically including the following steps: Based on the upper-level confidence bound formula, a set of candidate atomic functions is selected from the atomic function library according to the user query data.
3. The method for generating complex financial query statements based on search algorithms and action space decomposition according to claim 2, characterized in that, Based on the upper-level confidence bound formula, and according to user query data, preliminary candidate atomic functions are selected from the atomic function library. This includes the following steps: Using user query data as the root node, the most promising child node is selected from the state tree of the atomic function library for exploration to determine the set of candidate atomic functions.
4. The method for generating complex financial query statements based on search algorithms and action space decomposition according to claim 3, characterized in that: The formula for the upper-level confidence bound is: in: W(v) is the overall evaluation value of node v; v is the current node; W(v) is the cumulative reward of node v; N(v) is the number of visits to node v; N(p) is the number of visits to parent node p; C is a constant used to control the trade-off between exploration and exploitation.
5. The method for generating complex financial query statements based on search algorithms and action space decomposition according to claim 4, characterized in that, It also includes the following steps: Apply untried actions from the action space to the selected child nodes to generate new state nodes; each untried action is executed N times.
6. The method for generating complex financial query statements based on search algorithms and action space decomposition according to claim 5, characterized in that: The candidate atomic function set includes query atomic functions and filter atomic functions.
7. The method for generating complex financial query statements based on search algorithms and action space decomposition according to claim 6, characterized in that, Based on the set of candidate atomic functions, a function composition is constructed, which includes the following steps: Determine the required content based on the query atomic function; Determine the filtering conditions based on the atomic function being screened; Determine the function combination based on the required content and filtering conditions.
8. The method for generating complex financial query statements based on search algorithms and action space decomposition according to claim 7, characterized in that: The required content includes core fields or data sources.
9. The method for generating complex financial query statements based on search algorithms and action space decomposition according to claim 8, characterized in that, To verify the function combination and obtain the optimal function combination, the following steps are involved: Simulate and verify the function composition; if the syntax is correct and the logic is equivalent, a high reward will be given. Update high rewards upwards along the path; After multiple rounds of MCTS iterations, the evaluation function is combined to calculate the reward value; The function combination with the highest reward value is selected as the optimal function combination.
10. The method for generating complex financial query statements based on search algorithms and action space decomposition according to claim 9, characterized in that: The method for calculating the reward value is as follows: At the currently selected action, multiple function combinations are generated through N independent samplings and executed to obtain the execution result set S={s1,s2,…,sN}; the formula for calculating the reward value r is as follows: Where: S is the set of execution results; count(s) is the number of times result s appears in set S; It represents the frequency of the most frequently occurring result; N is the total number of samples. After obtaining the reward value r, perform backpropagation to update the state of all nodes on the path from the root node to the current leaf node: Cumulative reward score update: W(v) = W(v) + r; Access count updated: N = N + 1.