1
0

construct requests

This commit is contained in:
Antonio Cheong 2023-04-02 22:53:33 +08:00
parent 5b0989fbc3
commit c27f8a0c3a
6 changed files with 78 additions and 0 deletions

1
go.mod
View File

@ -4,6 +4,7 @@ go 1.20
require ( require (
github.com/gin-gonic/gin v1.9.0 github.com/gin-gonic/gin v1.9.0
github.com/google/uuid v1.3.0
github.com/r3labs/sse/v2 v2.10.0 github.com/r3labs/sse/v2 v2.10.0
) )

2
go.sum
View File

@ -24,6 +24,8 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
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 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4= github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=

View File

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

10
internal/types/api.go Normal file
View File

@ -0,0 +1,10 @@
package types
type APIRequest struct {
Messages []api_message `json:"messages"`
}
type api_message struct {
Role string `json:"role"`
Content string `json:"content"`
}

37
internal/types/chatgpt.go Normal file
View File

@ -0,0 +1,37 @@
package types
import "github.com/google/uuid"
type chatgpt_message struct {
ID uuid.UUID `json:"id"`
Role string `json:"role"`
Content chatgpt_content `json:"content"`
}
type chatgpt_content struct {
ContentType string `json:"content_type"`
Parts []string `json:"parts"`
}
type ChatGPTRequest struct {
Action string `json:"action"`
Messages []chatgpt_message `json:"messages"`
ParentMessageID string `json:"parent_message_id,omitempty"`
Model string `json:"model"`
}
func NewChatGPTRequest() ChatGPTRequest {
return ChatGPTRequest{
Action: "next",
ParentMessageID: uuid.NewString(),
Model: "text-davinci-002-render-sha",
}
}
func (c *ChatGPTRequest) AddMessage(role string, content string) {
c.Messages = append(c.Messages, chatgpt_message{
ID: uuid.New(),
Role: role,
Content: chatgpt_content{ContentType: "text", Parts: []string{content}},
})
}

15
main.go
View File

@ -1,6 +1,8 @@
package main package main
import ( import (
"freechatgpt/internal/chatgpt"
internal_types "freechatgpt/internal/types"
"os" "os"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -65,5 +67,18 @@ func main() {
ACCESS_TOKENS = tokens ACCESS_TOKENS = tokens
c.String(200, "tokens updated") c.String(200, "tokens updated")
}) })
/// Public routes
router.POST("/v1/chat/completions", func(c *gin.Context) {
var chat_request internal_types.APIRequest
err := c.BindJSON(&chat_request)
if err != nil {
c.String(400, "chat request not provided")
return
}
// Convert the chat request to a ChatGPT request
chatgpt_request := chatgpt.ConvertAPIRequest(chat_request)
c.JSON(200, chatgpt_request)
})
router.Run(HOST + ":" + PORT) router.Run(HOST + ":" + PORT)
} }