Git Product home page Git Product logo

vercel-proxy-openai's Introduction

使用 Vercel 反代 openai (图文教程)

简介

国内只是墙了 vercel 本身的域名,但是自定义域名没有墙,因此做了代理并绑定域名之后就可以用自己的域名在国内直连 openai api 了。

理论上也可以代理其他被墙的站点。

使用方法

  • 访问 openai api 时,将 "api.openai.com" 换成你的自定义域名,例如

    const host = 'YOUR DOMAIN'
    const url = `https://${host}/v1/chat/completions`
  • 可参考 OPENAI API 官方文档

    官方文档中文翻译(部分过时,仅供参考)

  • 在其他 chatgpt 项目中,可以将变量 "OPENAI_API_BASE_URL" 设置为你的域名

要求

  • Openai API KEY, 形如 "sk-xxxxxxxx" 的字符串

  • 一个域名(无需备案),没有的话可以在阿里云上买一个几块钱一年的 阿里云域名注册

示例

Postman

Example of Postman, Header Example of Postman, Body

以下代码均由 Postman 生成

cURL

curl --location 'https://YOUR DOMAIN(改成你的域名)/v1/chat/completions' \

--header 'Authorization: Bearer sk-xxxxxxxxxxxxx(改成你的APIKEY)' \
--header 'Content-Type: application/json' \
--data '{
    "model": "gpt-3.5-turbo",
    "messages": [
        {
            "role": "user",
            "content": "你好"
        }
    ]
}
'

Python - Requests

import requests
import json

url = "https://YOUR DOMAIN(改成你的域名)/v1/chat/completions"

payload = json.dumps({
  "model": "gpt-3.5-turbo",
  "messages": [
    {
      "role": "user",
      "content": "你好"
    }
  ]
})
headers = {
  'Authorization': 'Bearer sk-xxxxxxxxxxxxx(改成你的APIKEY)',
  'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

NodeJs - Axios

const axios = require('axios');
let data = JSON.stringify({
  "model": "gpt-3.5-turbo",
  "messages": [
    {
      "role": "user",
      "content": "你好"
    }
  ]
});

let config = {
  method: 'post',
  maxBodyLength: Infinity,
  url: 'https://YOUR DOMAIN(改成你的域名)/v1/chat/completions',
  headers: { 
    'Authorization': 'Bearer sk-xxxxxxxxxxxxx(改成你的APIKEY)', 
    'Content-Type': 'application/json'
  },
  data : data
};

axios.request(config)
.then((response) => {
  console.log(JSON.stringify(response.data));
})
.catch((error) => {
  console.log(error);
});

JavaScript - Fetch

var myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer sk-xxxxxxxxxxxxx(改成你的APIKEY)");
myHeaders.append("Content-Type", "application/json");

var raw = JSON.stringify({
  "model": "gpt-3.5-turbo",
  "messages": [
    {
      "role": "user",
      "content": "你好"
    }
  ]
});

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: raw,
  redirect: 'follow'
};

