Skip to content

Commit

Permalink
Merge branch 'xqdoo00o-master' into Add-Gemini
Browse files Browse the repository at this point in the history
  • Loading branch information
bi1101 committed Apr 16, 2024
2 parents 666166e + d5b9d8e commit d255460
Show file tree
Hide file tree
Showing 13 changed files with 694 additions and 374 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Create a fake API using ChatGPT's website

### Authentication

#### After 2024-04-02, accouts.txt is optional because no need authentication for gpt-3.5.

Access token and PUID(only for PLUS account) retrieval has been automated by [OpenAIAuth](https://github.com/xqdoo00o/OpenAIAuth/) with account email & password.

`accounts.txt` - A list of accounts separated by new line
Expand All @@ -23,7 +25,7 @@ email:password

All authenticated access tokens and PUID will store in `access_tokens.json`

Auto renew access tokens and PUID after 7 days
Auto renew access tokens and PUID after 1 day

Caution! please use unblocked ip for authentication, first login to `https://chat.openai.com/` to check ip availability if you can.

Expand Down
4 changes: 3 additions & 1 deletion README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

### 设置

#### 自2024-04-02起,可选配置accounts.txt,因为gpt-3.5无需登录了。

配置账户邮箱和密码,自动生成和更新Access tokens 和 PUID(仅PLUS账户)(使用[OpenAIAuth](https://github.com/xqdoo00o/OpenAIAuth/)

`accounts.txt` - 存放OpenAI账号邮箱和密码的文件
Expand All @@ -20,7 +22,7 @@

所有登录后的Access tokens和PUID会存放在`access_tokens.json`

每7天自动更新Access tokens和PUID
每天自动更新Access tokens和PUID

注意! 请使用未封锁的ip登录账号,请先打开浏览器登录`https://chat.openai.com/`以检查ip是否可用

Expand Down
13 changes: 8 additions & 5 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var accounts []Account

var validAccounts []string

const interval = time.Hour * 24 * 10
const interval = time.Hour * 24

type Account struct {
Email string `json:"username"`
Expand Down Expand Up @@ -57,13 +57,13 @@ func AppendIfNone(slice []string, i string) []string {
return append(slice, i)
}

func getSecret() (string, string) {
func getSecret() (string, tokens.Secret) {
if len(validAccounts) != 0 {
account := validAccounts[0]
validAccounts = append(validAccounts[1:], account)
return ACCESS_TOKENS.GetSecret(account)
return account, ACCESS_TOKENS.GetSecret(account)
} else {
return "", ""
return "", tokens.Secret{}
}
}

Expand All @@ -79,6 +79,9 @@ func readAccounts() {
for scanner.Scan() {
// Split by :
line := strings.Split(scanner.Text(), ":")
if len(line) < 2 {
continue
}
// Create an account
account := Account{
Email: line[0],
Expand Down Expand Up @@ -145,7 +148,7 @@ func scheduleTokenPUID() {
}
}
tokenProcess:
token, _ = ACCESS_TOKENS.GetSecret(account.Email)
token = ACCESS_TOKENS.GetSecret(account.Email).Token
expireTime, err := getTokenExpire(token)
nowTime := time.Now()
if err != nil {
Expand Down
16 changes: 7 additions & 9 deletions conversion/requests/chatgpt/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,29 @@ package chatgpt

import (
"fmt"
"freechatgpt/internal/tokens"
chatgpt_types "freechatgpt/typings/chatgpt"
official_types "freechatgpt/typings/official"
"strings"

arkose "github.com/xqdoo00o/funcaptcha"
)

func ConvertAPIRequest(api_request official_types.APIRequest, puid string, requireArk bool, dx string, proxy string) chatgpt_types.ChatGPTRequest {
func ConvertAPIRequest(api_request official_types.APIRequest, account string, secret *tokens.Secret, deviceId string, requireArk bool, dx string, proxy string) chatgpt_types.ChatGPTRequest {
chatgpt_request := chatgpt_types.NewChatGPTRequest()
var api_version int
if puid == "" {
if secret.PUID == "" {
api_request.Model = "gpt-3.5"
}
if strings.HasPrefix(api_request.Model, "gpt-3.5") {
api_version = 3
chatgpt_request.Model = "text-davinci-002-render-sha"
} else if strings.HasPrefix(api_request.Model, "gpt-4") {
api_version = 4
chatgpt_request.Model = api_request.Model
// Cover some models like gpt-4-32k
if len(api_request.Model) >= 7 && api_request.Model[6] >= 48 && api_request.Model[6] <= 57 {
chatgpt_request.Model = "gpt-4"
}
chatgpt_request.Model = "gpt-4"
}
if requireArk {
token, err := arkose.GetOpenAIToken(api_version, puid, dx, proxy)
token, err := arkose.GetOpenAIToken(api_version, secret.PUID, dx, proxy)
if err == nil {
chatgpt_request.ArkoseToken = token
} else {
Expand All @@ -38,11 +35,12 @@ func ConvertAPIRequest(api_request official_types.APIRequest, puid string, requi
chatgpt_request.PluginIDs = api_request.PluginIDs
chatgpt_request.Model = "gpt-4-plugins"
}
ifMultimodel := api_version == 4
for _, api_message := range api_request.Messages {
if api_message.Role == "system" {
api_message.Role = "critic"
}
chatgpt_request.AddMessage(api_message.Role, api_message.Content.(string))
chatgpt_request.AddMessage(api_message.Role, api_message.Content, ifMultimodel, account, secret, deviceId, proxy)
}
return chatgpt_request
}
Expand Down
4 changes: 2 additions & 2 deletions conversion/response/chatgpt/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ func ConvertToString(chatgpt_response *chatgpt_types.ChatGPTResponse, previous_t
translated_response := official_types.NewChatCompletionChunk(strings.Replace(chatgpt_response.Message.Content.Parts[0].(string), previous_text.Text, "", 1))
if role {
translated_response.Choices[0].Delta.Role = chatgpt_response.Message.Author.Role
} else if translated_response.Choices[0].Delta.Content == "" || (strings.HasPrefix(chatgpt_response.Message.Metadata.ModelSlug, "gpt-4") && translated_response.Choices[0].Delta.Content == "【") {
return translated_response.Choices[0].Delta.Content
} else if translated_response.Choices[0].Delta.Content == "" {
return ""
}
previous_text.Text = chatgpt_response.Message.Content.Parts[0].(string)
return "data: " + translated_response.String() + "\n\n"
Expand Down
77 changes: 40 additions & 37 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,36 @@ toolchain go1.22.0

require (
github.com/acheong08/endless v0.0.0-20230615162514-90545c7793fd
github.com/bogdanfinn/fhttp v0.5.24
github.com/bogdanfinn/tls-client v1.6.1
github.com/gin-contrib/cors v1.7.0
github.com/bogdanfinn/fhttp v0.5.28
github.com/bogdanfinn/tls-client v1.7.4
github.com/gin-contrib/cors v1.7.1
github.com/gin-gonic/gin v1.9.1
github.com/go-resty/resty/v2 v2.7.0
github.com/google/generative-ai-go v0.8.0
github.com/go-resty/resty/v2 v2.12.0
github.com/google/generative-ai-go v0.11.0
github.com/google/uuid v1.6.0
github.com/gorilla/websocket v1.5.1
github.com/joho/godotenv v1.5.1
github.com/sashabaranov/go-openai v1.20.3
github.com/tidwall/gjson v1.14.4
github.com/sashabaranov/go-openai v1.21.0
github.com/tidwall/gjson v1.17.1
github.com/xqdoo00o/OpenAIAuth v0.0.0-20240403091529-7ef147706fc4
github.com/xqdoo00o/funcaptcha v0.0.0-20240403090732-1b604d808f6c
github.com/zhu327/gemini-openai-proxy v0.0.0-20240314045503-f6afa6badbd2
google.golang.org/api v0.169.0
k8s.io/apimachinery v0.27.2
github.com/zhu327/gemini-openai-proxy v0.0.0-20240328042054-d2c6c4cdff01
golang.org/x/image v0.15.0
google.golang.org/api v0.172.0
k8s.io/apimachinery v0.29.3
)

require (
cloud.google.com/go/ai v0.3.2 // indirect
cloud.google.com/go/compute v1.24.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/longrunning v0.5.5 // indirect
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/bogdanfinn/utls v1.5.16 // indirect
github.com/bytedance/sonic v1.11.2 // indirect
cloud.google.com/go v0.112.2 // indirect
cloud.google.com/go/ai v0.4.0 // indirect
cloud.google.com/go/compute/metadata v0.3.0 // indirect
cloud.google.com/go/longrunning v0.5.6 // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/bogdanfinn/utls v1.6.1 // indirect
github.com/bytedance/sonic v1.11.3 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
Expand All @@ -44,44 +46,45 @@ require (
github.com/go-playground/validator/v10 v10.19.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.3 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/klauspost/compress v1.17.8 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/quic-go/quic-go v0.42.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/tam7t/hpkp v0.0.0-20160821193359-2b70b4024ed5 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.50.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.50.0 // indirect
go.opentelemetry.io/otel v1.25.0 // indirect
go.opentelemetry.io/otel/metric v1.25.0 // indirect
go.opentelemetry.io/otel/trace v1.25.0 // indirect
golang.org/x/arch v0.7.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/oauth2 v0.17.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/oauth2 v0.19.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20240213162025-012b6fc9bca9 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240213162025-012b6fc9bca9 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240304161311-37d4d3c04a78 // indirect
google.golang.org/grpc v1.62.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240415180920-8c6c420018be // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be // indirect
google.golang.org/grpc v1.63.2 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit d255460

Please sign in to comment.