Multi-tenant knowledge base retrieval method based on shared vectors and paragraph aggregation

CN123011948APending Publication Date: 2026-10-30HANGZHOU XIAOHUODUI DIGITAL TECHNOLOGY CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611065490.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-07-17
Publication Date
2026-10-30

AI Technical Summary

Technical Problem

[0004]现有技术在多租户场景下索引构建与维护的整体资源开销较高,检索效率与召回全面性难以兼顾,文本切分方式导致知识单元语义完整性不足,影响向量检索的匹配精度

Benefits of technology

[0051]本方案通过语义相似度聚合的方式形成标准化知识单元,保障每个知识单元具备完整的语义表达能力,有效提升向量检索的匹配精度。采用全局共享分层聚类索引结合租户子树层的架构设计,在实现多租户数据逻辑隔离的基础上共享全局语义聚类体系,大幅降低多租户场景下索引构建与维护的资源成本,同时支持索引容量的弹性扩展。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure FT_1
    Figure FT_1
  • Figure FT_2
    Figure FT_2
  • Figure FT_3
    Figure FT_3
Patent Text Reader

Abstract

The application is suitable for the field of natural language processing and vector retrieval technology, and provides a multi-tenant knowledge base retrieval method based on shared vectors and paragraph aggregation. The method receives a tenant natural language query and distributes the request by a large model. Semantic associated paragraphs, historical query pairing data are aggregated into standardized knowledge units with tenant identification and type labels. A shared hierarchical clustering index including a global layer and a tenant sub-tree layer is built. Knowledge units are recalled through two-way retrieval of tenant sub-trees and global clustering. A hierarchical structure is constructed to generate a structured query statement. Page compliance verification is performed according to the database dialect. Automatic retry is supported in case of query failure. Index dynamic splitting is realized by monitoring tenant data volume and access volume. The application considers multi-tenant data isolation and index resource reuse, optimizes semantic retrieval accuracy, adapts to multiple database syntaxes, and can adapt to changes in tenant business scale.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of natural language processing and vector retrieval technology, and particularly relates to a multi-tenant knowledge base retrieval method based on shared vectors and paragraph aggregation. Background Technology

[0002] With the rapid development of cloud computing and large language model technology, intelligent knowledge base retrieval technology based on natural language interaction has been widely applied in various enterprise-level SaaS service scenarios, and multi-tenant architecture has become the mainstream deployment mode for enterprise knowledge management and data query systems. The deep integration of vector retrieval technology and large language model generation capabilities has opened up the query link between unstructured business knowledge and structured databases, enabling users to directly obtain knowledge information and business data through natural language. Currently, the technology in this field continues to evolve towards reducing deployment costs, improving retrieval accuracy, strengthening data security, and adapting to diverse business scenarios. Balancing resource sharing efficiency and data isolation capabilities in multi-tenant scenarios has become the core direction for technology optimization.

[0003] Existing multi-tenant knowledge base retrieval solutions mostly adopt a single-tenant independent vector index architecture, building and maintaining a dedicated vector retrieval index for each tenant. After receiving a user's natural language query request, the system performs vector recall within the corresponding tenant's independent index, constructs prompt words based on the recalled knowledge base text content, feeds them into a large language model to generate a structured query statement, and finally submits it to the tenant's corresponding database for execution. Some solutions use fixed-length rules to segment the original text, while simultaneously relying on general syntax rules to perform basic compliance checks on the generated query statement.

[0004] Existing technologies in multi-tenant scenarios suffer from high overall resource overhead in index building and maintenance, making it difficult to balance retrieval efficiency and comprehensive recall. Text segmentation methods result in insufficient semantic integrity of knowledge units, impacting the matching accuracy of vector retrieval. The generated structured query statements have limited adaptability to different database dialects, lack sufficient security control over query execution, and the index architecture cannot be dynamically adjusted based on tenant business scale and access volume. Therefore, there is room for improvement in the overall resource utilization and operational stability of the system. Summary of the Invention

[0005] The purpose of this invention is to provide a multi-tenant knowledge base retrieval method based on shared vectors and paragraph aggregation, aiming to solve the technical problems existing in the prior art as identified in the background art.

[0006] This invention is implemented as follows: a multi-tenant knowledge base retrieval method based on shared vectors and paragraph aggregation, the method comprising:

[0007] Receive natural language query requests from target tenants, determine whether a database query is needed using a large language model, return a natural language response if not, and proceed with the retrieval process if yes;

[0008] Obtain the original text and historical query data of the target tenant's knowledge base, and aggregate semantically related paragraphs and query-structured statements into standardized knowledge units with tenant identifiers and knowledge type labels;

[0009] A globally shared hierarchical clustering index is constructed, which includes a global layer and tenant subtree layers mounted under each node of the global layer. The global layer consists of a clustering center node and associated global knowledge units. Each tenant subtree layer stores the knowledge unit vector and pagination verification rules of the corresponding tenant.

[0010] Based on the tenant identifier, two independent searches are performed from the global level and the tenant subtree level of the index to recall semantically matched knowledge units as search results;

[0011] The search results are injected into the database according to knowledge type and format. The prompt words are constructed by combining the database table structure and the de-identified sample data. The prompt words are then sent to the large language model to generate structured query results containing execution parameters.

[0012] Perform multi-dialect pagination forced validation on structured query statements. Based on pagination validation rules, validate whether the target database contains compliant pagination clauses according to its dialect. After the validation passes, execute in a parameterized manner.

[0013] If the execution fails, the failure message along with the original prompt will be fed back into the large language model for retry. During operation, the amount of tenant index data and the frequency of queries will be monitored. When the preset threshold is exceeded, the knowledge unit vector and pagination verification rules of the tenant subtree layer will be separated into independent indexes.

[0014] As a further aspect of the present invention, the step of receiving the natural language query request from the target tenant and determining whether a database query is needed through a large language model specifically includes:

[0015] A pre-defined function tool for querying the database is injected into the large language model. The function tool includes a function name, a function description, and parameter definitions. The function description is used to inform the large language model that the tool is used to perform database queries. The parameter definitions include a database identifier and query condition placeholders.

[0016] The natural language query request is sent as a user message to the large language model, which then decides whether to invoke the function tool based on the function tool itself.

[0017] If the large language model returns a tool call result and the tool call result indicates that a database query is required, then it is determined to be yes and the retrieval process begins. If the large language model returns a natural language response and does not contain a tool call instruction, then it is determined to be no and a natural language response is returned.

[0018] As a further aspect of the present invention, the step of pairing and aggregating semantically related paragraphs and query-structured statements into standardized knowledge units with tenant identifiers and knowledge type labels specifically includes:

[0019] Obtain the original text of the knowledge base of the target tenant, extract the semantic structure information of the title level and paragraph boundary of the original text of the knowledge base, and divide the original text of the knowledge base into an initial text fragment set. Input the adjacent text fragments in the initial text fragment set into the vector encoding model to obtain their respective first fragment vector and second fragment vector.

[0020] The cosine similarity between the first segment vector and the second segment vector is calculated as the semantic similarity. Adjacent text segments with semantic similarity exceeding a first preset threshold are merged into standardized knowledge units, and corresponding knowledge unit vectors are generated. The tenant identifier and paragraph boundary offset of the target tenant are associated with the standardized knowledge unit.

[0021] Obtain the historical query data of the target tenant, which includes historical natural language questions and corresponding historical structured query statements. Input the historical natural language questions into a vector encoding model to obtain question vectors.

