Skip to content

Commit

Permalink
feat: b64_json
Browse files Browse the repository at this point in the history
  • Loading branch information
deanxv committed Apr 27, 2024
1 parent f14a233 commit c96b98a
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 39 deletions.
2 changes: 1 addition & 1 deletion common/constants.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package common

var Version = "v4.4.5" // this hard coding will be replaced automatically when building, no need to manually change
var Version = "v4.4.6" // this hard coding will be replaced automatically when building, no need to manually change

const (
RequestIdKey = "X-Request-Id"
Expand Down
42 changes: 39 additions & 3 deletions controller/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"coze-discord-proxy/discord"
"coze-discord-proxy/model"
"coze-discord-proxy/telegram"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -412,9 +413,7 @@ func buildOpenAIGPT4VForImageContent(sendChannelId string, objs []interface{}) (
return "", fmt.Errorf("消息格式错误")
}
}
//if runeCount := len([]rune(content)); runeCount > 2000 {
// return "", fmt.Errorf("prompt最大为2000字符 [%v]", runeCount)
//}

return content, nil

}
Expand Down Expand Up @@ -540,6 +539,22 @@ func ImagesForOpenAI(c *gin.Context) {
})
return
}
if request.ResponseFormat == "b64_json" && reply.Data != nil && len(reply.Data) > 0 {
for _, data := range reply.Data {
base64Str, err := getBase64ByUrl(data.URL)
if err != nil {
c.JSON(http.StatusInternalServerError, model.OpenAIErrorResponse{
OpenAIError: model.OpenAIError{
Message: err.Error(),
Type: "request_error",
Code: "500",
},
})
return
}
data.B64Json = "data:image/webp;base64," + base64Str
}
}
replyResp = reply
case <-timer.C:
if replyResp.Data == nil {
Expand Down Expand Up @@ -691,3 +706,24 @@ func checkUserAuths(c *gin.Context) error {
}
return nil
}

func getBase64ByUrl(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", fmt.Errorf("failed to fetch image: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("received non-200 status code: %d", resp.StatusCode)
}

imgData, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read image data: %w", err)
}

// Encode the image data to Base64
base64Str := base64.StdEncoding.EncodeToString(imgData)
return base64Str, nil
}
24 changes: 12 additions & 12 deletions discord/processmessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ func processMessageUpdateForOpenAIImage(m *discordgo.MessageUpdate) model.OpenAI
submatches := re.FindAllStringSubmatch(m.Content, -1)

for _, match := range submatches {
response.Data = append(response.Data, struct {
URL string `json:"url"`
}{URL: match[1]})
response.Data = append(response.Data, &model.OpenAIImagesGenerationDataResponse{
URL: match[1],
})
}

if len(m.Embeds) != 0 {
Expand All @@ -89,9 +89,9 @@ func processMessageUpdateForOpenAIImage(m *discordgo.MessageUpdate) model.OpenAI
if m.Content != "" {
m.Content += "\n"
}
response.Data = append(response.Data, struct {
URL string `json:"url"`
}{URL: embed.Image.URL})
response.Data = append(response.Data, &model.OpenAIImagesGenerationDataResponse{
URL: embed.Image.URL,
})
}
}
}
Expand Down Expand Up @@ -170,9 +170,9 @@ func processMessageCreateForOpenAIImage(m *discordgo.MessageCreate) model.OpenAI
submatches := re.FindAllStringSubmatch(m.Content, -1)

for _, match := range submatches {
response.Data = append(response.Data, struct {
URL string `json:"url"`
}{URL: match[1]})
response.Data = append(response.Data, &model.OpenAIImagesGenerationDataResponse{
URL: match[1],
})
}

if len(m.Embeds) != 0 {
Expand All @@ -181,9 +181,9 @@ func processMessageCreateForOpenAIImage(m *discordgo.MessageCreate) model.OpenAI
if m.Content != "" {
m.Content += "\n"
}
response.Data = append(response.Data, struct {
URL string `json:"url"`
}{URL: embed.Image.URL})
response.Data = append(response.Data, &model.OpenAIImagesGenerationDataResponse{
URL: embed.Image.URL,
})
}
}
}
Expand Down
21 changes: 15 additions & 6 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,17 @@ const docTemplate = `{
}
}
},
"model.OpenAIImagesGenerationDataResponse": {
"type": "object",
"properties": {
"b64_json": {
"type": "string"
},
"url": {
"type": "string"
}
}
},
"model.OpenAIImagesGenerationRequest": {
"type": "object",
"properties": {
Expand All @@ -354,6 +365,9 @@ const docTemplate = `{
},
"prompt": {
"type": "string"
},
"response_format": {
"type": "string"
}
}
},
Expand All @@ -369,12 +383,7 @@ const docTemplate = `{
"data": {
"type": "array",
"items": {
"type": "object",
"properties": {
"url": {
"type": "string"
}
}
"$ref": "#/definitions/model.OpenAIImagesGenerationDataResponse"
}
}
}
Expand Down
21 changes: 15 additions & 6 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,17 @@
}
}
},
"model.OpenAIImagesGenerationDataResponse": {
"type": "object",
"properties": {
"b64_json": {
"type": "string"
},
"url": {
"type": "string"
}
}
},
"model.OpenAIImagesGenerationRequest": {
"type": "object",
"properties": {
Expand All @@ -346,6 +357,9 @@
},
"prompt": {
"type": "string"
},
"response_format": {
"type": "string"
}
}
},
Expand All @@ -361,12 +375,7 @@
"data": {
"type": "array",
"items": {
"type": "object",
"properties": {
"url": {
"type": "string"
}
}
"$ref": "#/definitions/model.OpenAIImagesGenerationDataResponse"
}
}
}
Expand Down
14 changes: 10 additions & 4 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ definitions:
content:
type: string
type: object
model.OpenAIImagesGenerationDataResponse:
properties:
b64_json:
type: string
url:
type: string
type: object
model.OpenAIImagesGenerationRequest:
properties:
channelId:
Expand All @@ -79,6 +86,8 @@ definitions:
type: string
prompt:
type: string
response_format:
type: string
type: object
model.OpenAIImagesGenerationResponse:
properties:
Expand All @@ -88,10 +97,7 @@ definitions:
type: boolean
data:
items:
properties:
url:
type: string
type: object
$ref: '#/definitions/model.OpenAIImagesGenerationDataResponse'
type: array
type: object
model.OpenAIMessage:
Expand Down
18 changes: 11 additions & 7 deletions model/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,20 @@ type OpenAIDelta struct {

type OpenAIImagesGenerationRequest struct {
OpenAIChatCompletionExtraRequest
Model string `json:"model"`
Prompt string `json:"prompt"`
Model string `json:"model"`
Prompt string `json:"prompt"`
ResponseFormat string `json:"response_format"`
}

type OpenAIImagesGenerationResponse struct {
Created int64 `json:"created"`
DailyLimit bool `json:"dailyLimit"`
Data []struct {
URL string `json:"url"`
} `json:"data"`
Created int64 `json:"created"`
DailyLimit bool `json:"dailyLimit"`
Data []*OpenAIImagesGenerationDataResponse `json:"data"`
}

type OpenAIImagesGenerationDataResponse struct {
URL string `json:"url"`
B64Json string `json:"b64_json"`
}

type OpenAIGPT4VImagesReq struct {
Expand Down

0 comments on commit c96b98a

Please sign in to comment.