1
0

initial test stream

This commit is contained in:
Antonio Cheong 2023-04-02 23:15:09 +08:00
parent c27f8a0c3a
commit 955e530e35
7 changed files with 89 additions and 6 deletions

6
go.mod
View File

@ -3,12 +3,16 @@ module freechatgpt
go 1.20
require (
github.com/bogdanfinn/tls-client v1.3.8
github.com/gin-gonic/gin v1.9.0
github.com/google/uuid v1.3.0
github.com/r3labs/sse/v2 v2.10.0
)
require (
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/bogdanfinn/fhttp v0.5.19 // indirect
github.com/bogdanfinn/utls v1.5.15 // indirect
github.com/bytedance/sonic v1.8.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
@ -17,12 +21,14 @@ require (
github.com/go-playground/validator/v10 v10.11.2 // indirect
github.com/goccy/go-json v0.10.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.15.12 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/tam7t/hpkp v0.0.0-20160821193359-2b70b4024ed5 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.9 // indirect
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect

12
go.sum
View File

@ -1,3 +1,11 @@
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/bogdanfinn/fhttp v0.5.19 h1:/FKuFAtSw3+iZyNkaWXRDSVqMmOvThDjXanlG6/DXos=
github.com/bogdanfinn/fhttp v0.5.19/go.mod h1:emv9FntlC5eAyrIUhCi6oC5NLoBC9d4AJLCq2T1bobY=
github.com/bogdanfinn/tls-client v1.3.8 h1:HIucpArqyqOUIN/7MDJJA9Ngt+lKHUyoQ2y2CUjIDNM=
github.com/bogdanfinn/tls-client v1.3.8/go.mod h1:dMt6v22wNyA1Y4e3Us6jK3lC64Y6FE2cziydZ7pGK3A=
github.com/bogdanfinn/utls v1.5.15 h1:XUUMJZh2AptaouuwUrc/RQOYgyV89rstC5Nj3FSP43s=
github.com/bogdanfinn/utls v1.5.15/go.mod h1:mHeRCi69cUiEyVBkKONB1cAbLjRcZnlJbGzttmiuK4o=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.8.0 h1:ea0Xadu+sHlu7x5O3gKhRpQ1IKiMrSiHttPF0ybECuA=
github.com/bytedance/sonic v1.8.0/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
@ -28,6 +36,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/compress v1.15.12 h1:YClS/PImqYbn+UILDnqxQCZ3RehC9N318SU3kElDUEM=
github.com/klauspost/compress v1.15.12/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrDDJnH7hvFVbGM=
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
@ -57,6 +67,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tam7t/hpkp v0.0.0-20160821193359-2b70b4024ed5 h1:YqAladjX7xpA6BM04leXMWAEjS0mTZ5kUU9KRBriQJc=
github.com/tam7t/hpkp v0.0.0-20160821193359-2b70b4024ed5/go.mod h1:2JjD2zLQYH5HO74y5+aE3remJQvl6q4Sn6aWA2wD1Ng=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.9 h1:rmenucSohSTiyL09Y+l2OCk+FrMxGMzho2+tjr5ticU=

View File

@ -1,11 +1,11 @@
package chatgpt
import (
internal_types "freechatgpt/internal/types"
typings "freechatgpt/internal/typings"
)
func ConvertAPIRequest(api_request internal_types.APIRequest) internal_types.ChatGPTRequest {
chatgpt_request := internal_types.NewChatGPTRequest()
func ConvertAPIRequest(api_request typings.APIRequest) typings.ChatGPTRequest {
chatgpt_request := typings.NewChatGPTRequest()
for _, api_message := range api_request.Messages {
chatgpt_request.AddMessage(api_message.Role, api_message.Content)
}

View File

@ -0,0 +1,53 @@
package chatgpt
import (
"bufio"
"bytes"
"encoding/json"
typings "freechatgpt/internal/typings"
http "github.com/bogdanfinn/fhttp"
tls_client "github.com/bogdanfinn/tls-client"
)
var (
jar = tls_client.NewCookieJar()
options = []tls_client.HttpClientOption{
tls_client.WithTimeoutSeconds(360),
tls_client.WithClientProfile(tls_client.Chrome_110),
tls_client.WithNotFollowRedirects(),
tls_client.WithCookieJar(jar), // create cookieJar instance and pass it as argument
}
client, _ = tls_client.NewHttpClient(tls_client.NewNoopLogger(), options...)
)
func constructRequest(body typings.ChatGPTRequest, puid string) (*http.Request, error) {
// JSONify the body and add it to the request
body_json, err := json.Marshal(body)
if err != nil {
return &http.Request{}, err
}
request, err := http.NewRequest(http.MethodPost, "https://chat.openai.com/backend-api/conversation", bufio.NewReader(bytes.NewBuffer(body_json)))
request.Header.Set("Host", "chat.openai.com")
request.Header.Set("Origin", "https://chat.openai.com/chat")
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Connection", "keep-alive")
request.Header.Set("Keep-Alive", "timeout=360")
request.Header.Set("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
request.AddCookie(&http.Cookie{
Name: "_puid",
Value: puid,
})
return request, err
}
func SendRequest(message typings.ChatGPTRequest, puid string) (*http.Response, error) {
request, err := constructRequest(message, puid)
if err != nil {
return &http.Response{}, err
}
response, err := client.Do(request)
return response, err
}

18
main.go
View File

@ -2,7 +2,8 @@ package main
import (
"freechatgpt/internal/chatgpt"
internal_types "freechatgpt/internal/types"
typings "freechatgpt/internal/typings"
"io"
"os"
"github.com/gin-gonic/gin"
@ -69,7 +70,7 @@ func main() {
})
/// Public routes
router.POST("/v1/chat/completions", func(c *gin.Context) {
var chat_request internal_types.APIRequest
var chat_request typings.APIRequest
err := c.BindJSON(&chat_request)
if err != nil {
c.String(400, "chat request not provided")
@ -77,7 +78,18 @@ func main() {
}
// Convert the chat request to a ChatGPT request
chatgpt_request := chatgpt.ConvertAPIRequest(chat_request)
c.JSON(200, chatgpt_request)
// c.JSON(200, chatgpt_request)
response, err := chatgpt.SendRequest(chatgpt_request, PUID)
if err != nil {
c.String(500, "error sending request")
return
}
defer response.Body.Close()
c.Stream(func(w io.Writer) bool {
// Write data to client
io.Copy(w, response.Body)
return false
})
})
router.Run(HOST + ":" + PORT)