[0022] Based on the similarity between the question vector and the knowledge unit vector of each standardized knowledge unit, the historical natural language question and the corresponding historical structured query statement are attached to the standardized knowledge unit with the highest semantic similarity. The knowledge type label of the standardized knowledge unit is assigned to the example class, and the knowledge type label of the standardized knowledge unit that has not been attached to any historical natural language question is assigned to the terminology class.

[0023] As a further embodiment of the present invention, the construction of a globally shared hierarchical clustering index specifically includes:

[0024] Global hierarchical clustering is performed on the knowledge unit vectors of all standardized knowledge units to generate a set of top-level cluster center nodes. Each top-level cluster center node contains a center vector and a list of global knowledge unit references covered by the node. The global knowledge units are standardized knowledge units mounted to the global layer. The set of top-level cluster center nodes and the child node hierarchy under each top-level cluster center node constitute the global layer.

[0025] The knowledge unit vectors of each standardized knowledge unit are assigned to the nearest top-level cluster center node according to their vector space distance from each top-level cluster center node, and the standardized knowledge units are added to the global knowledge unit reference list of the top-level cluster center node.

[0026] When the number of references in the global knowledge unit reference list under any top-level cluster center node exceeds the preset node capacity threshold, local recursive clustering is performed on all global knowledge unit references under the top-level cluster center node to generate new sub-layer cluster nodes.

[0027] Based on the tenant identifier associated with each standardized knowledge unit, the knowledge unit vectors of standardized knowledge units belonging to the same tenant are combined into the tenant subtree layer under each cluster center node of the global layer. The tenant subtree layer is stored as the index metadata record of the tenant. The index metadata record includes the tenant identifier, a reference list pointing to the paths of all knowledge unit vectors of the tenant in the cluster center node, and the number of knowledge unit vectors of the tenant under each cluster center node path and the pagination verification rules.

[0028] Obtain the dialect type of the target database of the target tenant, select the corresponding pagination validation rule from the preset dialect pagination rule library according to the dialect type, and write the pagination validation rule into the index metadata record of the target tenant as the pagination validation rule of the tenant. The pagination validation rule includes the pagination syntax regular expression corresponding to the database dialect and the structural requirements of the compliant pagination clause.

[0029] As a further aspect of the present invention, the step of performing two independent searches and recalling semantically matched knowledge units as search results specifically includes:

[0030] The natural language query request is converted into a query vector using a vector encoding model. The index metadata record of the target tenant is located from each tenant subtree layer based on the tenant identifier of the target tenant. The tenant subtree layer of the target tenant is extracted as the first retrieval range according to the reference list in the index metadata record.

[0031] Within the first retrieval range, the similarity between the query vector and each knowledge unit vector is calculated, and knowledge units with similarity exceeding a second preset threshold are selected as the first recall set.

[0032] Starting from the query vector, retrieve the N nearest top-level cluster center nodes in the set of top-level cluster center nodes in the global layer. Use the global knowledge unit reference list covered by the N top-level cluster center nodes as the second retrieval range. Within the second retrieval range, filter out the filtered knowledge units belonging to the target tenant based on the tenant identifier.

[0033] Calculate the similarity between the query vector and the knowledge unit vector of the filtered knowledge unit, select the filtered knowledge units whose similarity exceeds the second preset threshold as the second recall set, merge the first recall set and the second recall set and remove duplicates to obtain the recall knowledge unit set.

[0034] As a further aspect of the present invention, the generation of structured query results containing execution parameters specifically includes:

[0035] Extract units with the knowledge type label "Example" from the recalled knowledge unit set, and write the historical natural language questions and corresponding historical structured query statements attached to them into the first paragraph of the prompt words according to the preset reference query format.

[0036] Extract units with the knowledge type label "terminology" from the recalled knowledge unit set, and write the text content of the standardized knowledge units in the set into the second paragraph of the prompt word according to the preset business knowledge format.

[0037] Obtain the database table structure of the target database and the preset anonymized sample data, and write the database table structure into the third paragraph of the prompt word according to the preset database pattern format, and write the anonymized sample data into the fourth paragraph of the prompt word according to the preset data sample format.

[0038] The first, second, third, and fourth paragraphs are combined to form a complete prompt word, and the large language model generates a structured query result, which includes the structured query statement text and execution parameters.

[0039] As a further aspect of the present invention, the step of verifying whether the target database dialect contains a compliant pagination clause specifically includes:

[0040] Receive the structured query results generated by the large language model and extract the structured query statement text and execution parameters. Based on the tenant identifier of the target tenant, locate the index metadata record of the corresponding tenant subtree layer, and read the pre-stored pagination verification rules and the dialect type of the target database as the execution benchmark for this pagination verification.

[0041] Based on the confirmed target database dialect type, and based on the pagination syntax regular expression in the pagination validation rules, the extracted structured query text is subjected to full statement regular expression matching to verify whether the statement contains a pagination clause that conforms to the pagination syntax regular expression. At the same time, it is verified whether the parameter placeholders of the pagination clause correspond one-to-one with the pagination parameters in the execution parameters.

[0042] If the regular expression matching detects a pagination clause that matches the pagination syntax regular expression, and the structure of the pagination clause meets the structural requirements of the compliant pagination clause in the pagination validation rules, then the pagination validation is deemed successful. The execution parameters are then filled into the corresponding placeholders in the structured query statement text one by one to generate a parameterized executable structured query statement, which is then submitted to the target database for execution. If the execution is successful, the query result is returned; if the execution fails, the execution failure information is returned.

[0043] If no pagination clause matching the pagination syntax regular expression is detected by regular expression matching, the pagination validation is determined to be unsuccessful, a pagination validation failure message is generated, and the pagination validation failure message, along with the structured query statement text, is returned to the large language model, triggering the regeneration process of the structured query statement.

[0044] As a further aspect of the present invention, the step of re-feeding the failure information along with the original prompt words into the large language model for retrying, and monitoring the tenant index data volume and query frequency during operation, specifically includes:

[0045] Receive execution failure information returned by the target database, extract the failure reason description and corresponding error code, combine the execution failure information, structured query statement text and the original complete prompt words into retry prompt words, and send them into the large language model to regenerate the structured query results;

[0046] The pagination validation and database submission process is repeated using the regenerated structured query results. If the number of consecutive retries exceeds the preset retrieval threshold and still fails, the current query process is terminated, a query failure message is returned to the target tenant, and the current natural language query request is ended.

[0047] The total number of knowledge unit vectors contained in the tenant subtree layer of each tenant and the frequency of natural language query requests of each tenant per unit time are collected according to the preset statistical period and compared with the corresponding preset thresholds to determine whether they exceed the threshold range.

[0048] When the total number of knowledge unit vectors and the frequency of natural language query requests for any tenant both exceed the corresponding preset thresholds, the tenant subtree layer of that tenant will be completely stripped from the corresponding mount node of the globally shared hierarchical clustering index.

[0049] Each tenant's knowledge unit vector, after being stripped, is independently constructed into a single-tenant-specific index along with the pagination validation rules, while retaining the reference records of that tenant's knowledge unit vector at the global level.

[0050] The beneficial effects of this invention are:

[0051] This solution forms standardized knowledge units through semantic similarity aggregation, ensuring that each knowledge unit has complete semantic expression capabilities and effectively improving the matching accuracy of vector retrieval. It adopts an architecture design combining a globally shared hierarchical clustering index with tenant subtree layers. This achieves logical isolation of multi-tenant data while sharing a global semantic clustering system, significantly reducing the resource costs of index building and maintenance in multi-tenant scenarios, while also supporting elastic expansion of index capacity.

