> 技术文档 > OpenRouter开源的AI大模型路由工具,统一API调用

OpenRouter开源的AI大模型路由工具,统一API调用


简介

OpenRouter是一个开源的路由工具‌,它可以绕过限制调用GPT、Claude等国外模型。以下是对它的详细介绍:

一、主要功能

OpenRouter专注于将用户请求智能路由到不同的AI模型,并提供统一的访问接口。它就像一个“路由器”,能够根据预设规则或用户自定义策略,将用户输入的请求自动分配至最合适的AI模型。这大大简化了与AI模型交互的过程,降低了开发者的集成复杂度。

二、技术特点

  1. 多模型支持‌:OpenRouter集成了来自多个提供商的模型,包括知名的闭源模型(如OpenAI的GPT-4、Anthropic的Claude等)和开源模型(如LLaMA、Mistral等)。目前支持数十种模型,未来计划扩展到数百种。
  2. 统一API‌:OpenRouter的API与OpenAI的聊天API高度兼容,用户只需更换API密钥和基础URL,就能轻松切换模型,无需修改代码。
  3. 价格与性能优化‌:OpenRouter通过比较不同模型的定价和性能,帮助用户找到性价比最高的选项。它还会根据用户的需求(如低成本或高吞吐量)自动路由请求到最佳提供商。
  4. 自动路由与回退机制‌:通过“Auto Router”功能,OpenRouter会根据输入提示选择最适合的模型。如果某个模型或提供商不可用,它会自动尝试其他替代模型,确保服务不中断。

官网:OpenRouter

在OpenRouter,可以看到模型的排名,也可以看到某个模型的使用软件排名,比如DeepSeek-V3模型的使用软件排名为:

安装和设置

安装依赖库

只需要安装openai库即可

pip install openai

如果不是代码调用,而是直接用AI工具调用 OpenRouter ,那么openai也可以省略。

获取OpenRouter秘钥

登录openrouter官网,然后创建秘钥key,记下它,以备使用。

查找模型

在这里查找模型:Models | OpenRouter

决定使用DeepSeek: DeepSeek V3 Base (free) 这个模型。当然还可以使用DeepSeek: DeepSeek V3 0324 (free) 这个模型。

API接入

测试

可以使用如下语句测试API调用:

