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:24mlight A Share investment Agent build news query

From Leeroopedia
Knowledge Sources
Domains Data_Engineering, Finance
Last Updated 2026-10-10 14:00 GMT

Overview

Concrete tool for constructing agent-specific news search queries optimized for the Tavily API format, with rule-based and LLM-based query modes, provided by the A_Share_investment_Agent repository.

Description

The build_news_query function builds optimized search queries based on the agent type. It supports two modes:

  • Rule-based (default): Uses _build_enhanced_query() which combines Chinese keywords with optional English company names for international coverage.
  • LLM-based (env NEWS_QUERY_MODE=llm): Uses _llm_query() to generate queries via LLM.

Agent-specific query patterns:

  • macro_news_agent: CSI 300 market-wide macro news (央行政策, 证监会, 流动性, etc.)
  • macro_analyst_agent: Industry dynamics and policy changes
  • sentiment_agent: Company-specific news, announcements, earnings
  • market_snapshot: Capital flows, institutional activity

Usage

Import when you need to:

  • Build Tavily-compatible search queries for different agent types
  • Support both rule-based and LLM-based query generation

Code Reference

Source Location

  • Repository: A_Share_investment_Agent
  • File: src/tools/news_query_builder.py
  • Lines: L181-216 (build_news_query), L26-73 (_build_tavily_query), L76-120 (_build_enhanced_query), L123-166 (_llm_query)

Signature

def build_news_query(
    symbol: str,
    *,
    date: Optional[str] = None,
    agent_name: Optional[str] = None,
    trace_state: Optional[dict] = None,
    search_engine: str = "tavily",
) -> str:
    """Build optimized news search query for Tavily API."""

Import

from src.tools.news_query_builder import build_news_query

I/O Contract

Inputs

Name Type Required Description
symbol str Yes Stock code (6-digit)
date str No Date (retained for compat, not used for date operators)
agent_name str No Controls query type (macro_news_agent, macro_analyst_agent, sentiment_agent, market_snapshot)
trace_state dict No For LLM query mode logging
search_engine str No Search engine type (default: "tavily")

Outputs

Name Type Description
return str Optimized search query string (Chinese, Tavily-compatible)

Usage Examples

Rule-Based Query

from src.tools.news_query_builder import build_news_query

query = build_news_query("002848", agent_name="sentiment_agent")
# Returns: "帮我搜索最近一周{company_name}(002848)的新闻、公告、业绩和订单信息"

query = build_news_query("000300", agent_name="macro_news_agent")
# Returns: "帮我搜索最近一周沪深300与A股市场宏观新闻..."

LLM-Based Query

import os
os.environ["NEWS_QUERY_MODE"] = "llm"

query = build_news_query("002848", agent_name="sentiment_agent", trace_state=state)
# LLM generates a concise query, with advanced operators removed

Related Pages

Implements Principle

Requires Environment

Uses Heuristics

Page Connections

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