[0052] Two independent retrieval mechanisms combine the efficiency of intra-tenant retrieval with the comprehensiveness of global retrieval, significantly improving the accuracy and coverage of retrieval recall. Hint words are organized by knowledge type and combined with database table structure and anonymized sample data to improve the business adaptability and syntactic compliance of structured query generation. A multi-dialect pagination mandatory validation mechanism manages query execution risks and ensures stable database operation.

[0053] The failure retry loop improves the success rate of query execution. The dynamic index splitting mechanism can evolve the index architecture on demand according to the tenant's business scale and access volume, balancing system resource investment and performance experience, and improving the overall operating efficiency, data security and service availability of the multi-tenant knowledge base retrieval system, adapting to the differentiated business needs of tenants at different development stages. Attached Figure Description

[0054] Figure 1 A flowchart of a multi-tenant knowledge base retrieval method based on shared vectors and paragraph aggregation provided in an embodiment of the present invention;

[0055] Figure 2 A flowchart for receiving a natural language query request from a target tenant, provided for an embodiment of the present invention;

[0056] Figure 3 A flowchart for aggregating semantically related paragraphs and query-structured statements into standardized knowledge units with tenant identifiers and knowledge type labels, provided in an embodiment of the present invention;

[0057] Figure 4 A flowchart for constructing a globally shared hierarchical clustering index provided in an embodiment of the present invention;

[0058] Figure 5 A flowchart for performing two independent searches and recalling semantically matched knowledge units as search results, provided in an embodiment of the present invention;

[0059] Figure 6 A flowchart for generating structured query results containing execution parameters, provided in an embodiment of the present invention;

[0060] Figure 7 A flowchart for verifying whether a compliant pagination clause is contained based on the dialect of the target database, provided in an embodiment of the present invention;

[0061] Figure 8 This is a flowchart provided by an embodiment of the present invention, which re-injects failure information along with the original prompt words into the large language model for retrying, and monitors the amount of tenant index data and query frequency during operation. Detailed Implementation

[0062] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the invention.

[0063] Figure 1 A flowchart of a multi-tenant knowledge base retrieval method based on shared vectors and paragraph aggregation provided in an embodiment of the present invention is shown below. Figure 1 As shown, the method includes:

[0064] S100: Receive the natural language query request from the target tenant, determine whether a database query is needed through the large language model, return a natural language response if not, and proceed with the retrieval process if yes;

[0065] In multi-tenant scenarios, different tenants have significantly different business domains and query needs. Many daily inquiries and common-sense Q&A requests can be answered directly without accessing the business database. If all of these requests were to enter the subsequent retrieval and generation process, it would result in an inefficient consumption of system computing power and storage resources. This step addresses this by injecting a fully encapsulated database query tool into the large language model. This transforms data query capabilities into a standardized interface that the model can call autonomously. The model then autonomously determines whether to trigger a database query based on the semantic content of the natural language query, eliminating the need to rely on manually configured rule bases for intent matching.

[0066] This mode can adapt to the language expression habits and business query scenarios of different tenants, accurately distinguish between non-data requests and data query requests, and directly complete the processing and return of requests that can be directly answered at the entry layer, avoiding the ineffective occupation of resources across the entire chain, and effectively improving the overall concurrency carrying capacity and query response speed of the system.

[0067] S200: Obtain the original text and historical query data of the target tenant's knowledge base, and aggregate semantically related paragraphs and query-structured statements into standardized knowledge units with tenant identifiers and knowledge type labels;

[0068] Original knowledge base text is typically divided into paragraphs following document formatting logic. Fixed-length text segmentation can easily compromise semantic integrity, leading to semantic incompleteness in individual slices and reducing the matching accuracy of vector retrieval. This step first performs preliminary segmentation of the original text, then generates vector representations of adjacent segments using a vector encoding model. Adjacent segments with close semantic connections are merged based on cosine similarity to form standardized knowledge units that are semantically complete and independent. This ensures that each knowledge unit has complete semantic expression capabilities and improves the matching accuracy of vector retrieval.

[0069] Building upon this foundation, each knowledge unit is associated with a corresponding tenant's identity identifier, achieving logical isolation of multi-tenant knowledge data. This ensures that knowledge content is only visible to its respective tenant, meeting the data security and access control requirements of a multi-tenant system. Simultaneously, historical query data from tenants is incorporated. Pairing data between historical natural language questions and corresponding structured query statements is matched using vector similarity and then attached to the most semantically relevant knowledge unit. Knowledge unit types are tagged: units with historical query pairing data are labeled as example units, while units containing only pure business text are labeled as terminology units. These two types of knowledge units each fulfill their respective functions, providing grammatical reference and semantic interpretation, respectively, offering hierarchical and categorized knowledge materials for the prompt word construction phase.

[0070] S300, construct a globally shared hierarchical clustering index, which includes a global layer and tenant subtree layers mounted under each node of the global layer. The global layer consists of a clustering center node and associated global knowledge units. Each tenant subtree layer stores the knowledge unit vector and pagination verification rules of the corresponding tenant.

[0071] This step performs global hierarchical clustering on all standardized knowledge units of all tenants, generating a top-level set of cluster center nodes. Each cluster center corresponds to a semantic cluster. All knowledge units are assigned to the nearest cluster center node according to the Euclidean distance in the vector space, forming a globally unified semantic space partitioning system. All tenants share the same global clustering structure, eliminating the need to build independent clustering indexes for each tenant. This significantly reduces the computational overhead and storage costs of index building in multi-tenant scenarios. Simultaneously, the globally unified semantic space ensures consistency in semantic matching standards. When the number of knowledge unit references under a single cluster center node exceeds a set node capacity threshold, local recursive clustering is performed on the knowledge units under that node, generating multi-level sub-cluster nodes. This controls the size of knowledge units on a single node, avoiding increased retrieval traversal costs due to an excessive number of vectors on a single node. It also refines the granularity of semantic partitioning, narrows the comparison range of a single retrieval, and improves retrieval speed and accuracy.

[0072] This recursive expansion pattern does not require rebuilding the global index; it only expands downwards layer by layer under the corresponding nodes, supporting elastic scaling of index capacity as the knowledge base grows. Based on the global clustering structure, knowledge unit vectors of the same tenant are aggregated according to tenant identifiers, forming tenant subtree layers under each cluster center node, and generating tenant-specific index metadata records.

[0073] The index metadata records store tenant identifiers, a list of path references to the cluster nodes where knowledge units reside, the number of knowledge units under each node, and pagination validation rules. This achieves logical isolation of tenant data and allows for quick location of tenant knowledge storage locations via the reference list, avoiding the need to traverse the entire global dataset during retrieval. Pagination validation rules are stored in conjunction with the index metadata, adapting to the database dialect differences of different tenants. This eliminates the need to retrieve the rule base separately for each query, improving validation execution efficiency.

[0074] S400 performs two independent searches based on the tenant identifier, from the global layer of the index to the tenant subtree layer, and recalls semantically matched knowledge units as search results;

[0075] The first-path retrieval locates the corresponding tenant's index metadata based on the tenant identifier, directly performing vector similarity matching within the tenant's exclusive subtree layer, and filtering knowledge units that meet the similarity threshold to form the first-path recall set. This retrieval path is completely limited to the tenant's own knowledge scope, with a small data scale, fast retrieval response speed, and inherent data isolation properties, without touching the knowledge content of other tenants. It is the basic pathway to ensure retrieval efficiency and data security.

