A large language model tool invocation method based on multi-stage self-adaption and hard negative sample contrastive learning

By employing multi-stage adaptive and hard negative sample contrastive learning methods, this approach addresses the inefficiencies and inaccurate matching caused by excessive search space and semantic diversity in large-scale tool libraries, achieving efficient and accurate tool invocation and coherence in multi-turn dialogues.

CN122331990APending Publication Date: 2026-07-03HANGZHOU JUNTONG FUTURE TECHNOLOGY CO LTD

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
HANGZHOU JUNTONG FUTURE TECHNOLOGY CO LTD
Filing Date
2026-01-30
Publication Date
2026-07-03

AI Technical Summary

Technical Problem

The large search space caused by large-scale tool libraries, insufficient semantic diversity and robustness of embedding vectors, poor adaptability of data flow variation and cluster centers, and insufficient utilization of contextual information in multi-turn dialogues lead to inefficiency and inaccurate matching of existing large language model tool calling methods.

Method used

Employing a multi-stage adaptive and hard negative sample contrastive learning method, this approach combines contrastive learning training, hierarchical clustering, and adaptive updating of cluster centers with a large language model to generate standardized description vectors. It also dynamically adjusts the reinforcement rate and introduces a context weighting mechanism with a time decay factor to achieve efficient and accurate tool invocation.

Benefits of technology

It significantly improves the accuracy and efficiency of the tool invocation process, reduces response time, and enhances the coherence and user experience in multi-turn dialogues.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122331990A_ABST
    Figure CN122331990A_ABST
Patent Text Reader

Abstract

This invention discloses a method for invoking tools from a large language model based on multi-stage adaptive and hard-negative sample contrastive learning. The method includes: encoding user queries, tool descriptions, and server text descriptions into vectors and performing L2 normalization, followed by contrastive learning training; pruning the processed server and tool vectors using hierarchical clustering to obtain cluster prototype vectors, with adaptive updates to cluster centers during the hierarchical clustering process; generating standardized description vectors for the cluster prototype vectors using the large language model based on the user query vectors; dynamically adjusting the enhancement rate based on the semantic similarity between the standardized description vectors and the cluster prototype vectors; calculating historical relevance scores and a final matching score; and selecting the optimal output based on the final matching score. This invention enables large language models to efficiently and accurately select and invoke appropriate tools from a large-scale tool library.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of artificial intelligence and natural language processing technology, specifically relating to a method for calling a large language model tool based on multi-stage adaptive and hard negative sample contrastive learning. Background Technology

[0002] The rapid development of Large Language Models (LLMs) has demonstrated their strong application potential in various fields. However, to achieve efficient and reliable invocation of external functions, especially when dealing with large-scale tool libraries, the following inherent and increasingly prominent technical challenges are faced:

[0003] 1. Large search space due to large-scale tool libraries: With the development of the large language model ecosystem, the number of external tools has increased dramatically, leading to the rapid emergence of large-scale tool libraries. Existing tool invocation methods are inefficient when searching within such a vast search space, resulting in increased latency.

[0004] 2. Insufficient Semantic Diversity and Robustness of Embedded Vectors: Descriptions of external tools and user queries may exhibit high semantic diversity and complexity. Traditional vector representation methods or contrastive learning mechanisms lack sufficient robustness in handling these complex or ambiguous semantics, making accurate semantic matching and differentiation difficult and affecting the accuracy of tool invocation.

[0005] 3. Data Flow Variation and Poor Adaptability of Cluster Centers: External tool libraries may be constantly updated, or the distribution of user queries may change over time, leading to data flow variation. Traditional clustering methods or prototype models struggle to adaptively update to handle such long-term changes, causing cluster centers to fail to stably adapt to new data distributions, thus affecting the efficiency and quality of candidate set pruning.

[0006] 4. Insufficient utilization of contextual information in multi-turn dialogues: In multi-turn dialogue scenarios involving tool calls, existing methods often tend to process the current query in isolation. They struggle to effectively utilize relevant tool or server information already present in the dialogue history, resulting in a lack of coherence in tool calls during continuous interactions and reduced matching accuracy.

[0007] The present invention aims to overcome the above-mentioned defects of the prior art by constructing a tool invocation method based on a multi-stage dynamic adaptive model, which achieves efficient retrieval and accurate matching between LLM and large-scale external tool libraries, thereby significantly improving the accuracy, efficiency and reliability of tool invocation. Summary of the Invention

