Skip to content
This repository has been archived by the owner on Mar 20, 2024. It is now read-only.

Commit

Permalink
generate arkose token locally (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
linweiyuan committed Jun 17, 2023
1 parent 1be0b64 commit 3847719
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
38 changes: 23 additions & 15 deletions api/chatgpt/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"encoding/json"
"fmt"
"io"
"math/rand"
"strings"
"time"

"github.com/PuerkitoBio/goquery"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -41,21 +43,7 @@ func CreateConversation(c *gin.Context) {
}

if request.Model == gpt4Model || request.Model == gpt4BrowsingModel || request.Model == gpt4PluginsModel {
formParams := fmt.Sprintf(
"public_key=%s",
gpt4PublicKey,
)
req, _ := http.NewRequest(http.MethodPost, gpt4TokenUrl, strings.NewReader(formParams))
req.Header.Set("Content-Type", api.ContentType)
resp, err := api.Client.Do(req)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, api.ReturnMessage(err.Error()))
return
}

responseMap := make(map[string]string)
json.NewDecoder(resp.Body).Decode(&responseMap)
request.ArkoseToken = responseMap["token"]
request.ArkoseToken = fmt.Sprintf(arkoseTokenTemplate, generateRandomString(17), generateRandomString(10), gpt4ArkoseTokenPublicKey, generateRandomNumber())
}

resp, done := sendConversationRequest(c, request)
Expand Down Expand Up @@ -373,3 +361,23 @@ func handlePostOrPatch(c *gin.Context, req *http.Request, errorMessage string) {

io.Copy(c.Writer, resp.Body)
}

//goland:noinspection SpellCheckingInspection
func generateRandomString(length int) string {
rand.NewSource(time.Now().UnixNano())

charset := "0123456789abcdefghijklmnopqrstuvwxyz"
result := make([]byte, length)

for i := 0; i < length; i++ {
randomIndex := rand.Intn(len(charset))
result[i] = charset[randomIndex]
}

return string(result)
}

func generateRandomNumber() int {
rand.NewSource(time.Now().UnixNano())
return rand.Intn(100) + 1
}
11 changes: 6 additions & 5 deletions api/chatgpt/constant.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package chatgpt

//goland:noinspection SpellCheckingInspection
const (
apiPrefix = "https://chat.openai.com/backend-api"
getConversationsErrorMessage = "Failed to get conversations."
Expand All @@ -19,11 +20,11 @@ const (
getCsrfTokenErrorMessage = "Failed to get CSRF token."
authSessionUrl = "https://chat.openai.com/api/auth/session"

gpt4Model = "gpt-4"
gpt4BrowsingModel = "gpt-4-browsing"
gpt4PluginsModel = "gpt-4-plugins"
gpt4PublicKey = "35536E1E-65B4-4D96-9D97-6ADB7EFF8147"
gpt4TokenUrl = "https://tcr9i.chat.openai.com/fc/gt2/public_key/" + gpt4PublicKey
gpt4Model = "gpt-4"
gpt4BrowsingModel = "gpt-4-browsing"
gpt4PluginsModel = "gpt-4-plugins"
gpt4ArkoseTokenPublicKey = "35536E1E-65B4-4D96-9D97-6ADB7EFF8147"
arkoseTokenTemplate = `%s.%s|r=us-east-1|meta=3|meta_width=300|metabgclr=transparent|metaiconclr=%%23555555|guitextcolor=%%23000000|pk=%s|at=40|rid=%d|ag=101|cdn_url=https%%3A%%2F%%2Ftcr9i.chat.openai.com%%2Fcdn%%2Ffc|lurl=https%%3A%%2F%%2Faudio-us-east-1.arkoselabs.com|surl=https%%3A%%2F%%2Ftcr9i.chat.openai.com|smurl=https%%3A%%2F%%2Ftcr9i.chat.openai.com%%2Fcdn%%2Ffc%%2Fassets%%2Fstyle-manager`

actionContinue = "continue"
responseTypeMaxTokens = "max_tokens"
Expand Down

0 comments on commit 3847719

Please sign in to comment.