获取Gemini API KEY

准备3-4个谷歌账号,因为5月2日之后谷歌就开始收费,Gemini 1.5免费1分钟2次请求,一天50次,多个账号均衡一下请求,满足日常使用。
可以按照大佬分享方法注册账号 无需手机号认证注册谷歌邮箱
进入谷歌获取Get API key
现在不用申请直接可以使用的1.5模型

代理谷歌请求

Cloudflare 代理

可以参考zhile的 我们也要用Gemini Pro

Nginx代理

原理和上面一致,通过Nginx是把代理放到个人小机上面,这样要求你的小鸡能够正常访问谷歌。

这种形式的好处就是以固定IP的形式访问谷歌大模型服务。

在服务器运行测试命令,替换 YOUR_API_KEY 为你的API key,有回复可以进行下一步

curl \
  -H 'Content-Type: application/json' \
  -d '{"contents":[{"parts":[{"text":"Write a story about a magic backpack"}]}]}' \
  -X POST 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=YOUR_API_KEY'

Nginx配置server部分如下:

server {
    listen       443  ssl;
    server_name  _;
    root         /xxx/html/web;
    # ssl on;
    ssl_certificate /xxx/xxx/ca.pem;
    ssl_certificate_key /xxx/xxx/private.key;

    allow   103.21.244.0/22;
    allow   103.22.200.0/22;
    allow   103.31.4.0/22;
    allow   141.101.64.0/18;
    allow   108.162.192.0/18;
    allow   190.93.240.0/20;
    allow   188.114.96.0/20;
    allow   197.234.240.0/22;
    allow   198.41.128.0/17;
    allow   162.158.0.0/15;
    allow   104.16.0.0/13;
    allow   104.24.0.0/14;
    allow   172.64.0.0/13;
    allow   131.0.72.0/22;
    deny    all;

    location ^~ /Safari1261/ {
         # Google Gemini API 服务代理
         proxy_ssl_server_name on;
         #proxy_pass  https://api.openai.com;
         proxy_pass  https://generativelanguage.googleapis.com/;
         proxy_buffering off;
         client_max_body_size    300m;
    }
}

配置说明

  1. 域名解析走 Cloudflare 的DNS代理模型,然后在nginx里配置cloudflare ip段访问限制,只允许cf过来的请求访问
  2. 代理规则,匹配前缀为Safari1261/后的请求,注意https://generativelanguage.googleapis.com/ 后面是带 / ,注意转发到 Google 是不会带Safari1261/ 路径,另外注意配置不直接向互联网暴露谷歌代理服务,还可以和其他API服务共用443端口
  3. 配置root目录,编写一个简单的HTML页面,伪装成正常的站点,没有可以注释掉
  4. 不要加proxy_set_header X-Forwarded-For 此类的转发信息,否则会请求失败

测试代理是否配置正常,替换域名和API key

curl \
  -H 'Content-Type: application/json' \
  -d '{"contents":[{"parts":[{"text":"Write a story about a magic backpack"}]}]}' \
  -X POST 'https://example.com/Safari1261/v1beta/models/gemini-1.5-pro-latest:generateContent?key=YOUR_API_KEY'

接入One Api

使用one API 的好处就是可以代理不同种类的大模型服务,不需要修改客户端太多配置就能使用模型

新建渠道:

  1. 渠道类型: Google Gemini
  2. 渠道名:自己填写
  3. 请求地址: https://example.com/Safari1261
  4. 版本号:v1beta
  5. 模型:google-gemini-1.0google-gemini-1.5, google-gemini-vision
  6. 密钥:第一步获取的API key
  7. 模型映射: 模型映射
{
  "google-gemini-1.0": "gemini-1.0-pro-latest",
  "google-gemini-1.5": "gemini-1.5-pro-latest",
  "google-gemini-vision": "gemini-pro-vision"
}

注意没有用默认的模型,因为让客户端请求统一都走OpenAI的请求方式
通过以下命令查看可用的模型

curl https://example.com/Safari1261/v1beta/models?key=YOUR_API_KEY

返回如下结果:

