模型提供商(云端)

ADK-Rust 支持通过 adk-model crate 访问多个云端 LLM 提供商。所有提供商都实现了 Llm trait,使其可以在您的 agents 中互换使用。

概述

┌─────────────────────────────────────────────────────────────────────┐
│                     Cloud Model Providers                           │
├─────────────────────────────────────────────────────────────────────┤
│                                                                     │
│   • Gemini (Google)    ⭐ Default    - Multimodal, large context    │
│   • OpenAI (GPT-4o)    🔥 Popular    - Best ecosystem               │
│   • Anthropic (Claude) 🧠 Smart      - Best reasoning               │
│   • DeepSeek           💭 Thinking   - Chain-of-thought, cheap      │
│   • Groq               ⚡ Ultra-Fast  - Fastest inference           │
│                                                                     │
│   For local/offline models, see:                                    │
│   • Ollama     → ollama.md                                          │
│   • mistral.rs → mistralrs.md                                       │
│                                                                     │
└─────────────────────────────────────────────────────────────────────┘

快速比较

提供商最佳用途速度成本主要特点
Gemini通用用途⚡⚡⚡💰多模态,大上下文
OpenAI可靠性⚡⚡💰💰最佳生态系统
Anthropic复杂推理⚡⚡💰💰最安全,最周全
DeepSeek思维链⚡⚡💰思考模式,廉价
Groq速度敏感型⚡⚡⚡⚡💰最快推理速度

步骤 1:安装

将您需要的提供商添加到您的 Cargo.toml 中:

[dependencies]
# Pick one or more providers:
adk-model = { version = "0.2", features = ["gemini"] }        # Google Gemini (default)
adk-model = { version = "0.2", features = ["openai"] }        # OpenAI GPT-4o
adk-model = { version = "0.2", features = ["anthropic"] }     # Anthropic Claude
adk-model = { version = "0.2", features = ["deepseek"] }      # DeepSeek
adk-model = { version = "0.2", features = ["groq"] }          # Groq (ultra-fast)

# Or all cloud providers at once:
adk-model = { version = "0.2", features = ["all-providers"] }

步骤 2:设置您的 API 密钥

export GOOGLE_API_KEY="your-key"      # Gemini
export OPENAI_API_KEY="your-key"      # OpenAI
export ANTHROPIC_API_KEY="your-key"   # Anthropic
export DEEPSEEK_API_KEY="your-key"    # DeepSeek
export GROQ_API_KEY="your-key"        # Groq

Gemini (Google) ⭐ 默认

最适合: 通用目的、多模态任务、大型文档

主要亮点:

  • 🖼️ 原生多模态(图像、视频、音频、PDF)
  • 📚 高达 2M token 上下文窗口
  • 💰 有竞争力的价格
  • ⚡ 快速推理

完整工作示例

use adk_rust::prelude::*;
use adk_rust::Launcher;
use std::sync::Arc;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    dotenvy::dotenv().ok();
    
    let api_key = std::env::var("GOOGLE_API_KEY")?;
    let model = GeminiModel::new(&api_key, "gemini-2.0-flash")?;

    let agent = LlmAgentBuilder::new("gemini_assistant")
        .description("Gemini-powered assistant")
        .instruction("You are a helpful assistant powered by Google Gemini. Be concise.")
        .model(Arc::new(model))
        .build()?;

    Launcher::new(Arc::new(agent)).run().await?;
    Ok(())
}

可用模型

模型描述上下文
gemini-2.0-flash快速、高效(推荐)1M tokens
gemini-2.5-flash最新闪存模型1M tokens
gemini-2.5-pro功能最强大2M tokens

示例输出

👤 用户: 这张图片里有什么?[上传了一张猫的照片]

🤖 Gemini: 我看到一只毛茸茸的橙色虎斑猫坐在窗台上。这只猫似乎在向外看,阳光照亮了它的皮毛。它有绿色的眼睛,并带有虎斑猫典型的独特条纹。

OpenAI (GPT-4o) 🔥 热门

最适合: 生产应用、可靠性能、广泛功能

主要亮点:

  • 🏆 行业标准
  • 🔧 出色的工具/函数调用
  • 📖 最佳文档和生态系统
  • 🎯 一致、可预测的输出

完整工作示例

use adk_rust::prelude::*;
use adk_rust::Launcher;
use std::sync::Arc;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    dotenvy::dotenv().ok();
    
    let api_key = std::env::var("OPENAI_API_KEY")?;
    let model = OpenAIClient::new(OpenAIConfig::new(&api_key, "gpt-4o"))?;

    let agent = LlmAgentBuilder::new("openai_assistant")
        .description("OpenAI-powered assistant")
        .instruction("You are a helpful assistant powered by OpenAI GPT-4o. Be concise.")
        .model(Arc::new(model))
        .build()?;

    Launcher::new(Arc::new(agent)).run().await?;
    Ok(())
}

可用模型

模型描述上下文
gpt-4o功能最强大,多模态128K tokens
gpt-4o-mini快速,经济高效128K tokens
gpt-4-turbo上一代旗舰128K tokens
o1推理模型128K tokens

示例输出

👤 用户: 写一首关于 Rust 编程的俳句

🤖 GPT-4o: 内存安全至极,所有权守护每字节— 编译器,吾友。

Anthropic (Claude) 🧠 智能

最适合: 复杂推理、安全关键型应用、长文档

主要亮点:

  • 🧠 卓越的推理能力
  • 🛡️ 最注重安全
  • 📚 200K token 上下文
  • ✍️ 出色的写作质量

