Priori knowledge optimization method of intelligent agent and related device
By acquiring the agent's execution trajectory and converting it into a tool call graph, the system prompts and skill documents of the agent are optimized, solving the problems of low efficiency and poor interpretability in the existing technology. This achieves efficient and interpretable prior knowledge optimization, improving the agent's output effect and success rate.
Patent Information
- Application Number
- CN202611683147.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-10-28
- Publication Date
- 2026-11-27
AI Technical Summary
The prior knowledge optimization of existing intelligent agents relies on human experience, which is inefficient and highly subjective. Reinforcement learning-based methods have poor interpretability and high model training costs.
By acquiring multiple execution trajectories of the agent under the current system prompts and skill documents, tool call chains are extracted, merged into a tool call graph, and statistical analysis of tool call patterns is performed to generate optimized system prompts and skill documents. Graph theory is then used to achieve optimization without human intervention.
It improves the output performance and execution success rate of the agent, with high optimization efficiency and good interpretability, and requires no model training cost.
Smart Images

Figure CN123180205A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of intelligent agent technology, and in particular to a method and related apparatus for optimizing prior knowledge of intelligent agents. Background Technology
[0002] The quality of context engineering for an agent directly impacts its output performance and execution success rate. Prior knowledge optimization is a part of context engineering, encompassing the optimization of the system cue set and / or skill document set. Currently, prior knowledge optimization primarily relies on manual optimization based on human experience or automated optimization methods based on reinforcement learning. The former suffers from low optimization efficiency and high subjectivity, while the latter suffers from poor interpretability and high model training costs. Summary of the Invention
[0003] This application provides a method and related apparatus for optimizing prior knowledge of intelligent agents.
[0004] This application provides a prior knowledge optimization method for an intelligent agent, comprising: acquiring multiple execution trajectories of the intelligent agent for the same problem under the current system prompt word set and the current skill document set; extracting tool call chains from each execution trajectory, wherein in the tool call chain, nodes represent tool identifiers, directed edges connect adjacent node pairs, and the weight of the directed edges represents the transition cost from the preceding node to the following node in the adjacent node pair; merging the tool call chains to obtain a tool call graph; statistically analyzing the tool call patterns in the tool call graph, wherein the tool call patterns include at least one of high-frequency nodes, high-frequency adjacent node pairs, high-frequency sub-paths, and the path with the minimum total transition cost; and generating at least one of the optimized system prompt word set and the optimized skill document set based on the tool call patterns.
[0005] This application provides a prior knowledge optimization device for an intelligent agent, comprising: an acquisition module, an extraction module, a merging module, a statistics module, and a generation module. Specifically: the acquisition module acquires multiple execution trajectories of the intelligent agent for the same problem under the current system prompt word set and the current skill document set; the extraction module extracts tool call chains from each execution trajectory, where nodes represent tool identifiers, directed edges connect adjacent node pairs, and the weight of the directed edges represents the transition cost from the preceding node to the following node in an adjacent node pair; the merging module merges the tool call chains to obtain a tool call graph; the statistics module statistically analyzes the tool call patterns in the tool call graph, where the tool call patterns include at least one of high-frequency nodes, high-frequency adjacent node pairs, high-frequency sub-paths, and the path with the minimum total transition cost; and the generation module generates at least one of the optimized system prompt word set and the optimized skill document set based on the tool call patterns.
[0006] This application provides an electronic device, including a memory and a processor, wherein the processor is used to execute program instructions stored in the memory to implement the above-described method.
[0007] This application provides a computer-readable storage medium having program instructions stored thereon, which, when executed by a processor, implement the above-described method.
[0008] This application provides a computer program product, including a computer program that, when executed by a processor, implements the above-described method.
[0009] The above scheme obtains multiple execution trajectories of the agent under the current system prompt word set and the current skill document set. It then extracts tool call chains from each execution trajectory, merges these chains into a tool call graph, and optimizes at least one of the current system prompt word set and the current skill document set based on the tool call patterns in the graph. On one hand, it can convert the agent's historical execution trajectories (unstructured) for the same problem into a quantifiable tool call graph (structured). The quantified tool call patterns in the graph are then fed back to at least one of the current system prompt word set and the current skill document set, optimizing prior knowledge, improving its quality, and thus enhancing the agent's output and execution success rate. On the other hand, since no human intervention is required, the optimization efficiency is high and subjectivity is overcome. Because the process of "execution trajectory → tool call chain → tool call graph → tool call pattern" is implemented based on graph theory, it is interpretable and requires no training, thus offering good interpretability and eliminating model training costs.
[0010] It should be understood that the above general description and the following detailed description are exemplary and explanatory only, and are not intended to limit this application. Attached Figure Description
[0011] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with this application and, together with the specification, serve to explain the technical solutions of this application.
[0012] Figure 1 This is a flowchart illustrating an embodiment of the prior knowledge optimization method for intelligent agents provided in this application; Figure 2 This is a flowchart illustrating another embodiment of the prior knowledge optimization method for intelligent agents provided in this application; Figure 3 This is a flowchart illustrating a specific example of the prior knowledge optimization method for intelligent agents provided in this application. Figure 4 This is a schematic diagram of the tool call diagram in this application; Figure 5This is a schematic diagram of the structure of an embodiment of the electronic device of this application; Figure 6 This is a schematic diagram of the structure of an embodiment of the computer-readable storage medium of this application. Detailed Implementation
[0013] The embodiments of this application will now be described in detail with reference to the accompanying drawings.
[0014] In the following description, specific details such as particular system architectures, interfaces, and technologies are presented for illustrative purposes rather than for limiting purposes, in order to provide a thorough understanding of this application.
[0015] In this document, the term "and / or" merely describes the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent three cases: A alone, A and B simultaneously, and B alone. Additionally, the character " / " generally indicates that the preceding and following related objects have an "or" relationship. Furthermore, "many" in this document means two or more. Additionally, the term "at least one" in this document means any combination of at least two of any one or more of a plurality of objects. For example, including at least one of A, B, and C can mean including any one or more elements selected from the set consisting of A, B, and C. Finally, the term "several" in this document means any integer greater than 0, such as 1, 2, 3, 4, 5, ...
[0016] Figure 1 This is a flowchart illustrating an embodiment of the prior knowledge optimization method for intelligent agents provided in this application. Figure 1 As shown, in this embodiment, the prior knowledge optimization method for an agent may include the following steps: S110: Obtain multiple execution trajectories of the agent for the same problem under the current system prompt word set and the current skill document set.
[0017] The execution subject in this embodiment is an electronic device, which can be any electronic device with the ability to optimize prior knowledge of an intelligent agent.
[0018] The agent's prompt consists of three layers: system prompt, user prompt, and assistant message. The user prompt carries the user's question.
[0019] The agent supports multiple problem types, each associated with a corresponding system prompt word used to define the decision constraints and behavioral guidelines for that problem type. Some problem types are also associated with skill documents, which contain a sequence of steps for that problem type. This sequence includes descriptions of multiple steps, each including the name of the tool used, a summary of the tool's parameters, and precautions. For example, the skill document might include a tool call sequence like {Step 1 (Tool 1, Parameter Summary, Precautions), Step 2 (Tool 2, Parameter Summary, Precautions), Step 3 (Tool 3, Parameter Summary, Precautions)}.
[0020] The current system prompt word set contains current system prompt words for all question types supported by the agent. The current skill document set contains skill documents (Agent Skills) for all question types supported by the agent. If no skill documents are associated with any question type, the current skill document set is empty.
[0021] It can control the agent to repeatedly execute the same question multiple times under the current system prompt word set and the current skill document set, resulting in multiple execution trajectories.
[0022] The execution process of an intelligent agent facing the same problem can be roughly described as follows: After receiving the same problem, analyze the target problem type to which the same problem belongs; determine the target system prompt word of the target problem type from the current system prompt word set, and form the final prompt word using the user prompt word carrying the same problem, the target system prompt word, etc.; determine whether there is a target skill document of the target problem type in the current skill document set; if it exists, execute the task based on the final prompt word and the target skill document to obtain the task execution result; if it does not exist, execute the task based on the final prompt word to obtain the task execution result.
[0023] Each execution of an agent can form an execution trajectory. Since the large model on which the agent is based has randomness, the execution trajectories obtained in different executions may be different. The large model can be a large language model (LLM), etc.
[0024] S120: Extract tool call chains from each execution trajectory.
[0025] In the tool call chain, nodes represent the tool identifiers being called, and directed edges connect adjacent node pairs. The weight of the directed edge represents the transfer cost from the preceding node to the following node in the adjacent node pair.
[0026] Each execution path corresponds to a tool call chain. The transfer cost from an earlier node to a later node refers to the thinking cost incurred by the agent after obtaining the execution result of the earlier node in planning the next execution step and calling the later node. For example, in the adjacent node pair A→B, A is the earlier node and B is the later node. After the agent calls A to obtain the execution result of A, it thinks about B based on the execution result of A and the context. The transfer cost from A to B is the cost incurred by the agent in the thinking process.
[0027] S130: Merge all tool call chains to obtain a tool call graph.
[0028] The tool call graph contains multiple paths, which can be existing tool call chains before the merge or new tool call chains generated by the merge.
[0029] S140: Tool call patterns in the statistical tool call graph.
[0030] The tool invocation pattern includes at least one of the following: high-frequency nodes, high-frequency adjacent node pairs, high-frequency sub-paths, and the path with the minimum total transfer cost.
[0031] High-frequency nodes refer to nodes that appear more frequently than a preset frequency threshold across multiple paths. High-frequency adjacent node pairs refer to adjacent node pairs that appear more frequently than a preset frequency threshold. High-frequency sub-paths refer to sub-paths that appear more frequently than a preset frequency threshold across multiple paths. Sub-paths sequentially include at least three nodes. Total transition cost refers to the statistical result of the weights of each directed edge in the path; the statistical result can be the sum, mean, mode, median, etc. Minimizing the total transition cost means the highest execution efficiency and lowest resource consumption. Occurrence frequency refers to the number of times a node / adjacent node pair / sub-path appears in each path.
[0032] S150: Generate at least one of the following based on the tool call patterns: an optimized set of system prompts and an optimized set of skill documents.
[0033] In some embodiments, the current system prompts for the same question type in the current system prompt set can be optimized by utilizing tool call patterns to obtain an optimized system prompt set.
[0034] In some embodiments, the current set of skill documents can be optimized using tool invocation patterns to obtain an optimized set of skill documents. If there are target skill documents of the same question type in the current set of skill documents, the target skill documents can be modified using tool invocation patterns. If there are no target skill documents of the same question type in the current set of skill documents, target skill documents can be generated based on tool invocation patterns.
[0035] The above scheme obtains multiple execution trajectories of the agent under the current system prompt word set and the current skill document set. It then extracts tool call chains from each execution trajectory, merges these chains into a tool call graph, and optimizes at least one of the current system prompt word set and the current skill document set based on the tool call patterns in the graph. On one hand, it can convert the agent's historical execution trajectories (unstructured) for the same problem into a quantifiable tool call graph (structured). The quantified tool call patterns in the graph are then fed back to at least one of the current system prompt word set and the current skill document set, optimizing prior knowledge, improving its quality, and thus enhancing the agent's output and execution success rate. On the other hand, since no human intervention is required, the optimization efficiency is high and subjectivity is overcome. Because the process of "execution trajectory → tool call chain → tool call graph → tool call pattern" is implemented based on graph theory, it is interpretable and requires no training, thus offering good interpretability and eliminating model training costs.
[0036] Figure 2 This is a flowchart illustrating another embodiment of the prior knowledge optimization method for intelligent agents provided in this application. The execution trajectory sequentially includes multiple tool call records, each containing tool identifier representation parameters and transition cost representation parameters. Figure 2 As shown, in this embodiment, S120 includes: S121: Extract the tool identifier representation parameters and the transfer cost representation parameters from each tool call record respectively.
[0037] The tool identifier represents the parameter that represents the tool identifier, and the transfer cost represents the parameter that represents the transfer cost.
[0038] The transfer cost representation parameter may include at least one of execution latency and token cost. Execution latency refers to the delay incurred during the agent's thinking process. Token cost refers to the number of input and output tokens consumed during the agent's thinking process. The tool identifier representation parameter may include at least one of the tool name and parameter details.
[0039] S122: Determine each tool identifier based on the corresponding tool identifier representation parameters, and treat each tool identifier as a node.
[0040] S123: Based on each transfer cost characterization parameter, determine the transfer cost from each node to its adjacent subsequent nodes.
[0041] For each node, it is considered as the preceding node, and the transition cost of its adjacent following nodes is determined based on its corresponding transition cost representation parameter. If it has no following nodes, the transition cost is 0.
[0042] S124: Connect adjacent node pairs using directed edges, and use the transition cost from the preceding node to the following node in the adjacent node pair as the weight of the directed edge.
[0043] In some embodiments, the transfer cost characterization parameters include execution latency and lexical consumption. S123 includes: determining the transfer cost from the corresponding node to its adjacent subsequent nodes based on the execution latency and lexical consumption, wherein the transfer cost is positively correlated with the execution latency and lexical consumption.
[0044] Among them, the method of determining the transfer cost based on execution delay and lexical consumption can be, but is not limited to, weighted methods.
[0045] In some embodiments, before determining the transfer cost based on execution latency and lexical consumption, the execution latency and lexical consumption of each node in the tool call chain can be normalized separately.
[0046] In some embodiments, the transition cost of a node is a weighted sum of the normalized execution latency and lexical consumption of the corresponding node. The calculation formula is as follows: M=β×(normalized_latency)+γ×(normalized_token_cost).
[0047] Where M is the transfer cost, normalized_latency is the normalized execution latency, normalized_token_cost is the normalized token cost, and β and γ are weights, β+γ=1, which can be set according to the actual application scenario, for example, β=0.5 and γ=0.5.
[0048] The above scheme can perform a quantitative cost-benefit analysis of nodes from the dimensions of execution latency and lexical consumption, and obtain the node transfer cost.
[0049] In some embodiments, the tool identifier represents parameters including the tool name and parameter details. S122 includes: obtaining a parameter summary based on the parameter details; mapping the tool name and the parameter summary to obtain the tool identifier.
[0050] In particular, generating a unique identifier by combining the tool name and parameter summary can ensure the uniqueness of the tool identifier.
[0051] In some embodiments, the tool identifier can be obtained by hashing the tool name and parameter digest. For example, Node_ID = hash(tool_name + '|' + parameter_signature), where Node_ID is the tool identifier, tool_name is the tool name, and parameter_signature is the parameter digest.
[0052] In some embodiments, S130 includes: in response to the same directed edge appearing in different tool call chains, merging the weights of the same directed edge in the different tool call chains to obtain the weight of the same directed edge in the tool call graph.
[0053] The weight of the same directed edge in the tool call graph can be the maximum, minimum, median, mode, mean, etc. of the weights in different tool call chains.
[0054] In some embodiments, the fusion process includes averaging the weights of the same directed edge in different tool call chains to obtain the weight of the same directed edge in the tool call graph.
[0055] For example, suppose there are three tool call chains. The weight of the same directed edge is 10 in tool call chain 1, 20 in tool call chain 2, and 15 in tool call chain 3. The average of these three weights is the weight in the tool call graph, which is (10+20+15) / 3=15.
[0056] The above scheme merges the weights of the same directed edge in different tool call chains, which can eliminate random noise weights in a single tool call chain, making the weights in the resulting tool call graph more stable and representative.
[0057] In some embodiments, S140 includes: searching for the path with the minimum total transfer cost in the tool call graph, where the total transfer cost is a statistical result of the weights of each directed edge in the path. Based on this, S150 includes: in response to the existence of a target skill document in the current skill document set, modifying the target skill document according to the path with the minimum total transfer cost to obtain an optimized target skill document, where the target skill document is a skill document belonging to the same problem type; in response to the absence of a target skill document in the current skill document set, generating a target skill document according to the path with the minimum total transfer cost.
[0058] In the tool call graph, the starting node and ending node of the path are the same question and output, respectively.
[0059] The formula for calculating the search criteria is: ; in, The path with the minimum total transfer cost is s, where s is the starting node and t is the ending node. It is the set of all paths from s to t in the tool call graph, p is the path from s to t, e is the directed edge of p, and weight is the weight of the directed edge.
[0060] The search is implemented using a graph search algorithm, such as Dijkstra's algorithm or A... Algorithms (A* algorithm, etc.). Taking Dijkstra's algorithm as an example, its complexity is O(E log V), where V is the number of nodes in the tool call graph, E is the number of directed edges in the tool call graph, and the cumulative transition cost of the path being traversed is dist[v]=min(dist[v],dist[u]+weight(u->v)), where u is the node that has been reached, v is the next node to be reached, and weight(u->v) is the transition cost of node v (or the weight of the directed edge between u and v).
[0061] The target skill document can constrain the agent to prioritize calling tools according to the order in the target skill document when encountering the same problem type. For example, if the path with the lowest total transfer cost is "web search (web_search) -> read file (read_file) -> analyze (analyze)", the generated target skill document can stipulate that the agent should prioritize calling tools in this order when encountering the same problem type, and specify that the read_file tool must first obtain the file URL (Uniform Resource Locator) through web_search before calling it.
[0062] The above scheme automatically searches the tool call graph for the path with the minimum total transfer cost. Based on the path with the minimum total transfer cost, it generates a target skill document of the same problem type or modifies the target skill document. This feeds back the path with the minimum total transfer cost to the current skill document set, so that when the agent subsequently executes the same problem type, it is constrained by the target skill document and prioritizes following the path with the minimum total transfer cost. This reduces invalid tool calls and trial and error processes, and improves the output effect (output consistency, output quality) and execution success rate for the same problem type.
[0063] In some embodiments, S140 includes: the statistical tool calling at least one of the following in the graph: high-frequency nodes, high-frequency adjacent node pairs, high-frequency sub-paths, and the path with the minimum total transfer cost; based on this, S150 includes: generating structured optimization suggestions based on at least one of the following in the tool calling graph: high-frequency nodes, high-frequency adjacent node pairs, high-frequency sub-paths, and the path with the minimum total transfer cost, wherein the structured optimization suggestions include an optimization type and an optimization content description; modifying the target system prompts in the current system prompt word set according to the structured optimization suggestions to obtain optimized target system prompts, wherein the target system prompts are system prompts of the same problem type.
[0064] The generation of structured optimization suggestions can be achieved using a large model. That is, at least one of the following is input into the large model: high-frequency nodes, high-frequency adjacent node pairs, high-frequency sub-paths, and the path with the minimum total transition cost, to obtain the structured optimization suggestions output by the large model.
[0065] The optimization types can include add instructions, remove instructions, and reorder instructions. The optimization description includes the operation details corresponding to the optimization type.
[0066] For example, when the proportion of paths starting with web_search is greater than a preset proportion, web_search is identified as a high-frequency node. In this case, the optimization type can be to add an instruction, and the optimization content description can be to add the text description "If you need to get the latest information, search first" to the system prompt.
[0067] For example, when the proportion of read_file with low transfer cost appearing after web_search is greater than a preset proportion, it is determined that web_search and read_file are high-frequency adjacent node pairs. In this case, the optimization type can be to add an instruction, and the optimization content is described as "after searching for the official page, read the relevant files to obtain the context".
[0068] For example, when debug (the debugging tool) is identified as a low-transfer-cost node and its frequency is higher than a preset frequency threshold, the optimization type can be to add an instruction, and the optimization content is described as "when encountering an error, try to call debug to locate it first".
[0069] The above solution involves analyzing the usage patterns of frequently used and efficient tools from the tool call graph, generating structured optimization suggestions that include descriptions of optimization types and content, and then feeding these structured optimization suggestions back into the target system's prompts. Thus, the optimization process for the target system's prompts is clear and executable.
[0070] In some embodiments, after S150, the method further includes: obtaining the execution performance indicators before optimization, which include at least one of the execution success rate and execution convergence rate of each execution trajectory, the comprehensive execution latency of each tool call chain, and the comprehensive lexical consumption; controlling the agent to repeatedly execute the same problem multiple times under the optimization result to obtain the optimized execution performance indicators, which include the optimized system prompt word set and / or the optimized skill document set; if the optimized execution performance indicators are better than the execution performance indicators before optimization by a factor exceeding a preset magnitude threshold, then the optimized system prompt word set and / or the optimized skill document set are used as the initial values for the next iteration of optimization; otherwise, the current system prompt word set and / or the current skill document set are used as the initial values for the next iteration of optimization.
[0071] Among these, execution success rate refers to the proportion of successfully executed execution paths out of multiple execution paths. Execution convergence rate refers to the proportion of identical execution paths among multiple execution paths. Overall execution latency refers to the statistical result of the execution latency of each tool call chain, while the execution latency of a tool call chain refers to the statistical result of the execution latency of each tool within the tool call chain. This statistical result can be the maximum latency, average latency, sum, etc. Overall lexical consumption refers to the statistical result of the lexical consumption of each tool call chain, and is similar to overall execution latency, so it will not be elaborated here.
[0072] If the optimization of the performance metrics exceeds the preset threshold, the optimization is deemed effective / significant. The optimized set of system prompts and / or the optimized set of skill documents will be used as the initial values for the next iteration to ensure iterative optimization. If the optimization does not exceed the preset threshold, the optimization is deemed ineffective / insignificant. The process will revert to the state before the current optimization, and the current set of system prompts and / or the current set of skill documents will be used as the initial values for the next iteration.
[0073] The above scheme compares the indicators before and after optimization to determine whether the optimization is effective and whether the optimization effect is significant. If it is effective and significant, it iterates and optimizes based on the current optimization results. If it is ineffective or insignificant, it rolls back to the iteration optimization before the current optimization, thereby avoiding ineffective or insignificant optimization.
[0074] In some embodiments, prior to S120, the method includes: determining whether each execution trajectory was executed successfully; and removing the execution trajectory in response to failure to execute successfully.
[0075] The above approach, by eliminating unsuccessful execution traces before extracting the tool call chain, helps to remove noise data interference and ensures that the tool call patterns subsequently discovered have practical reference value.
[0076] In some embodiments, after S130, the method further includes: visualizing the tool call graph for user viewing.
[0077] The tool call graph can be converted to Graphviz or Mermaid format and rendered to a visualization interface using a graphics rendering engine. Nodes represent the tools called, lines represent the call order, and the weights on the lines represent the transition costs. Users can use this visualization interface to understand which tools the agent called, the order in which they were called, and the distribution of transition costs.
[0078] The above solution visualizes the tool call graph for users to view, enabling them to intuitively understand the agent's decision-making process and respond according to actual needs.
[0079] In some embodiments, the structured optimization suggestions and optimization effect data obtained in each iteration are recorded in JSON (JavaScript Object Notation) format for easy traceability and auditing.
[0080] To facilitate understanding, the prior knowledge optimization method for intelligent agents provided in this application will be explained below with a specific example.
[0081] Figure 3 This is a flowchart illustrating a specific example of the prior knowledge optimization method for intelligent agents provided in this application. For example... Figure 3 As shown, the prior knowledge optimization methods for intelligent agents include: I. Multiple rounds of execution and trajectory collection for the same problem The control agent executes the same question n times under the current system prompt word set and the current skill document set, resulting in n execution trajectories.
[0082] The execution trajectory sequentially includes multiple tool call records / steps. Each tool call record includes the tool name, parameter details, token consumption (number of input / output tokens), execution delay, and the index of the parent tool call record, which points to the previous tool call record. The execution trajectory can be collected by registering callbacks at the tool call interception point using a hook, a smart agent middleware.
[0083] II. Extraction of Tool Call Chain 1. Traverse each execution path and remove any execution paths that failed to execute.
[0084] 2. Extract the tool call chain from each of the remaining execution paths.
[0085] Take an execution trajectory as an example: (1) For each tool call record, extract the tool name, parameter details, execution delay, and lexical consumption from the tool call record.
[0086] (2) For each tool call record, obtain the parameter digest based on the parameter details, perform a hash mapping between the tool name and the parameter digest to obtain the tool identifier Node_ID=hash(tool_name+'|'+parameter_signature), and use the tool identifier as the node; weight the execution latency and token consumption to obtain the transfer cost M=β×(normalized_latency)+γ×(normalized_token_cost), where β=0.5 and γ=0.5.
[0087] (3) Connect adjacent node pairs with directed edges to obtain the tool call chain. For example, add a directed edge from node i to node i+1. Use the transition cost from the preceding node to the following node in the adjacent node pair as the weight of the directed edge.
[0088] 3. Merge the tool call chains to obtain the tool call graph. The tool call graph is a directed weighted multigraph.
[0089] During the merging process, if the same directed edge / node appears in multiple tool call chains, the frequency of occurrence of the same directed edge / node is recorded, and the weights of the same directed edge in multiple tool call chains are averaged to obtain the final weight. After obtaining the tool call graph, the tool call graph is output to the visualization interface in Graphviz format.
[0090] Figure 4 This is a schematic diagram of the tool call diagram in this application. For example... Figure 4 As shown, the tool call graph starts with the user-input question and ends with the output. There are multiple paths from the start to the end, and each path has multiple intermediate nodes between the start and end nodes, such as web search, web fetch, write to file, and read file. Adjacent nodes in each path are connected by directed edges. The attributes of intermediate nodes include their frequency of occurrence. Figure 4The diagram only illustrates some of the attributes of directed edges. The attributes of a directed edge include its frequency of occurrence and its weight. The weight represents the transition cost from one node to another. For example, a directed edge with the attribute "1×0.60" means that its frequency of occurrence is 1 and its weight is 0.60. Black dashed arrows represent virtual directed edges, green arrows represent directed edges with higher weights, yellow arrows represent directed edges with moderate weights, and black arrows represent directed edges with lower weights.
[0091] IV. Tool Calling Patterns in Statistical Tool Calling Charts 1. Use Dijkstra's algorithm to search for the path with the minimum total transition cost from the starting node to the ending node.
[0092] Continue reading Figure 4 ,according to Constraints, in Figure 4 The tool shown in the diagram searches and finds the path with the minimum total transfer cost connected by the red dashed arrow: "Problem → Web Search → Write to File → Write to File → Output".
[0093] 2. The statistical tool calls up the high-frequency nodes, high-frequency adjacent node pairs, and high-frequency sub-paths in the graph.
[0094] V. Feedback the tool usage patterns to the current system prompt word set and the current skill document set. 1. Utilize a large model to generate structured optimization suggestions for target system prompt words in the current system prompt word set based on tool call patterns.
[0095] Structured optimization suggestions include the optimization type, a description of the optimization content, and a confidence score. For example, when a path frequently begins with "web_search," a structured optimization suggestion might include adding the instruction "Search first if you need the latest information" to the target system's suggestion. Similarly, when the highly weighted "read_file" appears after a tool, a structured optimization suggestion might include adding the instruction "Read related files to obtain context" to the target system's suggestion. Structured optimization suggestions can be output in JSON format.
[0096] The target system prompts are optimized using structured optimization suggestions to obtain optimized system prompts.
[0097] 2. Generate or modify the target skill document in the current skill document set using the path with the minimum total transfer cost.
[0098] The target skill document includes the step sequence corresponding to the path with the minimum total transfer cost, a description of each step in the step sequence, quality indicators, and precautions. Quality indicators can include total transfer cost, etc.
[0099] VI. Optimization effect verification and iterative optimization (1) Obtain the performance metrics before optimization.
[0100] (2) Control the agent to execute the same question n times under the optimized system prompt word set and the optimized skill document set, and obtain the corresponding new n execution trajectories, and further obtain the optimized performance indicators.
[0101] (3) Determine the optimization range of the optimized execution performance index relative to the unoptimized execution performance index.
[0102] The optimization magnitude includes at least one of the following: reduction in overall token consumption, reduction in overall execution latency, increase in execution success rate, and increase in execution convergence rate.
[0103] (4) If the optimization range exceeds the preset range threshold, the optimized system prompt word set and the optimized skill document set shall be used as the initial values for the next iteration optimization; otherwise, the current system prompt word set and the current skill document set shall be used as the initial values for the next iteration optimization.
[0104] This application also provides a prior knowledge optimization device for an intelligent agent, comprising: an acquisition module, an extraction module, a merging module, a statistics module, and a generation module. Wherein: The acquisition module is used to acquire multiple execution trajectories of the agent for the same question under the current system prompt word set and the current skill document set.
[0105] The extraction module is used to extract tool call chains from each execution trajectory. In the tool call chain, nodes represent the tool identifiers that are called, and directed edges connect adjacent node pairs. The weight of the directed edges represents the transfer cost from the preceding node to the following node in the adjacent node pair.
[0106] The merge module is used to merge the various tool call chains to obtain the tool call graph.
[0107] The statistics module is used to analyze the tool call patterns in the tool call graph. The tool call patterns include at least one of the following: high-frequency nodes, high-frequency adjacent node pairs, high-frequency sub-paths, and the path with the minimum total transfer cost.
[0108] The generation module is used to generate at least one of the following based on the tool's calling patterns: an optimized set of system prompts and an optimized set of skill documents.
[0109] For further detailed descriptions of the prior knowledge optimization device for intelligent agents, please refer to the preceding embodiments, which will not be repeated here.
[0110] Figure 5 This is a schematic diagram of the structure of an embodiment of the electronic device of this application. Figure 5As shown, the electronic device 50 includes a memory 51 and a processor 52. The processor 52 is used to execute program instructions stored in the memory 51 to implement the steps in any of the above method embodiments. In a specific implementation scenario, the electronic device 50 may include, but is not limited to, a microcomputer or a server. In addition, the electronic device 50 may also include a laptop computer, a tablet computer, or other carrier device, which is not limited here.
[0111] Specifically, processor 52 controls itself and memory 51 to implement the steps in any of the above method embodiments. Processor 52 may also be referred to as a CPU (Central Processing Unit). Processor 52 may be an integrated circuit chip with signal processing capabilities. Processor 52 may also be a general-purpose processor, digital signal processor (DSP), application-specific integrated circuit (ASIC), field-programmable gate array (FPGA), or other programmable logic device, discrete gate or transistor logic device, or discrete hardware component. A general-purpose processor may be a microprocessor or any conventional processor. Furthermore, processor 52 may be implemented using integrated circuit chips.
[0112] Please see Figure 6 , Figure 6 This is a schematic diagram of a computer-readable storage medium according to an embodiment of the present application. The computer-readable storage medium 60 stores program instructions 601 thereon, which, when executed by a processor, implement the steps in any of the above method embodiments.
[0113] This application also provides a computer program product comprising a computer program that, when executed by a processor, can implement the steps of the methods described in any of the foregoing embodiments. Specifically, the computer program product can be a software or program product containing a computer program, capable of running on a computing device or stored on any available medium.
[0114] In some embodiments, the functions or modules of the apparatus provided in this disclosure can be used to perform the methods described in the above method embodiments. The specific implementation can be referred to the description of the above method embodiments, and for the sake of brevity, it will not be repeated here.
[0115] The description of the various embodiments above tends to emphasize the differences between the various embodiments. The similarities or similarities between them can be referred to, and for the sake of brevity, they will not be repeated here.
[0116] In the several embodiments provided in this application, it should be understood that the disclosed methods and apparatus can be implemented in other ways. For example, the apparatus implementations described above are merely illustrative. For instance, the division of modules or units is only a logical functional division, and in actual implementation, there may be other division methods. For example, units or components may be combined or integrated into another system, or some features may be ignored or not executed. In addition, the mutual coupling or direct coupling or communication connection shown or discussed may be through some interfaces, and the indirect coupling or communication connection of devices or units may be electrical, mechanical, or other forms.
[0117] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit. If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) or processor to execute all or part of the steps of the methods in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
Claims
1. A method for optimizing prior knowledge of an intelligent agent, characterized in that, include: Obtain multiple execution trajectories of the intelligent agent for the same problem under the current system prompt word set and the current skill document set; Tool call chains are extracted from each of the execution trajectories. In each tool call chain, a node represents the tool identifier being called, and a directed edge connects adjacent node pairs. The weight of the directed edge represents the transfer cost from the preceding node to the following node in the adjacent node pair. By merging the various tool call chains, a tool call graph is obtained; The tool call patterns in the tool call graph are statistically analyzed. The tool call patterns include at least one of the following: high-frequency nodes, high-frequency adjacent node pairs, high-frequency sub-paths, and the path with the minimum total transfer cost. Generate at least one of the following based on the tool call patterns: an optimized set of system prompts and an optimized set of skill documents.
2. The method according to claim 1, characterized in that, The execution trajectory sequentially includes multiple tool call records, each tool call record including tool identifier representation parameters and transfer cost representation parameters; the extraction of tool call chains from each of the execution trajectories includes: Extract the tool identifier representation parameter and the transfer cost representation parameter from each of the tool call records respectively; Each tool identifier is determined based on the corresponding tool identifier representation parameters, and each tool identifier is used as the node. Based on each of the aforementioned transfer cost characterization parameters, the transfer cost from each node to its adjacent subsequent node is determined accordingly. The directed edges are used to connect the adjacent node pairs, and the transition cost from the preceding node to the following node in the adjacent node pair is used as the weight of the directed edge.
3. The method according to claim 2, characterized in that, The transfer cost representation parameters include execution latency and lexical consumption. The determination of the transfer cost from each node to its adjacent subsequent nodes based on each of the transfer cost representation parameters includes: Based on the execution delay and the lexical consumption, the transfer cost from the corresponding node to its adjacent subsequent node is determined, and the transfer cost is positively correlated with the execution delay and the lexical consumption. And / or, the tool identifier characterization parameters include tool name and parameter details; the step of determining each tool identifier based on each of the tool identifier characterization parameters includes: A parameter summary is obtained based on the parameter details; The tool identifier is obtained by mapping the tool name and the parameter summary.
4. The method according to claim 1, characterized in that, The process of merging the various tool call chains to obtain a tool call graph includes: In response to the same directed edge appearing in different tool call chains, the weights of the same directed edge in the different tool call chains are merged to obtain the weight of the same directed edge in the tool call graph.
5. The method according to claim 4, characterized in that, The step of fusing the weights of the same directed edge in different tool call chains to obtain the weight of the same directed edge in the tool call graph includes: The weight of the same directed edge in the tool call graph is obtained by averaging the weights of the same directed edge in different tool call chains.
6. The method according to claim 1, characterized in that, The statistical analysis of tool call patterns in the tool call graph includes: The path with the minimum total transfer cost is searched in the tool call graph, where the total transfer cost is the statistical result of the weights of each directed edge in the path; The step of generating at least one of the optimized system prompt word set and the optimized skill document set based on the tool calling rules includes: In response to the existence of a target skill document in the current skill document set, the target skill document is modified according to the path with the minimum total transfer cost to obtain an optimized target skill document, wherein the target skill document is a skill document of the same problem type; If the target skill document does not exist in the current skill document set, the target skill document is generated according to the path with the minimum total transfer cost.
7. The method according to claim 1, characterized in that, The statistical analysis of tool call patterns in the tool call graph includes: The statistics show that the tool calls at least one of the following in the graph: the high-frequency node, the high-frequency adjacent node pair, the high-frequency sub-path, and the path with the minimum total transfer cost. The step of generating at least one of the optimized system prompt word set and the optimized skill document set based on the tool calling rules includes: Based on at least one of the high-frequency nodes, high-frequency adjacent node pairs, high-frequency sub-paths, and the path with the minimum total transfer cost in the tool call graph, a structured optimization suggestion is generated, which includes an optimization type and a description of the optimization content. Based on the structured optimization suggestions, the target system prompts in the current system prompt word set are modified to obtain optimized target system prompts, where the target system prompts are system prompts for the same question type.
8. The method according to claim 1, characterized in that, After generating at least one of the optimized system prompt word set and the optimized skill document set according to the tool calling rules, the method further includes: Obtain the execution performance indicators before optimization, which include at least one of the following: execution success rate and execution convergence rate of each execution trajectory, and comprehensive execution latency and comprehensive lexical consumption of each tool call chain. The agent is controlled to repeatedly execute the same problem multiple times under the optimized result to obtain the optimized execution performance index. The optimization result includes the optimized system prompt word set and / or the optimized skill document set. If the optimized performance index is better than the unoptimized performance index by a margin exceeding a preset threshold, then the optimized system prompt word set and / or the optimized skill document set will be used as the initial values for the next iteration of optimization; otherwise, the current system prompt word set and / or the current skill document set will be used as the initial values for the next iteration of optimization.
9. The method according to claim 1, characterized in that, Before extracting the tool call chain from each of the aforementioned execution trajectories, the process includes: For each execution trajectory, determine whether the execution trajectory was successfully executed; In response to failure, the execution trajectory is removed; And / or, after merging the tool call chains to obtain the tool call graph, the method further includes: The tool call graph is visualized for user viewing.
10. An electronic device, characterized in that, It includes a memory and a processor, the processor being configured to execute program instructions stored in the memory to implement the method of any one of claims 1-9.
11. A computer-readable storage medium / program product, characterized in that, It stores program instructions / computer programs that, when executed by a processor, implement the method of any one of claims 1-9.
