在 Agno 中组合 Groq 推理模型:reasoning_model 配置、模型混搭与速度对比实战
【免费下载链接】agnoBuild, run, and manage agent platforms.项目地址: https://gitcode.com/GitHub_Trending/ag/agno
导读
本指南基于 agno 仓库cookbook/10_reasoning/models/groq目录中的三个实战示例,系统讲解如何在 Agno Agent 中使用 Groq 托管的大模型完成"思考 + 作答"的推理链路。你将掌握:如何为 Agent 配置reasoning_model让推理模型先行思考、如何把 Groq 推理模型与 Anthropic Claude 等不同厂商模型混合组合、以及如何通过对比实验评估"是否启用推理模型"对响应速度的实际影响。文中所有代码均可直接复制运行,并附带源码级原理说明。
目录概览:Groq 推理与模型组合示例
cookbook/10_reasoning/models/groq/目录包含一个简短的 README 与三个核心示例脚本:
| 文件 | 主题 |
|---|---|
| 9_11_or_9_9.py | Groq 主模型 + DeepSeek 推理模型,处理数值比较问题 |
| deepseek_plus_claude.py | Groq 托管推理模型"思考",Claude 负责"作答" |
| fast_reasoning.py | 对比有无推理模型时的响应速度 |
README 原文即通过这三个示例点明目录主旨:"Groq reasoning and model-combination examples"(Groq 推理与模型组合示例)。以下逐一展开。
示例一:Groq 主模型 + DeepSeek 推理模型
完整代码
from agno.agent import Agent from agno.models.deepseek import DeepSeek from agno.models.groq import Groq # --------------------------------------------------------------------------- # Create Agent # --------------------------------------------------------------------------- agent = Agent( model=Groq( id="qwen/qwen3.6-27b", temperature=0.6, max_tokens=1024, top_p=0.95, ), reasoning_model=DeepSeek(id="deepseek-reasoner"), markdown=True, ) # --------------------------------------------------------------------------- # Run Agent # --------------------------------------------------------------------------- if __name__ == "__main__": agent.print_response( "9.11 and 9.9 -- which is bigger?", stream=True, show_full_reasoning=True, )配置解析
model=Groq(id="qwen/qwen3.6-27b", ...):主模型(用于最终作答)是 Groq 托管的 Qwen 系列模型。temperature=0.6、max_tokens=1024、top_p=0.95为推理采样参数。reasoning_model=DeepSeek(id="deepseek-reasoner"):启用 Agent 推理的关键参数。Agno 中reasoning_model的官方注释为"Enable reasoning by providing a reasoning_model (must be a native reasoning model)"(通过提供推理模型启用推理,且必须是原生推理模型),见 agent.py。此处指定 DeepSeek 的deepseek-reasoner作为"思考引擎"。markdown=True:让最终回答以 Markdown 格式渲染输出。show_full_reasoning=True:在print_response中传入,流式输出时完整展示推理过程(思考内容),而不是只显示最终答案。
运行前提
- 安装
agno以及groq、deepseek相关依赖(pip install groq deepseek,模型依赖包见各自模块导入要求)。 - 设置环境变量
GROQ_API_KEY。源码中,groq.py 的_get_client_params()会先读取getenv("GROQ_API_KEY"),未设置时抛出ModelAuthenticationError,提示"GROQ_API_KEY not set. Please set the GROQ_API_KEY environment variable.";DeepSeek 模型同样需要DEEPSEEK_API_KEY。 - 推理模型若为 Groq 托管的模型,则只需
GROQ_API_KEY即可同时服务推理与作答。
实测验证结果
根据同目录 TEST_LOG.md 记录:该示例实际运行通过,推理过程流式输出,Agent 正确回答 9.9 > 9.11。这印证了"推理模型先思考、主模型后作答"的链路在数值比较这类需要仔细判断的问题上的有效性(9.11 与 9.9 的十进制比较极易被模型答错,是常见的推理基准问题)。
示例二:Groq 推理 + Claude 作答的跨厂商组合
完整代码
from agno.agent import Agent from agno.models.anthropic import Claude from agno.models.groq import Groq # --------------------------------------------------------------------------- # Create Agent # --------------------------------------------------------------------------- agent = Agent( model=Claude(id="claude-sonnet-4-5"), reasoning_model=Groq( id="openai/gpt-oss-120b", temperature=0.6, max_tokens=1024, top_p=0.95, ), ) # --------------------------------------------------------------------------- # Run Agent # --------------------------------------------------------------------------- if __name__ == "__main__": agent.print_response( "9.11 and 9.9 -- which is bigger?", stream=True, show_full_reasoning=True, )配置解析
model=Claude(id="claude-sonnet-4-5"):主模型为 Anthropic Claude,负责把推理结果组织成最终回答。reasoning_model=Groq(id="openai/gpt-oss-120b", ...):推理模型换成了 Groq 托管的 OpenAI GPT-OSS 开放权重模型。也就是说,推理与作答可以由完全不同的两家厂商模型承担:Groq 负责"想",Anthropic 负责"写"。- 该示例需同时配置
GROQ_API_KEY与ANTHROPIC_API_KEY。
底层原理:Groq 推理模型如何被识别
Agno 通过 reasoning/groq.py 中的is_groq_reasoning_model()判断推理模型是否属于 Groq 生态,判定条件为模型类名是Groq且模型 id 包含deepseek、gpt-oss或qwen3之一:
def is_groq_reasoning_model(reasoning_model: Model) -> bool: return reasoning_model.__class__.__name__ == "Groq" and ( "deepseek" in reasoning_model.id.lower() or "gpt-oss" in reasoning_model.id.lower() or "qwen3" in reasoning_model.id.lower() )因此示例二中的openai/gpt-oss-120b(id 含gpt-oss)会被判定为 Groq 推理模型,走 Groq 专属的推理提取逻辑。被判定后,get_groq_reasoning()(同步)与aget_groq_reasoning()(异步)负责真正执行推理:它们用reasoning_agent.run(input=messages)单独运行一次推理 Agent,然后把回答中<think>…</think>标签之间的内容提取为reasoning_content,最终封装为role="assistant"且内容形如<thinking>...</thinking>的Message注入主对话(见 groq.py)。
需要说明的是:模型 id 与可用性随 Groq 平台动态变化。TEST_LOG.md 中记录了开发者因 Groq 不再服务某 Qwen 模型而将推理模型替换为openai/gpt-oss-20b的实例,说明遇到 404/模型不可用时,应根据 Groq Models API 当前实际提供的模型 id 调整。
示例三:有无推理模型的响应速度对比
完整代码
import time from agno.agent import Agent from agno.models.deepseek import DeepSeek from agno.models.groq import Groq from rich.console import Console # --------------------------------------------------------------------------- # Create Agents # --------------------------------------------------------------------------- console = Console() task = "What is 23 x 47? Show your step-by-step reasoning." # Fast agent - no reasoning model fast_agent = Agent( model=Groq(id="openai/gpt-oss-120b"), markdown=True, ) # Reasoning agent - uses DeepSeek for thinking reasoning_agent = Agent( model=Groq(id="qwen/qwen3.6-27b"), reasoning_model=DeepSeek(id="deepseek-reasoner"), markdown=True, ) # --------------------------------------------------------------------------- # Run Agents # --------------------------------------------------------------------------- if __name__ == "__main__": console.rule("[bold cyan]Groq Fast Reasoning Demo[/bold cyan]") console.rule("[bold green]Fast Agent (No Reasoning)[/bold green]") start = time.time() fast_agent.print_response(task, stream=True) console.print(f"\n[dim]Response time: {time.time() - start:.2f}s[/dim]") console.rule("[bold blue]Reasoning Agent (DeepSeek)[/bold blue]") start = time.time() reasoning_agent.print_response(task, stream=True, show_full_reasoning=True) console.print(f"\n[dim]Response time: {time.time() - start:.2f}s[/dim]")配置解析
fast_agent:只用 Groq 的openai/gpt-oss-120b,不配置reasoning_model,直接作答。reasoning_agent:Groq 的 Qwen 模型作答,配DeepSeek(id="deepseek-reasoner")先思考。- 速度度量:脚本用 Python 标准库
time.time()分别记录两次print_response(..., stream=True)的耗时,并用rich.console.Console的rule()与print()在终端中醒目地分隔与输出耗时结果。
结果解读
这是本目录唯一一个带"度量"的示例,它的目的不是断言"推理必然变慢",而是展示如何在同一任务上对比两种配置的端到端耗时。实际运行中通常可以预期:启用推理模型的 Agent 因多了一次推理 Agent 调用,首包时间与总耗时一般高于直接作答的快速 Agent;但这种额外延迟换来了对复杂问题(如本示例要求分步演算的乘法)更严谨的思考过程。
值得强调的是:不应仅凭本脚本的单次计时得出"Groq 快、推理慢"的普适结论。真实的延迟还取决于模型负载、网络与所选模型本身。该脚本的价值在于提供了一套可复现的对比框架——若要严谨评估,应多次运行取平均值。
深度原理:Agno 的推理(Reasoning)机制
三个示例背后共享同一个架构:推理与作答分离的双模型(甚至双厂商)流水线。其关键机制如下:
reasoning_model是开关:只要为 Agent 提供reasoning_model,Agno 就会在正式回答前,用一个独立的内部 Agent(reasoning_agent)运行一次推理,见 agent.py 中reasoning_model与reasoning_agent字段。- 按厂商分发推理逻辑:Agno 根据推理模型的类型与 id,将任务分发到对应的推理实现模块。Groq 生态走 reasoning/groq.py,其中
get_groq_reasoning_stream()支持流式推理:逐个事件累积reasoning_content或主内容,边思考边把内容吐给用户;DeepSeek、OpenAI、Gemini、Anthropic、Ollama 等厂商也都有各自独立的推理模块(同目录下的deepseek.py、openai.py、gemini.py、anthropic.py等)。 - 推理结果注入对话:推理内容被包装成
<thinking>...</thinking>形式的Message(携带reasoning_content字段)加入主对话上下文,供主模型参考后生成最终回答;这也是show_full_reasoning=True时用户能看到完整思考过程的原因。 - 指标聚合:推理 Agent 的运行指标会以
reasoning前缀聚合进父级运行指标中(见 groq.py),便于观测推理环节的额外开销。
这套机制的收益在于:开发者可以把"廉价快速的作答模型"与"昂贵但严谨的推理模型"自由组合,甚至跨厂商混搭(如示例二),在成本、速度与推理质量之间按需取舍。
延伸阅读
- cookbook/10_reasoning/models/groq/README.md:本目录的官方说明。
- cookbook/10_reasoning/models/groq/TEST_LOG.md:示例的实际运行验证记录。
- Groq 模型实现:
Groq模型类完整源码,包含全部请求参数(frequency_penalty、logit_bias、seed、stop、top_logprobs、user、extra_headers等)与客户端参数(base_url、timeout、max_retries等)。 - Groq 推理逻辑:Groq 推理模型的识别、同步/异步推理与流式推理实现。
- Agent 推理配置:
reasoning_model与reasoning_agent字段定义。 - 同目录的兄弟示例:cookbook/10_reasoning/models/deepseek/ 等展示了其他厂商推理模型的用法,可作为跨模型对比参考。
【免费下载链接】agnoBuild, run, and manage agent platforms.项目地址: https://gitcode.com/GitHub_Trending/ag/agno
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考