fetch("https://YOUR DOMAIN(改成你的域名)/v1/chat/completions", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

飞书机器人

Example of Feishu robot

部署

  1. 点击一键部署按钮

    Deploy with Vercel

  2. 用 Github 登录 Vercel,没有 Github 账户的去注册一个,网上很多教程就不展开了 Vercel login

  3. 登录之后点击 Create 按钮 Vercel create

  4. 接着等十几秒钟就创建好项目了,接下来进入仪表盘 Vercel dashboard

  5. 进入到项目里之后,依次点击 Settings -> Domains,然后添加你的域名。添加的域名类型有两种,一种是一级域名(xxxx.com)和二级域名(openai.xxxx.com),我个人推荐使用二级域名,因为一级域名一般用来做网站展示用,只能有一个,而二级域名可以有无限个(只要你有一个域名就可以自己创建无限个二级域名) Vercel domains

  6. 添加域名有三种方式,这里我们选第三种,因为简单 Add Domain

  7. 接下来会分两种情况,分为一级域名和二级域名,都有教程,以阿里云为例(其他厂商也是差不多的配置,很简单的)

    1. 一级域名

      添加一级域名后 Vercel 会提示让你添加 DNS 解析记录 DNS Config 在阿里云域名解析的解析设置里点击 添加记录,按照 Vercel 的提示配置好图中三个选项,点击确认 DNS Set 回 Vercel 点击 Refresh 按钮,出现下图所示的情况就表明配置完成了 Complete

    2. 二级域名(以 openai 主机记录为例,可以改成自己喜欢的)

      添加二级域名后 Vercel 会提示让你添加 DNS 解析记录 DNS Config 在阿里云域名解析的解析设置里点击 添加记录,按照 Vercel 的提示配置好图中三个选项,点击确认 DNS Set 回 Vercel 点击 Refresh 按钮,出现下图所示的情况就表明配置完成了 Complete

🎉接下来就可以愉快地使用 openai api 啦~

有问题可以 在此 留言

vercel-proxy-openai's People

Contributors

linlin00000000 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

vercel-proxy-openai's Issues

请教使用方法问题

纯小白,自己会照葫芦画瓢。

使用方法
访问 openai api 时,将 "api.openai.com" 换成你的自定义域名,例如
const host = 'YOUR DOMAIN'
const url = https://${host}/v1/chat/completions
在其他 chatgpt 项目中,可以将变量 "OPENAI_API_BASE_URL" 设置为你的域名

我在别人开发好的项目里填入域名都无法成功,和我没操作第一条有关系吗?谢谢

访问不到

用apifox 调用会出现
An error occurred with this application.

ROUTER_EXTERNAL_TARGET_ERROR

Python调用api方法时该怎么修改呢?

比如
原始调用方法是

import openai
openai.api_key = api
chat = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=messages,
            max_tokens=400,
        )

这段代码是不是修改成

import openai
openai.api_key = api
host = 'YOUR DOMAIN'
url=`https://${host}/v1/chat/completions`
openai.api_base= url
chat = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=messages,
            max_tokens=400,
        )

发布失败

Reliability: vercel-proxy-openai
·
Failed after 6s

image

访问也是404

looking at the logs, hkg1 is the culprit so I disabled it

{
"rewrites": [
{ "source": "/", "destination": "https://api.openai.com" },
{
"source": "/:match*",
"destination": "https://api.openai.com/:match*"
}
],
"github": {
"silent": true
},
"regions": [
"arn1",
"bom1",
"cdg1",
"cle1",
"cpt1",
"dub1",
"fra1",
"gru1",
// "hkg1", ip blocked by openai
"hnd1",
"iad1",
"icn1",
"kix1",
"lhr1",
"pdx1",
"sfo1",
"sin1",
"syd1"
]
}

一直遇到超时的问题

Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.._completion_with_retry in 1.0 seconds as it raised APIError: Invalid response object from API: 'An error occurred with this application.\n\nROUTER_EXTERNAL_TARGET_ERROR\n' (HTTP response code was 502).

请问这个和通过vercel转发有关系么

出现了openai.error.InvalidRequestError: Invalid URL (POST /v1/chat/completions/chat/completions)

您好,我在按您的教程配置完成后仍然出现了如下问题,想咨询一下解决方案:
openai.error.InvalidRequestError: Invalid URL (POST/v1/chat/completions/chat/completions)

代码如下:
import openai

openai.api_key = "****"
host = '*****'
url = f'https://{host}/v1/chat/completions'
openai.api_base = url
messages = [
{"role": "system", "content": "You are a news creator"},
{"role": "user",
"content": f"Write a news article with the title 你好 that spans 3-4 paragraphs and comprehensively covers pertinent and vital information on the topic. Your piece should be both informative and engaging, capturing readers' attention and keeping them informed throughout."},
]

调用 GPT-3.5-turbo API

completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages
)

获取生成的新闻稿内容

print(completion.choices[0].message["content"]

提示以下错误

{
"error": {
"message": "Invalid URL (GET /)",
"type": "invalid_request_error",
"param": null,
"code": null
}
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.