Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:666ghj BettaFish KeywordOptimizer optimize keywords

From Leeroopedia
Knowledge Sources
Domains NLP, Information_Retrieval, LLM_Prompting
Last Updated 2026-10-08 00:00 GMT

Overview

Concrete LLM-powered middleware that optimizes agent-generated search queries into multiple database-friendly keywords for the InsightEngine's local sentiment database.

Description

The KeywordOptimizer class uses an OpenAI-compatible LLM (default: Qwen3 via SiliconFlow) to transform a single agent-generated search query into multiple optimized keywords. These keywords are better suited for database LIKE queries on Chinese social media content. The optimizer understands that the agent's academic language (e.g., "教育领域AI应用") needs to be translated into colloquial social media terms (e.g., "AI教育", "智能教学", "在线教育AI"). It returns a `KeywordOptimizationResponse` with a list of optimized keywords, reasoning, and success status.

Usage

Import this class when you need to bridge between LLM-generated research queries and database search terms. It is called by `DeepSearchAgent.execute_search_tool` before each database search to expand a single query into multiple keywords.

Code Reference

Source Location

  • Repository: BettaFish
  • File: `InsightEngine/tools/keyword_optimizer.py:L36-297`

Signature

@dataclass
class KeywordOptimizationResponse:
    original_query: str
    optimized_keywords: List[str]
    reasoning: str
    success: bool
    error_message: str = ""

class KeywordOptimizer:
    def __init__(
        self,
        api_key: str = None,
        base_url: str = None,
        model_name: str = None
    ):
        """
        Args:
            api_key: SiliconFlow API key (defaults to settings.KEYWORD_OPTIMIZER_API_KEY)
            base_url: API base URL (defaults to SiliconFlow endpoint)
            model_name: Model name (defaults to settings.KEYWORD_OPTIMIZER_MODEL_NAME)
        """

    @with_graceful_retry(SEARCH_API_RETRY_CONFIG)
    def optimize_keywords(
        self,
        original_query: str,
        context: str = ""
    ) -> KeywordOptimizationResponse:
        """
        Optimize a search query into multiple database-friendly keywords.

        Args:
            original_query: Agent-generated search query
            context: Additional context (e.g., tool name)

        Returns:
            KeywordOptimizationResponse with optimized_keywords list
        """

Import

from InsightEngine.tools.keyword_optimizer import KeywordOptimizer, KeywordOptimizationResponse

I/O Contract

Inputs

Name Type Required Description
original_query str Yes Agent-generated search query
context str No Additional context (e.g., "使用search_topic_globally工具")

Outputs

Name Type Description
optimized_keywords List[str] Multiple database-friendly keywords
reasoning str LLM's reasoning for the optimization
success bool Whether optimization succeeded

Usage Examples

Optimize Search Keywords

from InsightEngine.tools.keyword_optimizer import KeywordOptimizer

optimizer = KeywordOptimizer()

response = optimizer.optimize_keywords(
    original_query="教育领域AI技术应用现状",
    context="使用search_topic_globally工具进行全局话题搜索"
)

if response.success:
    print(f"Original: {response.original_query}")
    print(f"Optimized keywords: {response.optimized_keywords}")
    # e.g., ["AI教育", "智能教学", "在线教育AI", "教育科技"]
    print(f"Reasoning: {response.reasoning}")

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment