banner
xiaole

xiaole

前端工程师 | Trying To Do Better
tg_channel
twitter
github
email

cloudflareを使用してapi.openai.comを中継します。

最近、ChatGPT プロジェクトを作成しました。このプロジェクトを家族と共有したいのですが、国内では直接 openai api にアクセスすることができないため、公式のインターフェースをプロキシする必要があります。

方法は、chatgptProxyAPI で提供されている方法を参考にしてください。

cloudflare pages の使用#

  1. GitHub を開き、リポジトリを作成します。リポジトリ名は任意で構いません。

image.png

  1. 新しいファイルを作成します。

image

ファイル名は _worker.js です。他のアドレスをプロキシしたい場合は、TELEGRAPH_URL を直接変更できます。

内容は以下の通りです:

const TELEGRAPH_URL = 'https://api.openai.com';


export default {
  async fetch(request, env) {
      const NewResponse = await handleRequest(request)
      return NewResponse
  },
};

async function handleRequest(request) {
  const url = new URL(request.url);
  const headers_Origin = request.headers.get("Access-Control-Allow-Origin") || "*"
  url.host = TELEGRAPH_URL.replace(/^https?:\/\//, '');
  const modifiedRequest = new Request(url.toString(), {
    headers: request.headers,
    method: request.method,
    body: request.body,
    redirect: 'follow'
  });
  const response = await fetch(modifiedRequest);
  const modifiedResponse = new Response(response.body, response);
  // Allow cross-origin access
  modifiedResponse.headers.set('Access-Control-Allow-Origin', headers_Origin);
  return modifiedResponse;
}

image.png

変更をコミットします。

  1. Cloudflare を開き、新しいページを作成し、自分の Git と接続します。

image.png

  1. 作成したリポジトリを選択します。
  2. デフォルトの設定を使用します(変更せずに次のステップに進みます)。
  3. デプロイが完了するのを待ち、作成したページに戻ると、アドレスが表示されます。

image.png

使用方法:#

プロジェクトでの使用方法

  1. https://power-chat.younglele.cn を開きます。
  2. OpenAI API Proxy の設定で、https://上記で取得したドメインアドレス/v1 を入力します。

image.png

  1. 保存して、ファイアウォールなしで openai api にアクセスできます。

Cloudflare Worker の使用方法もありますが、独自のドメインを持ち、Cloudflare にバインドする必要があります。それでもページの方法をお勧めします。

Cloudflare Worker の使用#

  1. Workers と Pages でアプリケーションを作成します。

image.png

  1. Worker の作成をクリックします。

image.png

  1. 名前を変更し、デプロイをクリックします(コードをデプロイした後に変更します)。

image.png

  1. コードの編集をクリックします。

image.png

以下のコードをコードエディタに貼り付け、保存してデプロイをクリックします。

const TELEGRAPH_URL = 'https://api.openai.com';

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  const url = new URL(request.url);
  const headers_Origin = request.headers.get("Access-Control-Allow-Origin") || "*"
  url.host = TELEGRAPH_URL.replace(/^https?:\/\//, '');
  const modifiedRequest = new Request(url.toString(), {
    headers: request.headers,
    method: request.method,
    body: request.body,
    redirect: 'follow'
  });
  const response = await fetch(modifiedRequest);
  const modifiedResponse = new Response(response.body, response);
  // Allow cross-origin access
  modifiedResponse.headers.set('Access-Control-Allow-Origin', headers_Origin);
  return modifiedResponse;
}

image.png

成功したら、作成した workers ページに戻ります。

これでまだうまくいかない場合は、workers.dev がまだブロックされているためです。この場合、ドメインをバインドする必要があります。

image.png

ただし、このカスタムドメインは、Cloudflare でアクティブな状態にあるドメインのみサポートされています。左側の ウェブサイト をクリックして、手順に従って独自のドメインを追加するか、Cloudflare で購入する必要があります。

image.png

読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。