[0008] This invention proposes a tool invocation method for large language models based on multi-stage adaptive and hard-negative sample contrastive learning. It aims to address the inefficiencies and inaccurate matching issues caused by the excessively large search space in existing large language model (LLM) tool invocation methods when the number of tools is large and semantically diverse. The invention includes the following main steps:

[0009] S1: Encode user queries, tool and server text descriptions into vectors and perform L2 normalization. Then, perform comparative learning training on the processed vectors.

[0010] S2: The processed server vector and tool vector are pruned through hierarchical clustering to obtain cluster prototype vectors. During the hierarchical clustering process, the cluster centers are adaptively updated.

[0011] S3: Based on the user query vector, generate a standardized description vector for the cluster prototype vector using a large language model;

[0012] The enhancement rate is dynamically adjusted based on the semantic similarity between the standardized description vector and the cluster prototype vector. The standardized description vector includes candidate tool description vectors and candidate server description vectors.

[0013] Calculate the historical relevance score and the final matching score, and select the optimal output based on the final matching score.

[0014] In step S1, the text descriptions of user queries, tools, and servers are encoded into vectors and subjected to L2 normalization. The processed vectors are then subjected to comparative learning training.

[0015] Specifically, comparative learning training includes:

[0016] The robust edge-contrast learning loss is calculated and expressed as:

[0017]

[0018] in, It is a robust edge contrast learning loss. This is the L2-normalized embedding vector of the anchor sample in the current batch, where j is the sample index, k is the index of the comparison sample, and pos is the set of positive samples. yes The positive sample vectors, and Neg is the negative sample set. Cosine similarity is used to measure the degree of similarity between vectors. It is a temperature parameter used to adjust the sensitivity to differences in similarity. Represents batch size;

[0019] Calculate variance control loss , represented as:

[0020]

[0021] in, It is variance control loss. Variance is a statistic used to measure the dispersion of a set of numerical values. The set of cosine similarities between all n anchor points in a batch and their positive samples;

[0022] The dynamic negative sample generation loss is calculated by obtaining difficult negative samples that are semantically similar to the positive samples but of different categories by calculating the cosine similarity between the positive sample vector of the current batch and the negative samples in the negative sample set in each iteration. Dynamically select hard-bearing samples during training. , represented as:

[0023]

[0024]

[0025] in, It is the dynamic negative sample generation loss. It is a difficult sample to bear. It is an anchor point. yes Positive sample vectors, Cosine similarity is used to measure the degree of similarity between vectors. This is a hyperparameter, representing the required similarity between the positive and negative samples to be at least higher than the similarity between the negative and negative samples. It is the hinge loss function; loss only occurs when the value inside the square brackets is greater than 0.

[0026] By setting hyperparameters to balance the various losses, the final contrastive loss function is obtained, expressed as:

[0027]

[0028] in and The hyperparameters are used to perform comparative learning and training on the processed vectors through the final contrastive loss function.

[0029] In step S2, the processed server vector and tool vector are pruned through hierarchical clustering to obtain cluster prototype vectors. During the hierarchical clustering process, the cluster centers are adaptively updated.

[0030] Hierarchical clustering is implemented using a hierarchical Gaussian mixture model, which includes first clustering and filtering all server vectors, and then clustering and filtering the tool vectors corresponding to each server separately.

[0031] Adaptive updates involve weighting the impact of historical cluster centers based on a time decay factor when updating cluster centers:

[0032]

[0033] in, It is the average embedding of similar samples in the current batch. It is the variance of the loss values ​​in this batch. It's a hyperparameter. These are the cluster center vectors obtained after this iteration. It is the previously stored cluster center vector. It is the time decay factor, where δ is the decay rate and t is the time step.

[0034] In step S3, S3: Based on the user query vector, a standardized description vector is generated for the cluster prototype vector through a large language model. The enhancement rate is dynamically adjusted based on the semantic similarity between the standardized description vector and the cluster prototype vector. The standardized description vector includes candidate tool description vectors and candidate server description vectors.

[0035] Calculate the historical relevance score and the final matching score, and select the optimal output based on the final matching score.

[0036] Specifically, the dynamic adjustment of the enhancement rate includes:

