Skip to content

Commit

Permalink
Merge pull request #47 from mylxsw/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
mylxsw authored Jan 31, 2024
2 parents 3a4805d + 0239f1f commit 7abbb01
Show file tree
Hide file tree
Showing 29 changed files with 751 additions and 278 deletions.
19 changes: 11 additions & 8 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ fallback-openai-autoproxy: false
# OpenAI 服务配置
fallback-openai-organization: ""
# 备用的 OpenAI 服务器地址,配置多个时会自动在多个服务之间平衡负载,不要忘记在在 URL 后面添加 /v1
fallback-openai-servers: ["https://api.openai.com/v1"]
fallback-openai-servers: [ "https://api.openai.com/v1" ]
# 备用的 OpenAI Keys,如果指定多个,会在多个服务之间平衡负载
fallback-openai-keys: [""]
fallback-openai-keys: [ "" ]

######## OpenAI Dalle 配置 ########
# OpenAI Dalle 服务配置,用于文生图
Expand All @@ -105,8 +105,8 @@ openai-dalle-autoproxy: false
openai-dalle-azure: false
openai-dalle-apiversion: "2023-05-15"
openai-dalle-organization: ""
openai-dalle-servers: ["https://api.openai.com/v1"]
openai-dalle-keys: [""]
openai-dalle-servers: [ "https://api.openai.com/v1" ]
openai-dalle-keys: [ "" ]

######## Anthropic #######

Expand Down Expand Up @@ -198,7 +198,7 @@ gpt360-apikey: ""
# https://github.com/songquanpeng/one-api

# 支持的模型列表,当前支持:chatglm_turbo, chatglm_pro, chatglm_std, chatglm_lite, PaLM-2
oneapi-support-models: []
oneapi-support-models: [ ]
enable-oneapi: false
oneapi-server: ""
oneapi-key: ""
Expand All @@ -208,7 +208,7 @@ oneapi-key: ""
# https://openrouter.ai/

# 支持的模型列表,
openrouter-support-models: ["01-ai/yi-34b-chat"]
openrouter-support-models: [ "01-ai/yi-34b-chat" ]
openrouter-autoproxy: false
enable-openrouter: false
openrouter-server: "https://openrouter.ai/api/v1"
Expand Down Expand Up @@ -273,8 +273,8 @@ getimgai-key: ""
# API 申请:https://www.lepton.ai/
enable-leptonai: false
leptonai-autoproxy: false
leptonai-qr-servers: ["https://aiqr.lepton.run"]
leptonai-keys: [""]
leptonai-qr-servers: [ "https://aiqr.lepton.run" ]
leptonai-keys: [ "" ]

# 艺术字字体文件,只支持 ttf 格式
font-path: "path/to/your/font/MaoKenShiJinHei-2.ttf"
Expand Down Expand Up @@ -420,6 +420,9 @@ cnlocal-model: "generalv2"
default-img2img-model: lb-realistic-versionv4.0
default-txt2img-model: sb-stable-diffusion-xl-1024-v1-0

# 图生图图像识别处理模型,用于识别图像内容,生成图生图的提示语,目前支持 xfyun,留空则表示不启用
img2img-recognition-provider: ""