{
  "models": [
    {
      "name": "models/chat-bison-001",
      "version": "001",
      "displayName": "PaLM 2 Chat (Legacy)",
      "description": "A legacy text-only model optimized for chat conversations",
      "inputTokenLimit": 4096,
      "outputTokenLimit": 1024,
      "supportedGenerationMethods": [
        "generateMessage",
        "countMessageTokens"
      ],
      "temperature": 0.25,
      "topP": 0.95,
      "topK": 40
    },
    {
      "name": "models/text-bison-001",
      "version": "001",
      "displayName": "PaLM 2 (Legacy)",
      "description": "A legacy model that understands text and generates text as an output",
      "inputTokenLimit": 8196,
      "outputTokenLimit": 1024,
      "supportedGenerationMethods": [
        "generateText",
        "countTextTokens",
        "createTunedTextModel"
      ],
      "temperature": 0.7,
      "topP": 0.95,
      "topK": 40
    },
    {
      "name": "models/embedding-gecko-001",
      "version": "001",
      "displayName": "Embedding Gecko",
      "description": "Obtain a distributed representation of a text.",
      "inputTokenLimit": 1024,
      "outputTokenLimit": 1,
      "supportedGenerationMethods": [
        "embedText",
        "countTextTokens"
      ]
    },
    {
      "name": "models/gemini-1.0-pro",
      "version": "001",
      "displayName": "Gemini 1.0 Pro",
      "description": "The best model for scaling across a wide range of tasks",
      "inputTokenLimit": 30720,
      "outputTokenLimit": 2048,
      "supportedGenerationMethods": [
        "generateContent",
        "countTokens"
      ],
      "temperature": 0.9,
      "topP": 1,
      "topK": 1
    },
    {
      "name": "models/gemini-1.0-pro-001",
      "version": "001",
      "displayName": "Gemini 1.0 Pro 001 (Tuning)",
      "description": "The best model for scaling across a wide range of tasks. This is a stable model that supports tuning.",
      "inputTokenLimit": 30720,
      "outputTokenLimit": 2048,
      "supportedGenerationMethods": [
        "generateContent",
        "countTokens",
        "createTunedModel"
      ],
      "temperature": 0.9,
      "topP": 1,
      "topK": 1
    },
    {
      "name": "models/gemini-1.0-pro-latest",
      "version": "001",
      "displayName": "Gemini 1.0 Pro Latest",
      "description": "The best model for scaling across a wide range of tasks. This is the latest model.",
      "inputTokenLimit": 30720,
      "outputTokenLimit": 2048,
      "supportedGenerationMethods": [
        "generateContent",
        "countTokens"
      ],
      "temperature": 0.9,
      "topP": 1,
      "topK": 1
    },
    {
      "name": "models/gemini-1.0-pro-vision-latest",
      "version": "001",
      "displayName": "Gemini 1.0 Pro Vision",
      "description": "The best image understanding model to handle a broad range of applications",
      "inputTokenLimit": 12288,
      "outputTokenLimit": 4096,
      "supportedGenerationMethods": [
        "generateContent",
        "countTokens"
      ],
      "temperature": 0.4,
      "topP": 1,
      "topK": 32
    },
    {
      "name": "models/gemini-1.5-pro-latest",
      "version": "001",
      "displayName": "Gemini 1.5 Pro",
      "description": "Mid-size multimodal model that supports up to 1 million tokens",
      "inputTokenLimit": 1048576,
      "outputTokenLimit": 8192,
      "supportedGenerationMethods": [
        "generateContent",
        "countTokens"
      ],
      "temperature": 1,
      "topP": 0.95
    },
    {
      "name": "models/gemini-pro",
      "version": "001",
      "displayName": "Gemini 1.0 Pro",
      "description": "The best model for scaling across a wide range of tasks",
      "inputTokenLimit": 30720,
      "outputTokenLimit": 2048,
      "supportedGenerationMethods": [
        "generateContent",
        "countTokens"
      ],
      "temperature": 0.9,
      "topP": 1,
      "topK": 1
    },
    {
      "name": "models/gemini-pro-vision",
      "version": "001",
      "displayName": "Gemini 1.0 Pro Vision",
      "description": "The best image understanding model to handle a broad range of applications",
      "inputTokenLimit": 12288,
      "outputTokenLimit": 4096,
      "supportedGenerationMethods": [
        "generateContent",
        "countTokens"
      ],
      "temperature": 0.4,
      "topP": 1,
      "topK": 32
    },
    {
      "name": "models/embedding-001",
      "version": "001",
      "displayName": "Embedding 001",
      "description": "Obtain a distributed representation of a text.",
      "inputTokenLimit": 2048,
      "outputTokenLimit": 1,
      "supportedGenerationMethods": [
        "embedContent"
      ]
    },
    {
      "name": "models/text-embedding-004",
      "version": "004",
      "displayName": "Text Embedding 004",
      "description": "Obtain a distributed representation of a text.",
      "inputTokenLimit": 2048,
      "outputTokenLimit": 1,
      "supportedGenerationMethods": [
        "embedContent"
      ]
    },
    {
      "name": "models/aqa",
      "version": "001",
      "displayName": "Model that performs Attributed Question Answering.",
      "description": "Model trained to return answers to questions that are grounded in provided sources, along with estimating answerable probability.",
      "inputTokenLimit": 7168,
      "outputTokenLimit": 1024,
      "supportedGenerationMethods": [
        "generateAnswer"
      ],
      "temperature": 0.2,
      "topP": 1,
      "topK": 40
    }
  ]
}

再以同样的步骤创建其他API key的渠道,只要在渠道的优先级保持一样,One API再收到请求时会自动均衡,优先级不一样,优先级低的请求次数会变少

NextChat客户端使用

这里使用ChatGPT-Next-Web ,采用Vercel部署程序,部署方法参考项目的说明

进入项目Settings->Environment Variables 设置CUSTOM_MODELS参数

+all,-gemini-pro,-gemini-1.0-pro,-gemini-pro-vision,-gemini-1.5-pro-latest,+google-gemini-1.0,+google-gemini-1.5

+all启用所有模型,然后-是不显示哪些模型,+新增哪些模型

修改完上面的配置,回到Deployments 选择最新的部署版本,点后面的...,选择Redeploy,等待重新部署完成。

经过上面的配置,回到ChatGPT-Next-Web 界面,如果以前使用过gpt的服务,直接选择google-gemini-1.5 就可以使用Gemini 大模型了,测试模型服务是否正常。
如果没有使用过,进入设置界面,点击自定义接口,配置OpenAI注意不是配置Google,填上下面的接口地址(One API的服务地址不带/v1)和API Key(One API的令牌)。

ChatGPT-Next-Web使用小窍门,配置可以放到链接里,方便分享给朋友们使用,进入页面后点击确定就可以直接使用

 https://nextchat域名/#/?settings={"key":"sk-xx","url":"https://oneapi域名:443"}

到这里就可以优雅流畅的使用Google Gemini 1.5 Pro服务了。