完整工作示例

use adk_rust::prelude::*;
use adk_rust::Launcher;
use std::sync::Arc;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    dotenvy::dotenv().ok();
    
    let api_key = std::env::var("ANTHROPIC_API_KEY")?;
    let model = AnthropicClient::new(AnthropicConfig::new(&api_key, "claude-sonnet-4-20250514"))?;

    let agent = LlmAgentBuilder::new("anthropic_assistant")
        .description("Anthropic 支持的助手")
        .instruction("你是一个由 Anthropic Claude 提供支持的有用助手。请简洁且周到。")
        .model(Arc::new(model))
        .build()?;

    Launcher::new(Arc::new(agent)).run().await?;
    Ok(())
}

可用模型

模型描述上下文
claude-sonnet-4-20250514最新的 Claude 4 Sonnet200K tokens
claude-opus-4-2025050514功能最强大的 Claude 4200K tokens
claude-3-5-sonnet-20241022Claude 3.5 Sonnet200K tokens

示例输出

👤 User: Explain quantum entanglement to a 10-year-old

🤖 Claude: Imagine you have two magic coins. When you flip them, they always 
land the same way - both heads or both tails - even if one coin is on Earth 
and the other is on the Moon! Scientists call this "entanglement." The coins 
are connected in a special way that we can't see, like invisible best friends 
who always make the same choice at the exact same time.

DeepSeek 💭 思考

最适合: 复杂问题解决、数学、编码、推理任务

主要亮点:

  • 💭 思考模式 - 显示思维链推理
  • 💰 极具成本效益(比 GPT-4 便宜 10 倍)
  • 🔄 对重复前缀进行上下文缓存
  • 🧮 擅长数学和编码

完整工作示例

use adk_rust::prelude::*;
use adk_rust::Launcher;
use std::sync::Arc;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    dotenvy::dotenv().ok();
    
    let api_key = std::env::var("DEEPSEEK_API_KEY")?;
    
    // 标准聊天模型
    let model = DeepSeekClient::chat(&api_key)?;
    
    // 或者:带思考模式的推理模型
    // let model = DeepSeekClient::reasoner(&api_key)?;

    let agent = LlmAgentBuilder::new("deepseek_assistant")
        .description("DeepSeek 支持的助手")
        .instruction("你是一个由 DeepSeek 提供支持的有用助手。请简洁。")
        .model(Arc::new(model))
        .build()?;

    Launcher::new(Arc::new(agent)).run().await?;
    Ok(())
}

可用模型

模型描述特殊功能
deepseek-chat快速聊天模型通用目的
deepseek-reasoner推理模型显示思考过程

示例输出(带思考模式的推理器)

👤 User: What's 17 × 23?

🤖 DeepSeek: <thinking>
Let me break this down:
17 × 23 = 17 × (20 + 3)
       = 17 × 20 + 17 × 3
       = 340 + 51
       = 391
</thinking>

The answer is 391.

Groq ⚡ 超高速

最适合: 实时应用、聊天机器人、速度敏感型任务

主要亮点:

  • 最快推理 - 比竞品快10倍
  • 🔧 LPU (Language Processing Unit) 技术
  • 💰 具有竞争力的价格
  • 🦙 运行 LLaMA、Mixtral、Gemma 模型

完整工作示例

use adk_rust::prelude::*;
use adk_rust::Launcher;
use std::sync::Arc;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    dotenvy::dotenv().ok();
    
    let api_key = std::env::var("GROQ_API_KEY")?;
    let model = GroqClient::llama70b(&api_key)?;

    let agent = LlmAgentBuilder::new("groq_assistant")
        .description("Groq-powered assistant")
        .instruction("You are a helpful assistant powered by Groq. Be concise and fast.")
        .model(Arc::new(model))
        .build()?;

    Launcher::new(Arc::new(agent)).run().await?;
    Ok(())
}

可用模型

模型方法描述
llama-3.3-70b-versatileGroqClient::llama70b()功能最强大
llama-3.1-8b-instantGroqClient::llama8b()速度最快
mixtral-8x7b-32768Custom config良好平衡

示例输出

👤 User: Quick! Name 5 programming languages

🤖 Groq (in 0.2 seconds): 
1. Rust
2. Python
3. JavaScript
4. Go
5. TypeScript

切换提供商

所有提供商都实现了相同的 Llm trait,因此切换很简单:

use adk_agent::LlmAgentBuilder;
use std::sync::Arc;

// Just change the model - everything else stays the same!
let model: Arc<dyn adk_core::Llm> = Arc::new(
    // Pick one:
    // GeminiModel::new(&api_key, "gemini-2.0-flash")?
    // OpenAIClient::new(OpenAIConfig::new(&api_key, "gpt-4o"))?
    // AnthropicClient::new(AnthropicConfig::new(&api_key, "claude-sonnet-4-20250514"))?
    // DeepSeekClient::chat(&api_key)?
    // GroqClient::llama70b(&api_key)?
);

let agent = LlmAgentBuilder::new("assistant")
    .instruction("You are a helpful assistant.")
    .model(model)
    .build()?;

示例

# Gemini (default)
cargo run --example quickstart

# OpenAI
cargo run --example openai_basic --features openai

# Anthropic
cargo run --example anthropic_basic --features anthropic

# DeepSeek
cargo run --example deepseek_basic --features deepseek
cargo run --example deepseek_reasoner --features deepseek  # Thinking mode

# Groq
cargo run --example groq_basic --features groq

相关


上一篇: ← 实时 Agent | 下一篇: Ollama (本地) →