[0037] Based on the semantic similarity relationship between the cluster prototype vectors and the standardized description vectors, the similarity results are subjected to function mapping and normalization.

[0038] Furthermore, when the number of cluster prototype vectors semantically close to the standardized description vector is greater than or equal to a preset threshold, the enhancement rate is increased; conversely, when the number of cluster prototype vectors semantically close to the standardized description vector is less than the preset threshold, the enhancement rate is decreased. The enhancement rate is expressed as:

[0039]

[0040] Where I is the enhancement rate, It's a hyperparameter. It is a measure of inconsistency between cluster prototype vectors, measuring the two best-matching cluster prototype vectors. and Distance metric between This is the user query vector, and k1 and k2 are related to the user query vector. The two closest candidate tool description vectors, It is a user query vector The vector of the tool cluster centers matched during the hierarchical pruning stage. It measures the user query vector The semantic similarity between its matched cluster prototype vectors It is a normalization function that normalizes the similarity values ​​to... between.

[0041] Specifically, calculating the historical relevance score includes:

[0042] Based on the current user query vector, extract historical server vectors and historical tool vectors that are semantically related to the user query vector from historical dialogue records;

[0043] The historical semantic similarity between the historical server vector and the historical tool vector and the current user query vector is calculated separately. A time decay factor is introduced to weight the historical semantic similarity at different time steps, so that the historical records that are closer to the current query in time have a higher influence weight.

[0044] The weighted historical semantic similarity is normalized to generate a historical relevance score, which is represented as:

[0045]

[0046] in, Here, q represents the historical relevance score, and q represents the current user query vector. It is cosine similarity, let the server and tool vectors of the i-th record in history be respectively , This is the time decay factor in this formula. The relative weights of servers and tools and L represents the total number of historical entries, numbered 1, 2, ..., L from oldest to most recent.

[0047] Specifically, calculating the final matching score includes:

[0048] Calculate the server matching score between the user query vector and the candidate server description vector, and the tool matching score between the user query vector and the candidate tool description vector, respectively.

[0049] The larger value between the server matching score and the tool matching score is selected as the base matching score. An enhancement rate is introduced to adjust the base matching score, thereby enhancing the ability to distinguish between candidate results with semantic differences or uncertainties.

[0050] Based on preset context weights, these weights, along with historical relevance scores, contribute to the base matching score. This comprehensive adjustment of historically relevant information from multiple rounds of dialogue yields the final matching score, expressed as:

[0051]

[0052]

[0053]

[0054] in, The final matching score is given by ω, where ω is the context weight. Represents server score. It is the embedding vector of the user query. It is the vector generated by the ideal description of the i-th candidate server. Represents instrumental scores, It is the embedding vector of the user query. It is the vector generated by the ideal description of the j-th candidate tool.

[0055] Compared with the prior art, the beneficial effects of the present invention are as follows:

[0056] 1. We employ contrastive learning with hard negative samples to construct a highly discriminative semantic foundation for highly similar tool descriptions;

[0057] 2. A dynamic clustering retrieval mechanism is designed to reduce the complexity of global search to cluster-level search, which greatly shortens the response time in tool library scenarios. At the same time, the cluster centers are dynamically adjusted with the input of new samples, avoiding retrieval failures caused by static indexes.

[0058] 3. By using a large language model to generate standardized description vectors, non-standard noise in the original API is eliminated. Combined with an adaptive inconsistency enhancement rate, the ambiguity of candidate results is quantified in real time. When the semantics of the best and second-best candidates are close, the decision logic is activated by increasing the enhancement rate, which significantly reduces the false trigger rate of edge intent.

[0059] 4. By introducing a context weighting mechanism with a time decay factor, the continuity of intent and referential connection in multi-turn dialogues are accurately captured, effectively balancing the relationship between user preferences and immediate needs. This makes the tool invocation process more in line with human continuous logical thinking and improves the level of intelligence in user experience. Attached Figure Description

[0060] Figure 1 This is a flowchart illustrating a method for calling a large language model tool based on multi-stage adaptive and hard negative sample contrastive learning, provided by the present invention. Detailed Implementation

