CallBack 定义方法——模具行业从业者必备知识手册
-
<h2>CallBack 定义方法</h2>
<h3>方法说明</h3>
<h4>1、BeforeModelCallBack</h4>
<p>type BeforeModelCallback func(ctx agent.CallbackContext, llmRequest <em>model.LLMRequest) (</em>model.LLMResponse, error)</p><p>BeforeModelCallback that is called before sending a request to the model.</p>
<p>If it returns non-nil LLMResponse or error, the actual model call is skipped</p>
<p>and the returned response/error is used.</p><h4>2、AfterModelCallback</h4>
<p>type AfterModelCallback func(ctx agent.CallbackContext, llmResponse <em>model.LLMResponse, llmResponseError error) (</em>model.LLMResponse, error)</p><p>AfterModelCallback that is called after receiving a response from the model.</p>
<p>If it returns non-nil LLMResponse or error, the actual model response/error</p>
<p>is replaced with the returned response/error.</p><h4>3、BeforeToolCallback</h4>
<p>type BeforeToolCallback func(ctx tool.Context, tool tool.Tool, args map[string]any) (map[string]any, error)</p><p>BeforeToolCallback is executed before a tool's Run method.</p>
<p>Callbacks are executed in the order they are provided.</p>
<p>If a callback returns a non-nil result or an error:</p>
<ul>
<li>execution of remaining callbacks stops</li>
<li>the actual tool call is skipped</li>
<li>the returned result is used as the tool result</li>
</ul><p>To modify tool arguments and still run the tool,</p>
<p>update args in place and return (nil, nil).</p><h4>4、AfterToolCallback</h4>
<p>type AfterToolCallback func(ctx tool.Context, tool tool.Tool, args, result map[string]any, err error) (map[string]any, error)</p>
<p>AfterToolCallback is a function type executed after a tool's Run method has completed, </p>
<p>regardless of whether the tool returned a result or an error.</p><p>Callbacks are executed in the order they are provided.</p>
<p>If a callback returns a non-nil result or an error:</p>
<ul>
<li>execution of remaining callbacks stops</li>
<li>the returned result and/or error is used as the final tool output</li>
</ul><h3>callback方法示例</h3>
<h4>1、BeforeModelCallBack 代码示例</h4>
<p>何时触发: 在LlmAgent流程中向 LLM 发送请求之前调用。</p>
<p>用途: 允许检查和修改发送给 LLM 的请求。用例包括添加动态指令、基于状态注入少量示例、修改模型配置、实现防护机制 (如亵渎过滤器) 或实现请求级缓存。</p>
<p>返回值效果: 如果回调返回 nil,LLM 继续其正常工作流程。如果回调返回 LlmResponse 对象,则跳过对 LLM 的调用。返回的 LlmResponse 直接使用,就像它来自模型一样。这对于实现防护栏或缓存非常强大。</p><pre><code>
func onBeforeModel(ctx agent.CallbackContext, req *model.LLMRequest) (*model.LLMResponse, error) {
log.Printf("[Callback] BeforeModel triggered for agent %q.", ctx.AgentName())// 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) > 0 { req.Config.SystemInstruction.Parts[0].Text = prefix + req.Config.SystemInstruction.Parts[0].Text } else { req.Config.SystemInstruction.Parts = append(req.Config.SystemInstruction.Parts, &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 &model.LLMResponse{ Content: &genai.Content{ Parts: []*genai.Part{{Text: "LLM call was blocked by before_model_callback."}}, Role: "model", }, }, nil } } } log.Println("[Callback] Proceedi
这个参数你们一般设多少?评论区说说。
</code></pre>