Skip to content

leokwsw/go-chatgpt-api

 
 

Repository files navigation

go-chatgpt-api

This is a personal maintenance version. Compared with the original version, there are the following changes and changes based on changes in OpenAI.

2024-04-19

  • Added audio transcription and speech creation functionality

2024-04-09

  • imitate update:support no auth conversation
    • POST /imitate/v1/chat/completions with fixed token by env value named CUSTOM_FREE_TOKEN(default is python) :
      • openai lib not allow no token
      • Sample Request main.py

2024-04-06

  • Update OpenAIAuth and funcaptcha library
  • POST /chatgpt/conversation, POST /chatgpt/backend-api/conversation and /api/conversation support no auth conversation

2024-03-23

  • Fix 403 Error
  • System Auto detect use Arkose Version
  • /chat/completions Support WSS, imitate also support
  • Auto get AccessToken(for imitate) and PUID(for Plus account) by (OPENAI_EMAIL and OPENAI_PASSWORD) or OPENAI_REFRESH_TOKEN
  • Support Use Ninja get Access Token (set NINJA_URL in .env)

Setup

  1. Create Har file Pool folder : Currently logged in account, using the GPT-4 model and most GPT-3.5 models, you need to configure a HAR file (file with .har suffix) to complete captcha verification.
    1. Use a Chromium-based browser (Chrome, Edge) to open the browser developer tools (F12), switch to the Network tab, and check the preserve log option.
    2. Log in to https://chat.openai.com/, create a new chat and select the GPT-4 model, enter any text, switch to the GPT-3.5 model, and enter any text.
    3. Click the Export HAR button under the Network tab to export the file chat.openai.com.har and place it in the harPool folder of the same level as this program.
  2. You can download the binary from releases or go run main.go

How to apply to third-party client-side (last update at : 2024/03/26)

.env

OPENAI_API_KEY=<your openai api key>
BASE_URL=http://localhost:8080/platform
...


linweiyuan README.md

一个尝试绕过 Cloudflare 来使用 ChatGPT 接口的程序

(本项目没什么问题的话,基本不会有什么大的更新了,够用了,同时欢迎 PR)


支持接口

  • https://chat.openai.com/auth/login 登录返回 accessToken(谷歌和微软账号暂不支持登录,但可正常使用其他接口)
  • 模型和插件查询
  • GPT-3.5GPT-4 对话增删改查及分享
  • https://platform.openai.com/playground 登录返回 apiKey
  • apiKey 余额查询
  • 等等 ...
  • 支持 ChatGPTAPI,接口 /imitate/v1/chat/completions,利用 accessToken 模拟 apiKey,实现伪免费使用 API ,从而支持集成仅支持 apiKey 调用的第三方客户端项目,分享一个好用的脚本测试 web-to-api (linweiyuan#251)
import openai

openai.api_key = "这里填 access token,不是 api key"
openai.api_base = "http://127.0.0.1:8080/imitate/v1"

while True:
    text = input("请输入问题:")
    response = openai.ChatCompletion.create(
        model='gpt-3.5-turbo',
        messages=[
            {'role': 'user', 'content': text},
        ],
        stream=True
    )

    for chunk in response:
        print(chunk.choices[0].delta.get("content", ""), end="", flush=True)
    print("\n")

范例(URL 和参数基本保持着和官网一致,部分接口有些许改动),部分例子,不是全部,理论上全部基于文本传输的接口都支持

https://github.com/linweiyuan/go-chatgpt-api/tree/main/example


使用的过程中遇到问题应该如何解决

汇总贴:linweiyuan#74

如果有疑问而不是什么程序出错其实可以在 Discussions 里发而不是新增 Issue

群聊:linweiyuan#197

再说一遍,不要来 Issues 提你的疑问(再提不回复直接关闭),有讨论区,有群,不要提脑残问题,反面教材:linweiyuan#255


配置

如需设置代理,可以设置环境变量 PROXY,比如 PROXY=http://127.0.0.1:20171 或者 PROXY=socks5://127.0.0.1:20170,注释掉或者留空则不启用

如果代理需账号密码验证,则 http://username:password@ip:port 或者 socks5://username:password@ip:port

如需配合 warp 使用:PROXY=socks5://chatgpt-proxy-server-warp:65535,因为需要设置 warp 的场景已经默认可以直接访问 ChatGPT 官网,因此共用一个变量不冲突(国内 VPS 不在讨论范围内,请自行配置网络环境,warp 服务在魔法环境下才能正常工作)

家庭网络无需跑 warp 服务,跑了也没用,会报错,仅在服务器需要

CONTINUE_SIGNAL=1,开启/imitate接口自动继续会话功能,留空关闭,默认关闭


GPT-4 相关模型目前需要验证 arkose_token ,社区已经有很多解决方案,请自行查找,其中一个能用的:linweiyuan#252

参考配置视频(拉到文章最下面点开视频,需要自己有一定的动手能力,根据你的环境不同自行微调配置):如何生成 GPT-4 arkose_token


根据你的网络环境不同,可以展开查看对应配置,下面例子是基本参数,更多参数查看 compose.yaml

直接利用现成的服务

服务器不定时维护,不保证高可用,利用这些服务导致的账号安全问题,与本项目无关

网络在直连或者通过代理的情况下可以正常访问 ChatGPT
  go-chatgpt-api:
    container_name: go-chatgpt-api
    image: linweiyuan/go-chatgpt-api
    ports:
      - 8080:8080
    environment:
      - TZ=Asia/Shanghai
    restart: unless-stopped
服务器无法正常访问 ChatGPT
  go-chatgpt-api:
    container_name: go-chatgpt-api
    image: linweiyuan/go-chatgpt-api
    ports:
      - 8080:8080
    environment:
      - TZ=Asia/Shanghai
      - PROXY=socks5://chatgpt-proxy-server-warp:65535
    depends_on:
      - chatgpt-proxy-server-warp
    restart: unless-stopped

  chatgpt-proxy-server-warp:
    container_name: chatgpt-proxy-server-warp
    image: linweiyuan/chatgpt-proxy-server-warp
    restart: unless-stopped

目前 warp 容器检测到流量超过 1G 会自动重启,如果你知道什么是 teams-enroll-token (不知道就跳过),可以通过环境变量 TEAMS_ENROLL_TOKEN 设置它的值,然后利用这条命令来检查是否生效

docker-compose exec chatgpt-proxy-server-warp warp-cli --accept-tos account | awk 'NR==1'

Account type: Free (没有生效)

Account type: Team (设置正常)

Render部署

点击下面的按钮一键部署,缺点是免费版本冷启动比较慢

Deploy to Render


如何集成其他第三方客户端(下面的内容不一定是最新,有问题请去各自项目查看)

环境变量

CHATGPT_BASE_URL=http://go-chatgpt-api:8080/chatgpt/backend-api/

config.cfg

[openai]
browserless_endpoint = "http://go-chatgpt-api:8080/chatgpt/backend-api/"

环境变量

API_REVERSE_PROXY=http://go-chatgpt-api:8080/chatgpt/backend-api/conversation

环境变量

CHATGPT_API_PREFIX=http://go-chatgpt-api:8080

application.yaml

proxy:
  url: http://go-chatgpt-api:8080

环境变量

BASE_URL=http://go-chatgpt-api:8080/imitate

相关博客(程序更新很多次,文章的内容可能和现在的不一样,仅供参考):ChatGPT


最后感谢各位同学

Made with contrib.rocks.