Database query method and device, storage medium and program product

By generating prompt words and using a large language model to generate database query statements, the problem of non-technical personnel being unable to perform database queries efficiently has been solved. This achieves efficient and accurate conversion of natural language to database queries, lowers the technical threshold, and ensures the correctness and accuracy of query results.

CN121743349APending Publication Date: 2026-03-27LINGGU LIUGUANG TECHNOLOGY (NANJING) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-12-18
Publication Date
2026-03-27

AI Technical Summary

Technical Problem

In existing technologies, the conversion from natural language to database queries heavily relies on technical personnel, making it difficult and inefficient for non-technical personnel to perform database queries.

Method used

By generating prompt words and utilizing a large language model, query statements that conform to database query rules are generated based on the prompt words, including system prompt words and user prompt words. Identification information is removed, and digital data format conversion and format verification are performed to ensure the accuracy and executability of the query statements.

Benefits of technology

It significantly improves the efficiency and accuracy of converting natural language into database query statements, lowers the technical threshold, allows non-technical personnel to directly query data through natural language, reduces reliance on professionals, avoids grammatical and logical errors, and ensures the accuracy of digital data and the correctness of query results.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121743349A_ABST
    Figure CN121743349A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a database query method and device, a storage medium and a program product, and relates to the field of data processing. The method comprises the steps of determining a corresponding first cue word according to a first query text and query rule information, wherein the query rule information is used for indicating a query rule corresponding to a first database; according to the first prompt word, generating a corresponding response statement through a large language model; determining a query statement of a target query structure in the response statement as a first database query statement; and performing data query in the first database according to the first database query statement. According to the method provided by the invention, the database query difficulty is reduced, and the database query efficiency is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of data processing, and in particular to a database query method, apparatus, storage medium, and program product. Background Technology

[0002] In the field of modern enterprise data management and analysis, natural language to database query conversion technology is widely used in scenarios such as log retrieval and IoT device data query.

[0003] In some implementations, querying the database requires technical personnel to translate the natural language query requirements into query statements that match the database. For example, technical personnel need to translate the natural language query requirements into Elasticsearch's domain-specific language (DSL).

[0004] However, the above-mentioned database query implementation method relies heavily on technical personnel, and it is difficult and inefficient for non-technical personnel to perform database queries. Summary of the Invention

[0005] This application provides a database query method, apparatus, storage medium, and program product to solve the technical problems of high difficulty and low efficiency in database queries.

[0006] In a first aspect, this application provides a database query method, comprising: determining a corresponding first prompt word based on a first query text and query rule information, wherein the first prompt word includes a system prompt word and a user prompt word, wherein the system prompt word includes at least one of the following: current timestamp, model structure information of the first database, query example, and query rule of the first database, and the user prompt word includes at least one of the following: query request information, timestamp calculation rule, timestamp calculation example, and timestamp format rule, and the query rule information is used to indicate the query rule corresponding to the first database;

[0007] Based on the first prompt word, the corresponding response statement is generated using a large language model;

[0008] The query statement that determines the target query structure in the response statement is the first database query statement, and the target query structure matches the first database;

[0009] Data is queried in the first database according to the query statement of the first database.

[0010] In one possible implementation of the first aspect, the query statement that determines the target query structure in the response statement is the first database query statement, including:

[0011] Remove the identification and / or explanation information from the response statement to obtain the first query statement;

[0012] The query statement that determines the target query structure in the first query statement is the first database query statement.

[0013] In one possible implementation of the first aspect, the query statement that determines the target query structure in the first query statement is the first database query statement, including:

[0014] The query statement that determines the target query structure in the first query statement is the second query statement;

[0015] If the numeric data in the second query statement exceeds the safe integer range, the numeric data in the second query statement is converted into numeric data in string format to obtain the first database query statement.

[0016] In one possible implementation of the first aspect, converting the numeric data in the second query statement into numeric data in string format includes:

[0017] Convert each key-value pair in the second query statement that has a numeric value to a string; or,

[0018] Convert each numeric element in the array of the second query statement to a string; or,

[0019] Convert the integer numbers in the second query statement to strings.

[0020] In one possible implementation of the first aspect, the query statement that determines the target query structure in the response statement is the first database query statement, including:

[0021] The first database query statement is obtained by extracting the JSON object within the parentheses of the response statement using a parenthesis matching algorithm.

[0022] In one possible implementation of the first aspect, the method further includes:

[0023] Perform format validation on the first database query statement and obtain the validation result. The validation result indicates whether the first database query statement passes the format validation.

[0024] Data is queried in the first database according to the query statement of the first database, including:

[0025] If the verification result indicates that the first database query statement passes the format verification, data is queried in the first database according to the first database query statement.

[0026] In one possible implementation of the first aspect, the first database query statement is format-validated to obtain a validation result, including:

[0027] If the first database query statement is a JSON object and includes all necessary fields, then the first database query statement passes the format validation.

[0028] If the format of the first database query statement is not a JSON object, and / or the first database query statement includes missing necessary fields, it is determined that the first database query statement has failed format validation.

[0029] Secondly, this application provides a database query apparatus, comprising:

[0030] The first determining module is used to determine the corresponding first prompt word based on the first query text and query rule information. The first prompt word includes system prompt words and user prompt words. The system prompt word includes at least one of the current timestamp, model structure information of the first database, query example, and query rules of the first database. The user prompt word includes at least one of the query requirement information, timestamp calculation rules, timestamp calculation example, and timestamp format rules. The query rule information is used to indicate the query rules corresponding to the first database.

[0031] The generation module is used to generate corresponding response statements based on the first prompt word using a large language model;

[0032] The second determining module is used to determine that the query statement with the target query structure in the response statement is a query statement of the first database, and the target query structure matches the first database.

[0033] The query module is used to query data in the first database based on the query statement of the first database.

[0034] Thirdly, this application provides an electronic device, including: a processor and a memory communicatively connected to the processor;

[0035] The memory stores the instructions that the computer executes;

[0036] The processor executes computer-executable instructions stored in memory to implement any of the methods of the first aspect.

[0037] Fourthly, this application provides a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, are used to implement the method of any one of the first aspects.

[0038] Fifthly, this application provides a computer program product, including a computer program that, when executed by a processor, implements the method of any one of the first aspects.

[0039] The database query method, apparatus, device, storage medium, and program products provided in this application significantly improve the efficiency and accuracy of converting natural language into first-order database query statements through prompt word generation and large language model generation capabilities, thereby enhancing database query efficiency. They lower the technical threshold for database queries, allowing non-technical personnel to directly query data using natural language without needing specialized knowledge, reducing reliance on professionals and simplifying database querying. Furthermore, prompt words guide the large language model to generate query statements that meet database requirements, avoiding syntax and logical errors and improving query accuracy. They also ensure that digital data (such as timestamps and product IDs) does not lose precision during JSON serialization. Finally, format validation and required field checks ensure that the generated query statements are directly executable, reducing manual debugging costs. Attached Figure Description

[0040] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application.

[0041] Figure 1 A schematic diagram illustrating a database query method provided in an embodiment of this application;

[0042] Figure 2 A schematic flowchart illustrating a database query method provided in an embodiment of this application;

[0043] Figure 3 This is a schematic diagram of the structure of a database query device provided in an embodiment of this application;

[0044] Figure 4 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application.

[0045] The accompanying drawings illustrate specific embodiments of this application, which will be described in more detail below. These drawings and descriptions are not intended to limit the scope of the concept in any way, but rather to illustrate the concept of this application to those skilled in the art through reference to particular embodiments. Detailed Implementation

[0046] Exemplary embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numbers in different drawings denote the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this application. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this application as detailed in the appended claims.

[0047] 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 used for analysis, data stored, data displayed, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties. Furthermore, the collection, storage, use, processing, transmission, provision, disclosure, and application of the relevant data all comply with the relevant laws, regulations, and standards of the relevant countries and regions, have taken necessary confidentiality measures, do not violate public order and good morals, and provide corresponding operation access points for users to choose to authorize or refuse.

[0048] Figure 1 This is a schematic diagram illustrating a scenario where the database query method of this application is applied. For example... Figure 1 As shown, a customer can interact with the electronic device, which can execute the database query method of this application embodiment to provide interactive services to the user. In some embodiments, the electronic device can also interact with a database server to obtain relevant data required to execute the database query method of this application embodiment.

[0049] In the field of modern enterprise data management and analysis, natural language to database query conversion technology is widely used in scenarios such as log retrieval and IoT device data query.

[0050] In some implementations, querying the database requires technical personnel to translate the natural language query requirements into query statements that match the database. For example, technical personnel need to translate the natural language query requirements into Elasticsearch DSL.

[0051] However, the above-mentioned methods of implementing database queries heavily rely on technical personnel, making it difficult and inefficient for non-technical personnel to perform database queries.

[0052] The database query method, apparatus, device, storage medium, and program products provided in this application are intended to solve the above-mentioned technical problems of the prior art.

[0053] The technical solution of this application and how the technical solution of this application solves the above-mentioned technical problems are described in detail below with specific embodiments. These specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described again in some embodiments. The embodiments of this application will now be described with reference to the accompanying drawings.

