LLM多代理聊天演示
一个具有可配置代理、工具调用(MCP)、RAG和实时流的多代理聊天系统。支持多个LLM提供商:Ollama(本地)、Gemini、Claude和ChatGPT。
主要特点
- 代理
- YAML驱动——在配置中定义角色、系统提示和允许的工具 - 多供应商LLM——Ollama(本地)、Gemini、Claude、ChatGPT,统一 Chat 协议 - 工具调用——代理作为工具访问所有功能(计算器、时间、用户上下文、RAG、MCP) - MCP——通过FastMCP将金融、天气、网络搜索作为工具公开 - RAG——带有嵌入的主题感知局部向量搜索,作为一种工具公开
- 编排
- 路由器——基于LLM的意图分类,选择最佳代理 - 评估员——判断响应质量,必要时转发给另一个代理 - 版主——带模式匹配的安全过滤器
- 用户界面
- 实时思考步骤与流式响应(SSE) - 具有完整对话历史记录的多回合聊天 - 编辑并重新启动之前的消息
- 命令行界面
- 带有markdown渲染、语法突出显示和思维旋转器的交互式CLI
建筑
- 前端 --React+TypeScript+Tailwind,通过SSE流式传输编排事件
- 命令行界面 --React+Ink(终端UI),与web前端相同的SSE流
- 后端 --FastAPI,无状态(接收每个请求的完整消息历史)
- 编排器 --协调管道的异步生成器:
1. 主持人 --阻止不安全的查询(模式匹配) 1. 路由器 --LLM电话,对意图进行分类并选择最佳代理 1. 代理 --使用筛选工具的LLM调用(每个代理只看到其允许的工具) 1. 评估者 --LLM呼叫以判断响应质量;如果不足,则转发给另一个代理(最多 max_forwards 尝试)
- 代理 --在YAML配置中定义角色、系统提示和工具列表(助理、知识、研究员、分析师、天气专家)
- 工具 --计算器、时间、用户上下文、RAG搜索和MCP服务(金融、天气、Tavily网络搜索)
思考UI和流媒体
UI通过SSE实时流式编排步骤——路由决策、工具调用、代理输出和评估结果在发生时显示。一旦最终答案到来,思考部分就会自动崩溃。
多回合聊天和编辑
聊天支持完整的多回合对话。您还可以编辑任何早期的用户消息——对话将从那时起用编辑后的文本重新开始。
工具调用
代理使用本机LLM工具调用——工具被定义为普通函数,并直接传递给模型。每次工具调用都是一个思考步骤。
型号说明
- 双子座/克劳德/ChatGPT 更可靠地遵循工具调用和格式化指令,但受API速率限制。
- 奥拉玛 像qwen2.5:7b这样的(本地)模型可能会忽略格式化指令(例如LaTeX分隔符)或产生不太准确的路由。较大的本地模型通常表现更好。
快速开始
先决条件
1.克隆和拉取模型
git clone git@github.com:naokishibuya/llm-rag-chat-demo.git
cd llm-rag-chat-demo
ollama pull qwen2.5:7b
ollama pull nomic-embed-text否则,后端将在首次使用时自动下载ollama模型(这可能需要时间)。
2.后端
cd backend
uv sync
# Optional: add API keys for Gemini / Claude / OpenAI / Tavily
cp .env.example .env
cd src
uv run uvicorn main:app --reloadAPI文档,网址:http://localhost:8000/docs
3.MCP服务(可选)
用于工具调用演示的本地金融和天气服务器:
cd services
uv sync
uv run python -m mcp_services.finance.server &
uv run python -m mcp_services.weather.server &或者在单独的终端中单独运行,而不发送到后台(&).
塔维利 如果满足以下条件,网络搜索可以开箱即用 TAVILY_API_KEY 已设置 backend/.env.
4.前端
cd frontend
npm install
npm run dev打开http://localhost:5173
5.CLI(web前端的替代品)
cd cli
npm install
npm run build
# Interactive chat (prompts for model selection)
npx chat-cli
# With a specific model
npx chat-cli --model gemini-2.5-flash
# Custom backend URL
npx chat-cli --server http://remote:8000
# List available models
npx chat-cli models会话中命令: /model (开关型号), /clear (重置历史记录), /quit (退出), /help
配置
所有配置都位于 backend/config/config.yaml.
代理
代理是以声明方式定义的——每个代理都有一个角色(由路由器使用)、一个系统提示和一系列允许的工具:
agents:
assistant:
role: "General chat, greetings, simple questions, time, math"
system_prompt: |
You are a helpful, friendly assistant.
Be concise but informative. If you don't know something, say so rather than making things up.
tools: [get_current_time, get_user_context, calculate]
knowledge:
role: "Questions about space, history, geography — topics in the local knowledge base"
system_prompt: |
You are a knowledge base specialist.
Search the knowledge base to answer questions. Cite sources when available.
If the knowledge base has no relevant results, say so.
tools: [search_knowledge_base]
researcher:
role: "General research, technical questions, how-to guides, current events, and anything not covered by other agents"
system_prompt: |
You are a research specialist.
Use web search to find accurate, up-to-date information.
Cite sources with links when available. Be thorough but concise.
tools: [tavily_search]
analyst:
role: "Finance, stocks, market data, and financial calculations"
system_prompt: |
You are a senior financial analyst with deep expertise in equity research.
When answering complex financial questions:
1. Break the question into research steps — gather price, ratios, financials as needed.
2. Use multiple tools to cross-validate findings (e.g. check both ratios and income trends).
3. Present data clearly in markdown tables when comparing numbers.
4. Provide structured analysis: key metrics, bull case, bear case, and a summary.
5. Always cite which data points support your conclusions.
Be precise with numbers. Never fabricate data — only report what the tools return.
tools: [get_stock_price, get_income_statement, get_balance_sheet, get_cash_flow, get_historical_prices, get_company_info, get_key_ratios, compare_stocks, calculate]
disclaimer: "This is for informational purposes only and not financial advice."
weather_expert:
role: "Weather queries, forecasts, conditions"
system_prompt: |
You are a weather specialist.
Provide clear, concise weather information.
Include relevant details like temperature, conditions, and forecasts.
tools: [get_weather, get_user_context]LLM
条目下 llm 成为UI下拉列表中的可选模型:
llm:
- class: backend.agent.llm.ollama.OllamaChat
model: qwen2.5:7b
params:
temperature: 0.3
- class: backend.agent.llm.gemini.GeminiChat
model: gemini-2.5-flash
api_key_env: GEMINI_API_KEY
params:
temperature: 0.3
- class: backend.agent.llm.anthropic.AnthropicChat
model: claude-haiku-4-5-20251001
api_key_env: ANTHROPIC_API_KEY
params:
temperature: 0.3
- class: backend.agent.llm.openai.OpenAIChat
model: gpt-4.1-nano
api_key_env: OPENAI_API_KEY
params:
temperature: 0.3当凭据丢失时,需要API密钥的模型将从UI中排除。
工作流程
workflow:
max_forwards: 2 # Max agent-to-agent forwards per query定价
这 pricing 该部分定义了用于UI中使用情况跟踪的每模型令牌成本(每1M令牌$)。这些可能与实际的供应商定价不同。本地模型默认为零。
项目结构
backend/
config/config.yaml # Agents, LLMs, MCP endpoints, RAG, pricing
knowledge/ # Documents for RAG indexing (by topic)
space/ # Solar system, space exploration
history/ # Ancient civilizations, modern history
geography/ # Countries, landmarks, natural wonders
src/main.py # FastAPI entrypoint
src/backend/
agent/ # Agent framework
agent.py # Agent class (name, system_prompt, act)
tools.py # Tool definitions, build_tools, filter_tools
types.py # Chat/Agent protocols, Message, Reply, UserContext
llm/ # Provider implementations (Ollama, Gemini, Anthropic, OpenAI)
registry.py # Model registry, resolve by name
pricer.py # Token cost calculator
mcp/ # MCP client and tool handler
rag/ # RAG client (numpy vector search, topic-aware)
orchestrator/ # Orchestration pipeline
orchestrator.py # Lifecycle (startup/shutdown), stream generator
router.py # LLM-based intent classification
evaluator.py # Response sufficiency check, agent forwarding
moderator.py # Safety filter
config.py # YAML config loader
api.py # REST/SSE endpoints
frontend/ # React + TypeScript + Tailwind
cli/ # Interactive terminal UI (React + Ink)
services/ # MCP servers (finance, weather)免责声明
本项目的财务分析功能是 仅用于教育和示范目的分析师代理人提供的任何内容均不构成财务建议、投资建议或背书。财务数据可能会延迟、不完整或不准确。在做出投资决策之前,一定要咨询合格的财务顾问。
