OpenRouter 支持

ToolUniverse 现已全面支持 OpenRouter,您可以通过一个统一的 API 访问来自多个提供商的多种 LLM 模型。

概述

OpenRouter 提供以下模型的访问权限:

  • **OpenAI**(GPT-5)

  • **Anthropic**(Claude Sonnet 4.5)

  • **Google**(Gemini 2.5 Flash,Gemini 2.5 Pro)

  • 以及更多!

配置

环境变量

要使用 OpenRouter,请设置以下环境变量:

# Required
export OPENROUTER_API_KEY="your_openrouter_api_key_here"

# Optional - for usage tracking and attribution
export OPENROUTER_SITE_URL="https://your-site.com"
export OPENROUTER_SITE_NAME="Your Application Name"

获取 API 密钥

  1. 访问 OpenRouter

  2. 注册账户

  3. 导航到 API 密钥部分

  4. 生成新的 API 密钥

  5. 将其复制并设置为 OPENROUTER_API_KEY 环境变量

使用 OpenRouter 与 AgenticTool

基本配置

配置 AgenticTool 使用 OpenRouter:

from tooluniverse import ToolUniverse
from tooluniverse.agentic_tool import AgenticTool

# Example tool configuration using OpenRouter
tool_config = {
    "name": "OpenRouter_Summarizer",
    "description": "Summarize text using OpenRouter models",
    "type": "AgenticTool",
    "prompt": "Summarize the following text:\n{text}",
    "input_arguments": ["text"],
    "parameter": {
        "type": "object",
        "properties": {
            "text": {
                "type": "string",
                "description": "The text to summarize",
                "required": True
            }
        },
        "required": ["text"]
    },
    "configs": {
        "api_type": "OPENROUTER",
        "model_id": "openai/gpt-5",
        "temperature": 0.7,
        "return_json": False
    }
}

# Initialize ToolUniverse and register the tool
tu = ToolUniverse()
tu.register_custom_tool(AgenticTool, tool_config=tool_config)

# Use the tool (requires OPENROUTER_API_KEY)
result = tu.run({"name": "OpenRouter_Summarizer", "arguments": {
    "text": "Your long text here..."
}})

使用 OpenRouter 配合 ToolFinderLLM

配置 ToolFinderLLM 以使用 OpenRouter 模型:

from tooluniverse import ToolUniverse
from tooluniverse.tool_finder_llm import ToolFinderLLM

# Create ToolUniverse instance
tu = ToolUniverse()

# Configure ToolFinderLLM with OpenRouter
tool_finder_config = {
    "type": "ToolFinderLLM",
    "name": "Tool_Finder_OpenRouter",
    "description": "Find tools using OpenRouter LLMs",
    "configs": {
        "api_type": "OPENROUTER",
        "model_id": "anthropic/claude-sonnet-4.5",
        "temperature": 0.1,
        "max_new_tokens": 4096,
        "return_json": True,
        "exclude_tools": ["Tool_RAG", "Tool_Finder", "Finish"]
    }
}

# Register and use (requires OPENROUTER_API_KEY)
tu.register_custom_tool(ToolFinderLLM, tool_config=tool_finder_config)
result = tu.run({"name": "Tool_Finder_OpenRouter", "arguments": {
    "description": "tools for protein analysis",
    "limit": 5
}})

备用配置

OpenRouter 被包含在默认的回退链中。如果主 API 失败,系统将自动尝试使用 OpenRouter:

# Default fallback chain (in order):
# 1. CHATGPT (Azure OpenAI)
# 2. OPENROUTER (with openai/gpt-5)
# 3. GEMINI (Google Gemini)

# You can customize the fallback chain with environment variable:
import os
import json

custom_chain = [
    {"api_type": "OPENROUTER", "model_id": "anthropic/claude-sonnet-4.5"},
    {"api_type": "OPENROUTER", "model_id": "openai/gpt-5"},
    {"api_type": "GEMINI", "model_id": "gemini-2.5-flash"}
]

os.environ["AGENTIC_TOOL_FALLBACK_CHAIN"] = json.dumps(custom_chain)

高级配置

自定义模型限制

为特定模型覆盖默认的令牌限制:

import os
import json

custom_limits = {
    "openai/gpt-5": {
        "max_output": 32000,  # Custom limit
        "context_window": 1048576
    },
    "anthropic/claude-sonnet-4.5": {
        "max_output": 4096,
        "context_window": 200000
    }
}

os.environ["OPENROUTER_DEFAULT_MODEL_LIMITS"] = json.dumps(custom_limits)