[0076] Relying solely on tenant-level retrieval has limitations. When a tenant's own knowledge base is small, the clustering granularity is coarse, easily leading to missed recalls of relevant knowledge units near semantic boundaries. The second-path retrieval starts from the top-level cluster centers of the global layer. First, it calculates the spatial distance between the query vector and each top-level cluster center, filters out the closest cluster centers, and then, within the global knowledge unit coverage of these cluster centers, filters out the target tenant's knowledge units by tenant identifier, performs similarity matching, and selects units that meet the threshold requirements to form the second-path recall set.

[0077] This approach leverages fine-grained semantic partitioning formed by the entire global dataset to overcome the limitations of local clustering in tenant subtrees, recalling relevant knowledge units missed by the first retrieval. The results from both retrieval paths are merged and deduplicated to form the final set of recalled knowledge units, balancing retrieval efficiency with comprehensive recall.

[0078] S500 injects the search results into the knowledge type format, combines the database table structure and desensitized sample data to construct prompt words, and sends them into the large language model to generate structured query results containing execution parameters;

[0079] This step organizes the recalled knowledge units into zones based on knowledge type tags. The historical natural language problems and corresponding structured query statements attached to the example knowledge units are organized into reference query paragraphs with prompt words in a unified format. This provides the large language model with sentence writing paradigms and business expression references for similar queries, reducing the probability of grammatical errors in the generated statements.

[0080] The business text content of terminology knowledge units is organized into business knowledge paragraphs containing prompt words, providing semantic explanations of business concepts and field meanings for the model, ensuring that the generated queries conform to business logic. This two-tiered knowledge partitioning arrangement aligns with the contextual processing logic of a large language model, avoiding model attention distraction caused by mixing different types of information, and improving the stability and accuracy of the generated results.

[0081] Based on this, the database table structure information and anonymized sample data of the target tenant are introduced and organized into database schema paragraphs and data sample paragraphs, respectively. This provides the model with the table names, field definitions, inter-table relationships and data type constraints of the target database. At the same time, the anonymized sample data shows the actual value format and business meaning of the fields, so that the query statements generated by the model strictly adapt to the tenant's database structure and reduce field misuse and value errors.

[0082] The anonymization process prevents real business data from entering the prompt word chain, ensuring the security of business data for multi-tenants. The four parts are combined in a fixed logical order to form a complete prompt word, which is then fed into a large language model to generate structured query results. The output includes both the structured query text and the corresponding execution parameters, achieving separation of the statement template and execution parameters, thus supporting parameterized execution and security verification.

[0083] S600 performs multi-dialect pagination forced validation on structured query statements. Based on pagination validation rules, it checks whether the target database contains compliant pagination clauses according to the dialect. After the validation passes, it is executed in a parameterized manner.

[0084] This step retrieves the corresponding tenant's database dialect type and pagination validation rules from the index metadata based on the tenant identifier. This serves as the execution benchmark for the validation, ensuring that the validation logic fully matches the tenant's actual database environment. The pagination validation rules are set with corresponding pagination syntax regular expressions and compliance structure requirements for various mainstream databases, covering multiple database types such as relational and analytical databases, and can adapt to the database selection of most tenants.

[0085] During the validation process, a full-statement regular expression match is performed on the generated structured query statement to check whether the statement contains pagination clauses that conform to the corresponding dialect norms. Simultaneously, it verifies that the parameter placeholders in the pagination clauses correspond one-to-one with the pagination parameters in the execution parameters, ensuring that the pagination parameters can be accurately injected into the statement. Statements that pass validation are executed in a parameterized manner. The execution parameters are filled into the corresponding placeholders one by one to generate an executable statement, which is then submitted to the target database. Parameterized execution effectively prevents SQL injection risks and improves database access security.

[0086] Statements that fail validation are immediately retried. The failure information and the original statement are returned to the large language model for correction, thus intercepting non-compliant statements from entering the database for execution. Forced pagination validation avoids full queries without pagination that scan large amounts of data, consuming excessive database computing power and connection resources. It also prevents large queries from a single tenant from affecting the access performance of other tenants, ensuring overall stability in a multi-tenant shared database resource environment.

[0087] S700, if execution fails, the failure information along with the original prompt words will be fed back into the large language model for retry. During operation, the amount of tenant index data and the frequency of queries will be monitored. When the preset threshold is exceeded, the knowledge unit vector and pagination verification rules of the tenant subtree layer will be separated into independent indexes.

[0088] For queries that fail to execute in the database, this step extracts the failure reason description and error code. The failure information, the original structured query statement, and the original error message are combined into a retry suggestion, which is then fed into a large language model to regenerate the query results. The model then corrects statement defects based on specific error information, automatically completing error repair without manual intervention. This forms a complete closed loop of generation, verification, execution, and retries, improving the success rate of query execution. The system sets a maximum number of retries. If the number of consecutive retries exceeds the threshold and the query still fails, the current query process is terminated and a failure message is returned. This avoids resource waste and query timeouts caused by infinite retries, prevents erroneous requests from continuously consuming system computing power, and ensures the overall service availability and response timeliness.

[0089] In addition to query fault tolerance, this step also continuously monitors the index data size and query access frequency of each tenant. These two metrics are collected at fixed statistical periods and compared with preset thresholds. When both metrics for a single tenant exceed the threshold, it indicates that the tenant's business scale and access volume have reached a high level. Continuing to mount it in the global shared index will affect global retrieval efficiency and cannot meet the tenant's high-performance access requirements. At this point, the tenant's subtree layer is completely separated from the global shared index, and an independent single-tenant-specific index is built by combining all its knowledge unit vectors and pagination validation rules, while retaining the tenant's knowledge unit reference records at the global layer.

[0090] After index splitting, the tenant has independent index resources, and the retrieval performance is not affected by the data growth of other tenants. It can support higher concurrent access and larger knowledge base scale, while the retention of global layer references can maintain the continuous effectiveness of the two-way retrieval mechanism and avoid the decline in recall caused by index splitting.

[0091] like Figure 2 As shown, receiving the natural language query request from the target tenant and determining whether a database query is needed using a large language model specifically includes:

[0092] S110, Inject a pre-defined function tool for querying the database into the large language model. The function tool includes a function name, a function description, and parameter definitions. The function description is used to inform the large language model that the tool is used to perform a database query. The parameter definitions include a database identifier and query condition placeholders.

[0093] S120, The natural language query request is sent as a user message to the large language model, and the large language model decides autonomously whether to call the function tool based on the function tool.

[0094] S130, if the large language model returns a tool call result and the tool call result indicates that a database query is required, then it is determined to be yes and the retrieval process is initiated; if the large language model returns a natural language response and does not contain a tool call instruction, then it is determined to be no and a natural language response is returned.

[0095] like Figure 3 As shown, the process of pairing and aggregating semantically related paragraphs and query-structured statements into standardized knowledge units with tenant identifiers and knowledge type labels specifically includes:

[0096] S210, obtain the original text of the knowledge base of the target tenant, extract the semantic structure information of the title level and paragraph boundary of the original text of the knowledge base, divide the original text of the knowledge base into an initial text fragment set, and input adjacent text fragments in the initial text fragment set into a vector encoding model to obtain their respective first fragment vector and second fragment vector;

[0097] S220, calculate the cosine similarity between the first segment vector and the second segment vector as semantic similarity, merge adjacent text segments whose semantic similarity exceeds a first preset threshold into standardized knowledge units, generate corresponding knowledge unit vectors, and associate the tenant identifier and paragraph boundary offset of the target tenant with the standardized knowledge unit;

