获取OpenAI API KEY

由于OpenAI官网的是不支持中国大陆的信用卡支付,这个步骤是比较复杂的。
目前是通过注册OCBC的借记卡实现支付,正常使用一个月了。OCBC银行卡办理网上有不少教程,不过关于推荐码不是一定要填,你不填也是可以注册成功,填了推荐码入金1000SGD之后推荐的人有奖励,个人返现不受推荐码限制。注册过程到拿到实体卡,到完全激活响应2周时间,入金通过工行转账2个小时就到账了。之后就可以付费了,选择美国免税区不用缴税。(题外话,原来办理OCBC是想着注册甲骨文云,实践下来不行)

另外还有一种简单方式 ,用中转服务,有一些汇率的手续费。
地址:https://api.oaipro.com/
充值就可以用,用这个中转,不需要配置nginx,直接跳到one api配置。

代理OpenAI请求

Nginx代理

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

这种形式的好处就是以固定IP的形式访问服务。

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

curl https://api.openai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "gpt-3.5-turbo-0125",
    "response_format": { "type": "json_object" },
    "messages": [
      {
        "role": "system",
        "content": "You are a helpful assistant designed to output JSON."
      },
      {
        "role": "user",
        "content": "Who won the world series in 2020?"
      }
    ]
  }'

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 ^~ /Trowel1563/ {
         # OpenAI API 服务代理
         proxy_ssl_server_name on;
         proxy_pass  https://api.openai.com;
         proxy_buffering off;
         client_max_body_size    300m;
    }
}

配置说明

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

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

curl https://example.com/Trowel1563/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "gpt-3.5-turbo-0125",
    "response_format": { "type": "json_object" },
    "messages": [
      {
        "role": "system",
        "content": "You are a helpful assistant designed to output JSON."
      },
      {
        "role": "user",
        "content": "Who won the world series in 2020?"
      }
    ]
  }'

接入One Api

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

新建渠道:

  1. 渠道类型: OpenAI
  2. 渠道名:自己填写
  3. 请求地址: https://example.com/Trowel1563
  4. 模型:gpt-4ogpt-3.5-turbo, gpt-4-turbo
  5. 密钥:第一步获取的API key

通过以下命令查看可用的模型

curl https://example.com/Trowel1563/v1/models \
  -H "Authorization: Bearer $OPENAI_API_KEY"

返回如下结果:

{
  "object": "list",
  "data": [
    {
      "id": "dall-e-3",
      "object": "model",
      "created": 1698785189,
      "owned_by": "system"
    },
    {
      "id": "whisper-1",
      "object": "model",
      "created": 1677532384,
      "owned_by": "openai-internal"
    },
    {
      "id": "davinci-002",
      "object": "model",
      "created": 1692634301,
      "owned_by": "system"
    },
    {
      "id": "dall-e-2",
      "object": "model",
      "created": 1698798177,
      "owned_by": "system"
    },
    {
      "id": "gpt-3.5-turbo-16k",
      "object": "model",
      "created": 1683758102,
      "owned_by": "openai-internal"
    },
    {
      "id": "tts-1-hd-1106",
      "object": "model",
      "created": 1699053533,
      "owned_by": "system"
    },
    {
      "id": "gpt-4o-2024-05-13",
      "object": "model",
      "created": 1715368132,
      "owned_by": "system"
    },
    {
      "id": "tts-1-hd",
      "object": "model",
      "created": 1699046015,
      "owned_by": "system"
    },
    {
      "id": "gpt-4o",
      "object": "model",
      "created": 1715367049,
      "owned_by": "system"
    },
    {
      "id": "gpt-4",
      "object": "model",
      "created": 1687882411,
      "owned_by": "openai"
    },
    {
      "id": "gpt-4-0613",
      "object": "model",
      "created": 1686588896,
      "owned_by": "openai"
    },
    {
      "id": "gpt-3.5-turbo-1106",
      "object": "model",
      "created": 1698959748,
      "owned_by": "system"
    },
    {
      "id": "gpt-3.5-turbo-0125",
      "object": "model",
      "created": 1706048358,
      "owned_by": "system"
    },
    {
      "id": "gpt-3.5-turbo-instruct-0914",
      "object": "model",
      "created": 1694122472,
      "owned_by": "system"
    },
    {
      "id": "gpt-3.5-turbo",
      "object": "model",
      "created": 1677610602,
      "owned_by": "openai"
    },
    {
      "id": "gpt-3.5-turbo-instruct",
      "object": "model",
      "created": 1692901427,
      "owned_by": "system"
    },
    {
      "id": "tts-1",
      "object": "model",
      "created": 1681940951,
      "owned_by": "openai-internal"
    },
    {
      "id": "gpt-3.5-turbo-0301",
      "object": "model",
      "created": 1677649963,
      "owned_by": "openai"
    },
    {
      "id": "babbage-002",
      "object": "model",
      "created": 1692634615,
      "owned_by": "system"
    },
    {
      "id": "gpt-4-1106-preview",
      "object": "model",
      "created": 1698957206,
      "owned_by": "system"
    },
    {
      "id": "gpt-4-turbo-2024-04-09",
      "object": "model",
      "created": 1712601677,
      "owned_by": "system"
    },
    {
      "id": "tts-1-1106",
      "object": "model",
      "created": 1699053241,
      "owned_by": "system"
    },
    {
      "id": "text-embedding-3-large",
      "object": "model",
      "created": 1705953180,
      "owned_by": "system"
    },
    {
      "id": "gpt-4-turbo",
      "object": "model",
      "created": 1712361441,
      "owned_by": "system"
    },
    {
      "id": "text-embedding-3-small",
      "object": "model",
      "created": 1705948997,
      "owned_by": "system"
    },
    {
      "id": "gpt-3.5-turbo-0613",
      "object": "model",
      "created": 1686587434,
      "owned_by": "openai"
    },
    {
      "id": "text-embedding-ada-002",
      "object": "model",
      "created": 1671217299,
      "owned_by": "openai-internal"
    },
    {
      "id": "gpt-4-1106-vision-preview",
      "object": "model",
      "created": 1711473033,
      "owned_by": "system"
    },
    {
      "id": "gpt-4-0125-preview",
      "object": "model",
      "created": 1706037612,
      "owned_by": "system"
    },
    {
      "id": "gpt-4-vision-preview",
      "object": "model",
      "created": 1698894917,
      "owned_by": "system"
    },
    {
      "id": "gpt-4-turbo-preview",
      "object": "model",
      "created": 1706037777,
      "owned_by": "system"
    },
    {
      "id": "gpt-3.5-turbo-16k-0613",
      "object": "model",
      "created": 1685474247,
      "owned_by": "openai"
    }
  ]
}

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

NextChat客户端使用

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

进入项目Settings->Environment Variables 设置CUSTOM_MODELS参数调整NextChat的模型选项

+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的服务,直接选择gpt-4o 就可以使用大模型了,测试模型服务是否正常。
如果没有使用过,进入设置界面,点击自定义接口,配置OpenAI,填上下面的接口地址(One API的服务地址不带/v1)和API Key(One API的令牌)。

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

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

到这里就可以优雅流畅的使用ChatGPT gpt-4o服务了。

最后再感叹一下gpt-4o响应速度确实快!!!