Intelligent agent-based statement conversion method and device, electronic equipment and storage medium
By extracting target information from query requests and analyzing the results of historical query requests in a preset database, the accuracy problem of large language models when generating SQL statements is solved, and higher-precision data queries are achieved.
Patent Information
- Application Number
- CN202511305652.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-12
- Publication Date
- 2025-10-17
- Estimated Expiration
- 2045-09-12
AI Technical Summary
When parsing query statements entered by users, existing large language models find it difficult to accurately extract core business elements such as analysis dimensions, indicators, and data cycles, resulting in incorrect logic in the generated SQL statements, affecting the accuracy of data queries.
By obtaining the query request input by the user, extracting the target information and encoding it into a semantic vector, matching similar historical query requests from the preset database, analyzing the conversion results, and combining the preset prompt words into the large language model to generate accurate SQL statements.
The accuracy of query direction and SQL statements output by large language models is improved, reducing the generation of erroneous logic.
Smart Images

Figure CN120804302A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of computer, in particular, the present application relates to a kind of sentence conversion method and device based on agent, electronic equipment and storage medium. BACKGROUND
[0002] With the explosive growth of data, the demand for automatically generating SQL (Structured Query Language) statements from natural language queries to achieve efficient data retrieval is increasingly urgent. Currently, in the existing technical solutions, the user inputs the natural query language into the trained large language model and inputs the relevant prompt words to make the large language model output the SQL statement converted from the natural query language.
[0003] However, in this way of the existing technical solution, the large language model has difficulty in accurately extracting and analyzing core business elements such as dimensions, indicators, and data periods when parsing the user input query statement. This leads to the model being prone to "hallucinations", generating SQL statements containing non-existent fields or incorrect logic, which seriously affects the accuracy of data query. SUMMARY
[0004] The present application aims to at least solve one of the above technical defects, and the technical solution provided by the embodiments of the present application is as follows: In a first aspect, the embodiments of the present application provide a sentence conversion method based on an agent, comprising: obtaining a query request of a first format input by a user, and extracting target information from the query request; wherein the target information represents the query range of the corresponding query request; encoding the target information to obtain a first semantic vector, and matching a first preset number of first historical query requests from a first preset database based on the first semantic vector; wherein the first preset database stores each historical query request and the first semantic vector corresponding to each historical query request; obtaining the historical sentence conversion results of the first preset number of first historical query requests respectively, analyzing each historical sentence conversion result to obtain first reference information about the query request; inputting the first reference information and a preset prompt word into a large language model together to make the large language model output a sentence conversion result of the query request; wherein the sentence conversion result is the expression form of the query request in a second format.
[0005] In a second aspect, the embodiments of the present application provide a sentence conversion device based on an agent, comprising: The information extraction module is configured to obtain a query request in a first format input by a user, and extract target information from the query request, wherein the target information is used to represent a query range of the corresponding query request. The historical request matching module is configured to encode the target information to obtain a first semantic vector, and match a first preset number of first historical query requests from a first preset database based on the first semantic vector, wherein the first preset database stores each historical query request and a first semantic vector corresponding to each historical query request. The sentence analysis module is configured to obtain a historical sentence conversion result of each of the first preset number of first historical query requests, analyze each historical sentence conversion result, and obtain first reference information about the query request. The sentence conversion module is configured to input the first reference information and a preset prompt word into a large language model together, so that the large language model outputs a sentence conversion result of the query request, wherein the sentence conversion result is an expression form of the query request in a second format.
[0006] In a third aspect, an embodiment of the present application provides an electronic device, including a memory, a processor, and a computer program stored in the memory. The processor executes the computer program to implement the method provided in the first aspect or any optional embodiment of the first aspect.
[0007] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, and the computer-readable storage medium stores a computer program. When the computer program is executed by a processor, the method provided in the first aspect or any optional embodiment of the first aspect is implemented.
[0008] The technical scheme provided by the embodiments of the present application has the following beneficial effects: Firstly, the scheme provided by the embodiments of the present application can determine the query range of the data to be searched by the query request by extracting the target information from the query request, thereby improving the accuracy of the query direction. Secondly, the scheme provided by the embodiments of the present application can match the first historical query request with reference value from the historical query request according to the extracted target information, and obtain the reference information about the conversion process of the query request according to each first historical query request, thereby providing rich reference content for the subsequent sentence conversion of the large language model.
[0009] Finally, the reference information and the preset prompt word are input into the large language model together, so that the large language model can convert the query request according to the provided reference content to obtain the final converted sentence conversion result.
[0010] The scheme provided in the application extracts target information in advance from the query request, extracts reference information corresponding to the query request from the conversion results corresponding to the historical query request according to the target information, and inputs the reference information into the large language model to make the large language model output the conversion result corresponding to the query request. Compared with the scheme in the prior art in which the query request is directly input into the large language model for conversion, the scheme can provide more accurate information for the large language model, and thus the conversion sentence output by the large language model has higher accuracy. BRIEF DESCRIPTION OF DRAWINGS
[0011] In order to more clearly illustrate the technical solutions in the embodiments of the application, the drawings needed to be used in the description of the embodiments of the application will be briefly introduced.
[0012] Figure 1 A flowchart of a sentence conversion method based on an agent provided in an embodiment of the application; Figure 2 A flowchart of a target field determination method in an example of an embodiment of the application; Figure 3 A flowchart of a historical query request recall method in an example of an embodiment of the application; Figure 4 A flowchart of a whole process of a sentence conversion method based on an agent in an example of an embodiment of the application; Figure 5 A structural block diagram of a sentence conversion device based on an agent provided in an embodiment of the application; Figure 6 A structural diagram of an electronic device provided in an embodiment of the application. DETAILED DESCRIPTION
[0013] The embodiments of the application will be described below in conjunction with the drawings in the application. It should be understood that the embodiments described below in conjunction with the drawings are exemplary descriptions of the technical solutions of the embodiments of the application, and do not limit the technical solutions of the embodiments of the application.
[0014] Those skilled in the art can understand that the singular forms "a", "an", "said" and "the" used herein include plural forms unless otherwise stated. It should be further understood that the terms "include" and "contain" used in the embodiments of the present application mean that the corresponding features can be implemented as the presented features, information, data, steps, operations, elements and / or components, but do not exclude other features, information, data, steps, operations, elements, components and / or their combinations supported by the present technology. It should be understood that when we say that an element is "connected" or "coupled" to another element, the element can be directly connected or coupled to the other element, or it can mean that the element and the other element establish a connection relationship through an intermediate element. In addition, "connected" or "coupled" used herein can include wireless connection or wireless coupling. The term "and / or" used herein indicates that at least one of the items defined by the term, for example, "A and / or B" can be implemented as "A", or as "B", or as "A and B".
[0015] In order to make the purpose, technical scheme and advantages of the present application clearer, the embodiments of the present application will be further described in detail below with reference to the accompanying drawings.
[0016] The technical scheme of the embodiments of the present application and the technical effects produced by the technical scheme of the present application will be described below through the description of several exemplary embodiments. It should be pointed out that the following embodiments can be mutually referenced, borrowed or combined, and the same terms, similar features and similar implementation steps in different embodiments will not be described repeatedly.
[0017] Figure 1 A flowchart of a sentence conversion method based on an intelligent agent is provided for the embodiments of the present application. The execution subject of the method can be a terminal (such as a computer, a mobile phone, etc.) or an intelligent agent, etc. As shown in Figure 1 The method can include: Step S101, obtaining a query request in a first format input by a user, and extracting target information from the query request; wherein the target information is used to represent the query range of the corresponding query request.
[0018] In the embodiments of the present application, the query request can be a request for obtaining one or more pieces of data stored in the database, the first format can be a natural language format, for example: "Please help me query the weather in Beijing this week", or other forms of language (such as programming code, etc.), which are not limited in the embodiments of the present application. The target information can include the dimension, time period, index and the like of the query request, for example, for the query request "Please help me query the weather in Beijing this week", the target information includes "Beijing" (the region queried, that is, the dimension), "this week" (the time period queried, that is, the time period), and "weather" (the content to be queried, that is, the index). Optionally, the index in the query request can be further limited, for example, "evening weather", wherein "evening" can also be referred to as "range", which is used to further limit the index.
[0019] Specifically, when the user inputs the query request in the first format, since the query request can contain content irrelevant to the query range, if these contents are not processed, it can cause the large language model to form an understanding deviation for the user's real query range, and further cause the converted query statement to be inaccurate. In order to solve the above problem, the scheme provided in the embodiments of the present application can first extract information from the query request, and extract the key content expressing the query range in the query request as the target information, thereby avoiding the interference caused by irrelevant content.
[0020] In step S102, the target information is encoded to obtain a first semantic vector, and a first preset number of first historical query requests are matched from a first preset database based on the first semantic vector; wherein the first preset database stores each historical query request and the first semantic vector corresponding to each historical query request.
[0021] In the embodiments of the present application, the encoding operation can use a model such as BGE-M3 (a general semantic vector model) to convert the target information into the form of a corresponding semantic vector. The first preset database can also be referred to as a vector library, which is used to store historical query requests and semantic vectors of the target information corresponding to each historical query request.
[0022] Specifically, after the target information is extracted from the query request, in order to provide more accurate information for the large language model, it can be checked from the first preset database whether there is a same or similar historical query request. The first preset database contains relatively accurate historical query requests, semantic vectors of target information corresponding to the historical query requests, and sentence transformation results. These accurate historical query requests and sentence transformation results can be used as reference information for the current query request. Specifically, the first semantic vector of the current query request can be matched with the first preset database to match historical query requests similar to the first semantic vector of the current query request. These historical query requests can be used as information reference for the large language model.
[0023] Optionally, when the first historical query request is matched, the first preset number of historical query requests closest to the first semantic vector can be determined as the first historical query request, or all historical query requests satisfying the preset condition can be determined as the first historical query request by setting the preset condition. The present application does not limit this.
[0024] In step S103, the historical sentence transformation results of the first preset number of first historical query requests are obtained, and the first reference information about the query request is obtained by analyzing the historical sentence transformation results.
[0025] Specifically, since the obtained first historical query requests are only a plurality of historical query requests in the first preset database that are "semantically closest" to the current query request, it cannot be determined that the semantics of these first historical query requests are similar to the semantics of the current query request. Therefore, further analysis of these first historical query requests is required. Specifically, the historical sentence transformation results corresponding to each of the first historical query requests can be obtained from the first preset database, and further analysis of the historical sentence transformation results can be performed. If the analysis result shows that the historical sentence transformation results are indeed similar to the semantics of the current query request, the generation process of the historical sentence transformation results can be used as the first reference information provided to the large language model. If the analysis result shows that the historical sentence transformation results are not similar to the semantics of the current query request, the first reference information needs to be further refined to provide to the large language model.
[0026] In step S104, the first reference information and the preset prompt word are input into the large language model to make the large language model output the sentence transformation result of the query request. The sentence transformation result is an expression form of the query request in a second format.
[0027] In the embodiments of the present application, the preset prompt word can be a command sentence used to instruct the large language model to perform a related operation, which can include the query request itself and the format that needs to be converted, for example: "According to the following reference information, help me convert 'Please help me query the weather in Beijing this week' into the form of sql language". The second format can be a format different from the first format, for example, when the first format is natural language, the second format can be a sql statement.
[0028] Specifically, after generating the first reference information, the first reference information and the preset prompt word can be input into the large language model together, and the large language model can convert the query request according to the first reference information and the preset prompt word, and then obtain the sentence conversion result of the query request in the second format.
[0029] The scheme provided by the present application first extracts target information from the query request, which can clearly determine the query range of the data to be searched in the current query request, thereby improving the accuracy of the query direction. Secondly, the scheme provided by the embodiments of the present application matches the first historical query request with reference value from the historical query request according to the extracted target information, and obtains the reference information about the conversion process of the current query request according to each first historical query request, which can provide rich reference content for the subsequent sentence conversion of the large language model.
[0030] Finally, the reference information and the preset prompt word are input into the large language model together, so that the large language model can convert the query request according to the provided reference content to obtain the final converted sentence conversion result.
[0031] The scheme provided by the present application extracts target information from the query request in advance, extracts reference information about the current query request from the conversion results corresponding to the historical query request according to the target information, and inputs the reference information into the large language model to make the large language model output the conversion result corresponding to the current query request. Compared with the scheme in the prior art that directly inputs the query request into the large language model for conversion, the scheme can provide more accurate information for the large language model, thereby making the conversion sentence output by the large language model more accurate.
[0032] On the basis of the above embodiments, as an optional embodiment, if the first similarity corresponding to each first historical query request is less than a first preset threshold, the first similarity is the similarity between the first semantic vector corresponding to the first historical query request and the first semantic vector of the target information. The historical sentence conversion results are analyzed to obtain the first reference information about the query request, specifically including: The query request is encoded to obtain a second semantic vector. For each historical query request in the first preset database, a second similarity between the second semantic vector of the historical query request and the second semantic vector of the query request is determined; the first preset database further includes the second semantic vector of each historical query request; A historical query request with a second similarity greater than a second preset threshold is determined as a second historical query request; A first data query table corresponding to each second historical query request and a query frequency of each first data query table are obtained from the first preset database; wherein the first preset database includes a plurality of data query tables and the query frequency of each data query table; Each first data query table is sorted in descending order of the query frequency, and a second preset number of first data query tables with a higher sorting result and the data query table corresponding to each first historical query request are all taken as second data query tables; Based on the semantics of each field in each second data query table, a target field is extracted from each second data query table, and the target field and target information are taken as the first reference information together.
[0033] In the embodiments of the present application, each second semantic vector can be a vector form representing the semantic of the entire statement of the corresponding query request. The data query table can be a database table where the data queried by the query request is located, and the field can be the name of a certain key (including primary key, foreign key, etc.) in the database table. The first preset threshold can be used to represent that the semantics of the first historical query request is the same or similar to the semantics of the current query request.
[0034] Specifically, after matching the first historical query requests from the first preset database, it is necessary to first judge whether the matched first historical query requests have the same or similar semantics as the current query request. The judgment method can be achieved by setting a first preset threshold. If the first similarity (the calculation method of the similarity mentioned in the embodiments of the present application can be the cosine value between two vectors) between the first semantic vector of a certain first historical query request and the first semantic vector corresponding to the current query request is greater than the first preset threshold, it can be considered that the semantics between the two query requests corresponding to the two first semantic vectors are the same or similar, otherwise it can be considered that the semantics between the two query requests are not the same or similar. If the first similarity between each first historical query request and the current query request is less than the first preset threshold, it can be considered that there is no first historical query request with the same or similar semantics as the current query request in the matched first historical query requests (it can be considered that the current query request is a new type of query request), and the information of the first historical query request cannot be directly used as a reference, so further information analysis and extraction is needed. At this time, the current query request needs to be encoded again to obtain the second semantic vector corresponding to the current query request (such as embedding model), and the historical query request can also be encoded in the same way to obtain the second semantic vector corresponding to each historical query request. The specific representation is as follows:
[0035] wherein q represents the current query request, e q represents the second semantic vector of the current query request, q i represents the current query request, e qi represents the second semantic vector of the current query request.
[0036] Then, according to the second semantic vector, the second historical query request with a second similarity greater than a second preset threshold is matched from the first preset database (the second preset threshold can be set to be less than the first preset threshold). The calculation method of the second similarity can be the same as that of the first similarity, which can be represented as follows:
[0037] wherein sim(q, q i ) represents the second similarity, e q represents the second semantic vector of the current query request, e qi represents the second semantic vector of the second historical query request.
[0038]
[0039] wherein I represents the set of second historical query requests, sim_score sim(q, q i ) represents the second similarity corresponding to the second historical query request, and θ represents the second preset threshold.
[0040] Then, it is further needed to acquire the first data table used by each second historical query request when querying data, and to acquire the number of times each first data table is queried due to historical query requests, and to select the second preset number of first data query tables with more number of times of being queried and the data query table corresponding to each first historical query request together as the second data query table (this process can be realized by RRF (Reciprocal Rank Fusion) technology fusion), and the more the number of times of being queried is, the more data types contained in the first data query table can be, and the greater the possibility that the first data query table contains data required by the current query request is.
[0041] After obtaining each second data query table, the fields contained in each second data query table can be acquired, each field is used to represent the data stored in the corresponding column in the corresponding second data query table, and a target field is selected from these fields, and the semantic content represented by the target field can be used as the first reference information of the current query request.
[0042] On the basis of each of the above embodiments, as an optional embodiment, the target field is extracted from each second data query table based on the semantics of each field in each second data query table, and specifically includes: For any two second data query tables, it is determined whether there is a field with the same semantics between the two second data query tables; For any field pair, the two fields in the field pair respectively belong to two second data query tables, and if it is determined that the two fields in the field pair have the same semantics, the two fields in the field pair are both determined as the target field.
[0043] Specifically, the field relationship between each data query table in the embodiments of the present application can be stored in the metadata database, so that the embodiments of the present application can determine whether there is a direct or indirect association between any two data query tables by searching the metadata database. For example, if data query table A and data query table B have a direct primary-foreign key relationship in the metadata database, the fields corresponding to the primary-foreign key relationship can be determined as target fields. If there is no direct primary-foreign key relationship, other tables having a primary-foreign key relationship with both data query table A and data query table B, such as data query table C, are extracted. By extracting the primary-foreign key fields and determining whether they are consistent, it is determined whether data query table A and data query table B are indirectly associated through data query table C. If there is an association, the fields corresponding to the primary-foreign key relationship between data query table A and data query table C and the fields corresponding to the primary-foreign key relationship between data query table B and data query table C can be determined as target fields.
[0044] Optionally, in the scheme of the embodiments of the present application, the selection of target fields can also refer to Figure 2 Figure 2 As shown in FIG. 6, the requirement description, dimensions, indicators and yardsticks, requirement account period and the like contained in the query request can be taken as dimension fields and indicator fields, the filtering conditions can be taken as filtering fields, the association relationship between each table in the database table can be taken as association fields, and the primary-foreign key relationship between tables can be taken as grouping fields.
[0045] On the basis of each of the above embodiments, as an optional embodiment, if the first similarity corresponding to the first historical query request is not less than a first preset threshold, the first similarity is the similarity between the semantic vector corresponding to the first historical query request and the first semantic vector of the target information. The first reference information about the query request is obtained by analyzing the historical statement conversion results, specifically including: The first historical query request with the first similarity not less than the first preset threshold is determined as a third historical query request. For each third historical query request, the target information is extracted from the historical statement conversion result of the third historical query request, and the second data query table corresponding to the third historical query request is obtained from the first preset database. The target information corresponding to each third historical query request, the table name of the data query table corresponding to the third historical query request, each field and the historical statement conversion result corresponding to the third historical query request are collectively taken as the first reference information.
[0046] Specifically, if there is a historical query request in each of the acquired first historical query requests that has a first similarity to the current query request that is not less than a first preset threshold, it indicates that this part of the historical query request and the current query request have the same or similar semantics, and then the information of this part of the historical query request in the sentence conversion process can be used as the first reference information. Specifically, this part of the historical query request is determined as a third historical query request, and then the demand name description of each third historical query request (for example, the demand is to query "weather"), the historical sentence conversion result of each third historical query request, and each second data query table searched when querying each third historical query request can be obtained, as well as the table name of each second data query table and each field contained in each second data query table. After integrating these information, they are collectively used as the first reference information to be provided to the large language model later.
[0047] On the basis of each of the above embodiments, as an optional embodiment, the first reference information and the preset prompt word are jointly input into the large language model to make the large language model output the sentence conversion result of the query request, and specifically includes: using the large language model to infer based on the first reference information and the preset prompt word to generate a first prediction result of the sentence conversion result corresponding to the query request; querying from a second preset database based on the first prediction result to obtain a query result; wherein the second preset database includes a plurality of data query tables for responding to query requests; if the query result is normal, the first prediction result is used as the sentence conversion result; if the query result is abnormal, at least one update step is executed until the query result is normal, and a second prediction result obtained by the last update step is used as the sentence conversion result; wherein the update step includes: acquiring abnormal information included in the current query result, and using the abnormal information as second reference information; inference based on the first reference information, each second reference information, and a preset prompt word to obtain a second prediction result.
[0048] In the embodiments of the present application, the prediction result can be a sentence conversion result pre-predicted by the large language model agent according to the provided reference information before the large language model outputs. The abnormal information can include all possible abnormal conditions in the query process, such as query result error, no related result is queried, and cannot be queried, etc.
[0049] Specifically, after the first reference information and the preset prompt word are input into the large language model, in order to improve the accuracy of the output sentence conversion result, the agent in the large language model will perform in advance the deduction conversion according to the above information, and the conversion result obtained by the first deduction conversion is the first prediction result. After obtaining the first prediction result, a simulated query can be performed from the second preset database according to the first prediction result. If the result of the simulated query does not appear abnormal, it indicates that the sentence obtained by the deduction conversion is accurate, and the first prediction result obtained by the deduction conversion can be directly output as the sentence conversion result. If the result of the simulated query appears abnormal, the deduction conversion needs to be performed again, and in the process of each subsequent deduction conversion, the abnormal information appearing in the previous deduction conversion process can be input as the second reference information to avoid repeated abnormal prediction results, until the second prediction result obtained by the subsequent deduction conversion no longer appears abnormal, and the second prediction result obtained by the last deduction conversion is output as the sentence conversion result.
[0050] On the basis of the above various embodiments, as an optional embodiment, the first preset number of first historical query requests are matched from the first preset database based on the first semantic vector, specifically including: For each historical query request in the first preset database, a first similarity between the semantic vector corresponding to the historical query request and the first semantic vector is determined. The first similarities of the historical query requests are sorted from large to small, and the historical query requests corresponding to the first similarities in the front of the sorting are determined as the first historical query requests.
[0051] Specifically, as shown in Figure 3 After the target information (including indexes, dimensions, etc., represented in the form of a list) extracted from the query request (corresponding to the query demand in the figure) is preprocessed, the target information is keyword extracted and vectorized to convert into a first semantic vector, the first semantic vector corresponding to the target information can be sequentially calculated with the first semantic vectors of each historical query request stored in the first preset database (the calculated similarity is the first similarity), and then the first historical query requests with the first similarity greater than the first preset number are selected (i.e. multi-path recall). After that, the multiple first historical query requests recalled are refined, which can effectively improve the data quality of the recall.
[0052] On the basis of the above various embodiments, as an optional embodiment, the first reference information and the preset prompt word are input into the large language model to make the large language model output the sentence conversion result of the query request, and then further including: input the sentence transformation result into a preset quality detection model, and output a quality detection result about the sentence transformation result; If the quality detection result indicates passing, the sentence transformation result and the corresponding query request are stored in the first preset database.
[0053] Specifically, when the large language model outputs the sentence transformation result each time, the obtained sentence transformation result can be "quality inspected", that is, the sentence transformation result can be input into a pre-trained quality detection model. The quality detection model can perform quality detection from the accuracy, simplicity and resource consumption of the query of the sentence transformation result. If the detection result passes, it indicates that the quality of the sentence transformation result is high, which can provide reference for subsequent query requests. Therefore, the sentence transformation result and the corresponding query request and the corresponding semantic vector can be stored in the first preset database.
[0054] Optionally, the quality detection can also be performed by manual intervention in the embodiment of the application, that is, a professional person judges the quality of each sentence transformation result.
[0055] The overall flow of the sentence transformation method provided in the embodiment of the application is introduced as follows. Figure 4 As shown in the figure, after receiving the query request sent by the user, first, the reference information of the query request is extracted by the request understanding module, and the query request can be classified according to the extraction result of the reference information to determine the type of the query request, and the index content to be queried by the query request is determined from the index library. Then, the request matching module is used to search whether there is a historical query request same as the query request in the first preset database. The query method can be to calculate the first similarity. If the matching result indicates that there is a same historical query request, the content recall module is used to recall this part of the historical query request, and the large language model prompt word combination is performed based on the historical query request. If the matching result indicates that there is no same historical query request, the approximate historical query request needs to be recalled from the first preset database, and the target field can be extracted from the recalled historical query request according to the library table metadata to realize field recall, and the large language model prompt word combination is performed according to the recalled field. Then, the content production module uses the large language model to produce the sentence transformation result corresponding to the query request, and uses the transformation sentence executor to perform self-correction based on the Recat mechanism to determine whether the generated sentence transformation result is abnormal. When no abnormality occurs, the sentence transformation result can be output. After the sentence transformation result is output, the quality of the sentence transformation can be audited by the knowledge backflow module, and the query request is stored in the first preset database after the audit passes to provide reference for subsequent query requests.
[0056] Figure 5 A structural block diagram of a sentence conversion device based on an agent provided by an embodiment of the present application is shown in Figure 5 The sentence conversion device based on an agent 500 can include an information extraction module 501, a historical request matching module 502, a sentence analysis module 503, and a sentence conversion module 504, wherein, The information extraction module 501 is configured to obtain a query request in a first format input by a user, and extract target information from the query request; wherein the target information is used to represent the query range of the corresponding query request; The historical request matching module 502 is configured to encode the target information to obtain a first semantic vector, and match a first preset number of first historical query requests from a first preset database based on the first semantic vector; wherein the first preset database stores each historical query request and a first semantic vector corresponding to each historical query request; The sentence analysis module 503 is configured to obtain a historical sentence conversion result of each of the first preset number of first historical query requests, analyze each historical sentence conversion result, and obtain first reference information about the query request; The sentence conversion module 504 is configured to input the first reference information and a preset prompt word into a large language model together, so that the large language model outputs a sentence conversion result of the query request; wherein the sentence conversion result is an expression form of the query request in a second format.
[0057] First, the scheme provided by the embodiment of the present application can determine the query range of the data to be searched by the query request by extracting the target information from the query request, thereby improving the accuracy of the query direction; Second, the scheme provided by the embodiment of the present application matches the first historical query request with reference value from the historical query request according to the extracted target information, obtains reference information about the conversion process of the query request according to each first historical query request, and can provide rich reference content for the subsequent sentence conversion of the large language model.
[0058] Finally, the reference information and the preset prompt word are input into the large language model together, so that the large language model can convert the query request according to the provided reference content to obtain the final converted sentence conversion result.
[0059] The scheme provided in the application extracts target information in advance from a query request, extracts reference information corresponding to the query request from the conversion results corresponding to the historical query requests according to the target information, and inputs the reference information into a large language model to make the large language model output the conversion result corresponding to the query request. Compared with the scheme in the prior art in which the query request is directly input into the large language model for conversion, the scheme can provide more accurate information for the large language model, and thus the conversion sentence output by the large language model has higher accuracy.
[0060] On the basis of each of the above embodiments, as an optional embodiment, if the first similarity corresponding to each first historical query request is less than a first preset threshold, the first similarity is the similarity between the first semantic vector corresponding to the first historical query request and the first semantic vector of the target information. The sentence analysis module is specifically configured to: encode the query request to obtain a second semantic vector; For each historical query request in the first preset database, determine a second similarity between the second semantic vector of the historical query request and the second semantic vector of the query request; the first preset database further includes the second semantic vector of each historical query request; determine the historical query request with the second similarity greater than a second preset threshold as a second historical query request; obtain, from the first preset database, the first data query table corresponding to each second historical query request and the number of queries of each first data query table; the first preset database includes a plurality of data query tables and the number of queries of each data query table; sort the first data query tables in descending order of the number of queries, and take the second preset number of first data query tables at the front of the sorting result and the data query table corresponding to each first historical query request as second data query tables; extract a target field from each second data query table based on the semantics of each field in each second data query table, and take the target field and the target information as the first reference information.
[0061] On the basis of each of the above embodiments, as an optional embodiment, the sentence analysis module is further configured to: For any two second data query tables, determine whether there is a field with the same semantics between the two second data query tables; For any field pair, the two fields in the field pair belong to two second data query tables respectively, if it is determined that the two fields in the field pair have the same semantics, the two fields in the field pair are both determined as target fields.
[0062] On the basis of each of the above embodiments, as an optional embodiment, if a first similarity corresponding to a first historical query request is not less than a first preset threshold, the first similarity is a similarity between a semantic vector corresponding to the first historical query request and a first semantic vector of the target information; The sentence analysis module can also be used for: determining the first historical query request with the first similarity not less than the first preset threshold as a third historical query request; For each third historical query request, extracting the target information from the historical sentence conversion result of the third historical query request, and obtaining a second data query table corresponding to the third historical query request from the first preset database; using the target information corresponding to each third historical query request, the table name of the data query table corresponding to the third historical query request, each field, and the historical sentence conversion result corresponding to the third historical query request as the first reference information.
[0063] On the basis of each of the above embodiments, as an optional embodiment, the sentence conversion module is specifically used for: using a large language model to infer based on the first reference information and a preset prompt word to generate a first prediction result of the sentence conversion result corresponding to the query request; querying from a second preset database based on the first prediction result to obtain a query result; wherein the second preset database includes a plurality of data query tables for responding to query requests; if the query result is normal, using the first prediction result as the sentence conversion result; if the query result is abnormal, performing at least one update step until the query result is normal, and using a second prediction result obtained by the last update step as the sentence conversion result; wherein the update step includes: obtaining abnormal information included in the current query result, and using the abnormal information as second reference information; inference based on the first reference information, each second reference information, and a preset prompt word to obtain a second prediction result.
[0064] On the basis of each of the above embodiments, as an optional embodiment, the historical request matching module is specifically used for: for each historical query request in the first preset database, determining a first similarity between a semantic vector corresponding to the historical query request and a first semantic vector; sorting the first similarities of each historical query request from large to small, and determining the historical query requests corresponding to the first preset number of first similarities in the front of the sorting as first historical query requests.
[0065] On the basis of each of the above embodiments, as an optional embodiment, the apparatus further comprises a quality detection module, specifically configured to: input the sentence conversion result into a preset quality detection model, and output a quality detection result about the sentence conversion result; if the quality detection result indicates passing, store the sentence conversion result and the corresponding query request into a first preset database.
[0066] Reference will be made to the following Figure 6 , which shows a structural schematic diagram of an electronic device (for example, a terminal device or a server that executes the method shown in Figure 1 Embodiments of the present application can include, but are not limited to, mobile terminals such as mobile phones, notebook computers, digital broadcast receivers, PDAs (Personal Digital Assistants), PADs (Tablet Personal Computers), PMPs (Portable Multimedia Players), vehicle terminals (for example, car navigation terminals), wearable devices, and the like, as well as fixed terminals such as digital TVs, desktop computers, and the like. Figure 6 The electronic device shown is only an example, and should not impose any limitation on the functions and use range of embodiments of the present application.
[0067] The electronic device includes a memory and a processor, the memory is used to store a program for executing the method described in each of the above method embodiments, and the processor is configured to execute the program stored in the memory. Here, the processor can be referred to as the processing device 601 described below, and the memory can include at least one of a read-only memory (ROM) 602, a random access memory (RAM) 603, and a storage device 608, as shown below: As Figure 6 shown, the electronic device can include a processing device (for example, a central processing unit, a graphics processing unit, or the like), which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) or a program loaded from a storage device into a random access memory (RAM). In the random access memory (RAM), various programs and data required for the operation of the electronic device are also stored. The processing device, the read-only memory (ROM), and the random access memory (RAM) are connected to each other through a bus 604. An input / output (I / O) interface 605 is also connected to the bus.
[0068] Generally, the following devices can be connected to the I / O interface: input device 606 including, for example, a touch screen, a touch pad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, and the like; output device 607 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, and the like; storage device including, for example, a tape, a hard disk, and the like; and communication device 609. The communication device can allow the electronic device to communicate wirelessly or wiredly with other devices to exchange data. Although Figure 6 An electronic device having various devices is illustrated, but it is understood that all of the illustrated devices are not required to be implemented or possessed. More or less devices can alternatively be implemented or possessed.
[0069] In particular, the processes described above with reference to the flowcharts can be implemented as a computer software program according to embodiments of the present application. For example, embodiments of the present application include a computer program product comprising a computer program carried on a non-transitory computer-readable medium, the computer program containing program code for executing the methods illustrated by the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network through a communication device, or installed from a storage device, or installed from a read-only memory (ROM). When the computer program is executed by a processing device, the above-described functions defined in the methods of embodiments of the present application are performed.
[0070] It should be noted that the computer-readable storage medium in the present application can be a computer-readable signal medium or a computer-readable storage medium or any combination of the two. The computer-readable storage medium may, for example, but is not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or apparatus, or any combination of the above. More specific examples of computer-readable storage media can include, but are not limited to, an electrical connection having one or more wires, a portable computer diskette, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present application, the computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, device or apparatus. In the present application, the computer-readable signal medium can include a data signal carried in a baseband or as a part of a carrier wave, which carries computer-readable program code. Such a propagated data signal can take various forms, including but not limited to an electromagnetic signal, an optical signal or any suitable combination of the above. The computer-readable signal medium can also be any computer-readable medium other than the computer-readable storage medium, which can send, propagate or transmit a program for use by or in conjunction with an instruction execution system, device or apparatus. The program code contained in the computer-readable medium can be transmitted by any suitable medium, including but not limited to a wire, a cable, an RF (radio frequency) or the like, or any suitable combination of the above.
[0071] In some embodiments, the client, server, can communicate using any current known or future developed network protocol, such as HTTP (HyperText Transfer Protocol), and can be interconnected with any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include local area networks ("LANs"), wide area networks ("WANs"), the Internet, and peer-to-peer networks (e.g., ad hoc peer-to-peer networks), as well as any current known or future developed networks.
[0072] The above computer-readable medium can be contained in the above electronic device; or can exist separately without being assembled into the electronic device.
[0073] The above computer-readable medium carries one or more programs, when the above one or more programs are executed by the electronic device, the electronic device: Obtain a query request in a first format input by a user, and extract target information from the query request, wherein the target information is used to represent a query range of the corresponding query request; encode the target information to obtain a first semantic vector, and match a first preset number of first historical query requests from a first preset database based on the first semantic vector; wherein the first preset database stores each historical query request and a first semantic vector corresponding to each historical query request; obtain a historical sentence conversion result of each of the first preset number of first historical query requests, analyze each historical sentence conversion result to obtain first reference information about the query request; and input the first reference information and a preset prompt word into a large language model together, so that the large language model outputs a sentence conversion result of the query request; wherein the sentence conversion result is an expression form of the query request in a second format.
[0074] Computer program code for carrying out operations of the present application can be written in any combination of one or more programming languages, including an object oriented programming language such as Java, Smalltalk, C++ or the like and conventional procedural programming languages, such as the "C" programming language or similar programming languages. The program code can execute entirely on the user's computer, partly on the user's computer, as a stand-alone software package, partly on the user's computer and partly on a remote computer or entirely on the remote computer or server. In the latter scenario, the remote computer can be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or the connection can be made to an external computer (for example, through the Internet using an Internet Service Provider).
[0075] The flow diagrams and the block diagrams in the drawings are illustrations of architectures, functionalities, and operations of possible implementations of systems, methods, and computer program products according to various embodiments of present application. In this regard, each block in the flow diagrams or block diagrams can represent a module, a procedure, or a portion of code, which comprises one or more executable instructions for implementing the specified logical functions. It should also be noted that in some alternative implementations, the functions noted in the blocks can occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently or the blocks may
[0076] The modules or units involved in the embodiments of the present application can be implemented by software or by hardware. In some cases, the name of the module or unit does not constitute a limitation on the module or unit itself. For example, the first constraint obtaining module can also be referred to as a "module for obtaining a first constraint".
[0077] The functions described above in this document can be performed, at least in part, by one or more hardware logic components. For example, and without limitation, illustrative types of hardware logic components that can be used include Field-programmable Gate Arrays (FPGAs), Application-specific Integrated Circuits (ASICs), Application-specific Standard Products (ASSPs), System-on-a-chip systems (SOCs), Complex Programmable Logic Devices (CPLDs), etc.
[0078] In the context of the present application, a machine-readable medium can be a tangible medium that contains or stores a program for use by or in connection with an instruction execution system, apparatus, or device. The machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium can include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any suitable combination of the foregoing. More specific examples of the machine-readable storage medium will include one or more lines of electrical connections, portable computer disks, hard disk drives, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or Flash memory), optical fibers, portable compact disc read-only memories (CD-ROMs), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.
[0079] It should be understood that, although the steps in the flowcharts of the accompanying drawings are shown in a certain order according to the arrows, the steps are not necessarily executed in the order indicated by the arrows. Unless otherwise specified herein, the steps are not necessarily limited in execution order, and can be executed in other orders. Moreover, at least some of the steps in the flowcharts of the accompanying drawings can include multiple sub-steps or multiple stages, which are not necessarily executed at the same time, but can be executed at different times, and the execution order is not necessarily sequential, but can be executed in rotation or alternation with at least some of the other steps or sub-steps or stages of other steps.
[0080] The above only describes some embodiments of the present application. It should be noted that, for those skilled in the art, without departing from the principles of the present application, a number of improvements and refinements can be made, and these improvements and refinements should also be considered as falling within the scope of protection of the present application.
Claims
1. A sentence conversion method based on an agent, characterized in that: include: Obtaining a query request in a first format input by a user, and extracting target information from the query request; wherein the target information is used to represent a query scope of the corresponding query request; Encoding the target information to obtain a first semantic vector, and matching a first preset number of first historical query requests from a first preset database based on the first semantic vector; wherein the first preset database stores each historical query request and a first semantic vector corresponding to each historical query request; Obtaining historical statement conversion results of each of the first preset number of first historical query requests, analyzing each historical statement conversion result, and obtaining first reference information about the query request; The first reference information and the preset prompt word are inputted into a large language model, so that the large language model outputs a sentence conversion result of the query request; wherein the sentence conversion result is an expression form of the query request in the second format.
2. The method according to claim 1, characterized in that If the first similarity corresponding to each first historical query request is less than a first preset threshold, the first similarity is the similarity between the first semantic vector corresponding to the first historical query request and the first semantic vector of the target information; The analysis of the conversion results of each historical statement to obtain first reference information about the query request includes: Encoding the query request to obtain a second semantic vector; For each historical query request in the first preset database, determining a second similarity between the second semantic vector of the historical query request and the second semantic vector of the query request; the first preset database also includes the second semantic vector of each historical query request; Determine the historical query request whose second similarity is greater than a second preset threshold as a second historical query request; Obtaining from the first preset database a first data query table corresponding to each second historical query request and the number of times each first data query table has been queried; wherein the first preset database includes a plurality of data query tables and the number of times each data query table has been queried; Sort the first data query tables in descending order of the number of queries, and use a second preset number of first data query tables with the highest sorting results and the data query tables corresponding to the first historical query requests as the second data query tables; Based on the semantics of each field in each second data query table, a target field is extracted from each second data query table, and the target field and the target information are used together as the first reference information.
3. The method according to claim 2, characterized in that The step of extracting target fields from each second data query table based on the semantics of each field in each second data query table includes: For any two second data query tables, determining whether there are fields with the same semantics between the two second data query tables; For any field pair, the two fields in the field pair belong to two second data query tables respectively. If it is determined that the two fields in the field pair have the same semantics, both fields in the field pair are determined as the target fields.
4. The method according to claim 1, wherein If there is a first similarity corresponding to a first historical query request that is not less than a first preset threshold, the first similarity is the similarity between the semantic vector corresponding to the first historical query request and the first semantic vector of the target information; The analysis of the conversion results of each historical statement to obtain first reference information about the query request includes: Determining the first historical query request whose first similarity is not less than the first preset threshold as a third historical query request; For each third historical query request, extract target information from a historical statement conversion result of the third historical query request, and obtain a second data query table corresponding to the third historical query request from the first preset database; The target information corresponding to each third historical query request, the table name and each field of the data query table corresponding to the third historical query request, and the historical statement conversion result corresponding to the third historical query request are collectively used as the first reference information.
5. The method according to claim 1, wherein The step of inputting the first reference information and the preset prompt word into a large language model so that the large language model outputs a sentence conversion result of the query request includes: Performing reasoning using the large language model based on the first reference information and the preset prompt word to generate a first prediction result of a sentence conversion result corresponding to the query request; Based on the first prediction result, a query is performed from a second preset database to obtain a query result; wherein the second preset database includes a plurality of data query tables for responding to query requests; If the query result is normal, the first prediction result is used as the statement conversion result; If the query result is abnormal, perform at least one update step until the query result is normal, and use the second prediction result obtained in the last update step as the statement conversion result; The updating step includes: Acquire exception information included in the current query result, and use the exception information as second reference information; Reasoning is performed based on the first reference information, each second reference information, and the preset prompt word to obtain a second prediction result.
6. The method according to claim 1, characterized in that The matching a first preset number of first historical query requests from a first preset database based on the first semantic vector includes: For each historical query request in the first preset database, determining a first similarity between a semantic vector corresponding to the historical query request and the first semantic vector; The first similarities of the historical query requests are sorted from large to small, and the historical query requests corresponding to the first preset number of first similarities that are ranked high are determined as the first historical query requests.
7. The method according to claim 1, characterized in that The step of inputting the first reference information and the preset prompt word into a large language model so that the large language model outputs a sentence conversion result of the query request further includes: Inputting the sentence conversion result into a preset quality detection model, and outputting a quality detection result of the sentence conversion result; If the quality inspection result characterizes passing, the statement conversion result and the corresponding query request are stored together in the first preset database.
8. A sentence conversion device based on an agent, characterized in that: include: An information extraction module is configured to obtain a query request in a first format input by a user and extract target information from the query request; wherein the target information is used to represent a query scope of the corresponding query request; a historical request matching module, configured to encode the target information to obtain a first semantic vector, and match a first preset number of first historical query requests from a first preset database based on the first semantic vector; wherein the first preset database stores each historical query request and the first semantic vector corresponding to each historical query request; A statement analysis module, configured to obtain a historical statement conversion result of each of the first preset number of first historical query requests, analyze each historical statement conversion result, and obtain first reference information about the query request; A sentence conversion module is used to input the first reference information and the preset prompt word into the large language model, so that the large language model outputs a sentence conversion result of the query request; wherein the sentence conversion result is an expression of the query request in the second format.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory, characterized in that: The processor executes the computer program to implement the method according to any one of claims 1 to 7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
SQL statement generation method and device
CN118377783A
Data query method and device, readable medium, electronic equipment and program product
CN118964390A
Method and device for converting natural language question into SQL statement and medium
CN119597785A
Structured query statement generation method and device, medium and program product
CN119739734A
Database query method and device, computer equipment, readable storage medium and program product
CN120386791A