[0098] ;

[0099] in, The cosine similarity is the semantic similarity between the first and second segment vectors. This refers to the first segment vector output by the vector encoding model from the previous segment in a pair of adjacent text segments. This refers to the second segment vector output by the vector encoding model in a context of adjacent text segments. Let the vector magnitude of the first segment vector be _____. Let be the vector magnitude of the second segment vector.

[0100] S230, Obtain the historical query data of the target tenant. The historical query data includes historical natural language questions and corresponding historical structured query statements. Input the historical natural language questions into a vector encoding model to obtain question vectors.

[0101] S240, based on the similarity between the question vector and the knowledge unit vector of each standardized knowledge unit, the historical natural language question and the corresponding historical structured query statement are attached to the standardized knowledge unit with the highest semantic similarity, the knowledge type label of the standardized knowledge unit is assigned to the example class, and the knowledge type label of the standardized knowledge unit that has not been attached to any historical natural language question is assigned to the terminology class.

[0102] like Figure 4 As shown, the construction of the globally shared hierarchical clustering index specifically includes:

[0103] S310, Perform global hierarchical clustering on the knowledge unit vectors of all standardized knowledge units to generate a set of top-level cluster center nodes. Each top-level cluster center node contains a center vector and a list of global knowledge unit references covered by the node. The global knowledge unit is a standardized knowledge unit mounted to the global layer. The set of top-level cluster center nodes and the child node hierarchy under each top-level cluster center node constitute the global layer.

[0104] S320, the knowledge unit vectors of each standardized knowledge unit are assigned to the nearest top-level cluster center node according to the vector space distance from each top-level cluster center node, and the standardized knowledge units are added to the global knowledge unit reference list of the top-level cluster center node;

[0105] Vector space distance calculation:

[0106] ;

[0107] The nearest cluster center assignment rule:

[0108] ;

[0109] in: For the first The knowledge unit vector of the first standardized knowledge unit and the first Euclidean distance between the center vectors of the top-level cluster center nodes For the first A knowledge unit vector corresponding to a standardized knowledge unit. For the first The center vector corresponding to the top-level cluster center node Let be the dimension of the vector. For the first The first knowledge unit vector of the th knowledge unit vector dimensional components, For the first The first cluster center vector dimensional components, In order to be with the first The number of the top-level cluster center node whose vector distance is closest to each knowledge unit. This represents the total number of top-level cluster center nodes. This is an operation to determine the cluster center number that minimizes the distance.

[0110] S330, when the number of references in the global knowledge unit reference list under any top-level cluster center node exceeds the preset node capacity threshold, perform local recursive clustering on all global knowledge unit references under the top-level cluster center node to generate new sub-layer cluster nodes;

[0111] The triggering mechanism for local recursive clustering is as follows:

[0112] The system counts the number of global knowledge unit references attached to each top-level cluster center node in real time. When the reference count of any node exceeds the preset node capacity threshold, a local recursive clustering operation is immediately triggered for all global knowledge unit references under that node. If the number of references of the generated sub-layer nodes still exceeds the capacity threshold during the recursion process, the system continues to cluster downwards until the number of references of all sub-nodes is lower than the threshold, at which point the recursion terminates.

[0113] This mechanism can control the size of knowledge units in a single node, avoiding excessive vectors under a single cluster center, which would increase traversal costs and retrieval latency during searching, thus ensuring retrieval efficiency. At the same time, by generating multi-level child nodes through recursive clustering, the semantic division of the vector space is made more refined, reducing the comparison range of a single search and improving the accuracy of the search. It also enables elastic expansion of the index capacity. When the total amount of knowledge base grows, there is no need to reconstruct the global index. Instead, it is only necessary to expand the index layer by layer under the corresponding node, which greatly reduces the computational cost of index maintenance and the impact on business.

[0114] S340, based on the tenant identifier associated with each standardized knowledge unit, the knowledge unit vectors of standardized knowledge units belonging to the same tenant are combined into the tenant subtree layer of the tenant under each clustering center node of the global layer. The tenant subtree layer is stored as the index metadata record of the tenant. The index metadata record includes the tenant identifier, a reference list pointing to the paths of all knowledge unit vectors of the tenant in the clustering center node, and the number of knowledge unit vectors of the tenant under each clustering center node path and the pagination verification rules.

[0115] For index metadata records, the tenant identifier serves as a unique identifier for each tenant, used to quickly locate the corresponding tenant's index metadata during retrieval. This enables data isolation and access control between tenants, ensuring that only their own tenant's knowledge content can be accessed, preventing cross-tenant data leakage. A reference list pointing to the cluster center node paths of all knowledge unit vectors for that tenant records the storage location of all knowledge unit vectors in the global hierarchical clustering index. During retrieval, it's unnecessary to traverse all nodes of the global index; the reference list directly locates the node path where the tenant's data is located, significantly narrowing the retrieval scope and improving retrieval speed. The number of knowledge unit vectors for that tenant under each cluster center node path is used for index capacity statistics, threshold judgment, and retrieval performance prediction, while also providing quantitative data for subsequent index splitting and load balancing. Pagination validation rules store the pagination validation logic for the corresponding tenant's database dialect, providing an execution benchmark for subsequent structured query pagination compliance validation. This eliminates the need to repeatedly retrieve the rule base for each query, improving validation execution efficiency.

[0116] S350, obtain the dialect type of the target database of the target tenant, select the corresponding pagination verification rule from the preset dialect pagination rule library according to the dialect type, and write the pagination verification rule into the index metadata record of the target tenant as the pagination verification rule of the tenant. The pagination verification rule includes the pagination syntax regular expression corresponding to the database dialect and the structural requirements of the compliant pagination clause.

[0117] The target database refers to the database used by the target tenant's own business system to store core business data. It is the data source for the final execution of structured query statements. Each tenant can independently configure its own target database.

[0118] This solution adopts a pre-built dialect pagination rule base plus statement-level regular expression enforcement. For the five mainstream relational and analytical database dialects in multi-tenant production environments, dedicated pagination verification rules are customized and built into the rule base. Each rule includes two core verification items: the pagination syntax regular expression for the corresponding dialect and the compliant pagination clause structure requirements. After the tenant binds its own database dialect type, the system directly loads the corresponding rule to perform the verification, without the need for dynamic adaptation at runtime.

[0119] For MySQL dialects, this solution uses the LIMIT pagination syntax as the core validation benchmark. Regular expressions are used to match the two standard ways of writing the LIMIT offset parameter and the number of rows per page parameter, as well as the LIMIT number of rows per page parameter and OFFSET offset parameter. The validation ensures that the parameter placeholders in the pagination clause correspond one-to-one with the number of rows per page and offset parameters in the execution parameters, thus ensuring that the generated statement is fully compatible with MySQL syntax specifications.

[0120] For the PostgreSQL dialect, this solution's pre-defined rules cover the mainstream pagination syntax LIMIT...OFFSET..., while also being compatible with the SQL standard FETCHFIRST...ROWSONLY syntax. Regular expressions are used to validate the order of pagination keywords and the position of parameters. For the Oracle dialect, this solution's pre-defined rules adapt to both versions of the syntax system, supporting both the standard OFFSET...ROWSFETCHNEXT...ROWSONLY pagination syntax of versions 12c and above, and also compatible with the nested ROWNUM pagination structure of earlier versions. Corresponding regular expressions are set to perform layered syntax validation for each version.