[0054] Figure 2 This is a flowchart illustrating a database query method provided in an embodiment of this application, as shown below. Figure 2 As shown, the method includes:

[0055] S201. The electronic device determines the corresponding first prompt word based on the first query text and query rule information.

[0056] In this embodiment of the application, the first query text may be a first query text in natural language. The electronic device can determine the corresponding first prompt word based on the first query text in natural language and the query rule information corresponding to the first database. The first prompt word can be used to input into the large language model, thereby guiding the large language model to generate a database query statement for the first database.

[0057] The query rule information is used to indicate the query rules corresponding to the first database. These are the constraints and specifications of the first database on the query statement, such as the time field of the query statement must use a millisecond timestamp.

[0058] The first prompt word can include system prompt words and user prompt words.

[0059] The system prompts can be information about technical constraints such as database field type, query type, and data model specifications. For example, they can include at least one of the following: current timestamp, model structure information of the first database, query example, and query rules of the first database. In addition, other information can be included, but this is not limited here.

[0060] User suggestions are the natural language query input by the user and the calculation rules related to the query (such as timestamp calculation formulas). For example, they may include at least one of the following: query information, timestamp calculation rules, timestamp calculation examples, and timestamp format rules. In addition, other information may be included, but this is not limited here.

[0061] S202. The electronic device generates the corresponding response statement based on the first prompt word using a large language model.

[0062] After determining the first prompt word, the electronic device can input it into the large language model. The large language model can then generate the corresponding response based on the first prompt word.

[0063] S203. The electronic device determines that the query statement of the target query structure in the response statement is the first database query statement, and the target query structure matches the first database.

[0064] After the large language model generates the response statement, the electronic device can determine that the query statement of the target query structure in the response statement is the first database query statement, which matches the query rules of the first database.

[0065] For example, an electronic device can determine that the JSON object in the response statement is the first database query statement.

[0066] S204. The electronic device performs a data query in the first database according to the first database query statement.

[0067] After generating a first database query statement through a large language model, the electronic device performs a data query in the first database based on the first database query statement, thereby obtaining the corresponding query results.

[0068] This possible implementation addresses the high technical barriers and poor accuracy of natural language to database query conversion in existing technologies by constructing prompts containing data model information. The data model information (such as field types and query rules) in the prompts provides clear contextual constraints for the large language model, enabling it to accurately understand user needs and generate query statements that conform to the target database specifications. For example, when a user enters "query products created in the last 5 days with prices between 1000-5000 yuan," the timestamp calculation rules in the system prompts guide the model to automatically generate a time range query, and the field type descriptions (such as nested) in the user prompts ensure the correct generation of the nested query structure. This method eliminates the need for users to master database syntax; they can obtain structured queries directly through natural language input, significantly improving query efficiency and accuracy while reducing reliance on technical professionals.

[0069] In some embodiments, the electronic device determines the corresponding system prompt words and user prompt words based on the first query text and query rule information.

[0070] The following is an example of a method for generating prompt words on an electronic device:

[0071] When generating the first prompt word, the electronic device can first obtain the current system time and convert it into the corresponding millisecond timestamp. For example, the electronic device can first obtain the current system time as: 2024-01-01 00:00:00, and its corresponding millisecond timestamp is: 1704067200000.

[0072] Then, the electronic device can construct the following system prompts based on the timestamps mentioned above and the query rules information corresponding to the first database:

[0073] The role definition text could be: "You are a professional Elasticsearch query expert who is skilled at translating natural language query requirements into standard ES query DSL statements."

[0074] The time reference information can be: "Current timestamp (milliseconds): 1704067200000", "Please use this time as the reference for time concepts such as "now" and "current".

[0075] The data model structure can be a field structure that describes the Elasticsearch index. For example, it can include descriptions of field types (such as text, keyword, nested, long, etc.) and descriptions of query methods (such as match, term, range, nested, etc.).

[0076] Query rules can include: "must return a valid JSON format Elasticsearch query", "the query must conform to the Elasticsearch DSL specification", and "time fields must use millisecond timestamp numbers, not date strings", etc.

[0077] In some embodiments, system prompts may also include other information, such as query examples. System prompts may include common query examples, correct query statement formats, and other query examples. In addition, system prompts may include other information, which is not limited here.

[0078] The electronic device can also determine user suggestion words as shown below based on the first query text and query rule information:

[0079] User query information can be directly embedded with the user's natural language query requirements, such as: "Query products created in the last 5 days with prices between 1000 and 5000 yuan".

[0080] The timestamp calculation rule can include: "Timestamp X days ago = Current timestamp - X * 24 * 60 * 60 * 1000".

