Database query method and device, computer equipment, readable storage medium and program product
By analyzing problem fields and using vector libraries and knowledge graphs to build query statements, the problem of low database query accuracy is solved and accurate data query is achieved.
Patent Information
- Application Number
- CN202510395862.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-31
- Publication Date
- 2025-07-29
AI Technical Summary
In the prior art, database query accuracy is not high, and it is difficult to accurately convert problems entered by users into SQL statements for querying, resulting in the inability to accurately query the required data.
By analyzing the pending problems, obtain the problem fields, use the vector library and knowledge graph of the target database, and build query statements in combination with the large language model, accurately match the target fields and obtain table information, and finally generate the target query statement for database query.
Improve the accuracy of database queries, avoid query errors caused by natural language ambiguity and ambiguity, and ensure the accuracy and efficiency of data queries.
Smart Images

Figure CN120386791A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular, to a database query method, apparatus, computer device, computer-readable storage medium, and computer program product. Background Art
[0002] With the rapid development of computer technology and artificial intelligence technology, big data analysis and intelligent processing technologies have emerged. Among them, database query is a key link, which is used to extract valuable information from massive data.
[0003] In the related art, the problem input by the user is usually directly converted into an SQL (Structured Query Language) statement, and then data query is directly performed from the database based on the SQL statement. However, it is difficult to accurately convert the query intention in the problem into an SQL statement by this method, resulting in low accuracy of database query. Summary of the Invention
[0004] Based on this, in view of the above technical problems, it is necessary to provide a database query method, apparatus, computer device, computer-readable storage medium, and computer program product that can improve the accuracy of database query.
[0005] In a first aspect, this application provides a database query method, including:
[0006] Parsing a problem to be processed for data query to obtain problem fields;
[0007] Obtaining a vector library related to a target database, where the vector library is constructed according to vectors of semantic similar fields of each target field in the target database;
[0008] Querying target fields matching the problem fields according to the vectors of the problem fields and the vector library, and obtaining table information related to the queried target fields from a knowledge graph;
[0009] Based on the table information and the problem to be processed, constructing a generation prompt text for generating a query statement, calling a large language model, and outputting a target query statement for the problem to be processed according to the generation prompt text;
[0010] Performing data query on the target database based on the target query statement to obtain a query result.
[0011] In a second aspect, this application also provides a database query apparatus, including:
[0012] A problem parsing module, configured to parse a problem to be processed for data query to obtain problem fields;
[0013] A vector library acquisition module, configured to acquire a vector library related to a target database, where the vector library is constructed based on vectors of semantic similarity fields of each target field in the target database;
[0014] A field query module, configured to query a target field that matches the problem field according to the vector of the problem field and the vector library, and acquire table information related to the queried target field from a knowledge graph;
[0015] A query statement determination module, configured to construct a generation prompt text for generating a query statement based on the table information and the problem to be processed, call a large language model, and output a target query statement for the problem to be processed according to the generation prompt text;
[0016] A data query module, configured to perform a data query on the target database based on the target query statement to obtain a query result.
[0017] In a third aspect, the present application further provides a computer device, including a memory and a processor, where the memory stores a computer program, and when the processor executes the computer program, the following steps are implemented:
[0018] Parse a problem to be processed for data query to obtain a problem field;
[0019] Acquire a vector library related to a target database, where the vector library is constructed based on vectors of semantic similarity fields of each target field in the target database;
[0020] Query a target field that matches the problem field according to the vector of the problem field and the vector library, and acquire table information related to the queried target field from a knowledge graph;
[0021] Construct a generation prompt text for generating a query statement based on the table information and the problem to be processed, call a large language model, and output a target query statement for the problem to be processed according to the generation prompt text;
[0022] Perform a data query on the target database based on the target query statement to obtain a query result.
[0023] In a fourth aspect, the present application further provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the following steps are implemented:
[0024] Parse a problem to be processed for data query to obtain a problem field;
[0025] Obtain a vector library related to the target database, where the vector library is constructed based on the vectors of the semantic similar fields of each target field in the target database;
[0026] According to the vector of the problem field and the vector library, query the target field that matches the problem field, and obtain the table information related to the queried target field from the knowledge graph;
[0027] Based on the table information and the problem to be processed, construct a generation prompt text for generating a query statement, call a large language model, and according to the generation prompt text, output a target query statement for the problem to be processed;
[0028] Based on the target query statement, perform a data query on the target database to obtain a query result.
[0029] In a fifth aspect, the present application further provides a computer program product, including a computer program, which when executed by a processor implements the following steps:
[0030] Parse the problem to be processed for data query to obtain a problem field;
[0031] Obtain a vector library related to the target database, where the vector library is constructed based on the vectors of the semantic similar fields of each target field in the target database;
[0032] According to the vector of the problem field and the vector library, query the target field that matches the problem field, and obtain the table information related to the queried target field from the knowledge graph;
[0033] Based on the table information and the problem to be processed, construct a generation prompt text for generating a query statement, call a large language model, and according to the generation prompt text, output a target query statement for the problem to be processed;
[0034] Based on the target query statement, perform a data query on the target database to obtain a query result.
[0035] The above database query method, device, computer equipment, computer-readable storage medium, and computer program product can accurately analyze the key content of the problem to be processed by parsing the problem to be processed for data query, so as to obtain the problem field, avoid processing irrelevant content in the problem to be processed subsequently, and ensure the accuracy and query efficiency of data query. Then, a vector library related to the target database is obtained. Since the vector library is constructed based on the vectors of the semantically similar fields of each target field in the target database, according to the vector of the problem field and the vector library, the target field that matches the problem field is queried. That is, it can accurately query the target field with a similar meaning to the problem field, and can avoid the situation where fields with similar meanings cannot be queried due to the ambiguity and polysemy of natural language. Then, the table information related to the queried target field is obtained from the knowledge graph; based on the table information and the problem to be processed, a generation prompt text for generating a query statement is constructed. In this way, by utilizing the semantic understanding ability of the large language model, according to the generation prompt text, the target query statement regarding the problem to be processed can be accurately constructed. Therefore, based on the target query statement, the target database can be accurately queried for data to obtain the query result, thereby improving the accuracy of database query. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] In order to more clearly illustrate the technical solutions in the embodiments of the present application or related technologies, the following will briefly introduce the drawings required to be used in the description of the embodiments of the present application or related technologies. Obviously, the following drawings are only some embodiments of the present application. For those of ordinary skill in the art, without creative efforts, other related drawings can also be obtained based on these drawings.
[0037] Figure 1 It is an application environment diagram of the database query method in an embodiment;
[0038] Figure 2 It is a schematic flowchart of the database query method in an embodiment;
[0039] Figure 3 It is a schematic flowchart of the knowledge graph construction steps in an embodiment;
[0040] Figure 4 It is a schematic diagram of the database query framework in an embodiment;
[0041] Figure 5 It is a schematic diagram of the data query process in an embodiment;
[0042] Figure 6 It is a structural block diagram of the database query device in an embodiment;
[0043] Figure 7It is the internal structure diagram of a computer device in an embodiment. Detailed implementation manners
[0044] In order to make the objectives, technical solutions and advantages of the present application more clear and understandable, the present application will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0045] In the related art, when performing data query, it is usually to query the database after converting the problem input by the user into an SQL statement. However, the accuracy of directly converting into an SQL statement is not high. For example, due to the ambiguity and polysemy of natural language, and different professional directions have different understandings of the meaning of statements, it is difficult for natural language to correctly map to the fields of the database, resulting in low conversion accuracy. Therefore, the required data cannot be accurately queried.
[0046] The data query method provided by the embodiment of the present application first parses the problem to be processed to obtain problem fields, and then, with the help of the vector library constructed according to the vectors of the semantic similar fields of each target field in the target database, can accurately query the target fields with similar meanings to the problem fields, and can avoid the inability to query fields with similar meanings due to the ambiguity and polysemy of natural language. Then, the table information can be queried according to the determined target fields, and the target query statement can be accurately constructed based on the table information with the help of the semantic understanding ability of the large language model, so as to realize accurate data query in the target database and obtain the query result, thereby improving the accuracy of database query.
[0047] The database query method provided by the embodiment of the present application can be applied to the application environment as Figure 1 shown. Among them, the terminal 102 communicates with the server 104 through the network. The data storage system can store the data that the server 104 needs to process. The data storage system can be integrated on the server 104, or can be placed in the cloud or other network servers. The database query method provided by the embodiment of the present application can be executed independently by the terminal 102 or the server 104, or can be executed jointly by the terminal 102 and the server 104, and the specific is not limited.
[0048] In one embodiment, taking the collaborative execution of the terminal 102 and the server 104 as an example for illustration: The terminal 102 responds to the problem input operation for data query and obtains the problem to be processed for data query. The terminal 102 sends the problem to be processed to the server 104. The server 104 parses the problem to be processed for data query to obtain problem fields; obtains a vector library related to the target database, and the vector library is constructed based on the vectors of the semantic similar fields of each target field in the target database; the server 104 queries the target fields that match the problem fields according to the vectors of the problem fields and the vector library, and obtains table information related to the queried target fields from the knowledge graph; the server 104 constructs a generation prompt text for generating a query statement based on the table information and the problem to be processed, calls a large language model, and outputs a target query statement for the problem to be processed according to the generation prompt text; the server 104 performs a data query on the target database based on the target query statement and obtains a query result. The server 104 sends the query result to the terminal 102 to instruct the terminal 102 to display the query result.
[0049] Among them, the terminal 102 can be but is not limited to various personal computers, laptop computers, smartphones, tablet computers, etc. The server 104 can be an independent physical server, a server cluster or a distributed system composed of multiple physical servers, or a cloud server providing cloud computing services.
[0050] In an exemplary embodiment, as Figure 2 shown, a database query method is provided. Taking the method applied to the Figure 1 server 104 as an example for illustration, it includes the following steps 202 to step 210. Among them:
[0051] Step 202, parse the problem to be processed for data query to obtain problem fields.
[0052] Among them, data query refers to the process of performing data query in the target database. The target database can be a database associated with the current business scenario. For example, the target database is a database for querying communication service data in a communication scenario. The problem to be processed is a problem about data query. For example, the problem to be processed Q1: Please query the antenna models associated with device 1 in community A?, and another example is the problem to be processed Q2: Please query the community load of community A?. The problem field refers to the field where the key content is located in the problem to be processed, and the key content is the content related to the query intent. For example, in Q1, the problem field can be community A, or device 1, or the antenna model; and in Q2, the problem field can be community A, or the community load.
[0053] Optionally, after the server obtains the problem to be processed for data query, it preprocesses the problem to be processed to delete irrelevant or invalid words, and obtains the processed problem. The processed problem is parsed to obtain at least one problem field.
[0054] Optionally, the server performs word segmentation on the problem to be processed to obtain multiple word segments, filters out each word segment related to the query intention from the multiple word segments, and uses each filtered word segment as a problem field.
[0055] Optionally, before parsing the problem to be processed for data query, the server first checks whether the problem to be processed has a query intention. If it does, it executes step 202. If not, it sends feedback information to the terminal to instruct the terminal to ask the user again to obtain a new problem to be processed with a query intention.
[0056] In one embodiment, parsing the problem to be processed for data query to obtain problem fields includes: obtaining an extraction prompt text template for field extraction, filling the problem to be processed into the extraction prompt text template to generate an extraction prompt text; calling a large language model, and outputting problem fields according to the extraction prompt text.
[0057] Among them, the extraction prompt text template is a prompt text template for field extraction, and the extraction prompt text template includes guiding content, which is used to guide the large language model to understand field extraction. For example, the guiding content is: You are a field extraction robot, please extract the problem fields in the input problem.
[0058] For example, the problem to be processed Q1: Please query the antenna model associated with device 1 in community A?. The extraction prompt text generated after inputting the problem to be processed Q1 into the extraction prompt text template is: You are a field extraction robot, please extract the problem fields from "Please query the antenna model associated with device 1 in community A?". The problem fields output by the large language model based on the extraction prompt text are: "Community A", "Device 1", "Antenna model".
[0059] In some other embodiments, the method further includes: after the server fills the problem to be processed into the prompt text template to generate an extraction prompt text, the large language model performs semantic understanding based on the extraction prompt text and fails to extract problem fields related to the query intention, and the output is empty. The server generates an information indicating field extraction failure based on the empty output result and returns it to the terminal to instruct the terminal to ask the user again to obtain a new problem to be processed with a query intention.
[0060] In this embodiment, an extraction prompt text for field extraction is automatically generated by extracting a prompt text template and a problem to be processed. In this way, the semantic understanding ability of the large language model can be utilized to perform semantic understanding based on the extraction prompt text, so as to accurately extract problem fields.
[0061] Step 204: Obtain a vector library related to the target database. The vector library is constructed based on the vectors of semantic similar fields of each target field in the target database.
[0062] Among them, the target database is a database for data query. Exemplarily, the target database is an SQL (Structured Query Language) database. The target database includes multiple tables, and each table involves at least one target field. The target field can be an SQL field. The semantic similar field of the target field refers to a field that has the same or similar semantics as the target field. The vector library is constructed based on the vectors of the target field and its semantic similar fields. Exemplarily, the vector library includes the vectors of the semantic similar fields of each target field. In one embodiment, the vector library can be deployed locally or on other servers.
[0063] Optionally, the server filters out the vector library related to the target database from multiple pre-constructed vector libraries. For example, vector libraries are respectively constructed for different databases. Therefore, the vector library corresponding to the target database is filtered out from multiple vector libraries.
[0064] Optionally, after the server determines the vector library related to the target database, when it is verified that the vector library is in an available state, the following step 206 is executed. Exemplarily, the available state can be an idle state. For example, if the number of queries currently querying the vector library reaches a threshold, it is in a busy state and needs to wait. After waiting for a preset period, when it is determined that the vector library is in an idle state, the following step 206 is executed. Exemplarily, the available state can also be a valid state. If the construction duration of the vector library does not exceed the duration threshold, it is determined to be in a valid state. If the construction duration exceeds the duration threshold, it is in an invalid state, indicating that at least one vector in the vector library needs to be updated and replaced.
[0065] In one embodiment, the vector library construction step includes: for each target field in the target database, obtain multiple semantic similar fields that are semantically similar to the target field from different knowledge bases; group the multiple semantic similar fields to obtain at least one field group; determine the vector of each field group according to the vectors of the semantic similar fields in each field group, and store the vector of each field group corresponding to the target field to obtain the vector library.
[0066] Among them, the knowledge base is obtained by acquiring corresponding professional knowledge according to actual needs in the same business scenario and storing the professional knowledge in the corresponding library. It can be understood that there are multiple knowledge bases in the same business scenario. The glyph differences of each pair of semantically similar fields within the same field group are small, while the glyph differences of each pair of semantically similar fields in different field groups are large. For example, in a wireless scenario, "load" can be represented by "PRB (Physical Resource Block) utilization rate", or by "load", or by "PRB occupancy rate". Then, the glyph differences between "load" and "load" are small and can be grouped into one field group, and the glyph differences between "PRB utilization rate" and "PRB occupancy rate" are small and can be grouped into one field group. Obviously, the glyph differences between "load" and "PRB utilization rate" are large and do not belong to the same field group. Therefore, in this example, there are two field groups, where field group G1 includes "load" and "load"; field group G2 includes "PRB utilization rate" and "PRB occupancy rate".
[0067] Optionally, for each target field in the target database, the server obtains other fields that are synonymous with the target field from different knowledge bases in the business scenario and uses these other fields as the semantically similar fields of the target field. Exemplarily, according to the target field and each knowledge base, a synonymous query hint text template is called to generate a synonymous query hint text, and a large language model is called to perform semantic understanding on the synonymous query hint text, and multiple semantically similar fields of the target field are output. The multiple semantically similar fields of the target field may or may not include the target field, and there is no specific limitation.
[0068] Optionally, after determining the multiple semantically similar fields of each target field, for each target field, the server groups the semantically similar fields with glyph differences less than the difference threshold within the multiple semantically similar fields into the same field group. It should be noted that the glyph differences of the multiple semantically similar fields of the target field may be very large or very small. The larger the glyph difference, the greater the corresponding vector difference. In order to facilitate subsequent accurate query of vectors from the vector library, it is necessary to group the semantically similar fields with small glyph differences into one field group.
[0069] In one embodiment, grouping multiple semantically similar fields to obtain at least one field group includes: for each semantically similar field, calculating the field coincidence degree between the semantically similar field and other semantically similar fields respectively; grouping the other semantically similar fields corresponding to the field coincidence degree greater than or equal to the threshold, and the semantically similar field into the same field group.
[0070] Among them, the field overlap degree reflects the glyph difference between two semantically similar fields. The field overlap degree is negatively correlated with the glyph difference, that is, the greater the field overlap degree, the smaller the corresponding glyph difference, and the smaller the field overlap degree, the greater the corresponding glyph difference.
[0071] Exemplarily, after determining multiple semantically similar fields of the target field, for each semantically similar field among the multiple semantically similar fields, according to the vector similarity degree between the vector of this semantically similar field and the vectors of other semantically similar fields among the multiple semantically similar fields, the field overlap degree between the semantically similar field and other semantically similar fields is obtained. The vector similarity degree is positively correlated with the field overlap degree. For example, the vector of each semantically similar field can be calculated according to the vectorization algorithm, such as using the bge-m3 (new embedding model) algorithm to determine the vector of each semantically similar field respectively.
[0072] Exemplarily, for each semantically similar field among the multiple semantically similar fields, calculate the edit distance (Levenshtein distance) between this semantically similar field and other semantically similar fields among the multiple semantically similar fields, and obtain the field overlap degree between the semantically similar field and other semantically similar fields. The edit distance is negatively correlated with the field overlap degree. The edit distance refers to the minimum number of edits required to convert one field into another between two fields.
[0073] Exemplarily, after determining the field overlap degree between this semantically similar field and other semantically similar fields, for this semantically similar field, compare each corresponding field overlap degree with a threshold, filter out the other semantically similar fields corresponding to the field overlap degree greater than or equal to the threshold, and group the filtered other semantically similar fields and this semantically similar field into the same field group.
[0074] In this embodiment, by calculating the character overlap degree between each semantically similar field and other semantically similar fields, the multiple semantically similar fields of the target field can be grouped according to glyph similarity to obtain at least one field group, which is convenient for quickly querying the vector matching the target field subsequently.
[0075] Optionally, after the server determines at least one field group, for each field group, based on the vectors of at least one semantically similar field in the field group, it determines the vector of the field group, and stores the vectors of at least one field group corresponding to the target field in the vector library. There is corresponding information for each target field in the target database in the vector library, and the corresponding information includes the vectors of at least one field group corresponding to the target field. Of course, the method can also be: after determining the vectors of at least one field group corresponding to the target field, add the vectors of at least one field group to the vector library, and add a note to the vectors of at least one field group, and the note is the name of the target field.
[0076] In one embodiment, determining the vector of each field group according to the vectors of semantically similar fields in each field group includes: for each field group, sorting the vectors of at least one semantically similar field in the field group to obtain a vector sequence; using the vector located in the middle position of the vector sequence as the vector of the field group.
[0077] In this embodiment, by sorting the vectors of at least one semantically similar field in each field group, the vector in the middle position can be accurately located, that is, the difference between the vector in the middle position and other vectors in the field group is small. Therefore, the vector in the middle position can be directly used as the vector of the field group, which can improve the efficiency of determining the vector of the field group.
[0078] In other embodiments, determining the vector of each field group according to the vectors of semantically similar fields in each field group includes: for each field group, calculating the average value of the vectors of at least one semantically similar field in the field group, and using the average value as the vector of the field group. In this embodiment, the vector of the field group is more reasonable and accurate through mean calculation.
[0079] In this embodiment, for each target field in the target database, semantically similar fields with the same or similar meaning as the target field are queried from different knowledge bases. Considering that the glyphs of semantically similar fields may be dissimilar, resulting in a large difference in the vectors of each semantically similar field of the target field, multiple semantically similar fields are grouped to obtain at least one field group, and the vectors of at least one field group are stored corresponding to the target field to obtain a vector library. In this way, the vector matching the problem field can be queried from the vector library subsequently, so as to find the target field corresponding to the problem field, realizing the accurate query from the problem field to the target field in the target database.
[0080] Step 206, according to the vector of the problem field and the vector library, query the target field that matches the problem field, and obtain the table information related to the queried target field from the knowledge graph.
[0081] Among them, the target field matching the problem field may mean that the problem field and the target field have the same or similar semantics. The knowledge graph records the table information in the target database, and the table information includes the information of the table where the target field is located and the association relationship between tables.
[0082] In one embodiment, as Figure 3 shown, it is a schematic flowchart of the knowledge graph construction steps in one embodiment. The construction steps of the knowledge graph include:
[0083] Step 302, extract each target table and each target field in each target table from the target database.
[0084] Exemplarily, the server constructs information extraction text based on the target database, calls the large language model, and extracts each target table and each target field in each target table in the target database through the information extraction text.
[0085] Step 304, for each target table, query the associated table that has an association relationship with the target table according to the foreign key field of the target table, and generate the association information of the target table based on the associated table.
[0086] Among them, the foreign key field of the target table records the other tables associated with the target table. The association information contains the information of the other tables associated with this target table.
[0087] Step 306, create a knowledge graph according to the association information of each target table and the field information of each target field in each target table.
[0088] Exemplarily, for each target table, create a first node of the knowledge graph for the target table, create a second node of the knowledge graph for each target field in the target table and the relevant information of each target field, and create an association relationship between the first node and the second node. The relationship description should be the relationship between the field and the table in the target database. According to the association information of the target table, establish an association relationship between the first node and the nodes corresponding to the other tables with an association relationship, which is used to describe the relationship between the target table and the other tables. Among them, the relevant information of the field can be the name, format, value requirement, etc. of the field.
[0089] In this embodiment, by extracting the target tables and target fields in the target database to construct a matching knowledge graph, in this way, when querying data, the complex information problem of the table can be solved according to this knowledge graph. When performing an intention query, only extract the information of the tables corresponding to the problem fields of the problem to be processed in the knowledge graph and the association relationship between these tables, simplify the complex information of the database, and improve the understanding ability of the large model.
[0090] In one embodiment, according to the vector of the question field and the vector library, the target field that matches the question field is queried, including: obtaining the vectors of at least one field group corresponding to each target field from the vector library; querying the field group to which the maximum vector similarity belongs based on the vector similarity between the vector of the question field and the vectors of each field group; and using the target field corresponding to the queried field group as the target field that matches the question field.
[0091] Exemplarily, after the server determines at least one question field, for each question field, the server obtains the vectors of at least one field group corresponding to each target field from the vector library to obtain the vectors of all field groups. After calculating the vector of this question field, the server calculates the vector similarity between the vector of this question field and the vectors of each field group. If the maximum vector similarity is greater than or equal to the similarity threshold, the target field corresponding to the field group to which the maximum vector similarity belongs is used as the target field that matches this question field (i.e., the queried target field). For example, there are M1 target fields in the database, and each target field has at least one field group. The total number of all field groups is counted as M2, that is, the sum of the numbers of at least one field group of each target field is obtained. For the question field F1, calculate the vector similarity between the vector E1 of F1 and the vectors of each field group, that is, obtain M2 vector similarities. If the maximum vector similarity is greater than the similarity threshold, determine the field group corresponding to the maximum vector similarity as Gy, determine the target field associated with Gy, and use the associated target field as the target field that matches F1.
[0092] In this embodiment, through the vector of the question field, the most similar vector can be queried from the vector library. Thus, according to the target field corresponding to the field group to which the queried vector belongs, the target field that matches the question field can be accurately matched.
[0093] Optionally, after the server queries the target field that matches the question field, it calls the knowledge graph and uses the queried target field as an index to find the table information related to the queried target field from the knowledge graph.
[0094] Step 208, based on the table information and the question to be processed, construct a generation prompt text for generating a query statement, call the large language model, and according to the generation prompt text, output the target query statement for the question to be processed.
[0095] Among them, the target query statement is an SQL statement.
[0096] Exemplarily, after the server determines the table information corresponding to each question field, it calls a generation prompt text template, fills in the table information corresponding to each question field and the question to be processed into the generation prompt text template, and obtains a generation prompt text for generating a query statement. The server calls a large language model to perform semantic understanding based on the generation prompt text and outputs a target query statement for the question to be processed.
[0097] Of course, to ensure accuracy, the generation prompt text template also needs to be filled with the target fields matched by the question fields; for this purpose, the target fields matched by each question field, the table information corresponding to each question field, and the question to be processed are filled into the generation prompt text template to obtain the generation prompt text, and the above step of calling the large language model is continued.
[0098] Step 210: Based on the target query statement, perform a data query on the target database to obtain a query result.
[0099] Optionally, after the server determines the target query statement, it performs a data query in the target database based on the target query statement and returns the query result to the terminal.
[0100] In the above database query method, by parsing the question to be processed for data query, the key content of the question to be processed can be accurately analyzed to obtain the question fields, avoiding subsequent processing of irrelevant content in the question to be processed, and ensuring the accuracy and query efficiency of the data query. Then, a vector library related to the target database is obtained. Since the vector library is constructed based on the vectors of the semantic similar fields of each target field in the target database, the target fields matching the question fields can be queried according to the vectors of the question fields and the vector library. That is, the target fields with similar meanings to the question fields can be accurately queried, and the situation of not being able to query out fields with similar meanings due to the ambiguity and polysemy of natural language can be avoided. Then, the table information related to the queried target fields is obtained from the knowledge graph; based on the table information and the question to be processed, a generation prompt text for generating a query statement is constructed. In this way, by utilizing the semantic understanding ability of the large language model, according to the generation prompt text, the target query statement for the question to be processed can be accurately constructed. Therefore, based on the target query statement, accurate data query can be performed on the target database to obtain a query result, thereby improving the accuracy of the database query.
[0101] In a specific embodiment, such as Figure 4As shown in the figure, it is a schematic diagram of a database query framework in an embodiment. The terminal obtains the problem to be processed input by the user, and the terminal inputs the problem to be processed to the server. The server calls the intent query system to perform data query based on the problem to be processed and obtains the query result. The server returns the query result to the terminal. Among them, the intent query system can be deployed inside the server. The process of the intent query system performing data query can be referred to below. For example, Figure 5 As shown in the figure, it is a schematic diagram of the data query process in an embodiment. For specific reference, see the following:
[0102] Step 1: The intent query system obtains the problem to be processed.
[0103] Step 2: The intent query system constructs and extracts a prompt text according to the problem to be processed, and calls a large language model according to the extracted prompt text to perform field extraction, obtaining at least one problem field.
[0104] Optionally, the intent query system obtains an extraction prompt text template for field extraction, fills the problem to be processed into the extraction prompt text template to generate an extraction prompt text; calls a large language model, and outputs at least one problem field according to the extraction prompt text. For example, by calling the large language model, problem field 1 and problem field 2 are obtained.
[0105] Step 3: For each problem field, the intent query system calls the vector library to perform problem field matching to obtain a target field that matches each problem field.
[0106] Optionally, the vector library construction steps include: for each target field in the target database, obtaining multiple semantically similar fields that are semantically similar to the target field from different knowledge bases; for each semantically similar field, calculating the field coincidence degree between the semantically similar field and other semantically similar fields respectively; grouping the other semantically similar fields and the semantically similar fields corresponding to the field coincidence degree greater than or equal to the threshold into the same field group. For each field group, sorting the vectors of at least one semantically similar field in the field group to obtain a vector sequence; using the vector located in the middle position of the vector sequence as the vector of the field group. Storing the vectors of each field group corresponding to the target field to obtain a vector library.
[0107] Optionally, for each problem field, the intent query system obtains the vectors of at least one field group corresponding to each target field from the vector library; based on the vector similarity between the vector of the problem field and the vectors of each field group, queries the field group to which the maximum vector similarity belongs; uses the target field corresponding to the queried field group as the target field that matches the problem field. For example, for problem field 1, the matching target field, that is, SQL field 1, is obtained by executing Step 3. Similarly, for problem field 2, the matching target field, that is, SQL field 2, is obtained by executing Step 3.
[0108] Step 4: The intent query system queries the table information through the knowledge graph based on the target fields matched by each question field, obtains the table information corresponding to each question field, and constructs and generates a prompt text based on the table information corresponding to each question field and the question to be processed.
[0109] For example, by executing Step 4, it is determined that Table 1 belongs to SQL field 1, Table 2 belongs to SQL field 2, and the associated field between Table 1 and Table 2.
[0110] Step 5: The intent query system calls the large language model, generates a query statement according to the generated prompt text, and obtains the target query statement.
[0111] For example, by filling the question to be processed, Table 1 belonging to SQL field 1, Table 2 belonging to SQL field 2, and the associated field between Table 1 and Table 2 into the generated prompt text template, the generated prompt text is obtained, the large language model is called, and based on the generated prompt text, an SQL query statement is output.
[0112] Step 6: The intent query system queries the target database according to the target query statement to obtain a query result.
[0113] In this embodiment, by parsing the question to be processed for data query, the key content of the question to be processed can be accurately analyzed to obtain the question fields, avoiding subsequent processing of irrelevant content in the question to be processed, and ensuring the accuracy and query efficiency of data query. Then, a vector library related to the target database is obtained. Since the vector library is constructed based on the vectors of the semantically similar fields of each target field in the target database, according to the vectors of the question fields and the vector library, the target fields matching the question fields are queried. That is, the target fields with similar meanings to the question fields can be accurately queried, and it can be avoided that fields with similar meanings cannot be queried due to the ambiguity and polysemy of natural language. Then, the table information related to the queried target fields is obtained from the knowledge graph; based on the table information and the question to be processed, a prompt text for generating a query statement is constructed. In this way, by utilizing the semantic understanding ability of the large language model, according to the generated prompt text, the target query statement regarding the question to be processed can be accurately constructed. Therefore, based on the target query statement, the target database can be accurately queried for data to obtain a query result, thereby improving the accuracy of database query.
[0114] It should be understood that although the steps in the flowcharts involved in the above-described embodiments are shown in sequence according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless there is a clear description in this article, the execution of these steps has no strict order limit, and these steps can be executed in other orders. Moreover, at least a part of the steps in the flowcharts involved in the above-described embodiments may include multiple steps or multiple stages. These steps or stages are not necessarily executed at the same time, but can be executed at different times. The execution order of these steps or stages is not necessarily sequential, but can be executed alternately or in turn with at least a part of other steps or steps or stages in other steps.
[0115] Based on the same inventive concept, the embodiments of the present application also provide a database query device for implementing the above-mentioned database query method. The implementation solutions provided by this device for solving problems are similar to the implementation solutions recorded in the above method. Therefore, the specific limitations in one or more embodiments of the following database query devices can refer to the limitations on the database query method in the above text, and will not be repeated here.
[0116] In an exemplary embodiment, as Figure 6 shown, a database query device 600 is provided, including: a problem parsing module 602, a vector library acquisition module 604, a field query module 606, a query statement determination module 608, and a data query module 610, where:
[0117] The problem parsing module 602 is configured to parse the problem to be processed for data query to obtain problem fields;
[0118] The vector library acquisition module 604 is configured to acquire a vector library related to the target database, and the vector library is constructed based on the vectors of semantic similar fields of each target field in the target database;
[0119] The field query module 606 is configured to query the target fields that match the problem fields according to the vectors of the problem fields and the vector library, and obtain the table information related to the queried target fields from the knowledge graph;
[0120] The query statement determination module 608 is configured to construct a generation prompt text for generating a query statement based on the table information and the problem to be processed, call a large language model, and output a target query statement for the problem to be processed according to the generation prompt text;
[0121] The data query module 610 is configured to perform data query on the target database based on the target query statement to obtain a query result.
[0122] In one embodiment, the problem parsing module 602 is configured to obtain an extraction prompt text template for field extraction, fill the problem to be processed into the extraction prompt text template to generate an extraction prompt text, and call a large language model to output problem fields according to the extraction prompt text.
[0123] In one embodiment, the device further includes a vector library construction module, which is configured to, for each target field in the target database, obtain multiple semantically similar fields that are semantically similar to the target field from different knowledge bases, group the multiple semantically similar fields to obtain at least one field group, determine the vector of each field group according to the vectors of the semantically similar fields in each field group, and store the vector of each field group corresponding to the target field to obtain a vector library.
[0124] In one embodiment, the vector library construction module is configured to calculate the field coincidence degree between each semantically similar field and other semantically similar fields respectively for each semantically similar field, and group the other semantically similar fields corresponding to the field coincidence degree greater than or equal to the threshold and the semantically similar fields into the same field group.
[0125] In one embodiment, the vector library construction module is configured to, for each field group, sort the vectors of at least one semantically similar field in the field group to obtain a vector sequence, and use the vector located in the middle position of the vector sequence as the vector of the field group.
[0126] In one embodiment, the field query module 606 is configured to obtain the vectors of at least one field group corresponding to each target field from the vector library, query the field group to which the maximum vector similarity belongs based on the vector similarity between the vector of the problem field and the vectors of each field group, and use the target field corresponding to the queried field group as the target field matching the problem field.
[0127] Each module in the above database query device can be implemented in whole or in part by software, hardware, and their combination. The above modules can be embedded in the processor in the computer device in hardware form or be independent of it, or can be stored in the memory in the computer device in software form so that the processor can call and execute the operations corresponding to the above modules.
[0128] In an exemplary embodiment, a computer device is provided. The computer device can be a server, and its internal structure diagram can be as Figure 7As shown in the figure. The computer device includes a processor, a memory, an input / output interface (Input / Output, abbreviated as I / O), and a communication interface. Among them, the processor, the memory, and the input / output interface are connected through a system bus, and the communication interface is connected to the system bus through the input / output interface. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program, and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The input / output interface of the computer device is used to exchange information between the processor and external devices. The communication interface of the computer device is used to communicate with external terminals through a network connection. When the computer program is executed by the processor, it implements a database query method.
[0129] Those skilled in the art can understand that Figure 7 the structure shown in the figure is only a block diagram of some structures related to the solution of this application, and does not constitute a limitation on the computer device to which the solution of this application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine some components, or have different component arrangements.
[0130] In an exemplary embodiment, a computer device is provided, including a memory and a processor. A computer program is stored in the memory. When the processor executes the computer program, the following steps are implemented: parsing the problem to be processed for data query to obtain problem fields; obtaining a vector library related to the target database, where the vector library is constructed based on the vectors of semantic similar fields of each target field in the target database; querying the target fields that match the problem fields according to the vectors of the problem fields and the vector library, and obtaining table information related to the queried target fields from the knowledge graph; based on the table information and the problem to be processed, constructing a generation prompt text for generating a query statement, calling a large language model, and according to the generation prompt text, outputting a target query statement for the problem to be processed; based on the target query statement, performing a data query on the target database to obtain a query result.
[0131] In one embodiment, when the processor executes the computer program, the following steps are further implemented: obtaining an extraction prompt text template for field extraction, filling the problem to be processed into the extraction prompt text template to generate an extraction prompt text; calling a large language model, and according to the extraction prompt text, outputting the problem fields.
[0132] In one embodiment, when the processor executes the computer program, the following steps are further implemented: for each target field in the target database, obtain multiple semantically similar fields that are semantically similar to the target field from different knowledge bases; group the multiple semantically similar fields to obtain at least one field group; determine the vector of each field group according to the vectors of the semantically similar fields in each field group, and store the vector of each field group corresponding to the target field to obtain a vector library.
[0133] In one embodiment, when the processor executes the computer program, the following steps are further implemented: for each semantically similar field, calculate the field coincidence degree between the semantically similar field and other semantically similar fields respectively; group the other semantically similar fields corresponding to the field coincidence degree greater than or equal to the threshold and the semantically similar field into the same field group.
[0134] In one embodiment, when the processor executes the computer program, the following steps are further implemented: for each field group, sort the vectors of at least one semantically similar field in the field group to obtain a vector sequence; use the vector located in the middle position of the vector sequence as the vector of the field group.
[0135] In one embodiment, when the processor executes the computer program, the following steps are further implemented: from the vector library, obtain the vectors of at least one field group corresponding to each target field; based on the vector similarity between the vector of the problem field and the vectors of each field group, query the field group to which the maximum vector similarity belongs; use the target field corresponding to the queried field group as the target field that matches the problem field.
[0136] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the following steps are implemented: parse the to-be-processed problem for data query to obtain a problem field; obtain a vector library related to the target database, where the vector library is constructed according to the vectors of the semantically similar fields of each target field in the target database; according to the vector of the problem field and the vector library, query the target field that matches the problem field, and obtain the table information related to the queried target field from the knowledge graph; based on the table information and the to-be-processed problem, construct a generation prompt text for generating a query statement, call a large language model, and according to the generation prompt text, output a target query statement for the to-be-processed problem; based on the target query statement, perform data query on the target database to obtain a query result.
[0137] In one embodiment, when the computer program is executed by the processor, the following steps are further implemented: obtain an extraction prompt text template for field extraction, fill the to-be-processed problem into the extraction prompt text template to generate an extraction prompt text; call a large language model, and according to the extraction prompt text, output a problem field.
[0138] In one embodiment, when the computer program is executed by a processor, the following steps are further implemented: for each target field in the target database, obtain multiple semantically similar fields that are semantically similar to the target field from different knowledge bases; group the multiple semantically similar fields to obtain at least one field group; determine the vector of each field group according to the vectors of the semantically similar fields in each field group, and store the vector of each field group corresponding to the target field to obtain a vector library.
[0139] In one embodiment, when the computer program is executed by a processor, the following steps are further implemented: for each semantically similar field, calculate the field coincidence degree between the semantically similar field and other semantically similar fields respectively; group the other semantically similar fields corresponding to the field coincidence degree greater than or equal to the threshold and the semantically similar field into the same field group.
[0140] In one embodiment, when the computer program is executed by a processor, the following steps are further implemented: for each field group, sort the vectors of at least one semantically similar field in the field group to obtain a vector sequence; use the vector located in the middle position of the vector sequence as the vector of the field group.
[0141] In one embodiment, when the computer program is executed by a processor, the following steps are further implemented: from the vector library, obtain the vectors of at least one field group corresponding to each target field; based on the vector similarity between the vector of the problem field and the vectors of each field group, query the field group to which the maximum vector similarity belongs; use the target field corresponding to the queried field group as the target field that matches the problem field.
[0142] In one embodiment, a computer program product is provided, including a computer program, which when executed by a processor, implements the following steps: parse the problem to be processed for data query to obtain a problem field; obtain a vector library related to the target database, where the vector library is constructed according to the vectors of the semantically similar fields of each target field in the target database; according to the vector of the problem field and the vector library, query the target field that matches the problem field, and obtain the table information related to the queried target field from the knowledge graph; based on the table information and the problem to be processed, construct a generation prompt text for generating a query statement, call a large language model, and according to the generation prompt text, output a target query statement for the problem to be processed; based on the target query statement, perform a data query on the target database to obtain a query result.
[0143] In one embodiment, when the computer program is executed by a processor, the following steps are further implemented: obtain an extraction prompt text template for field extraction, fill the problem to be processed into the extraction prompt text template to generate an extraction prompt text; call a large language model, and according to the extraction prompt text, output a problem field.
[0144] In one embodiment, when the computer program is executed by a processor, the following steps are further implemented: for each target field in the target database, obtain multiple semantically similar fields that are semantically similar to the target field from different knowledge bases; group the multiple semantically similar fields to obtain at least one field group; determine the vector of each field group according to the vectors of the semantically similar fields in each field group, and store the vector of each field group corresponding to the target field to obtain a vector library.
[0145] In one embodiment, when the computer program is executed by a processor, the following steps are further implemented: for each semantically similar field, calculate the field coincidence degree between the semantically similar field and other semantically similar fields respectively; group the other semantically similar fields corresponding to the field coincidence degree greater than or equal to the threshold and the semantically similar field into the same field group.
[0146] In one embodiment, when the computer program is executed by a processor, the following steps are further implemented: for each field group, sort the vectors of at least one semantically similar field in the field group to obtain a vector sequence; use the vector located in the middle position of the vector sequence as the vector of the field group.
[0147] In one embodiment, when the computer program is executed by a processor, the following steps are further implemented: from the vector library, obtain the vectors of at least one field group corresponding to each target field; based on the vector similarity between the vector of the problem field and the vectors of each field group, query the field group to which the maximum vector similarity belongs; use the target field corresponding to the queried field group as the target field matching the problem field.
[0148] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use, and processing of relevant data need to comply with relevant regulations.
[0149] Those of ordinary skill in the art can understand that all or part of the processes in the methods of the above embodiments can be completed by instructing relevant hardware through a computer program. The computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above methods. Among them, any reference to a memory, a database, or other media used in the embodiments provided in the present application can include at least one of non-volatile memory and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetoresistive random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM), etc. The databases involved in the embodiments provided in the present application can include at least one of relational databases and non-relational databases. Non-relational databases can include distributed databases based on blockchain, etc., without limitation. The processors involved in the embodiments provided in the present application can be general-purpose processors, central processing units, graphics processing units, digital signal processors, programmable logic devices, data processing logics based on quantum computing, artificial intelligence (AI) processors, etc., without limitation.
[0150] The technical features of the above embodiments can be combined arbitrarily. For the sake of brevity of description, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, it should be considered as falling within the scope recorded in the present application.
[0151] The above-described embodiments merely represent several implementation manners of the present application. The description thereof is relatively specific and detailed, but it should not be construed as a limitation to the patent scope of the present application. It should be noted that for those of ordinary skill in the art, without departing from the concept of the present application, several modifications and improvements can still be made, and these all fall within the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the appended claims.
Claims
1. A database query method, characterized in that, The method includes: Parsing the problem to be processed for data query to obtain problem fields; Obtaining a vector library related to the target database, where the vector library is constructed based on vectors of semantic similar fields of each target field in the target database; Querying for target fields that match the problem fields based on the vectors of the problem fields and the vector library, and obtaining table information related to the queried target fields from the knowledge graph; Constructing a generation prompt text for generating a query statement based on the table information and the problem to be processed, calling a large language model, and outputting a target query statement for the problem to be processed according to the generation prompt text; Performing a data query on the target database based on the target query statement to obtain a query result.
2. The method according to claim 1, characterized in that The parsing of the problem to be processed for data query to obtain problem fields includes: Obtaining an extraction prompt text template for field extraction, filling the problem to be processed into the extraction prompt text template to generate an extraction prompt text; Calling a large language model and outputting problem fields according to the extraction prompt text.
3. The method according to claim 1, characterized in that The steps for constructing the vector library include: For each target field in the target database, obtaining multiple semantic similar fields that are semantically similar to the target field from different knowledge bases; Grouping the multiple semantic similar fields to obtain at least one field group; Determining the vector of each field group according to the vectors of the semantic similar fields in each field group, and storing the vector of each field group corresponding to the target field to obtain a vector library.
4. The method according to claim 3, characterized in that The grouping of the multiple semantic similar fields to obtain at least one field group includes: For each semantic similar field, calculating the field coincidence degree between the semantic similar field and other semantic similar fields respectively; Grouping other semantic similar fields corresponding to a field coincidence degree greater than or equal to a threshold and the semantic similar field into the same field group.
5. The method according to claim 3, characterized in that, The determining of the vector of each field group according to the vectors of the semantic similar fields in each field group includes: For each field group, sorting the vectors of at least one semantic similar field in the field group to obtain a vector sequence; Using the vector located in the middle position of the vector sequence as the vector of the field group.
6. The method according to claim 1, wherein The querying for target fields that match the problem fields based on the vectors of the problem fields and the vector library includes: Obtaining the vectors of at least one field group corresponding to each target field from the vector library; Querying for the field group to which the maximum vector similarity belongs based on the vector similarity between the vector of the problem field and the vectors of each field group; Using the target field corresponding to the queried field group as the target field that matches the problem field.
7. A database query device, characterized in that The device includes: A problem parsing module for parsing the problem to be processed for data query to obtain problem fields; A vector library obtaining module for obtaining a vector library related to the target database, where the vector library is constructed based on vectors of semantic similar fields of each target field in the target database; A field query module, configured to query a target field that matches the problem field according to the vector of the problem field and the vector library, and obtain table information related to the queried target field from the knowledge graph; A query statement determination module, configured to construct a generation prompt text for generating a query statement based on the table information and the problem to be processed, call a large language model, and output a target query statement for the problem to be processed according to the generation prompt text; A data query module, configured to perform a data query on the target database based on the target query statement to obtain a query result.
8. A computer device, comprising a memory and a processor, the memory storing a computer program, characterized in that, When the processor executes the computer program, the steps of the method according to any one of claims 1 to 6 are implemented.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.
10. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.
Citation Information
Cited By
Intelligent agent-based statement conversion method and device, electronic equipment and storage medium
CN120804302A
Chart matching display method, electronic device and computer readable storage medium
CN122452523A