[0121] For SQL Server dialects, this solution uses the standard pagination syntax OFFSET...ROWSFETCHNEXT...ROWSONLY as the validation benchmark, while also being compatible with pagination syntax using TOP combined with row number functions, ensuring that pagination clauses conform to SQL Server syntax constraints.

[0122] For the ClickHouse dialect, this solution uses pre-defined rules to match its LIMIT...OFFSET... pagination syntax, while also verifying the compliance of the pagination clause's position in the complete query statement, thus adapting to the query structure characteristics of analytical databases.

[0123] Different databases have significantly different pagination syntax rules, and a uniform validation rule cannot adapt to all database dialects, easily leading to false positives or false negatives. Matching the pagination validation rules to the dialect type of the tenant's target database ensures the accuracy of pagination validation, accurately identifies pagination clauses that conform to the corresponding database syntax, and avoids compliant statements being mistakenly blocked or non-compliant statements being allowed to pass. At the same time, it allows the validation logic to be precisely aligned with the tenant's actual database environment, ensuring that the final generated structured query statement can be correctly executed in the target database, improving the adaptability and reliability of pagination validation.

[0124] like Figure 5 As shown, the execution of two independent searches and the retrieval of semantically matched knowledge units as search results specifically includes:

[0125] S410, the natural language query request is converted into a query vector through a vector encoding model, the index metadata record of the target tenant is located from each tenant subtree layer based on the tenant identifier of the target tenant, and the tenant subtree layer of the target tenant is extracted as the first retrieval range according to the reference list in the index metadata record.

[0126] S420, within the first retrieval range, calculate the similarity between the query vector and each knowledge unit vector, and filter knowledge units with similarity exceeding a second preset threshold as the first recall set;

[0127] S430, starting from the query vector, retrieve the N nearest top-level clustering center nodes in the set of top-level clustering center nodes in the global layer, and use the global knowledge unit reference list covered by the N top-level clustering center nodes as the second retrieval range, and filter out the filtered knowledge units belonging to the target tenant based on the tenant identifier within the second retrieval range.

[0128] Distance calculation between query vector and cluster center:

[0129] ;

[0130] Top-N filtering rules:

[0131] All Sort in ascending order, then retrieve the first [item name] after sorting. The corresponding set of cluster center nodes ,satisfy .

[0132] Parameter description: For query vector and the first The Euclidean distance in vector space between the center vectors of the top-level cluster center nodes. The query vector is obtained by converting a natural language query request using a vector encoding model. For the first The center vector corresponding to the top-level cluster center node The preset number of nearest cluster centers to be selected. The closest one obtained by retrieval A set of top-level cluster center nodes to The cluster center numbers are arranged in ascending order of distance.

[0133] The first retrieval method operates only within the tenant's own subtree layer. Limited by the clustering granularity and data distribution of the tenant's subtree, it may miss relevant knowledge units due to vector allocation biases. The second retrieval method starts from the top-level cluster center of the global layer, overcoming the local limitations of the tenant's subtree. It matches the clusters most relevant to the query intent in the global semantic space, recalling potentially missed knowledge units within the tenant's subtree. After merging and deduplicating the results from both methods, the high efficiency of the first retrieval method and the comprehensiveness of the second method are simultaneously achieved, significantly improving the overall recall and accuracy.

[0134] The value of N is determined by the balance between retrieval performance and recall. It is usually set based on the total number of global cluster centers, the overall size of the knowledge base, and retrieval latency requirements, with a typical range of 3-10. In practical implementation, it can be optimized through offline testing: gradually increase the value of N, simultaneously observe the changes in recall rate and retrieval time, and determine the final value of N when the increase in recall rate tends to level off and the retrieval time does not exceed the preset latency threshold; it can also be dynamically adapted according to the size of the tenant's knowledge base, with a larger tenant corresponding to a more appropriate increase in the value of N.

[0135] S440, calculate the similarity between the query vector and the knowledge unit vector of the filtered knowledge unit, filter out the filtered knowledge units with similarity exceeding the second preset threshold as the second recall set, merge the first recall set and the second recall set and remove duplicates to obtain the recall knowledge unit set.

[0136] like Figure 6 As shown, generating structured query results containing execution parameters specifically includes:

[0137] S510, extract the units with the knowledge type label as example class from the recalled knowledge unit set, and write the historical natural language questions and corresponding historical structured query statements attached to them into the first paragraph of the prompt words according to the preset reference query format;

[0138] S520: Extract units labeled "Terminology" from the recalled knowledge unit set, and write the text content of the standardized knowledge units within them into the second paragraph of the prompt word according to the preset business knowledge format.

[0139] Distinguishing between examples and terms and writing them in separate paragraphs serves two purposes.

[0140] Firstly, to adapt to the contextual understanding logic of large language models, reference query examples and business terminology knowledge are placed separately, conforming to the model's reading habits of prompt words. This allows the model to first establish a grammatical paradigm by referring to the query writing methods of similar questions, and then supplement semantic information by combining business terminology knowledge, thereby improving the accuracy and grammatical compliance of generated structured query statements.

[0141] Secondly, to decouple the functions of different types of knowledge, exemplary knowledge serves as a "grammatical example," providing sentence structure references and writing standards for the model, reducing the probability of grammatical errors in generated statements; terminological knowledge serves as a "semantic explanation," providing definitions of business fields and concepts for the model, ensuring that generated query statements conform to business semantic logic. Thirdly, it facilitates flexible control over prompt word content, allowing for flexible adjustment of the recall quantity and injection priority of the two types of knowledge according to the query scenario, avoiding the mixing of different types of knowledge that could lead to model attention dispersion, and improving the stability and consistency of generated results.

[0142] S530, obtain the database table structure of the target database and the preset de-identified sample data, and write the database table structure into the third paragraph of the prompt word according to the preset database mode format, and write the de-identified sample data into the fourth paragraph of the prompt word according to the preset data sample format.

[0143] S540, the first paragraph, the second paragraph, the third paragraph and the fourth paragraph are combined to form a complete prompt word, and the large language model generates a structured query result, which includes the structured query statement text and execution parameters.

[0144] like Figure 7 As shown, the step of verifying whether the target database dialect contains a compliant pagination clause specifically includes:

[0145] S610 receives the structured query results generated by the large language model and extracts the structured query statement text and execution parameters. Based on the tenant identifier of the target tenant, it locates the index metadata record of the corresponding tenant subtree layer and reads the pre-stored pagination verification rules and the dialect type of the target database as the execution basis for this pagination verification.

[0146] S620, based on the confirmed target database dialect type, and based on the pagination syntax regular expression in the pagination verification rules, perform full statement regular expression matching on the extracted structured query statement text, verify whether the statement contains a pagination clause that conforms to the pagination syntax regular expression, and at the same time verify whether the parameter placeholders of the pagination clause correspond one-to-one with the pagination parameters in the execution parameters.

[0147] S630, if the regular expression matching detects a pagination clause that conforms to the pagination syntax regular expression, and the structure of the pagination clause conforms to the structural requirements of the compliant pagination clause in the pagination validation rules, then the pagination validation is determined to be successful. The execution parameters are filled into the corresponding placeholders in the structured query statement text one by one to generate a parameterized executable structured query statement, which is then submitted to the target database for execution. If the execution is successful, the query result is returned; if the execution fails, the execution failure information is returned.