[0081] In addition, user suggestions can also include examples of timestamp calculations. For example, in the query "Query products created in the last 5 days," the timestamp from 5 days ago needs to be determined. Based on "Current timestamp (milliseconds): 1704067200000," the timestamp from 5 days ago can be determined as "5-day-ago timestamp = 1704067200000 - 52460601000 = 1703635200000." For instance, an electronic device can thus determine a portion of the query as: {"query": {"range": {"created_at": {"gte":1703635200000, "lte": 1704067200000}}}}".

[0082] In this embodiment of the application, the first prompt word determined by the electronic device may include system prompt words and user prompt words. In addition, it may also include other prompt words, which are not limited here.

[0083] In this possible implementation, the collaborative input of system prompts and user prompts enables the large language model to accurately understand user needs and the field structure of the target database, generating more standardized query statements. For example, when a user enters "query products created in the last 5 days," the timestamp rules in the system prompts guide the model to automatically generate a range query, while the field type descriptions (such as nested) in the user prompts ensure the correct generation of nested structures, significantly improving the accuracy and standardization of query results. Furthermore, electronic devices can automatically convert relative time expressions such as "last X days" into timestamp range queries, avoiding errors from manual calculations.

[0084] In some embodiments, the electronic device generates a corresponding response statement based on the first prompt word using a large language model, which may specifically include:

[0085] The electronic device inputs the first prompt word into the large language model to obtain the corresponding response statement. Understandably, the electronic device inputs the first prompt word into the large language model, which can then generate the corresponding response statement. This response statement requires further processing by the electronic device to obtain the first database query statement that can be queried in the database.

[0086] For example, an electronic device can first call a large language model and set the corresponding model parameters. For instance, the electronic device can call a large language model A and set the model parameters, including setting the temperature parameter Temperature to 0.3, the kernel sampling parameter TopP to 0.8, and the maximum number of tokens MaxTokens to 2000.

[0087] The temperature parameter controls the randomness of the text generated by the large language model, with a maximum value of 1. The larger the temperature parameter value, the stronger the randomness of the text generated by the large language model; the smaller the temperature parameter value, the weaker the randomness of the text generated by the large language model.

[0088] The kernel sampling parameter refers to dynamically selecting the smallest set of words whose cumulative probability reaches p as sampling candidates. For example, if Top-p=0.8, the large language model will randomly select one from the most likely words with a cumulative probability of 80%, instead of selecting from all words.

[0089] The maximum number of tokens (MaxTokens) can limit the maximum number of tokens that the model can generate in a single run.

[0090] The electronic device can then input the first prompt word into the large language model, which includes both system prompt words and user prompt words. The large language model can output the corresponding response statement, and the electronic device can then determine the first database query statement based on this response statement. For example, the response statement output by the large language model may include not only the query statement but also code block markers and explanatory text, allowing the electronic device to determine from the response statement that the query statement is the first database query statement.

[0091] In some embodiments, the electronic device determines a first database query statement based on a response statement, which may specifically include:

[0092] The response statement includes identification information and / or explanatory information. The electronic device can first remove the identification information and / or explanatory information from the response statement to obtain the first query statement.

[0093] This identification information can be used to identify different content within the response statement. For example, a response statement may include: a JSON object, explanation information for that JSON object, and identifiers for identifying the JSON object and the explanation information.

[0094] Then the electronic device determines that the query statement with the target query structure in the first query statement is a query statement for the first database. The target query structure matches the first database, that is, the query statement with the target query structure can be used to query data in the first database.

[0095] For example, a large language model can output text containing Elasticsearch's domain-specific language (DSL), which may also include code block markers and explanatory text.

[0096] For example, electronic devices can remove the markdown code block marker JSON (i.e., identification information) from the response statement and remove the explanatory text from the response statement, keeping only the JSON part, thus obtaining the first query statement.

[0097] The first query text in JSON format matches the target query structure of the first database, so the electronic device can determine that the JSON-formatted query statement in the first query statement is the query statement of the first database.

[0098] In some embodiments, the electronic device determines that the query statement of the target query structure in the response statement is the first database query statement. Specifically, it may extract the JSON object in the parentheses of the response statement as the first database query statement through a parenthesis matching algorithm.

[0099] For example, an electronic device can use a bracket matching algorithm to extract a JSON object from the first query statement. The device can first find the position of the first "{" character, then use a counter to track the nesting level of brackets. When a "{" is encountered, the counter is incremented by 1; when a "}" is encountered, the counter is decremented by 1. When the counter reaches zero, a matching "}" is found, and the text from the first "{" to the matching "}" is extracted. For example:

[0100] JSON

[0101] / / Counter = 1