[0061] This invention provides a method for tool invocation based on multi-stage adaptive and hard negative sample contrastive learning for large language models. This method aims to address the inefficiency and inaccurate matching problems caused by the excessively large search space in existing methods when dealing with a large number of tools with diverse semantics. To solve this problem, this invention introduces a model context protocol. This protocol, through the rational management and utilization of contextual information, such as user queries, dialogue history, and tool and server descriptions, enables the model to quickly and accurately select appropriate tools and services from a vast tool library. The core objective of the model context protocol is to ensure a precise match between tools and user needs by dynamically adjusting semantic information during the tool invocation process, thereby significantly improving the efficiency and accuracy of tool invocation.

[0062] The core stages of the method of this invention are as follows:

[0063] S1: Encode user queries, tool and server text descriptions into vectors and perform L2 normalization. Then, perform comparative learning training on the processed vectors.

[0064] S2: The processed server vector and tool vector are pruned through hierarchical clustering to obtain cluster prototype vectors. During the hierarchical clustering process, the cluster centers are adaptively updated.

[0065] S3: Based on the user query vector, generate a standardized description vector for the cluster prototype vector using a large language model;

[0066] The enhancement rate is dynamically adjusted based on the semantic similarity between the standardized description vector and the cluster prototype vector. The standardized description vector includes candidate tool description vectors and candidate server description vectors.

[0067] Calculate the historical relevance score and the final matching score, and select the optimal output based on the final matching score.

[0068] like Figure 1 As shown, in step S1, the text descriptions of user queries, tools, and servers are encoded into vectors and subjected to L2 normalization. The processed vectors are then subjected to comparative learning training.

[0069] Using a pre-trained sentence converter model to process user query q and server set and toolset To eliminate the influence of vector magnitude on similarity calculation, all vectors are L2 normalized and projected onto a unit hypersphere, making cosine similarity equivalent to vector dot product.

[0070]

[0071] This invention proposes a cohesive-separative contrastive training mechanism to enhance the robustness of embedding vectors in handling semantic diversity. This mechanism works collaboratively through the following two components:

[0072] The separation component minimizes the similarity between samples of different categories, ensuring that semantically different tools or servers are far apart in the embedding space, thereby avoiding false matches.

[0073] The cohesive part enhances the model's ability to identify similar tools or servers by maximizing the similarity between similar samples, ensuring that semantically similar tool descriptions or server texts are close in the embedding space.

[0074] Specifically, the robust edge contrast learning loss is calculated and expressed as:

[0075]

[0076] in, It is a robust edge contrast learning loss. This is the L2-normalized embedding vector of the anchor sample in the current batch, where j is the sample index, k is the index of the comparison sample, and pos is the set of positive samples. yes The positive sample vectors, and Neg is the negative sample set. Cosine similarity is used to measure the degree of similarity between vectors. It is a temperature parameter used to adjust the sensitivity to differences in similarity. Represents batch size;

[0077] Specifically, calculate the variance control loss. , represented as:

[0078]

[0079] in, It is variance control loss. Variance is a statistic used to measure the dispersion of a set of numerical values. The set of cosine similarities between all n anchor points in a batch and their positive samples;

[0080] Specifically, the dynamic negative sample generation loss is calculated by obtaining difficult negative samples that are semantically similar to the positive samples but of different categories by calculating the cosine similarity between the positive sample vector of the current batch and the negative samples in the negative sample set each time. Dynamically select hard-bearing samples during training. This is to improve the model's discriminative ability.

[0081]

[0082]

[0083] in, It is the dynamic negative sample generation loss. It is a difficult sample to bear. It is an anchor point. yes Positive sample vectors, Cosine similarity is used to measure the degree of similarity between vectors. This is a hyperparameter, representing the required similarity between the positive and negative samples to be at least higher than the similarity between the negative and negative samples. It is the hinge loss function; loss only occurs when the value inside the square brackets is greater than 0.

[0084] Furthermore, by setting hyperparameters to balance the various losses, the final contrastive loss function is obtained, expressed as:

[0085] in and is a hyperparameter used to balance various losses. This mechanism minimizes the similarity between samples of different classes and maximizes the similarity between samples of the same class, making the embedding vector more robust when dealing with complex or ambiguous semantics.

[0086] like Figure 1 As shown, in step S2, the processed server vector and tool vector are pruned by hierarchical clustering to obtain cluster prototype vectors. During the hierarchical clustering process, the cluster centers are adaptively updated.

[0087] Based on semantic similarity, servers and tools are pruned to quickly narrow the search space to a manageable range. To ensure that tool selection matches the server and provides accurate services, this invention dynamically adjusts the pairing of tools and servers in multi-turn dialogues through a context protocol.

