通过转发请求访问 openai

380 阅读1分钟

遇到的问题

openai 的访问地址在国外,国内访问会被墙

解决方法

image.png 通过 cloudflare 转发请求

步骤

  1. 登录/注册 cloudflare
  2. 启动 aigateway
  3. 新建 provider
  4. 导入环境变量export PROXY_URL=https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai
  5. 设置 open.api_base
    import os
    import openai
    # 设置代理
    proxy_url = os.getenv("PROXY_URL")
    if proxy_url is not None:
        openai.api_base = proxy_url