Fork me on GitHub

云端模型_vs_本地模型部署

OpenRoute云端模型(推荐)

  1、前往官网 OpenRouter  创建API Key,自己的API key可以存放于系统的环境变量中(去年一个BT币大佬把密钥放在代码里面直播时暴露了,两分钟被人转走了过10W美金)

image

  2、搜索云端模型:Compare AI Models: Pricing, Context & Benchmarks | OpenRouter  搜索带free字段的模型。

image

  3、获取访问代码,需要根据自己的开发语言和工具找对应的模板,我用的是OpenAI(虚拟环境中先pip安装OpenAI库)。

image

  4、测试

from openai import OpenAI
import os

client = OpenAI(
  base_url="https://openrouter.ai/api/v1",
  api_key=os.getenv("DASHSCOPE_API_KEY") #环境变量进行保存,我创建了两个Key,为后面做区分
)

# First API call with reasoning
response = client.chat.completions.create(
  model="inclusionai/ling-3.0-flash:free",
  messages=[{
    "role": "system",
    "content": "你能做什么?"
  }
  ],
  # stream=True
  extra_body={"reasoning": {"enabled": True}}
)

response = response.choices[0].message

print(f"{response.content}")

image

Ollama部署本地模型

  1、软件安装及模型下载

  下载后打开软件默认的模型只有少数几个,前往模型库下载自己适配的模型,我的电脑配置是带着Xe集成显卡,因此尝试部署:

ollama run deepseek-r1:7b

  建议:集成显卡<2b,4G显存<8b,8G显存<14b.

  安装完毕后可以cmd查看模型:

image

image

  2、模型测试

from openai import OpenAI
import os

client = OpenAI(
  # base_url="https://openrouter.ai/api/v1"
  base_url="http://localhost:11434/v1", #本地部署
  api_key=os.getenv("DASHSCOPE_API_KEY") #环境变量进行保存
)

# First API call with reasoning
response = client.chat.completions.create(
  # model="inclusionai/ling-3.0-flash:free",
  model="deepseek-r1:7b",  #本地部署
  messages=[{
    "role": "system",
    "content": "你是谁?"
  }
  ],
  extra_body={"reasoning": {"enabled": False}}
)

response = response.choices[0].message

print(f"{response.content}")

image

posted @ 2026-08-04 15:25  张一默  阅读(3)  评论(0)    收藏  举报