[0102] "query": { / / Counter = 2

[0103] "match": { / / Counter = 3

[0104] Title: Elasticsearch

[0105] / / Counter = 2

[0106] / / Counter = 1

[0107] / / Counter = 0

[0108] / / Find documents containing "Elasticsearch"

[0109] After removing the identifier "JSON" and the explanatory text "Find documents containing 'Elasticsearch'", the JSON object can be extracted from the electronic device.

[0110] In this possible implementation, the JSON object is extracted using a bracket matching algorithm, and its conformity to database specifications is verified, which significantly improves the executability of the generated query statement.

[0111] In some embodiments, the electronic device needs to ensure the precision of the numerical data in the query statement. Therefore, the electronic device determines the query statement with the target query structure in the first query statement as the first database query statement, which may specifically include:

[0112] The electronic device first determines that the query statement with the target query structure in the first query statement is the second query statement. Then, it parses the numeric data in the second query statement using a numeric type-preserving parser.

[0113] If the numeric data in the second query statement exceeds the safe integer range, the electronic device can convert the numeric data into string format numeric data to obtain the first database query statement.

[0114] If the numerical data in the second query statement does not exceed the safe integer range, the electronic device does not process the numerical data and obtains the first database query statement.

[0115] Understandably, in order to ensure the accuracy of the numerical data in the query statement, electronic devices can convert the numerical data into string format when the value of the data exceeds the safe integer range.

[0116] For example, to maintain the precision of large numbers in a query, an electronic device can first check whether the range of each number in the query exceeds the safe integer range. For instance, the device can first determine if each number in the query exceeds the safe integer range [9007199254740991, -9007199254740991], which is the safe integer range of JavaScript ±2^53. If the number is an integer and exceeds the safe integer range, the device can serialize the number into a string format, such as "12345678901234567". If the number is an integer and exceeds the safe integer range, the number format can be preserved.

[0117] It is understood that, in the embodiments of this application, the safe integer range of digital data is determined according to the required number type, and is not specifically limited here.

[0118] In some embodiments, the electronic device converts the numeric data in the second query statement into numeric data in string format, which may specifically include:

[0119] If the numeric data in the second query statement is in key-value pairs, the electronic device converts each key-value pair with a numeric value in the second query statement into a string. Alternatively,

[0120] If the numeric data in the second query statement is an array, the electronic device converts each numeric element in the array in the second query statement into a string. Alternatively,

[0121] If the numeric data in the second query statement is an integer, the electronic device will convert the integer in the second query statement into a string.

[0122] For example, an electronic device can iterate through each numeric data in a query to determine if it exceeds the safe integer range. For instance, the device can recursively process nested structures. For map-type numeric data (key-value pairs), the device can recursively process each key-value pair, checking the type of each value. If it's a map or array, the recursion continues; if it's a regular value (such as a string or number), it's processed as needed (e.g., checking if it's a large integer). For array types, the device can recursively process each element, similarly checking the type and recursively processing each element. This ensures that large numbers in all nested structures maintain their correct precision.

[0123] In this possible implementation, precision preservation processing is used to ensure that large numbers do not lose precision during conversion, avoiding abnormal query results caused by JavaScript floating-point limitations and ensuring the correctness of query results.

[0124] In some embodiments, the electronic device may also serialize the processed JSON object into a string to generate a JSON string as a first database query statement.

[0125] In some embodiments, the query statement can be a JSON object. When parsing the query statement, the electronic device can use a JSON parser that supports numeric type preservation, i.e., the parser can support the json.Number type (such as json.Number in Go). This JSON parser does not convert numbers to floating-point types when parsing JSON, preserving the original numerical format and maintaining the original precision of the numerical data in the query statement during parsing.

[0126] In some embodiments, before determining to generate a first database query statement and performing a data query based on the generated first database query statement, the electronic device may also perform format validation on the first database query statement. Therefore, the method may further include the following steps:

[0127] S205. The electronic device performs format validation on the query statement of the first database and obtains the corresponding validation result.

[0128] Accordingly, if the verification result indicates that the first database query statement has passed the format verification, the electronic device can perform a data query in the first database according to the first database query statement.

[0129] If the verification result indicates that the first database query statement has failed the format verification, the electronic device may not perform a data query in the first database according to the first database query statement.

[0130] In some embodiments, the electronic device performs format validation on the first database query statement, which may specifically include:

[0131] If the first database query statement is a JSON object and includes all necessary fields, then the first database query statement passes the format validation.

[0132] If the first database query statement is not a JSON object, and / or if the first database query statement includes missing necessary fields, it is determined that the first database query statement has failed format validation.

[0133] Here, the first format is the format corresponding to the first database, and the required field is a field that must exist in the database query statement, such as the query field.

[0134] For example, the electronic device can first verify whether the statement format of the first database query statement is a JSON object; if the statement format of the first database query statement is a first format, the electronic device can determine whether the first database query statement includes the necessary fields and obtain the verification result.

[0135] Specifically, if the first database query statement includes the necessary fields, the obtained verification result indicates that the first database query statement has passed the format verification; if the first database query statement does not include the necessary fields, the obtained verification result indicates that the first database query statement has failed the format verification.

[0136] If the format of the first database query statement is not a JSON object, the electronic device can determine the validation result, which indicates that the first database query statement has failed the format validation.

[0137] This possible implementation significantly improves the executability of the generated query statement by checking the validity of required fields and JSON format. For example, if a user enters "query products priced above 1000 yuan," and the model-generated DSL statement lacks the `query` field, the electronic device will directly intercept and display an error message, preventing invalid queries from being executed. This solves the query failure problem caused by format errors in existing technologies and improves the accuracy of database queries.

[0138] For example, in the first format JSON format, the electronic device can first perform JSON format verification on the first database query statement; if the first database query statement fails the JSON format verification, it can be determined that the first database query statement has failed the format verification.

[0139] If the first database query statement is validated using JSON format, determine whether the first database query statement includes the necessary fields.

[0140] For example, the first database query statement can be in JSON string format, and the electronic device can parse the first database query statement in JSON string format.

[0141] If parsing fails, the electronic device can determine that the first database query statement has failed format validation. For example, the electronic device can also generate a corresponding validation result indicating that the JSON format of the first database query statement is invalid.

[0142] If parsing is successful, the electronic device can also check whether the first database query statement includes necessary fields. If it does not include necessary fields, i.e., necessary fields are missing, it can also be determined that the first database query statement has failed format validation. The electronic device can also generate a corresponding validation result, indicating that the first database query statement is missing necessary fields.

[0143] For example, an electronic device can check whether the first database query statement includes the "query" field. If the "query" field is not included, it can be determined that the first database query statement has failed format validation. The electronic device can also generate a corresponding validation result indicating that the "query" field is missing from the first database query statement.

[0144] In this embodiment, the electronic device significantly improves the efficiency and accuracy of converting natural language into a first database query statement through prompt word generation and the generation capabilities of a large language model. This lowers the technical barrier to database queries, allowing non-technical personnel to directly query data using natural language without needing specialized knowledge, thus reducing reliance on professionals. Furthermore, by guiding the model to generate query statements that conform to database requirements, syntax and logical errors are avoided, improving the accuracy of database queries. It also ensures that digital data (such as timestamps and product IDs) does not lose precision during JSON serialization. Finally, format validation and required field checks ensure that the generated query statements are directly executable, reducing manual debugging costs.

[0145] Figure 3 This is a schematic diagram of the structure of a database query device provided in an embodiment of this application, as shown below. Figure 3 As shown, the database query device 300 provided in this embodiment includes:

[0146] The first determining module 301 is used to determine the corresponding first prompt word based on the first query text and query rule information. The first prompt word includes system prompt words and user prompt words. The system prompt word includes at least one of the current timestamp, model structure information of the first database, query example, and query rules of the first database. The user prompt word includes at least one of the query requirement information, timestamp calculation rules, timestamp calculation example, and timestamp format rules. The query rule information is used to indicate the query rules corresponding to the first database.

[0147] The generation module 302 is used to generate the corresponding response statement based on the first prompt word through a large language model;

[0148] The second determining module 303 is used to determine that the query statement of the target query structure in the response statement is the first database query statement, and the target query structure matches the first database.

[0149] The query module 304 is used to query data in the first database according to the query statement of the first database.

[0150] In one possible implementation, the second determining module 303 is specifically used to: remove the identification information and / or explanation information from the response statement to obtain the first query statement; and determine that the query statement with the target query structure in the first query statement is the first database query statement.

[0151] In one possible implementation, the second determining module 303 is specifically used to: determine the query statement with the target query structure in the first query statement as the second query statement; and if the numeric data in the second query statement exceeds the safe integer range, convert the numeric data in the second query statement into numeric data in string format to obtain the first database query statement.

[0152] In one possible implementation, the second determining module 303 is specifically used to: convert each key-value pair in the second query statement that has a numeric value into a string; or, convert each numeric element in the array in the second query statement into a string; or, convert the integer numbers in the second query statement into strings.

[0153] In one possible implementation, the second determining module 303 is specifically used to: extract the JSON object in the parentheses of the response statement as the first database query statement using a parenthesis matching algorithm.

[0154] In one possible implementation, the database query device 300 further includes:

[0155] The verification module 305 is used to perform format verification on the first database query statement and obtain the verification result, which indicates whether the first database query statement passes the format verification.

[0156] Correspondingly, the query module 304 is specifically used to: perform data query in the first database according to the first database query statement when the verification result indicates that the first database query statement has passed the format verification.

[0157] In one possible implementation, the verification module 305 is specifically used to: determine that the first database query statement passes format verification if the first database query statement is a JSON object and includes no missing necessary fields; and determine that the first database query statement fails format verification if the statement format of the first database query statement is not a JSON object and / or the first database query statement includes missing necessary fields.

[0158] The database query device provided in this embodiment can execute the method provided in the above method embodiment. Its implementation principle and technical effect are similar, and will not be described in detail here.

[0159] Figure 4 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Figure 4 As shown, the electronic device 40 may include a memory 401 and a processor 402. Optionally, the electronic device may also include a transceiver 403, wherein the memory 401 and the processor 402 communicate with each other; for example, the memory 401, the processor 402 and the transceiver 403 may communicate via a communication bus 404, the memory 401 is used to store a computer program, and the processor 402 executes the computer program to implement the method of the above embodiments.

[0160] Optionally, the aforementioned processor can be a Central Processing Unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), etc. The general-purpose processor can be a microprocessor or any conventional processor. The steps in the method embodiments disclosed in this application can be directly implemented by a hardware processor, or implemented by a combination of hardware and software modules within the processor.

