Skip to content

Commit 0c57259

Browse files
UnderTreeTechsjy3
authored andcommitted
feat: default filtering of thought content in openai compatible LLMs
1 parent 8090d86 commit 0c57259

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

agent/llmagent/agent.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ import (
2727
"github.com/volcengine/veadk-go/utils"
2828
"google.golang.org/adk/agent"
2929
"google.golang.org/adk/agent/llmagent"
30+
adkModel "google.golang.org/adk/model"
3031
"google.golang.org/adk/tool"
32+
"google.golang.org/genai"
3133
)
3234

3335
type Config struct {
@@ -38,6 +40,7 @@ type Config struct {
3840
ModelAPIKey string
3941
ModelExtraConfig map[string]any
4042
KnowledgeBase *knowledgebase.KnowledgeBase
43+
EnableThought bool
4144
}
4245

4346
func New(cfg *Config) (agent.Agent, error) {
@@ -50,7 +53,10 @@ func New(cfg *Config) (agent.Agent, error) {
5053
if cfg.Description == "" {
5154
cfg.Description = prompts.DEFAULT_DESCRIPTION
5255
}
53-
56+
// default filtering of thought content in openai compatible LLMs
57+
if !cfg.EnableThought {
58+
cfg.AfterModelCallbacks = []llmagent.AfterModelCallback{ThoughtFilterCallback}
59+
}
5460
if cfg.Model == nil {
5561
if cfg.ModelName == "" {
5662
cfg.ModelName = utils.GetEnvWithDefault(common.MODEL_AGENT_NAME, configs.GetGlobalConfig().Model.Agent.Name, common.DEFAULT_MODEL_AGENT_NAME)
@@ -86,3 +92,32 @@ func New(cfg *Config) (agent.Agent, error) {
8692
}
8793
return llmagent.New(cfg.Config)
8894
}
95+
96+
// ThoughtFilterCallback flexible control over thought visibility
97+
func ThoughtFilterCallback(ctx agent.CallbackContext, llmResponse *adkModel.LLMResponse, llmResponseError error) (*adkModel.LLMResponse, error) {
98+
if llmResponseError != nil || llmResponse == nil || llmResponse.Content == nil {
99+
return nil, nil
100+
}
101+
102+
var filteredParts []*genai.Part
103+
hasThought := false
104+
105+
for _, part := range llmResponse.Content.Parts {
106+
if !part.Thought {
107+
filteredParts = append(filteredParts, part)
108+
} else {
109+
hasThought = true
110+
}
111+
}
112+
113+
if hasThought {
114+
newResponse := *llmResponse
115+
newResponse.Content = &genai.Content{
116+
Role: llmResponse.Content.Role,
117+
Parts: filteredParts,
118+
}
119+
return &newResponse, nil
120+
}
121+
122+
return nil, nil
123+
}

0 commit comments

Comments
 (0)