[0088] Contextual information such as user queries, historical dialogues, tool and server descriptions is fully utilized. During the pruning phase, the system not only matches tools based on the current query semantics, but also considers the relationship and compatibility between tools and servers.

[0089] Specifically, GMM is used to cluster all server vectors, with the number of components set to [number missing]. =⌈ ⌉, M is the number of server vectors. Calculate the likelihood score of the query vector under each component. And select the one with the highest score. The clusters are divided into a preliminary pruned server set S′.

[0090]

[0091] in It is the probability density function of a Gaussian distribution. It is the cluster center and mean vector of the k-th cluster component. It is the covariance matrix of the k-th clustering component.

[0092] Tool-level clustering: For server sets that have undergone preliminary pruning Each selected server The associated tool vector Independently fit GMM, with the number of components set to . =⌈ Choose the one with the highest score. The tools are clustered to form a compact set of tool candidates T′.

[0093] Specifically, this invention introduces an adaptive update mechanism to ensure that cluster centers can continuously reflect data variations;

[0094] The adaptive update process is represented as:

[0095]

[0096] in, It is the average embedding of similar samples in the current batch. It is the variance of the loss values ​​in this batch. It's a hyperparameter. These are the cluster center vectors obtained after this iteration. It is the previously stored cluster center vector. δ is the time decay factor, where δ is the decay rate and t is the time step. This ensures that the influence of older data points on cluster centers will weaken over time, thus better adapting to new data distributions.

[0097] The adaptive update mechanism, based on the variance of the current batch of data and combined with a time decay factor, enables large language models to more robustly adapt to long-term changes in the data stream.

[0098] In this way, the large language model can adjust the cluster centers in a timely manner, avoid excessive influence from old data, and ensure that the clustering results are always consistent with the latest data distribution, thereby improving the accuracy of tool calls and matching; the selection of tools and servers is more in line with actual needs, can efficiently filter according to context, significantly reduce the search space, and ensure that the selected tool-server pair can accurately respond to user requests.

[0099] like Figure 1As shown, in step S3, based on the user query vector, a standardized description vector is generated for the cluster prototype vector through a large language model. The enhancement rate is dynamically adjusted based on the semantic similarity between the standardized description vector and the cluster prototype vector. The standardized description vector includes candidate tool description vectors and candidate server description vectors.

[0100] Calculate the historical relevance score and the final matching score, and select the optimal output based on the final matching score.

[0101] Specifically, based on the user query vector, the large language model is guided by prompt words to generate standardized descriptions that fit the current needs for the clustering prototype vectors, eliminating the expression differences between descriptions of different tools and different servers, and further transforming them into corresponding standardized description vectors through a preset embedding model. The standardized description vectors include candidate tool description vectors and candidate server description vectors.

[0102] In one embodiment, when the query is "find a suitable API for text analysis", the LLM generates a description such as "this tool supports large-scale text processing and has NLP capabilities" for each candidate tool, and generates corresponding performance and compatibility descriptions for the server. Based on these semantic descriptions rewritten by the LLM, the system can more accurately evaluate the matching degree between the server-tool combination and the user's needs, thereby completing the final optimal selection.

[0103] Specifically, to avoid over-enhancement in semantically clear scenarios and insufficient discrimination ability in semantically ambiguous scenarios, the enhancement rate I is dynamically adjusted by performing function mapping and normalization on the similarity results based on the semantic similarity relationship between the clustering prototype vector and the standardized description vector.

[0104] Furthermore, when the number of cluster prototype vectors semantically close to the standardized description vector is greater than or equal to a preset threshold, the matching result is ambiguous, so the enhancement rate is increased. Conversely, when the number of cluster prototype vectors semantically close to the standardized description vector is less than the preset threshold, the matching result is clear, so the enhancement rate is decreased. This enhances the ability of the candidate re-ranking stage to distinguish between differentiated matching results. This preset threshold does not represent all cases and can be modified according to the actual situation. The enhancement rate I is expressed as:

[0105]

[0106] Where I is the enhancement rate, It's a hyperparameter. It is a measure of inconsistency between cluster prototype vectors, measuring the two best-matching cluster prototype vectors. and Distance metric between This is the user query vector, and k1 and k2 are related to the user query vector. The two closest candidate tool description vectors, It is a user query vector The vector of the tool cluster centers matched during the hierarchical pruning stage. It measures the user query vector The semantic similarity between its matched cluster prototype vectors It is a normalization function that normalizes the similarity values ​​to... between;

