Skip to content

Commit

Permalink
feat: support openai images generations format
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Dec 29, 2023
1 parent 7ab1629 commit 5593a09
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<meta name="baidu-site-verification" content="codeva-TJkbi40ZBi" />
<link href="https://open.lightxi.com/fonts/Andika" rel="stylesheet">
<link href="https://open.lightxi.com/fonts/Jetbrains-Mono" rel="stylesheet">
<link href="https://open.lightxi.com/jsdelivr/npm/[email protected]/dist/katex.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" rel="stylesheet">
<link rel="manifest" href="/site.webmanifest">
<script src="/workbox.js" defer></script>
</head>
Expand Down
2 changes: 1 addition & 1 deletion app/src/dialogs/MaskDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { splitList } from "@/utils/base.ts";
import { maskEvent } from "@/events/mask.ts";

function getEmojiSource(emoji: string): string {
return `https://cdn.staticfile.org/emoji-datasource-apple/14.0.0/img/apple/64/${emoji}.png`;
return `https://cdn.jsdelivr.net/npm/emoji-datasource-apple/img/apple/64/${emoji}.png`;
}

function MaskItem({ mask }: { mask: Mask }) {
Expand Down
6 changes: 3 additions & 3 deletions manager/chat_completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func ChatRelayAPI(c *gin.Context) {
}
}

func GetChatProps(form RelayForm, messages []globals.Message, buffer *utils.Buffer, plan bool) *adapter.ChatProps {
func getChatProps(form RelayForm, messages []globals.Message, buffer *utils.Buffer, plan bool) *adapter.ChatProps {
return &adapter.ChatProps{
Model: form.Model,
Message: messages,
Expand All @@ -90,7 +90,7 @@ func sendTranshipmentResponse(c *gin.Context, form RelayForm, messages []globals
cache := utils.GetCacheFromContext(c)

buffer := utils.NewBuffer(form.Model, messages, channel.ChargeInstance.GetCharge(form.Model))
err := channel.NewChatRequest(auth.GetGroup(db, user), GetChatProps(form, messages, buffer, plan), func(data string) error {
err := channel.NewChatRequest(auth.GetGroup(db, user), getChatProps(form, messages, buffer, plan), func(data string) error {
buffer.Write(data)
return nil
})
Expand Down Expand Up @@ -159,7 +159,7 @@ func sendStreamTranshipmentResponse(c *gin.Context, form RelayForm, messages []g

go func() {
buffer := utils.NewBuffer(form.Model, messages, channel.ChargeInstance.GetCharge(form.Model))
err := channel.NewChatRequest(auth.GetGroup(db, user), GetChatProps(form, messages, buffer, plan), func(data string) error {
err := channel.NewChatRequest(auth.GetGroup(db, user), getChatProps(form, messages, buffer, plan), func(data string) error {
partial <- getStreamTranshipmentForm(id, created, form, buffer.Write(data), buffer, false, nil)
return nil
})
Expand Down
66 changes: 66 additions & 0 deletions manager/images.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package manager

import (
"chat/adapter"
"chat/admin"
"chat/auth"
"chat/channel"
"chat/globals"
"chat/utils"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"strings"
"time"
)
Expand Down Expand Up @@ -52,6 +57,67 @@ func ImagesRelayAPI(c *gin.Context) {
createRelayImageObject(c, form, prompt, created, user, false)
}

func getImageProps(form RelayImageForm, messages []globals.Message, buffer *utils.Buffer, plan bool) *adapter.ChatProps {
return &adapter.ChatProps{
Model: form.Model,
Message: messages,
Plan: plan,
Token: 2500,
Buffer: *buffer,
}
}

func getUrlFromBuffer(buffer *utils.Buffer) string {
content := buffer.Read()

urls := utils.ExtractImageUrls(content)
if len(urls) > 0 {
return urls[len(urls)-1]
}

return ""
}

func createRelayImageObject(c *gin.Context, form RelayImageForm, prompt string, created int64, user *auth.User, plan bool) {
db := utils.GetDBFromContext(c)
cache := utils.GetCacheFromContext(c)

messages := []globals.Message{
{
Role: globals.User,
Content: prompt,
},
}

buffer := utils.NewBuffer(form.Model, messages, channel.ChargeInstance.GetCharge(form.Model))
err := channel.NewChatRequest(auth.GetGroup(db, user), getImageProps(form, messages, buffer, plan), func(data string) error {
buffer.Write(data)
return nil
})

admin.AnalysisRequest(form.Model, buffer, err)
if err != nil {
auth.RevertSubscriptionUsage(db, cache, user, form.Model)
globals.Warn(fmt.Sprintf("error from chat request api: %s (instance: %s, client: %s)", err, form.Model, c.ClientIP()))

sendErrorResponse(c, err)
return
}

CollectQuota(c, user, buffer, plan, err)

image := getUrlFromBuffer(buffer)
if image == "" {
sendErrorResponse(c, fmt.Errorf("no image generated"), "image_generation_error")
return
}

c.JSON(http.StatusOK, RelayImageResponse{
Created: created,
Data: []RelayImageData{
{
Url: image,
},
},
})
}
1 change: 1 addition & 0 deletions manager/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func Register(app *gin.RouterGroup) {
app.GET("/dashboard/billing/usage", GetBillingUsage)
app.GET("/dashboard/billing/subscription", GetSubscription)
app.POST("/v1/chat/completions", ChatRelayAPI)
app.POST("/v1/images/generations", ImagesRelayAPI)

broadcast.Register(app)
}
10 changes: 6 additions & 4 deletions manager/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,13 @@ type RelayImageForm struct {
N *int `json:"n,omitempty"`
}

type RelayImageData struct {
Url string `json:"url"`
}

type RelayImageResponse struct {
Created int `json:"created"`
Data []struct {
Url string `json:"url"`
} `json:"data"`
Created int64 `json:"created"`
Data []RelayImageData `json:"data"`
}

func transformContent(content interface{}) string {
Expand Down

0 comments on commit 5593a09

Please sign in to comment.