######## 虚拟模型配置 ########
enable-virtual-model: false
virtual-model-implementation: "openai"
Expand Down
5 changes: 5 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ type Config struct {
DefaultImageToImageModel string `json:"default_image_to_image_model" yaml:"default_image_to_image_model"`
DefaultTextToImageModel string `json:"default_text_to_image_model" yaml:"default_text_to_image_model"`

// 图生图图像识别处理模型,用于识别图像内容,生成图生图的提示语
ImageToImageRecognitionProvider string `json:"img2img-recognition-provider" yaml:"img2img-recognition-provider"`

// 虚拟模型
EnableVirtualModel bool `json:"enable_virtual_model" yaml:"enable_virtual_model"`
VirtualModel VirtualModel `json:"virtual_model" yaml:"virtual_model"`
Expand Down Expand Up @@ -529,6 +532,8 @@ func Register(ins *app.App) {
DefaultImageToImageModel: ctx.String("default-img2img-model"),
DefaultTextToImageModel: ctx.String("default-txt2img-model"),

ImageToImageRecognitionProvider: ctx.String("img2img-recognition-provider"),

EnableVirtualModel: ctx.Bool("enable-virtual-model"),
VirtualModel: VirtualModel{
Implementation: ctx.String("virtual-model-implementation"),
Expand Down
2 changes: 2 additions & 0 deletions config/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ func initCmdFlags(ins *app.App) {
ins.AddStringFlag("default-img2img-model", "lb-realistic-versionv4.0", "默认的图生图模型,值取自数据表 image_model.model_id")
ins.AddStringFlag("default-txt2img-model", "sb-stable-diffusion-xl-1024-v1-0", "默认的文生图模型,值取自数据表 image_model.model_id")

ins.AddStringFlag("img2img-recognition-provider", "", "图生图图像识别处理模型,用于识别图像内容,生成图生图的提示语,目前支持 xfyun,留空则表示不启用")

ins.AddBoolFlag("enable-virtual-model", "是否启用虚拟模型")
ins.AddStringFlag("virtual-model-implementation", "openai", "虚拟模型实现厂商")
ins.AddStringFlag("virtual-model-nanxian-rel", "gpt-3.5-turbo", "南贤大模型实现")
Expand Down
2 changes: 1 addition & 1 deletion docker-build.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

VERSION=1.0.10
VERSION=1.0.12

docker buildx build --platform=linux/amd64,linux/arm64 -t mylxsw/aidea-server:$VERSION . --push

28 changes: 14 additions & 14 deletions internal/queue/lepton.go → internal/queue/artistic.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/mylxsw/aidea-server/pkg/ai/lepton"
"github.com/mylxsw/aidea-server/pkg/ai/openai"
"github.com/mylxsw/aidea-server/pkg/image"
repo2 "github.com/mylxsw/aidea-server/pkg/repo"
"github.com/mylxsw/aidea-server/pkg/repo"
"github.com/mylxsw/aidea-server/pkg/uploader"
"github.com/mylxsw/aidea-server/pkg/youdao"
"github.com/mylxsw/asteria/log"
Expand Down Expand Up @@ -68,20 +68,20 @@ func NewArtisticTextCompletionTask(payload any) *asynq.Task {
return asynq.NewTask(TypeArtisticTextCompletion, data)
}

func BuildArtisticTextCompletionHandler(client *lepton.Lepton, translator youdao.Translater, up *uploader.Uploader, rep *repo2.Repository, oai openai.Client) TaskHandler {
func BuildArtisticTextCompletionHandler(leptonClient *lepton.Lepton, translator youdao.Translater, up *uploader.Uploader, rep *repo.Repository, oai openai.Client) TaskHandler {
return func(ctx context.Context, task *asynq.Task) (err error) {
var payload ArtisticTextCompletionPayload
if err := json.Unmarshal(task.Payload(), &payload); err != nil {
return err
}

if err := rep.Queue.Update(context.TODO(), payload.GetID(), repo2.QueueTaskStatusRunning, nil); err != nil {
if err := rep.Queue.Update(context.TODO(), payload.GetID(), repo.QueueTaskStatusRunning, nil); err != nil {
log.WithFields(log.Fields{"payload": payload}).Errorf("set task status to running failed: %s", err)
return err
}

if payload.CreatedAt.Add(5 * time.Minute).Before(time.Now()) {
rep.Queue.Update(context.TODO(), payload.GetID(), repo2.QueueTaskStatusFailed, ErrorResult{Errors: []string{"任务处理超时"}})
rep.Queue.Update(context.TODO(), payload.GetID(), repo.QueueTaskStatusFailed, ErrorResult{Errors: []string{"任务处理超时"}})
log.WithFields(log.Fields{"payload": payload}).Errorf("task expired")
return nil
}
Expand All @@ -92,8 +92,8 @@ func BuildArtisticTextCompletionHandler(client *lepton.Lepton, translator youdao
err = err2.(error)

// 更新创作岛历史记录
if err := rep.Creative.UpdateRecordByTaskID(ctx, payload.GetUID(), payload.GetID(), repo2.CreativeRecordUpdateRequest{
Status: repo2.CreativeStatusFailed,
if err := rep.Creative.UpdateRecordByTaskID(ctx, payload.GetUID(), payload.GetID(), repo.CreativeRecordUpdateRequest{
Status: repo.CreativeStatusFailed,
Answer: err.Error(),
}); err != nil {
log.WithFields(log.Fields{"payload": payload}).Errorf("update creative failed: %s", err)
Expand All @@ -104,7 +104,7 @@ func BuildArtisticTextCompletionHandler(client *lepton.Lepton, translator youdao
if err := rep.Queue.Update(
context.TODO(),
payload.GetID(),
repo2.QueueTaskStatusFailed,
repo.QueueTaskStatusFailed,
ErrorResult{
Errors: []string{err.Error()},
},
Expand Down Expand Up @@ -146,7 +146,7 @@ func BuildArtisticTextCompletionHandler(client *lepton.Lepton, translator youdao
panic(fmt.Errorf("不支持该艺术字类型: %v", payload.Type))
}

res, err := client.ImageGenerate(ctx, lepton.QRImageRequest{
res, err := leptonClient.ImageGenerate(ctx, lepton.QRImageRequest{
Prompt: prompt,
NegativePrompt: negativePrompt,
Model: payload.ArtisticType,
Expand All @@ -171,7 +171,7 @@ func BuildArtisticTextCompletionHandler(client *lepton.Lepton, translator youdao

imageUrls, err := res.UploadResources(ctx, up, payload.GetUID())
if err != nil {
log.With(payload).Errorf("上传图片到七牛云存储失败: %", err)
log.With(payload).Errorf("上传图片到七牛云存储失败: %v", err)
panic(fmt.Errorf("上传图片到七牛云存储失败: %w", err))
}

Expand All @@ -190,14 +190,14 @@ func BuildArtisticTextCompletionHandler(client *lepton.Lepton, translator youdao
panic(err)
}

req := repo2.CreativeRecordUpdateRequest{
Status: repo2.CreativeStatusSuccess,
req := repo.CreativeRecordUpdateRequest{
Status: repo.CreativeStatusSuccess,
Answer: string(retJson),
QuotaUsed: payload.GetQuota(),
}

if prompt != payload.Prompt || negativePrompt != payload.NegativePrompt {
ext := repo2.CreativeRecordUpdateExtArgs{}
ext := repo.CreativeRecordUpdateExtArgs{}
if prompt != payload.Prompt {
ext.RealPrompt = prompt
}
Expand All @@ -219,15 +219,15 @@ func BuildArtisticTextCompletionHandler(client *lepton.Lepton, translator youdao
ctx,
payload.GetUID(),
payload.GetQuota(),
repo2.NewQuotaUsedMeta("leptonai", modelUsed...),
repo.NewQuotaUsedMeta("leptonai", modelUsed...),
); err != nil {
log.Errorf("used quota add failed: %s", err)
}

return rep.Queue.Update(
context.TODO(),
payload.GetID(),
repo2.QueueTaskStatusSuccess,
repo.QueueTaskStatusSuccess,
CompletionResult{
Resources: imageUrls,
ValidBefore: time.Now().Add(7 * 24 * time.Hour),
Expand Down
3 changes: 2 additions & 1 deletion internal/queue/consumer/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func (p Provider) Boot(resolver infra.Resolver) {
userSvc *service.UserService,
dalleClient *openai.DalleImageClient,
leptonClient *lepton.Lepton,
aiProvider *chat.AIProvider,
) {
log.Debugf("register all queue handlers")
mux.HandleFunc(queue.TypeOpenAICompletion, queue.BuildOpenAICompletionHandler(openaiClient, rep))
Expand All @@ -106,7 +107,7 @@ func (p Provider) Boot(resolver infra.Resolver) {
mux.HandleFunc(queue.TypeSignup, queue.BuildSignupHandler(rep, mailer, ding))
mux.HandleFunc(queue.TypePayment, queue.BuildPaymentHandler(rep, mailer, que, ding))
mux.HandleFunc(queue.TypeBindPhone, queue.BuildBindPhoneHandler(rep, mailer))
mux.HandleFunc(queue.TypeImageGenCompletion, queue.BuildImageCompletionHandler(leapClient, stabaiClient, deepaiClient, fromstonClient, dashscopeClient, getimgaiClient, translater, uploader, rep, openaiClient, dalleClient))
mux.HandleFunc(queue.TypeImageGenCompletion, queue.BuildImageCompletionHandler(conf, aiProvider, leapClient, stabaiClient, deepaiClient, fromstonClient, dashscopeClient, getimgaiClient, translater, uploader, rep, openaiClient, dalleClient))
mux.HandleFunc(queue.TypeFromStonCompletion, queue.BuildFromStonCompletionHandler(fromstonClient, uploader, rep))
mux.HandleFunc(queue.TypeDashscopeImageCompletion, queue.BuildDashscopeImageCompletionHandler(dashscopeClient, uploader, rep, translater, openaiClient))
mux.HandleFunc(queue.TypeGetimgAICompletion, queue.BuildGetimgAICompletionHandler(getimgaiClient, translater, uploader, rep, openaiClient))
Expand Down
Loading

0 comments on commit 7abbb01

Please sign in to comment.