from openai import OpenAIclient = OpenAI( base_url=\"https://openrouter.ai/api/v1\", api_key=\"\",)completion = client.chat.completions.create( extra_headers={ \"HTTP-Referer\": \"\", # Optional. Site URL for rankings on openrouter.ai. \"X-Title\": \"\", # Optional. Site title for rankings on openrouter.ai. }, extra_body={}, model=\"deepseek/deepseek-v3-base:free\", messages=[ { \"role\": \"user\", \"content\": \"What is the meaning of life?\" } ])print(completion.choices[0].message.content)

把自己的key填进去就行了

from openai import OpenAIclient = OpenAI( base_url=\"https://openrouter.ai/api/v1\", api_key=\"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\",)completion = client.chat.completions.create( extra_headers={ \"HTTP-Referer\": \"airoot.org\", # Optional. Site URL for rankings on openrouter.ai. \"X-Title\": \"airoot\", # Optional. Site title for rankings on openrouter.ai. }, extra_body={}, model=\"deepseek/deepseek-v3-base:free\", messages=[ { \"role\": \"user\", \"content\": \"What is the meaning of life?\" } ])print(completion.choices[0].message.content)

但是在实际执行后就卡住了,不明白什么原因(后来就好了...不知道是不是时间和网络问题)

换用CURL进行测试,有时候会有空白的回车输出。不知道是不是openai版本不对导致的? (弄明白了,就是刚开始会打好几个回车符....)

curl https://openrouter.ai/api/v1/chat/completions \\ -H \"Content-Type: application/json\" \\ -H \"Authorization: Bearer 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\" \\ -d \'{ \"model\": \"deepseek/deepseek-chat-v3-0324:free\", \"messages\": [ { \"role\": \"user\", \"content\": \"What is the meaning of life?\" } ] }\'

也可用python直接调用requests进行了测试:

import requestsimport jsonresponse = requests.post( url=\"https://openrouter.ai/api/v1/chat/completions\", headers={ \"Authorization\": \"Bearer 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\", \"Content-Type\": \"application/json\", \"HTTP-Referer\": \"airoot.org\", # Optional. Site URL for rankings on openrouter.ai. \"X-Title\": \"airoot\", # Optional. Site title for rankings on openrouter.ai. }, data=json.dumps({ \"model\": \"deepseek/deepseek-chat-v3-0324:free\", \"messages\": [ { \"role\": \"user\", \"content\": \"What is the meaning of life?\" } ], }))print(response.content)

 测试通过,输出:

print(response.content)b\'\\n \\n\\n \\n\\n \\n\\n \\n\\n \\n\\n \\n\\n \\n\\n \\n\\n \\n\\n \\n\\n \\n\\n \\n\\n \\n\\n \\n\\n \\n\\n \\n\\n \\n\\n \\n\\n \\n\\n \\n\\n \\n\\n \\n\\n \\n\\n \\n{\"id\":\"gen-1743689975-7NxUuvZaolfKKM9NJaz7\",\"provider\":\"Chutes\",\"model\":\"deepseek/deepseek-chat-v3-0324\",\"object\":\"chat.completion\",\"created\":1743689975,\"choices\":[{\"logprobs\":null,\"finish_reason\":\"stop\",\"native_finish_reason\":\"stop\",\"index\":0,\"message\":{\"role\":\"assistant\",\"content\":\"The meaning of life is one of the most profound and debated questions in philosophy, religion, science, and personal reflection. Different perspectives offer various answers:\\\\n\\\\n### 1. **Philosophical Perspectives** \\\\n - **Existentialism (e.g., Sartre, Camus):** Life has no inherent meaning\\xe2\\x80\\x94it\\xe2\\x80\\x99s up to each individual to create their own purpose through choices and actions. \\\\n - **Absurdism (Camus):** The search for meaning in a meaningless universe is absurd, but we must embrace life passionately anyway. \\\\n - **Stoicism:** Meaning comes from living virtuously, accepting what we can\\xe2\\x80\\x99t control, and focusing on inner peace. \\\\n\\\\n### 2. **Religious/Spiritual Views** \\\\n - **Theistic religions (Christianity, Islam, Hinduism, etc.):** Life\\xe2\\x80\\x99s purpose is tied to divine will\\xe2\\x80\\x94serving God, achieving enlightenment, or fulfilling a spiritual destiny. \\\\n - **Buddhism:** Meaning arises from ending suffering (dukkha) through enlightenment (nirvana) and compassion. \\\\n\\\\n### 3. **Scientific Perspectives** \\\\n - **Evolutionary Biology:** Life\\xe2\\x80\\x99s \\\\\"purpose\\\\\" is survival and reproduction, but humans seek deeper significance beyond biology. \\\\n - **Cosmology:** In a vast, indifferent universe, meaning is a human construct\\xe2\\x80\\x94we give it value through curiosity, connection, and exploration. \\\\n\\\\n### 4. **Personal Meaning** \\\\n Many find purpose in relationships, creativity, helping others, personal growth, or leaving a legacy. Viktor Frankl (Holocaust survivor and psychiatrist) argued that meaning comes from suffering with dignity, love, and purposeful work. \\\\n\\\\n### **A Practical Answer?** \\\\nThere\\xe2\\x80\\x99s no single \\\\\"correct\\\\\" answer\\xe2\\x80\\x94it\\xe2\\x80\\x99s a personal journey. You might explore: \\\\n- What brings you joy or fulfillment? \\\\n- How do you impact others? \\\\n- What legacy do you want to leave? \\\\n\\\\nWould you like insights from a specific tradition or thinker?\",\"refusal\":null}}],\"usage\":{\"prompt_tokens\":10,\"completion_tokens\":417,\"total_tokens\":427}}\'

在Cherry Studio中配置使用

打开Cherry Studio,点击设置,在模型平台里找到OpenRouter,写入API秘钥,在“管理”里面加入DeepSeek-v3模型,点击“检查”, 测试通过。

在“助手”也就是chat页面,进行对话:

总结

OpenRouter可以很好的绕过封锁!

另外OpenRouter还提供一些免费调用AI大模型调用,比如DeepSeek-v3就可以免费调用,真是太棒了!

 

另外根据调试记录,有如下3点需要注意:

1 秘钥key注意写对

2 有的模型会输出一些回车,导致误以为模型有问题

3 有时候会很慢或卡住,但换个时间就好了。

调试

测试curl调用报错{\"error\":{\"message\":\"Input required: specify \\\"prompt\\\" or \\\"messages\\\"\",\"code\":400}

{\"error\":{\"message\":\"Input required: specify \\\"prompt\\\" or \\\"messages\\\"\",\"code\":400},\"user_id\":\"user_2v6XcG4tOfqFTUs1itXIJCI4Mic\"}

不明白哪里错了,反正最后就没报错了。

报错:\"No auth credentials found\"

key写错了,改正即可

使用openai和curl测试没有得到期望的输出

有时候可以看到光标在往下走,但是看不到输出信息。不明白为什么。

换用DeepSeek: DeepSeek V3 0324 (free)模型就好了。

在换回deepseek/deepseek-v3-base:free 这个模型,也测试通过了。

需要注意的3点:

1 秘钥key注意写对

2 有的模型会输出一些回车,导致误以为模型有问题

3 有时候会很慢或卡住,但换个时间就好了。