[0161] This application also provides a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, implement the methods in any of the above method embodiments.

[0162] This application also provides a computer program product, including a computer program that, when executed by a processor, implements the methods in any of the above method embodiments.

[0163] All or part of the steps in the above method embodiments can be implemented by hardware related to program instructions. The aforementioned program can be stored in a readable memory. When the program is executed, it performs the steps of the above method embodiments; and the aforementioned memory (storage medium) includes: read-only memory (ROM), RAM, flash memory, hard disk, solid-state drive, magnetic tape, floppy disk, optical disk, and any combination thereof.

[0164] This application describes embodiments with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this application. It should be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processing unit of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processing unit of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart illustrations. Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.

[0165] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.

[0166] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.

[0167] Obviously, those skilled in the art can make various modifications and variations to the embodiments of this application without departing from the spirit and scope of this application. Therefore, if these modifications and variations to the embodiments of this application fall within the scope of the claims of this application and their equivalents, this application also intends to include these modifications and variations.

[0168] In this application, the term "comprising" and its variations can refer to non-limiting inclusion; the term "or" and its variations can refer to "and / or". The terms "first", "second", etc., in this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. In this application, "multiple" refers to two or more. "And / or" describes the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, and B existing alone. The character " / " generally indicates that the preceding and following related objects have an "or" relationship.

