在 Agno 中组合 Groq 推理模型:reasoning_model 配置、模型混搭与速度对比实战

📅 发布时间:2026/9/10 22:50:19
在 Agno 中组合 Groq 推理模型:reasoning_model 配置、模型混搭与速度对比实战
在 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.pyGroq 主模型 DeepSeek 推理模型处理数值比较问题deepseek_plus_claude.pyGroq 托管推理模型思考Claude 负责作答fast_reasoning.py对比有无推理模型时的响应速度README 原文即通过这三个示例点明目录主旨Groq reasoning and model-combination examplesGroq 推理与模型组合示例。以下逐一展开。示例一Groq 主模型 DeepSeek 推理模型完整代码from agno.agent import Agent from agno.models.deepseek import DeepSeek from agno.models.groq import Groq # --------------------------------------------------------------------------- # Create Agent # --------------------------------------------------------------------------- agent Agent( modelGroq( idqwen/qwen3.6-27b, temperature0.6, max_tokens1024, top_p0.95, ), reasoning_modelDeepSeek(iddeepseek-reasoner), markdownTrue, ) # --------------------------------------------------------------------------- # Run Agent # --------------------------------------------------------------------------- if __name__ __main__: agent.print_response( 9.11 and 9.9 -- which is bigger?, streamTrue, show_full_reasoningTrue, )配置解析modelGroq(idqwen/qwen3.6-27b, ...)主模型用于最终作答是 Groq 托管的 Qwen 系列模型。temperature0.6、max_tokens1024、top_p0.95为推理采样参数。reasoning_modelDeepSeek(iddeepseek-reasoner)启用 Agent 推理的关键参数。Agno 中reasoning_model的官方注释为Enable reasoning by providing a reasoning_model (must be a native reasoning model)通过提供推理模型启用推理且必须是原生推理模型见 agent.py。此处指定 DeepSeek 的deepseek-reasoner作为思考引擎。markdownTrue让最终回答以 Markdown 格式渲染输出。show_full_reasoningTrue在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( modelClaude(idclaude-sonnet-4-5), reasoning_modelGroq( idopenai/gpt-oss-120b, temperature0.6, max_tokens1024, top_p0.95, ), ) # --------------------------------------------------------------------------- # Run Agent # --------------------------------------------------------------------------- if __name__ __main__: agent.print_response( 9.11 and 9.9 -- which is bigger?, streamTrue, show_full_reasoningTrue, )配置解析modelClaude(idclaude-sonnet-4-5)主模型为 Anthropic Claude负责把推理结果组织成最终回答。reasoning_modelGroq(idopenai/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-120bid 含gpt-oss会被判定为 Groq 推理模型走 Groq 专属的推理提取逻辑。被判定后get_groq_reasoning()同步与aget_groq_reasoning()异步负责真正执行推理它们用reasoning_agent.run(inputmessages)单独运行一次推理 Agent然后把回答中think…/think标签之间的内容提取为reasoning_content最终封装为roleassistant且内容形如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( modelGroq(idopenai/gpt-oss-120b), markdownTrue, ) # Reasoning agent - uses DeepSeek for thinking reasoning_agent Agent( modelGroq(idqwen/qwen3.6-27b), reasoning_modelDeepSeek(iddeepseek-reasoner), markdownTrue, ) # --------------------------------------------------------------------------- # 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, streamTrue) 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, streamTrue, show_full_reasoningTrue) console.print(f\n[dim]Response time: {time.time() - start:.2f}s[/dim])配置解析fast_agent只用 Groq 的openai/gpt-oss-120b不配置reasoning_model直接作答。reasoning_agentGroq 的 Qwen 模型作答配DeepSeek(iddeepseek-reasoner)先思考。速度度量脚本用 Python 标准库time.time()分别记录两次print_response(..., streamTrue)的耗时并用rich.console.Console的rule()与print()在终端中醒目地分隔与输出耗时结果。结果解读这是本目录唯一一个带度量的示例它的目的不是断言推理必然变慢而是展示如何在同一任务上对比两种配置的端到端耗时。实际运行中通常可以预期启用推理模型的 Agent 因多了一次推理 Agent 调用首包时间与总耗时一般高于直接作答的快速 Agent但这种额外延迟换来了对复杂问题如本示例要求分步演算的乘法更严谨的思考过程。值得强调的是不应仅凭本脚本的单次计时得出Groq 快、推理慢的普适结论。真实的延迟还取决于模型负载、网络与所选模型本身。该脚本的价值在于提供了一套可复现的对比框架——若要严谨评估应多次运行取平均值。深度原理Agno 的推理Reasoning机制三个示例背后共享同一个架构推理与作答分离的双模型甚至双厂商流水线。其关键机制如下reasoning_model是开关只要为 Agent 提供reasoning_modelAgno 就会在正式回答前用一个独立的内部 Agentreasoning_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_reasoningTrue时用户能看到完整思考过程的原因。指标聚合推理 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),仅供参考