Skip to content
This repository has been archived by the owner on Aug 10, 2023. It is now read-only.

Docker #2

Merged
merged 7 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/build_docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: build_docker

on:
push:
branches: [master]
release:
types: [created] # 表示在创建新的 Release 时触发

jobs:
build_docker:
name: Build docker
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- run: |
echo "本次构建的版本为:${GITHUB_REF_NAME} (但是这个变量目前上下文中无法获取到)"
echo 本次构建的版本为:${{ github.ref_name }}
env

- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
id: docker_build
uses: docker/build-push-action@v4
with:
context: .
push: true
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64,linux/arm64
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/chatgpt-to-api:${{ github.ref_name }}
${{ secrets.DOCKERHUB_USERNAME }}/chatgpt-to-api:latest
26 changes: 26 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 设置基础镜像为 Golang 官方镜像
FROM golang:1.20.3-alpine

# 设置环境变量
# Reverse Proxy - Available on accessToken
# Default: https://bypass.churchless.tech/api/conversation
ENV API_REVERSE_PROXY 'https://bypass.churchless.tech/api/conversation'
ENV SERVER_HOST '0.0.0.0'

# 设置工作目录为 /app
WORKDIR /app

# 将本地应用程序复制到容器中
COPY . .

# 下载应用程序所需的依赖项
RUN go mod download

# 构建应用程序二进制文件
RUN go build -o app

# 暴露应用程序运行的端口
EXPOSE 8080

# 启动应用程序
CMD ["./app"]
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# ChatGPT-to-API
Create a fake API using ChatGPT's website

**API endpoint: http://127.0.0.1:8080/v1/chat/completions.**

**When calling the API, you must include the authorization parameter in the request header: `'Authorization':'Bearer ' + accessToken`.**

**You can get your accessToken from the following link: [ChatGPT](https://chat.openai.com/api/auth/session)**

**This API can be used with the project [BetterChatGPT](https://github.com/huangzt/BetterChatGPT)**

## Docker build & Run

```bash
docker build -t chatgpt-to-api .

# Running the API
docker run --name chatgpttoapi -d -p 127.0.0.1:8080:8080 chatgpt-to-api

# API path
http://127.0.0.1:8080/v1/chat/completions

```

## Docker compose

[Hub address](https://hub.docker.com/repository/docker/acheong08/chatgpt-to-api/general)

```yml
version: '3'

services:
app:
image: acheong08/chatgpt-to-api # Use latest tag
container_name: chatgpttoapi
restart: unless-stopped
ports:
- '8080:8080'
environment:
SERVER_HOST: 0.0.0.0
SERVER_PORT: 8080
ADMIN_PASSWORD: TotallySecurePassword
# Reverse Proxy - Available on accessToken
API_REVERSE_PROXY: https://bypass.churchless.tech/api/conversation
# If the parameter API_REVERSE_PROXY is empty, the default request URL is https://chat.openai.com/backend-api/conversation, and the PUID is required.
# You can get your PUID for Plus account from the following link: https://chat.openai.com/api/auth/session.
PUID: xxx

```
18 changes: 18 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: '3'

services:
app:
image: acheong08/chatgpt-to-api # 总是使用latest,更新时重新pull该tag镜像即可
container_name: chatgpttoapi
restart: unless-stopped
ports:
- '8080:8080'
environment:
SERVER_HOST: 0.0.0.0
SERVER_PORT: 8080
ADMIN_PASSWORD: TotallySecurePassword
# Reverse Proxy - Available on accessToken
API_REVERSE_PROXY: https://bypass.churchless.tech/api/conversation
# If the parameter API_REVERSE_PROXY is empty, the default request URL is https://chat.openai.com/backend-api/conversation, and the PUID is required.
# You can get your PUID for Plus account from the following link: https://chat.openai.com/api/auth/session.
PUID: xxx
15 changes: 14 additions & 1 deletion handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,20 @@ func nightmare(c *gin.Context) {
// Convert the chat request to a ChatGPT request
chatgpt_request := chatgpt.ConvertAPIRequest(chat_request)
// c.JSON(200, chatgpt_request)
response, err := chatgpt.SendRequest(chatgpt_request, &PUID, ACCESS_TOKENS.GetToken())

authHeader := c.GetHeader("Authorization")
token := ACCESS_TOKENS.GetToken()
if authHeader != "" {
// 如果Authorization头不为空,则提取其中的token
// 首先将Bearer前缀替换为空字符串
customAccessToken := strings.Replace(authHeader, "Bearer ", "", 1)
if customAccessToken != "" {
token = customAccessToken
println("customAccessToken set:" + customAccessToken)
}
}

response, err := chatgpt.SendRequest(chatgpt_request, &PUID, token)
if err != nil {
c.JSON(500, gin.H{
"error": "error sending request",
Expand Down
18 changes: 16 additions & 2 deletions internal/chatgpt/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package chatgpt
import (
"bytes"
"encoding/json"
"os"

typings "freechatgpt/internal/typings"

Expand All @@ -18,17 +19,30 @@ var (
tls_client.WithNotFollowRedirects(),
tls_client.WithCookieJar(jar), // create cookieJar instance and pass it as argument
}
client, _ = tls_client.NewHttpClient(tls_client.NewNoopLogger(), options...)
client, _ = tls_client.NewHttpClient(tls_client.NewNoopLogger(), options...)
http_proxy = os.Getenv("http_proxy")
API_REVERSE_PROXY = os.Getenv("API_REVERSE_PROXY")
)

func SendRequest(message typings.ChatGPTRequest, puid *string, access_token string) (*http.Response, error) {
if http_proxy != "" {
client.SetProxy(http_proxy)
println("Proxy set:" + http_proxy)
}

apiUrl := "https://chat.openai.com/backend-api/conversation"
if API_REVERSE_PROXY != "" {
apiUrl = API_REVERSE_PROXY
println("API_REVERSE_PROXY set:" + API_REVERSE_PROXY)
}

// JSONify the body and add it to the request
body_json, err := json.Marshal(message)
if err != nil {
return &http.Response{}, err
}

request, err := http.NewRequest(http.MethodPost, "https://chat.openai.com/backend-api/conversation", bytes.NewBuffer(body_json))
request, err := http.NewRequest(http.MethodPost, apiUrl, bytes.NewBuffer(body_json))
if err != nil {
return &http.Response{}, err
}
Expand Down