[0169] It should be noted that, for the sake of simplicity, the foregoing method embodiments are all described as a series of actions. However, those skilled in the art should understand that this application is not limited to the described order of actions, as some steps may be performed in other orders or simultaneously according to this application. Furthermore, those skilled in the art should also understand that the embodiments described in the specification are all optional embodiments, and the actions and modules involved are not necessarily essential to this application.

[0170] It should be further noted that although the steps in the flowchart are shown sequentially according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowchart may include multiple sub-steps or multiple stages. These sub-steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these sub-steps or stages is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the sub-steps or stages of other steps.

[0171] It should be understood that the above-described device embodiments are merely illustrative, and the device of this application can also be implemented in other ways. For example, the division of units / modules in the above embodiments is only a logical functional division, and there may be other division methods in actual implementation. For example, multiple units, modules, or components may be combined, or integrated into another system, or some features may be ignored or not executed.

[0172] Furthermore, unless otherwise specified, the functional units / modules in the various embodiments of this application can be integrated into one unit / module, or each unit / module can exist physically separately, or two or more units / modules can be integrated together. The integrated units / modules described above can be implemented in hardware or as software program modules.

[0173] When integrated units / modules are implemented in hardware, the hardware can be digital circuits, analog circuits, etc. The physical implementation of the hardware structure includes, but is not limited to, transistors, memristors, etc. Unless otherwise specified, the processor can be any suitable hardware processor, such as a CPU, GPU, FPGA, DSP, and ASIC, etc. Unless otherwise specified, the storage unit can be any suitable magnetic or magneto-optical storage medium, such as Resistive Random Access Memory (RRAM), Dynamic Random Access Memory (DRAM), Static Random Access Memory (SRAM), Enhanced Dynamic Random Access Memory (EDRAM), High-Bandwidth Memory (HBM), Hybrid Memory Cube (HMC), etc.

