<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[CallBack 定义方法——模具行业从业者必备知识手册]]></title><description><![CDATA[<p dir="auto">&lt;h2&gt;CallBack 定义方法&lt;/h2&gt;</p>
<p dir="auto">&lt;h3&gt;方法说明&lt;/h3&gt;</p>
<p dir="auto">&lt;h4&gt;1、BeforeModelCallBack&lt;/h4&gt;<br />
&lt;p&gt;type BeforeModelCallback func(ctx agent.CallbackContext, llmRequest &lt;em&gt;model.LLMRequest) (&lt;/em&gt;model.LLMResponse, error)&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;BeforeModelCallback that is called before sending a request to the model.&lt;/p&gt;<br />
&lt;p&gt;If it returns non-nil LLMResponse or error, the actual model call is skipped&lt;/p&gt;<br />
&lt;p&gt;and the returned response/error is used.&lt;/p&gt;</p>
<p dir="auto">&lt;h4&gt;2、AfterModelCallback&lt;/h4&gt;<br />
&lt;p&gt;type AfterModelCallback func(ctx agent.CallbackContext, llmResponse &lt;em&gt;model.LLMResponse, llmResponseError error) (&lt;/em&gt;model.LLMResponse, error)&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;AfterModelCallback that is called after receiving a response from the model.&lt;/p&gt;<br />
&lt;p&gt;If it returns non-nil LLMResponse or error, the actual model response/error&lt;/p&gt;<br />
&lt;p&gt;is replaced with the returned response/error.&lt;/p&gt;</p>
<p dir="auto">&lt;h4&gt;3、BeforeToolCallback&lt;/h4&gt;<br />
&lt;p&gt;type BeforeToolCallback func(ctx tool.Context, tool tool.Tool, args map[string]any) (map[string]any, error)&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;BeforeToolCallback is executed before a tool's Run method.&lt;/p&gt;<br />
&lt;p&gt;Callbacks are executed in the order they are provided.&lt;/p&gt;<br />
&lt;p&gt;If a callback returns a non-nil result or an error:&lt;/p&gt;<br />
&lt;ul&gt;<br />
&lt;li&gt;execution of remaining callbacks stops&lt;/li&gt;<br />
&lt;li&gt;the actual tool call is skipped&lt;/li&gt;<br />
&lt;li&gt;the returned result is used as the tool result&lt;/li&gt;<br />
&lt;/ul&gt;</p>
<p dir="auto">&lt;p&gt;To modify tool arguments and still run the tool,&lt;/p&gt;<br />
&lt;p&gt;update args in place and return (nil, nil).&lt;/p&gt;</p>
<p dir="auto">&lt;h4&gt;4、AfterToolCallback&lt;/h4&gt;<br />
&lt;p&gt;type AfterToolCallback func(ctx tool.Context, tool tool.Tool, args, result map[string]any, err error) (map[string]any, error)&lt;/p&gt;<br />
&lt;p&gt;AfterToolCallback is a function type executed after a tool's Run method has completed, &lt;/p&gt;<br />
&lt;p&gt;regardless of whether the tool returned a result or an error.&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;Callbacks are executed in the order they are provided.&lt;/p&gt;<br />
&lt;p&gt;If a callback returns a non-nil result or an error:&lt;/p&gt;<br />
&lt;ul&gt;<br />
&lt;li&gt;execution of remaining callbacks stops&lt;/li&gt;<br />
&lt;li&gt;the returned result and/or error is used as the final tool output&lt;/li&gt;<br />
&lt;/ul&gt;</p>
<p dir="auto">&lt;h3&gt;callback方法示例&lt;/h3&gt;</p>
<p dir="auto">&lt;h4&gt;1、BeforeModelCallBack 代码示例&lt;/h4&gt;<br />
&lt;p&gt;何时触发： 在LlmAgent流程中向 LLM 发送请求之前调用。&lt;/p&gt;<br />
&lt;p&gt;用途： 允许检查和修改发送给 LLM 的请求。用例包括添加动态指令、基于状态注入少量示例、修改模型配置、实现防护机制 (如亵渎过滤器) 或实现请求级缓存。&lt;/p&gt;<br />
&lt;p&gt;返回值效果： 如果回调返回 nil，LLM 继续其正常工作流程。如果回调返回 LlmResponse 对象，则跳过对 LLM 的调用。返回的 LlmResponse 直接使用，就像它来自模型一样。这对于实现防护栏或缓存非常强大。&lt;/p&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
func onBeforeModel(ctx agent.CallbackContext, req *model.LLMRequest) (*model.LLMResponse, error) {<br />
log.Printf("[Callback] BeforeModel triggered for agent %q.", ctx.AgentName())</p>
<pre><code>// Modification Example: Add a prefix to the system instruction.
if req.Config.SystemInstruction != nil {
    prefix := "[Modified by Callback] "
    // This is a simplified example; production code might need deeper checks.
    if len(req.Config.SystemInstruction.Parts) &gt; 0 {
        req.Config.SystemInstruction.Parts[0].Text = prefix + req.Config.SystemInstruction.Parts[0].Text
    } else {
        req.Config.SystemInstruction.Parts = append(req.Config.SystemInstruction.Parts, &amp;genai.Part{Text: prefix})
    }
    log.Printf("[Callback] Modified system instruction.")
}

// Skip Example: Check for "BLOCK" in the user's prompt.
for _, content := range req.Contents {
    for _, part := range content.Parts {
        if strings.Contains(strings.ToUpper(part.Text), "BLOCK") {
            log.Println("[Callback] 'BLOCK' keyword found. Skipping LLM call.")
            return &amp;model.LLMResponse{
                Content: &amp;genai.Content{
                    Parts: []*genai.Part{{Text: "LLM call was blocked by before_model_callback."}},
                    Role:  "model",
                },
            }, nil
        }
    }
}

log.Println("[Callback] Proceedi
</code></pre>
<hr />
<p dir="auto">这个参数你们一般设多少？评论区说说。<br />
&lt;/code&gt;&lt;/pre&gt;</p>
]]></description><link>https://forum.mj7.cn/topic/7116/callback-定义方法-模具行业从业者必备知识手册</link><generator>RSS for Node</generator><lastBuildDate>Fri, 02 Oct 2026 16:11:26 GMT</lastBuildDate><atom:link href="https://forum.mj7.cn/topic/7116.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 19 Apr 2026 21:35:01 GMT</pubDate><ttl>60</ttl></channel></rss>