[0148] S640, if no pagination clause matching the pagination syntax regular expression is detected by regular expression matching, it is determined that the pagination validation fails, a pagination validation failure message is generated, and the pagination validation failure message along with the structured query statement text is returned to the large language model, triggering the regeneration process of the structured query statement.

[0149] Returning pagination validation failure information to the large language model for regeneration leverages the model's error correction capabilities. Specific pagination errors are fed back to the model, allowing it to specifically correct statements based on the error descriptions and supplement pagination clauses that conform to the corresponding dialect. This automatically fixes grammatical defects without manual intervention, improving query generation success rates. This approach forms a closed-loop mechanism of "generation-validation-retry," enabling the model to quickly adapt to the target database's pagination syntax requirements through real-time error feedback. Compared to single-generation, this significantly reduces the probability of missing pagination clauses and pagination syntax errors, ensuring that the final executed query conforms to the database dialect and avoiding database performance overload and data security risks caused by full queries without pagination. Thirdly, by accumulating optimization samples through error feedback, the model's adaptation to the tenant's database dialect is indirectly improved, reducing the frequency of similar errors in the long run.

[0150] like Figure 8 As shown, the step of re-feeding the failure information along with the original prompt words into the large language model for retrying, and monitoring the tenant index data volume and query frequency during operation, specifically includes:

[0151] S710 receives the execution failure information returned by the target database, extracts the failure reason description and corresponding error code, combines the execution failure information, the structured query statement text and the original complete prompt words into retry prompt words, and sends them into the large language model to regenerate the structured query results;

[0152] S720: Repeat the pagination validation and database submission process using the regenerated structured query results. If the number of consecutive retries exceeds the preset retrieval threshold and still fails, terminate the current query process, return a query execution failure message to the target tenant, and end the current natural language query request.

[0153] Setting a retry threshold can prevent resource waste and query timeouts caused by infinite retries. When a query statement has a fundamental error that cannot be fixed by retries, the process is terminated in time to prevent continuous consumption of large language model computing power and database connection resources, thus ensuring the overall service availability of the system.

[0154] Controlling the response time of a single query and limiting the number of retries within a reasonable range avoids excessive query latency caused by multiple retries, ensuring a good query experience for tenants. Thirdly, it prevents error amplification and system cascading failures. If a batch of erroneous queries occur, unlimited retries will exponentially amplify the system load. Setting a threshold can block the error propagation effect and ensure system stability. Furthermore, the retry threshold can also serve as a statistical indicator of query quality, used to identify frequently failing query scenarios and assist in the subsequent optimization of knowledge base content and suggestion word templates.

[0155] S730 collects the total number of knowledge unit vectors contained in the tenant subtree layer of each tenant and the frequency of natural language query requests of each tenant per unit time according to the preset statistical period, and compares them with the corresponding preset thresholds to determine whether they exceed the threshold range.

[0156] Calculation of query frequency per unit time:

[0157] ;

[0158] Threshold exceeds the judgment condition:

[0159] ;

[0160] in: For the first Frequency of natural language query requests per tenant per unit of time For the first time in the statistical period The total number of natural language query requests received by each tenant. The preset statistical period duration, For the first The determination result of whether the metric of an individual tenant exceeds the threshold range. For the first time in the statistical period The total number of knowledge unit vectors contained in the tenant subtree layer of each tenant. The preset threshold for the number of knowledge units, This is a preset query frequency threshold. This indicates that the threshold range has been exceeded. This indicates that the threshold range has not been exceeded.

[0161] S740, when the total number of knowledge unit vectors and the frequency of natural language query requests for any tenant both exceed the corresponding preset thresholds, the tenant subtree layer of that tenant is completely stripped from the corresponding mount node of the globally shared hierarchical clustering index.

[0162] S750 separates all knowledge unit vectors of the stripped tenant and pagination validation rules into a single tenant-specific index, while retaining the reference records of the tenant's knowledge unit vectors at the global layer.

[0163] Retaining global layer reference records ensures the continued effectiveness of the two-way retrieval mechanism. After a tenant index is independently split, the second-way global retrieval can still locate the relevant knowledge unit of that tenant through the global layer reference records, maintaining the original two-way recall logic and avoiding a decrease in recall rate due to the failure of the global retrieval path after the split. Simultaneously, it maintains the stability of global semantic clustering. Retaining reference records prevents semantic shifts in the global cluster center after tenant data is stripped, eliminating the need to re-execute global clustering, reducing the impact of index splitting on the global index structure, and reducing index maintenance overhead. Thirdly, it enhances the disaster recovery and scalability of the index system. When a tenant-specific index fails, the data access path can be quickly restored through global layer references; at the same time, it reserves a data foundation for subsequent cross-tenant global retrieval, global knowledge recommendation, and other expansion capabilities.

[0164] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0165] The embodiments described above are merely illustrative of several implementations of the present invention, and while the descriptions are specific and detailed, they should not be construed as limiting the scope of the present invention. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of the present invention, and these modifications and improvements all fall within the scope of protection of the present invention. Therefore, the scope of protection of this patent should be determined by the appended claims.

[0166] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention should be included within the protection scope of the present invention.

Claims

1. A multi-tenant knowledge base retrieval method based on shared vectors and paragraph aggregation, characterized in that, The method includes: Receive natural language query requests from target tenants, determine whether a database query is needed using a large language model, return a natural language response if not, and proceed with the retrieval process if yes; Obtain the original text and historical query data of the target tenant's knowledge base, and aggregate semantically related paragraphs and query-structured statements into standardized knowledge units with tenant identifiers and knowledge type labels; A globally shared hierarchical clustering index is constructed, which includes a global layer and tenant subtree layers mounted under each node of the global layer. The global layer consists of a clustering center node and associated global knowledge units. Each tenant subtree layer stores the knowledge unit vector and pagination verification rules of the corresponding tenant. Based on the tenant identifier, two independent searches are performed from the global level and the tenant subtree level of the index to recall semantically matched knowledge units as search results; The search results are injected into the database according to knowledge type and format. The prompt words are constructed by combining the database table structure and the de-identified sample data. The prompt words are then sent to the large language model to generate structured query results containing execution parameters. Perform multi-dialect pagination forced validation on structured query statements. Based on pagination validation rules, validate whether the target database contains compliant pagination clauses according to its dialect. After the validation passes, execute in a parameterized manner. If the execution fails, the failure message along with the original prompt will be fed back into the large language model for retry. During operation, the amount of tenant index data and the frequency of queries will be monitored. When the preset threshold is exceeded, the knowledge unit vector and pagination verification rules of the tenant subtree layer will be separated into independent indexes.

2. The method according to claim 1, characterized in that, The process of receiving a natural language query request from the target tenant and determining whether a database query is needed using a large language model specifically includes: A pre-defined function tool for querying a database is injected into a large language model. The function tool includes a function name, a function description, and parameter definitions. The function description is used to inform the large language model that the tool is used to perform a database query, and the parameter definitions include a database identifier and query condition placeholders. The natural language query request is sent as a user message to the large language model, which then decides whether to invoke the function tool based on the function tool itself. If the large language model returns a tool call result and the tool call result indicates that a database query is required, then it is determined to be yes and the retrieval process begins. If the large language model returns a natural language response and does not contain a tool call instruction, then it is determined to be no and a natural language response is returned.

