Appearance
Ollama 云服务
Ollama 云服务是一种全新的模型运行方式,让您无需强大的 GPU 即可运行大型语言模型。云模型会自动卸载到 Ollama 的云服务上,同时提供与本地模型相同的功能,让您能够继续使用本地工具运行那些无法在个人电脑上运行的大型模型。
🌟 云模型特点
主要优势
- 无需强大硬件: 不需要高端 GPU 即可运行大型模型
- 无缝集成: 与本地 Ollama 工具完全兼容
- 相同体验: 提供与本地模型相同的 API 和功能
- 自动卸载: 智能地将计算任务转移到云端
- 成本效益: 按需使用,无需投资昂贵硬件
适用场景
- 个人开发者想要体验大型模型
- 硬件资源有限的用户
- 需要偶尔使用大型模型的场景
- 原型开发和测试
- 教育和学习用途
📋 支持的云模型
Ollama 目前支持以下云模型,更多模型即将推出:
可用模型列表
- deepseek-v3.1:671b-cloud - DeepSeek V3.1 (671B 参数)
- gpt-oss:20b-cloud - GPT-OSS (20B 参数)
- gpt-oss:120b-cloud - GPT-OSS (120B 参数)
- kimi-k2:1t-cloud - Kimi K2 (1T 参数)
- qwen3-coder:480b-cloud - Qwen3 Coder (480B 参数)
模型特点
| 模型 | 参数量 | 特长领域 | 适用场景 |
|---|---|---|---|
| deepseek-v3.1:671b-cloud | 671B | 通用对话、推理 | 复杂问题解答、创意写作 |
| gpt-oss:20b-cloud | 20B | 轻量级对话 | 日常对话、简单任务 |
| gpt-oss:120b-cloud | 120B | 高质量对话 | 专业咨询、深度分析 |
| kimi-k2:1t-cloud | 1T | 超大规模推理 | 复杂推理、专业领域 |
| qwen3-coder:480b-cloud | 480B | 代码生成 | 编程助手、代码审查 |
🚀 开始使用云模型
1. 账户注册和登录
首先需要在 ollama.com 创建账户:
bash
# 登录或创建账户
ollama signin按照提示完成以下步骤:
- 输入邮箱地址
- 验证邮箱(检查收件箱)
- 设置密码
- 完成账户创建
2. 运行云模型
登录成功后,可以直接运行云模型:
bash
# 运行 GPT-OSS 120B 云模型
ollama run gpt-oss:120b-cloud
# 运行 DeepSeek V3.1 云模型
ollama run deepseek-v3.1:671b-cloud
# 运行 Qwen3 Coder 云模型
ollama run qwen3-coder:480b-cloud3. 交互式对话
bash
# 启动交互式对话
ollama run gpt-oss:120b-cloud
# 在提示符下输入问题
>>> 请解释什么是机器学习?
# 退出对话
>>> /bye🔌 API 访问
直接 API 访问
云模型也可以通过 ollama.com 的 API 直接访问,此时 ollama.com 充当远程 Ollama 主机。
1. 创建 API 密钥
- 访问 ollama.com
- 登录您的账户
- 进入 API 设置页面
- 创建新的 API 密钥
- 安全保存密钥
2. 列出可用模型
bash
# 获取可用模型列表
curl https://ollama.com/api/tags3. Python API 调用
首先安装 Ollama Python 库:
bash
pip install ollama然后进行 API 调用:
python
from ollama import Client
# 创建客户端
client = Client(
host="https://ollama.com",
headers={'Authorization': '<your-api-key>'}
)
# 定义对话消息
messages = [
{
'role': 'user',
'content': 'Why is the sky blue?',
},
]
# 流式响应
for part in client.chat('gpt-oss:120b', messages=messages, stream=True):
print(part['message']['content'], end='', flush=True)4. JavaScript API 调用
javascript
// 安装 ollama JavaScript 库
// npm install ollama
import { Ollama } from 'ollama'
const ollama = new Ollama({
host: 'https://ollama.com',
headers: {
'Authorization': '<your-api-key>'
}
})
const messages = [
{
role: 'user',
content: 'Explain quantum computing in simple terms'
}
]
// 流式响应
const response = await ollama.chat({
model: 'gpt-oss:120b-cloud',
messages: messages,
stream: true,
})
for await (const part of response) {
process.stdout.write(part.message.content)
}5. cURL 示例
bash
# 发送聊天请求
curl -X POST https://ollama.com/api/chat \
-H "Content-Type: application/json" \
-H "Authorization: <your-api-key>" \
-d '{
"model": "gpt-oss:120b-cloud",
"messages": [
{
"role": "user",
"content": "写一首关于春天的诗"
}
],
"stream": false
}'
# 生成文本
curl -X POST https://ollama.com/api/generate \
-H "Content-Type: application/json" \
-H "Authorization: <your-api-key>" \
-d '{
"model": "qwen3-coder:480b-cloud",
"prompt": "编写一个 Python 函数来计算斐波那契数列",
"stream": false
}'⚙️ 高级配置
环境变量设置
bash
# 设置云服务 API 密钥
export OLLAMA_API_KEY="your-api-key"
# 设置云服务主机
export OLLAMA_CLOUD_HOST="https://ollama.com"
# 设置请求超时时间
export OLLAMA_REQUEST_TIMEOUT="300"混合使用本地和云模型
python
from ollama import Client
# 本地客户端
local_client = Client(host="http://localhost:11434")
# 云服务客户端
cloud_client = Client(
host="https://ollama.com",
headers={'Authorization': '<your-api-key>'}
)
# 根据需求选择使用本地或云模型
def get_response(prompt, use_cloud=False):
if use_cloud:
response = cloud_client.generate(
model='gpt-oss:120b-cloud',
prompt=prompt
)
else:
response = local_client.generate(
model='llama3.2',
prompt=prompt
)
return response['response']
# 使用示例
local_response = get_response("简单问题", use_cloud=False)
cloud_response = get_response("复杂推理问题", use_cloud=True)💰 计费和限制
使用限制
- 请求频率: 每分钟最多 60 次请求
- 并发连接: 最多 5 个并发连接
- 响应长度: 单次响应最多 4096 tokens
- 上下文长度: 根据模型不同而变化
计费方式
- 按 token 计费: 根据输入和输出 token 数量
- 免费额度: 新用户享有一定免费额度
- 订阅计划: 提供不同级别的订阅服务
查看使用情况
bash
# 查看账户使用情况
ollama usage
# 查看当前月份统计
ollama usage --month current
# 查看详细计费信息
ollama billing🔒 安全和隐私
数据安全
- 传输加密: 所有数据传输使用 HTTPS/TLS 加密
- 访问控制: 基于 API 密钥的身份验证
- 数据隔离: 用户数据完全隔离
- 合规性: 符合主要数据保护法规
隐私保护
- 数据不存储: 对话内容不会永久存储
- 匿名处理: 个人信息匿名化处理
- 用户控制: 用户完全控制数据使用
- 透明度: 清晰的隐私政策和使用条款
最佳实践
python
# 不要在代码中硬编码 API 密钥
import os
from ollama import Client
# 从环境变量读取 API 密钥
api_key = os.getenv('OLLAMA_API_KEY')
if not api_key:
raise ValueError("请设置 OLLAMA_API_KEY 环境变量")
client = Client(
host="https://ollama.com",
headers={'Authorization': api_key}
)
# 敏感信息处理
def sanitize_input(text):
# 移除或替换敏感信息
import re
# 移除邮箱
text = re.sub(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b', '[EMAIL]', text)
# 移除电话号码
text = re.sub(r'\b\d{3}-\d{3}-\d{4}\b', '[PHONE]', text)
return text🛠️ 故障排除
常见问题
1. 登录失败
bash
# 清除本地认证信息
ollama signout
# 重新登录
ollama signin2. API 密钥无效
- 检查密钥是否正确复制
- 确认密钥未过期
- 重新生成新的 API 密钥
3. 请求超时
python
# 增加超时时间
from ollama import Client
client = Client(
host="https://ollama.com",
headers={'Authorization': '<your-api-key>'},
timeout=300 # 5 分钟超时
)4. 模型不可用
bash
# 检查可用模型列表
curl https://ollama.com/api/tags
# 确认模型名称正确
ollama run gpt-oss:120b-cloud # 注意 -cloud 后缀错误代码说明
| 错误代码 | 说明 | 解决方案 |
|---|---|---|
| 401 | 未授权 | 检查 API 密钥 |
| 403 | 禁止访问 | 检查账户状态和权限 |
| 429 | 请求过多 | 降低请求频率 |
| 500 | 服务器错误 | 稍后重试或联系支持 |
| 503 | 服务不可用 | 检查服务状态页面 |
调试技巧
python
import logging
from ollama import Client
# 启用调试日志
logging.basicConfig(level=logging.DEBUG)
client = Client(
host="https://ollama.com",
headers={'Authorization': '<your-api-key>'}
)
try:
response = client.chat(
model='gpt-oss:120b-cloud',
messages=[{'role': 'user', 'content': 'Hello'}]
)
print(response)
except Exception as e:
print(f"错误: {e}")
# 查看详细错误信息
import traceback
traceback.print_exc()📊 性能优化
请求优化
python
# 批量处理请求
async def batch_requests(prompts, model='gpt-oss:120b-cloud'):
import asyncio
from ollama import AsyncClient
client = AsyncClient(
host="https://ollama.com",
headers={'Authorization': '<your-api-key>'}
)
async def process_prompt(prompt):
return await client.generate(model=model, prompt=prompt)
tasks = [process_prompt(prompt) for prompt in prompts]
results = await asyncio.gather(*tasks)
return results
# 使用连接池
import httpx
from ollama import Client
# 自定义 HTTP 客户端
http_client = httpx.Client(
limits=httpx.Limits(max_keepalive_connections=5, max_connections=10),
timeout=httpx.Timeout(30.0)
)
client = Client(
host="https://ollama.com",
headers={'Authorization': '<your-api-key>'},
http_client=http_client
)缓存策略
python
import hashlib
import json
from functools import lru_cache
class CloudModelCache:
def __init__(self, max_size=100):
self.cache = {}
self.max_size = max_size
def get_cache_key(self, model, prompt, **kwargs):
# 创建缓存键
data = {'model': model, 'prompt': prompt, **kwargs}
return hashlib.md5(json.dumps(data, sort_keys=True).encode()).hexdigest()
def get(self, model, prompt, **kwargs):
key = self.get_cache_key(model, prompt, **kwargs)
return self.cache.get(key)
def set(self, model, prompt, response, **kwargs):
key = self.get_cache_key(model, prompt, **kwargs)
if len(self.cache) >= self.max_size:
# 删除最旧的条目
oldest_key = next(iter(self.cache))
del self.cache[oldest_key]
self.cache[key] = response
# 使用缓存
cache = CloudModelCache()
def cached_generate(model, prompt, **kwargs):
# 检查缓存
cached_response = cache.get(model, prompt, **kwargs)
if cached_response:
return cached_response
# 调用 API
response = client.generate(model=model, prompt=prompt, **kwargs)
# 存储到缓存
cache.set(model, prompt, response, **kwargs)
return response