Skip to content

Commit 6534c6d

Browse files
committed
perf: keyword-based tool routing to reduce LLM token overhead
Instead of sending all 40 tool definitions (~5,000 tokens) with every LLM call, match user message keywords to tool categories and only include relevant tools. Saves 60-80% tool tokens for most requests. Categories: gig, skill, social, cw, market, platform. Fallback: if no category matches, include minimal discovery set.
1 parent 80617db commit 6534c6d

1 file changed

Lines changed: 71 additions & 14 deletions

File tree

internal/web/chat.go

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (s *ChatSession) Chat(ctx context.Context, userMsg string) (string, *Action
120120
var err error
121121

122122
if tp, ok := s.provider.(tools.ChatToolProvider); ok {
123-
toolList := s.buildToolList()
123+
toolList := s.buildToolList(userMsg)
124124
if mightNeedTools(userMsg) {
125125
// Agentic path: tool-calling loop (when the message likely needs tools).
126126
msgs := s.buildToolMessages(miningContext)
@@ -186,16 +186,66 @@ func (s *ChatSession) toSession() *Session {
186186
}
187187
}
188188

189-
// buildToolList returns the tools available for this session.
190-
// Includes market and platform action tools when a market client is configured.
191-
func (s *ChatSession) buildToolList() []tools.Tool {
189+
// toolCategory groups for keyword-based routing.
190+
// Only relevant tool categories are sent to the LLM, saving ~60-80% tool tokens.
191+
var toolRoutes = []struct {
192+
keywords []string
193+
category string
194+
}{
195+
{[]string{"gig", "hunt", "task", "job", "claim", "deliver", "approve", "dispute", "cancel", "rate", "withdraw",
196+
"gig_", "/gig/", "悬赏", "任务", "接单", "认领", "交付", "审批", "争议"}, "gig"},
197+
{[]string{"skill", "buy", "purchase", "publish", "listing", "bounty",
198+
"sk_", "/skill/", "技能", "购买", "发布", "赏金"}, "skill"},
199+
{[]string{"nearby", "friend", "follower", "following", "message", "moment", "follow", "unfollow", "post", "social", "mail", "dm",
200+
"附近", "好友", "关注", "粉丝", "消息", "私信", "动态", "发帖"}, "social"},
201+
{[]string{"balance", "transfer", "cw", "wallet", "history", "allowance",
202+
"余额", "转账", "钱包"}, "cw"},
203+
{[]string{"market", "browse", "order", "swap", "listing",
204+
"市场", "挂单", "交易"}, "market"},
205+
{[]string{"bug", "issue", "report", "feedback", "问题", "反馈", "报告"}, "platform"},
206+
}
207+
208+
func matchToolCategories(msg string) map[string]bool {
209+
lower := strings.ToLower(msg)
210+
cats := map[string]bool{}
211+
for _, route := range toolRoutes {
212+
for _, kw := range route.keywords {
213+
if strings.Contains(lower, kw) {
214+
cats[route.category] = true
215+
break
216+
}
217+
}
218+
}
219+
return cats
220+
}
221+
222+
// buildToolList returns tools filtered by relevance to the user message.
223+
// Built-in tools are always included; platform tools are added only when
224+
// their category keywords match, saving significant token overhead.
225+
func (s *ChatSession) buildToolList(userMsg string) []tools.Tool {
192226
list := tools.Defaults()
193-
if s.marketClient != nil {
194-
// Market discovery
227+
if s.marketClient == nil {
228+
return list
229+
}
230+
231+
cats := matchToolCategories(userMsg)
232+
233+
// If no specific category matched but mightNeedTools was true,
234+
// include a small discovery set so the LLM can still help.
235+
if len(cats) == 0 {
195236
list = append(list, tools.NewMarketBrowseTool(s.marketClient))
196237
list = append(list, tools.NewMarketLookupTool(s.marketClient))
238+
list = append(list, tools.NewCWBalanceTool(s.marketClient))
239+
list = append(list, tools.NewReportIssueTool(s.marketClient))
240+
return list
241+
}
242+
243+
// Market discovery — always include if any platform category matched
244+
list = append(list, tools.NewMarketBrowseTool(s.marketClient))
245+
list = append(list, tools.NewMarketLookupTool(s.marketClient))
246+
247+
if cats["gig"] {
197248
list = append(list, tools.NewGigMyTool(s.marketClient))
198-
// Gig actions
199249
list = append(list, tools.NewGigClaimTool(s.marketClient))
200250
list = append(list, tools.NewGigDeliverTool(s.marketClient))
201251
list = append(list, tools.NewGigApproveTool(s.marketClient))
@@ -204,18 +254,17 @@ func (s *ChatSession) buildToolList() []tools.Tool {
204254
list = append(list, tools.NewGigCancelTool(s.marketClient))
205255
list = append(list, tools.NewGigRateTool(s.marketClient))
206256
list = append(list, tools.NewGigWithdrawTool(s.marketClient))
207-
// Skill market
257+
}
258+
if cats["skill"] {
208259
list = append(list, tools.NewSkillMarketBrowseTool(s.marketClient))
209260
list = append(list, tools.NewSkillMarketDetailTool(s.marketClient))
210261
list = append(list, tools.NewSkillBuyTool(s.marketClient))
211262
list = append(list, tools.NewSkillMyPurchasesTool(s.marketClient))
212263
list = append(list, tools.NewSkillMySkillsTool(s.marketClient))
213264
list = append(list, tools.NewSkillRequestsTool(s.marketClient))
214265
list = append(list, tools.NewSkillRequestClaimTool(s.marketClient))
215-
list = append(list, tools.NewCWMarketMineTool(s.marketClient))
216-
list = append(list, tools.NewCWMarketBuyOrdersTool(s.marketClient))
217-
list = append(list, tools.NewCWMarketFillOrderTool(s.marketClient))
218-
// Social
266+
}
267+
if cats["social"] {
219268
list = append(list, tools.NewSocialPostTool(s.marketClient))
220269
list = append(list, tools.NewSocialFollowTool(s.marketClient))
221270
list = append(list, tools.NewSocialUnfollowTool(s.marketClient))
@@ -226,13 +275,21 @@ func (s *ChatSession) buildToolList() []tools.Tool {
226275
list = append(list, tools.NewSocialMomentsReadTool(s.marketClient))
227276
list = append(list, tools.NewSocialLikeMomentTool(s.marketClient))
228277
list = append(list, tools.NewSocialConnectionsTool(s.marketClient))
229-
// CW economy
278+
}
279+
if cats["cw"] {
230280
list = append(list, tools.NewCWBalanceTool(s.marketClient))
231281
list = append(list, tools.NewCWTransferTool(s.marketClient))
232282
list = append(list, tools.NewCWHistoryTool(s.marketClient))
233-
// Platform
283+
}
284+
if cats["market"] {
285+
list = append(list, tools.NewCWMarketMineTool(s.marketClient))
286+
list = append(list, tools.NewCWMarketBuyOrdersTool(s.marketClient))
287+
list = append(list, tools.NewCWMarketFillOrderTool(s.marketClient))
288+
}
289+
if cats["platform"] {
234290
list = append(list, tools.NewReportIssueTool(s.marketClient))
235291
}
292+
236293
return list
237294
}
238295

0 commit comments

Comments
 (0)