[0174] If the integrated unit / module is implemented as a software program module and sold or used as an independent product, it can be stored in a computer-readable storage device (CMD). Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a memory and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods of the various embodiments of this application. The aforementioned memory includes various media capable of storing program code, such as a USB flash drive, read-only memory (ROM), random access memory (RAM), portable hard drive, magnetic disk, or optical disk.

[0175] In the above embodiments, the descriptions of each embodiment have their own emphasis. For parts not described in detail in a certain embodiment, please refer to the relevant descriptions of other embodiments. The technical features of the above embodiments can be combined arbitrarily. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as the combination of these technical features does not contradict each other, it should be considered within the scope of this specification.

[0176] Other embodiments of this application will readily occur to those skilled in the art upon consideration of the specification and practice of the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of this application that follow the general principles of this application and include common knowledge or customary techniques in the art not disclosed herein. The specification and examples are to be considered exemplary only, and the true scope and spirit of this application are indicated by the following claims.

[0177] It should be understood that this application is not limited to the precise structure described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of this application is limited only by the appended claims.

Claims

1. A database query method, characterized in that, The method includes: The first prompt word is determined based on the first query text and query rule information. The first prompt word includes system prompt words and user prompt words. The system prompt word includes at least one of the following: current timestamp, model structure information of the first database, query example, and query rule of the first database. The user prompt word includes at least one of the following: query request information, timestamp calculation rule, timestamp calculation example, and timestamp format rule. The query rule information is used to indicate the query rule corresponding to the first database. Based on the first prompt word, the corresponding response statement is generated using a large language model; The query statement with the target query structure in the response statement is determined to be a first database query statement, and the target query structure matches the first database; Perform a data query in the first database based on the first database query statement.

2. The method according to claim 1, characterized in that, The query statement that determines the target query structure in the response statement to be a first database query statement includes: Remove the identification information and / or explanation information from the response statement to obtain the first query statement; The query statement that determines the target query structure in the first query statement is the first database query statement.

3. The method according to claim 2, characterized in that, The step of determining that the query statement containing the target query structure in the first query statement is the first database query statement includes: The query statement that determines the target query structure in the first query statement is the second query statement; If the numeric data in the second query statement exceeds the safe integer range, the numeric data in the second query statement is converted into numeric data in string format to obtain the first database query statement.

4. The method according to claim 3, characterized in that, The step of converting the numeric data in the second query statement into string-format numeric data includes: Convert each key-value pair in the second query statement that has a numeric value to a string; or, Convert each numeric element in the array of the second query statement to a string; or, Convert the integer numbers in the second query statement to strings.

5. The method according to claim 1, characterized in that, The query statement that determines the target query structure in the response statement to be a first database query statement includes: The JSON object within the parentheses of the response statement is extracted using a parenthesis matching algorithm to form the first database query statement.

6. The method according to any one of claims 1-5, characterized in that, The method further includes: The first database query statement is format-validated to obtain a validation result, which indicates whether the first database query statement passes the format validation. The step of querying data in the first database according to the first database query statement includes: If the verification result indicates that the first database query statement passes the format verification, a data query is performed in the first database according to the first database query statement.

7. The method according to claim 6, characterized in that, The step of performing format validation on the first database query statement to obtain the validation result includes: If the first database query statement is a JSON object and the first database query statement includes all necessary fields without missing, then the first database query statement is determined to have passed format validation. If the format of the first database query statement is not a JSON object, and / or the first database query statement includes missing necessary fields, it is determined that the first database query statement has failed format validation.

8. A database query device, characterized in that, The device includes: The first determining module is used to determine the corresponding first prompt word based on the first query text and query rule information. The first prompt word includes system prompt words and user prompt words. The system prompt word includes at least one of the following: current timestamp, model structure information of the first database, query example, and query rule of the first database. The user prompt word includes at least one of the following: query request information, timestamp calculation rule, timestamp calculation example, and timestamp format rule. The query rule information is used to indicate the query rule corresponding to the first database. The generation module is used to generate corresponding response statements based on the first prompt word using a large language model; The second determining module is used to determine that the query statement of the target query structure in the response statement is a first database query statement, and the target query structure matches the first database. The query module is used to perform data queries in the first database based on the query statement of the first database.

9. An electronic device, characterized in that, include: A processor, and a memory communicatively connected to the processor; The memory stores computer-executed instructions; The processor executes computer execution instructions stored in the memory to implement the method as described in any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions, which, when executed by a processor, are used to implement the method as described in any one of claims 1 to 7.

11. A computer program product, characterized in that, Includes a computer program that, when executed by a processor, implements the method of any one of claims 1 to 7.