[0107] Specifically, based on the current user query vector, historical server vectors and historical tool vectors that are semantically related to the user query vector are extracted from historical dialogue records;

[0108] The historical semantic similarity between the historical server vector and the historical tool vector and the current user query vector is calculated separately. A time decay factor is introduced to weight the historical semantic similarity at different time steps, so that the historical records that are closer to the current query in time have a higher influence weight.

[0109] The weighted historical semantic similarity is normalized to generate a historical relevance score, which is represented as:

[0110]

[0111] in, Here, q represents the historical relevance score, and q represents the current user query vector. It is cosine similarity, let the server and tool vectors of the i-th record in history be respectively , This is the time decay factor in this formula. The relative weights of servers and tools and L represents the total number of historical entries, numbered 1, 2, ..., L from oldest to most recent;

[0112] Specifically, the server matching score between the user query vector and the candidate server description vector, and the tool matching score between the user query vector and the candidate tool description vector are calculated respectively.

[0113] The larger value between the server matching score and the tool matching score is selected as the base matching score. An enhancement rate is introduced to adjust the base matching score, thereby enhancing the ability to distinguish between candidate results with semantic differences or uncertainties.

[0114] Based on preset context weights, these weights, along with historical relevance scores, contribute to the base matching score. This comprehensive adjustment of historically relevant information from multiple rounds of dialogue yields the final matching score, expressed as:

[0115]

[0116]

[0117]

[0118] in, The final matching score is given by ω, where ω is the context weight. Represents server score. It is the embedding vector of the user query. It is the vector generated by the ideal description of the i-th candidate server. Represents instrumental scores, It is the embedding vector of the user query. This is the vector generated by the ideal description of the j-th candidate tool. This mechanism enables the model to better utilize contextual information in multi-turn dialogues, improving the coherence and accuracy of tool calls.

[0119] The server-tool pair with the highest final match score is selected as the final output.

[0120] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of a necessary general-purpose hardware platform, or by a combination of hardware and software. Based on this understanding, the above technical solutions, in essence or the part that contributes to the prior art, can be embodied in the form of a computer product. The present invention can be implemented in the form of a computer program product on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0121] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and are not intended to limit it. Other embodiments may also be used. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. These modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A large language model tool invocation method based on multi-stage self-adaption and hard negative sample contrastive learning, characterized in that, Includes the following steps: S1: Encode user queries, tool and server text descriptions into vectors and perform L2 normalization. Then, perform comparative learning training on the processed vectors. S2: The processed server vector and tool vector are pruned through hierarchical clustering to obtain cluster prototype vectors. During the hierarchical clustering process, the cluster centers are adaptively updated. S3: Based on the user query vector, generate a standardized description vector for the clustering prototype vector using a large language model; The enhancement rate is dynamically adjusted based on the semantic similarity between the standardized description vector and the clustering prototype vector. The standardized description vector includes candidate tool description vectors and candidate server description vectors. Calculate the historical relevance score and the final matching score, and select the optimal output based on the final matching score.

2. The large language model tool invocation method based on multi-stage self-adaption and difficult negative sample contrastive learning according to claim 1, characterized in that, In step S1, the contrastive learning training includes: Calculate robust edge contrast learning loss , represented as: in, It is a robust edge contrast learning loss. This is the L2-normalized embedding vector of the anchor sample in the current batch, where j is the sample index, k is the index of the comparison sample, and pos is the set of positive samples. yes The positive sample vectors, and Neg is the negative sample set. Cosine similarity is used to measure the degree of similarity between vectors. It is a temperature parameter used to adjust the sensitivity to differences in similarity. Represents batch size; The variance control loss is calculated and expressed as: in, It is variance control loss. Variance is a statistic used to measure the dispersion of a set of numerical values. The set of cosine similarities between all n anchor points in a batch and their positive samples; The dynamic negative sample generation loss is calculated by obtaining difficult negative samples that are semantically similar to the positive samples but of different categories by calculating the cosine similarity between the positive sample vector of the current batch and the negative samples in the negative sample set each time. These difficult negative samples are dynamically selected during training, as follows: in, It is the dynamic negative sample generation loss. It is a difficult sample to bear. It is an anchor point. yes Positive sample vectors, Cosine similarity is used to measure the degree of similarity between vectors. This is a hyperparameter, representing the required similarity between the positive and negative samples to be at least higher than the similarity between the negative and negative samples. It is the hinge loss function; loss only occurs when the value inside the square brackets is greater than 0. By setting hyperparameters to balance the various losses, the final contrastive loss function is obtained, expressed as: in, and The hyperparameters are used to perform comparative learning training on the processed vector through the final comparative loss function.