3. The method according to claim 2, characterized in that, The process of pairing and aggregating semantically related paragraphs and query-structured statements into standardized knowledge units with tenant identifiers and knowledge type labels specifically includes: Obtain the original text of the knowledge base of the target tenant, extract the semantic structure information of the title level and paragraph boundary of the original text of the knowledge base, and divide the original text of the knowledge base into an initial text fragment set. Input the adjacent text fragments in the initial text fragment set into the vector encoding model to obtain their respective first fragment vector and second fragment vector. The cosine similarity between the first segment vector and the second segment vector is calculated as the semantic similarity. Adjacent text segments with semantic similarity exceeding a first preset threshold are merged into standardized knowledge units, and corresponding knowledge unit vectors are generated. The tenant identifier and paragraph boundary offset of the target tenant are associated with the standardized knowledge unit. Obtain the historical query data of the target tenant, which includes historical natural language questions and corresponding historical structured query statements. Input the historical natural language questions into a vector encoding model to obtain question vectors. Based on the similarity between the question vector and the knowledge unit vector of each standardized knowledge unit, the historical natural language question and the corresponding historical structured query statement are attached to the standardized knowledge unit with the highest semantic similarity. The knowledge type label of the standardized knowledge unit is assigned to the example class, and the knowledge type label of the standardized knowledge unit that has not been attached to any historical natural language question is assigned to the terminology class.

4. The method according to claim 3, characterized in that, The construction of the globally shared hierarchical clustering index specifically includes: Global hierarchical clustering is performed on the knowledge unit vectors of all standardized knowledge units to generate a set of top-level cluster center nodes. Each top-level cluster center node contains a center vector and a list of global knowledge unit references covered by the node. The global knowledge units are standardized knowledge units mounted to the global layer. The set of top-level cluster center nodes and the child node hierarchy under each top-level cluster center node constitute the global layer. The knowledge unit vectors of each standardized knowledge unit are assigned to the nearest top-level cluster center node according to their vector space distance from each top-level cluster center node, and the standardized knowledge units are added to the global knowledge unit reference list of the top-level cluster center node. When the number of references in the global knowledge unit reference list under any top-level cluster center node exceeds the preset node capacity threshold, local recursive clustering is performed on all global knowledge unit references under the top-level cluster center node to generate new sub-layer cluster nodes. Based on the tenant identifier associated with each standardized knowledge unit, the knowledge unit vectors of standardized knowledge units belonging to the same tenant are combined into the tenant subtree layer under each cluster center node of the global layer. The tenant subtree layer is stored as the index metadata record of the tenant. The index metadata record includes the tenant identifier, a reference list pointing to the paths of all knowledge unit vectors of the tenant in the cluster center node, and the number of knowledge unit vectors of the tenant under each cluster center node path and the pagination verification rules. Obtain the dialect type of the target database of the target tenant, select the corresponding pagination validation rule from the preset dialect pagination rule library according to the dialect type, and write the pagination validation rule into the index metadata record of the target tenant as the pagination validation rule of the tenant. The pagination validation rule includes the pagination syntax regular expression corresponding to the database dialect and the structural requirements of the compliant pagination clause.

5. The method according to claim 4, characterized in that, The process of performing two independent searches and recalling semantically matched knowledge units as search results specifically includes: The natural language query request is converted into a query vector using a vector encoding model. The index metadata record of the target tenant is located from each tenant subtree layer based on the tenant identifier of the target tenant. The tenant subtree layer of the target tenant is extracted as the first retrieval range according to the reference list in the index metadata record. Within the first retrieval range, the similarity between the query vector and each knowledge unit vector is calculated, and knowledge units with similarity exceeding a second preset threshold are selected as the first recall set. Starting from the query vector, retrieve the N nearest top-level cluster center nodes in the set of top-level cluster center nodes in the global layer. Use the global knowledge unit reference list covered by the N top-level cluster center nodes as the second retrieval range. Within the second retrieval range, filter out the filtered knowledge units belonging to the target tenant based on the tenant identifier. Calculate the similarity between the query vector and the knowledge unit vector of the filtered knowledge unit, select the filtered knowledge units whose similarity exceeds the second preset threshold as the second recall set, merge the first recall set and the second recall set and remove duplicates to obtain the recall knowledge unit set.

6. The method according to claim 5, characterized in that, The generation of structured query results containing execution parameters specifically includes: Extract units with the knowledge type label "Example" from the recalled knowledge unit set, and write the historical natural language questions and corresponding historical structured query statements attached to them into the first paragraph of the prompt words according to the preset reference query format. Extract units with the knowledge type label "terminology" from the recalled knowledge unit set, and write the text content of the standardized knowledge units in the set into the second paragraph of the prompt word according to the preset business knowledge format. Obtain the database table structure of the target database and the preset anonymized sample data, and write the database table structure into the third paragraph of the prompt word according to the preset database pattern format, and write the anonymized sample data into the fourth paragraph of the prompt word according to the preset data sample format. The first, second, third, and fourth paragraphs are combined to form a complete prompt word, and the large language model generates a structured query result, which includes the structured query statement text and execution parameters.

7. The method according to claim 6, characterized in that, The step of checking whether the target database dialect contains compliant pagination clauses specifically includes: Receive the structured query results generated by the large language model and extract the structured query statement text and execution parameters. Locate the index metadata record of the corresponding tenant subtree layer based on the tenant identifier of the target tenant, and read the pre-stored pagination verification rules and the dialect type of the target database as the execution benchmark for this pagination verification. Based on the confirmed target database dialect type, and based on the pagination syntax regular expression in the pagination validation rules, the extracted structured query text is subjected to full statement regular expression matching to verify whether the statement contains a pagination clause that conforms to the pagination syntax regular expression. At the same time, it is verified whether the parameter placeholders of the pagination clause correspond one-to-one with the pagination parameters in the execution parameters. If the regular expression matching detects a pagination clause that matches the pagination syntax regular expression, and the structure of the pagination clause meets the structural requirements of the compliant pagination clause in the pagination validation rules, then the pagination validation is deemed successful. The execution parameters are then filled into the corresponding placeholders in the structured query statement text one by one to generate a parameterized executable structured query statement, which is then submitted to the target database for execution. If the execution is successful, the query result is returned; if the execution fails, the execution failure information is returned. If no pagination clause matching the pagination syntax regular expression is detected by regular expression matching, the pagination validation is determined to be unsuccessful, a pagination validation failure message is generated, and the pagination validation failure message, along with the structured query statement text, is returned to the large language model, triggering the regeneration process of the structured query statement.

8. The method according to claim 7, characterized in that, The step of re-feeding the failure information along with the original prompt words back into the large language model for retrying, and monitoring the tenant index data volume and query frequency during operation, specifically includes: Receive execution failure information returned by the target database, extract the failure reason description and corresponding error code, combine the execution failure information, structured query statement text and the original complete prompt words into retry prompt words, and send them into the large language model to regenerate the structured query results; The pagination validation and database submission process is repeated using the regenerated structured query results. If the number of consecutive retries exceeds the preset retrieval threshold and still fails, the current query process is terminated, a query failure message is returned to the target tenant, and the current natural language query request is ended. The total number of knowledge unit vectors contained in the tenant subtree layer of each tenant and the frequency of natural language query requests of each tenant per unit time are collected according to the preset statistical period and compared with the corresponding preset thresholds to determine whether they exceed the threshold range. When the total number of knowledge unit vectors and the frequency of natural language query requests for any tenant both exceed the corresponding preset thresholds, the tenant subtree layer of that tenant will be completely stripped from the corresponding mount node of the globally shared hierarchical clustering index. Each tenant's knowledge unit vector, after being stripped, is independently constructed into a single-tenant-specific index along with the pagination validation rules, while retaining the reference records of that tenant's knowledge unit vector at the global level.