3. The method for calling a large language model tool based on multi-stage adaptive and hard negative sample contrastive learning according to claim 1, characterized in that, In step S2 The hierarchical clustering is implemented using a hierarchical Gaussian mixture model, which includes first clustering and filtering all server vectors, and then clustering and filtering the tool vectors corresponding to each server. The adaptive update includes introducing a weighted average of the impact of historical cluster centers based on a time decay factor when updating cluster centers: in, It is the average embedding of similar samples in the current batch. It is the variance of the loss values ​​in this batch. It's a hyperparameter. These are the cluster center vectors obtained after this iteration. It is the previously stored cluster center vector. It is the time decay factor, where δ is the decay rate and t is the time step.

4. The method for calling a large language model tool based on multi-stage adaptive and hard negative sample contrastive learning according to claim 1, characterized in that, In step S3, the dynamic adjustment of the enhancement rate includes: Based on the semantic similarity relationship between the clustering prototype vector and the standardized description vector, the similarity results are subjected to function mapping and normalization.

5. The method for calling a large language model tool based on multi-stage adaptive and hard negative sample contrastive learning according to claim 4, characterized in that: When the number of cluster prototype vectors semantically close to the standardized description vector is greater than or equal to a preset threshold, the strengthening rate is increased; conversely, when the number of cluster prototype vectors semantically close to the standardized description vector is less than the preset threshold, the strengthening rate is decreased. The strengthening rate is expressed as: Where I is the enhancement rate, It's a hyperparameter. It is a measure of inconsistency between cluster prototype vectors, measuring the two best-matching cluster prototype vectors. and Distance metric between This is the user query vector, and k1 and k2 are related to the user query vector. The two closest candidate tool description vectors, It is a user query vector The vector of the tool cluster centers matched during the hierarchical pruning stage. It measures the user query vector The semantic similarity between its matched cluster prototype vectors It is a normalization function that normalizes the similarity values ​​to... between.

6. The method for calling a large language model tool based on multi-stage adaptive and hard negative sample contrastive learning according to claim 1, characterized in that, In step S3, calculating the historical relevance score includes: Based on the current user query vector, extract the historical server vector and historical tool vector that are semantically related to the user query vector from the historical dialogue records; The historical semantic similarity between the historical server vector and the historical tool vector and the current user query vector is calculated respectively. A time decay factor is introduced to weight the historical semantic similarity at different time steps, so that the historical records that are closer to the current query in time have a higher influence weight. The weighted historical semantic similarity is normalized to generate a historical relevance score, which is expressed as: in, Here, q represents the historical relevance score, and q represents the current user query vector. It is cosine similarity, let the server and tool vectors of the i-th record in history be respectively , This is the time decay factor in this formula. The relative weights of servers and tools and L represents the total number of historical entries, numbered 1, 2, ..., L from oldest to most recent.

7. The method for calling a large language model tool based on multi-stage adaptive and hard negative sample contrastive learning according to claim 1, characterized in that, In step S3, calculating the final matching score includes: Calculate the server matching score between the user query vector and the candidate server description vector, and the tool matching score between the user query vector and the candidate tool description vector, respectively. The larger value between the server matching score and the tool matching score is selected as the base matching score. The enhancement rate is introduced to adjust the base matching score, thereby enhancing the ability to distinguish when there are semantic differences or uncertainties in the candidate results. Based on preset context weights, the historical relevance score, together with the historical relevance score, is used to comprehensively correct historical information in multi-turn dialogues, thereby obtaining the final matching score, expressed as: in, The final matching score is given by ω, where ω is the context weight. Represents server score. It is the embedding vector of the user query. It is the vector generated by the ideal description of the i-th candidate server. Represents instrumental scores, It is the embedding vector of the user query. It is the vector generated by the ideal description of the j-th candidate tool.