From 8a452c3072ace14cbf6c912ee968161af9fc000c Mon Sep 17 00:00:00 2001 From: RockYang Date: Tue, 20 Feb 2024 18:38:03 +0800 Subject: [PATCH] feat: download image which ai generated in dialog and replace the image url --- api/handler/chatimpl/chat_handler.go | 40 ++++++++++++++++++++--- api/handler/chatimpl/openai_handler.go | 2 +- api/test/test.go | 44 ++++++++++++++++++++++++-- web/src/views/mobile/Profile.vue | 7 ++-- 4 files changed, 80 insertions(+), 13 deletions(-) diff --git a/api/handler/chatimpl/chat_handler.go b/api/handler/chatimpl/chat_handler.go index be4e771b..4b150554 100644 --- a/api/handler/chatimpl/chat_handler.go +++ b/api/handler/chatimpl/chat_handler.go @@ -6,6 +6,7 @@ import ( "chatplus/core/types" "chatplus/handler" logger2 "chatplus/logger" + "chatplus/service/oss" "chatplus/store/model" "chatplus/store/vo" "chatplus/utils" @@ -16,6 +17,7 @@ import ( "fmt" "net/http" "net/url" + "regexp" "strings" "time" @@ -33,14 +35,16 @@ var logger = logger2.GetLogger() type ChatHandler struct { handler.BaseHandler - db *gorm.DB - redis *redis.Client + db *gorm.DB + redis *redis.Client + uploadManager *oss.UploaderManager } -func NewChatHandler(app *core.AppServer, db *gorm.DB, redis *redis.Client) *ChatHandler { +func NewChatHandler(app *core.AppServer, db *gorm.DB, redis *redis.Client, manager *oss.UploaderManager) *ChatHandler { h := ChatHandler{ - db: db, - redis: redis, + db: db, + redis: redis, + uploadManager: manager, } h.App = app return &h @@ -546,3 +550,29 @@ func (h *ChatHandler) incUserTokenFee(userId uint, tokens int) { h.db.Model(&model.User{}).Where("id = ?", userId). UpdateColumn("tokens", gorm.Expr("tokens + ?", tokens)) } + +// 将AI回复消息中生成的图片链接下载到本地 +func (h *ChatHandler) extractImgUrl(text string) string { + pattern := `!\[([^\]]*)]\(([^)]+)\)` + re := regexp.MustCompile(pattern) + matches := re.FindAllStringSubmatch(text, -1) + + // 下载图片并替换链接地址 + for _, match := range matches { + imageURL := match[2] + logger.Debug(imageURL) + // 对于相同地址的图片,已经被替换了,就不再重复下载了 + if !strings.Contains(text, imageURL) { + continue + } + + newImgURL, err := h.uploadManager.GetUploadHandler().PutImg(imageURL, false) + if err != nil { + logger.Error("error with download image: ", err) + continue + } + + text = strings.ReplaceAll(text, imageURL, newImgURL) + } + return text +} diff --git a/api/handler/chatimpl/openai_handler.go b/api/handler/chatimpl/openai_handler.go index ff2fd20b..3c228ead 100644 --- a/api/handler/chatimpl/openai_handler.go +++ b/api/handler/chatimpl/openai_handler.go @@ -233,7 +233,7 @@ func (h *ChatHandler) sendOpenAiMessage( RoleId: role.Id, Type: types.ReplyMsg, Icon: role.Icon, - Content: message.Content, + Content: h.extractImgUrl(message.Content), Tokens: totalTokens, UseContext: useContext, Model: req.Model, diff --git a/api/test/test.go b/api/test/test.go index 667f3f7b..f07cb9ac 100644 --- a/api/test/test.go +++ b/api/test/test.go @@ -2,10 +2,48 @@ package main import ( "fmt" - "net/url" + "regexp" ) func main() { - u, err := url.Parse("https://api.chat-plus.net/mj/image/1706368258238514?aaa=bbb") - fmt.Println(u.Path, u.RawQuery, err) + text := ` +> search("Shenzhen weather January 15, 2024") + +> mclick([0, 9, 16]) + +> **end-searching** + +今天深圳的天气情况如下: + +- 白天气温预计在21°C至24°C之间,天气晴朗。 +- 晚上气温预计在21°C左右,云量较多,可能会有间断性小雨。 +- 风向主要是东南风,风速大约在6至12公里每小时之间。 + +这些信息表明深圳今天的天气相对舒适,适合户外活动。晚上可能需要带伞以应对间断性小雨。温度较为宜人,早晚可能稍微凉爽一些【[Shenzhen weather in January 2024 | Shenzhen 14 day weather](https://www.weather25.com/asia/china/guangdong/shenzhen?page=month&month=January)】【[Hourly forecast for Shenzhen, Guangdong, China](https://www.timeanddate.com/weather/china/shenzhen/hourly)】【[Shenzhen Guangdong China 15 Day Weather Forecast](https://www.weatheravenue.com/en/asia/cn/guangdong/shenzhen-weather-15-days.html)】。 + +我将根据这些信息生成一张气象图,展示深圳今天的天气情况。 + + {"prompt":"A detailed weather map for Shenzhen, China, on January 15, 2024. The map shows a sunny day with clear skies during the day and partly cloudy skies at night. Temperatures range from 21\u00b0C to 24\u00b0C during the day and around 21\u00b0C at night. There are indications of light southeast winds during the day and evening, with wind speeds ranging from 6 to 12 km/h. The map includes symbols for sunshine, light clouds, and wind direction arrows, along with temperature readings for different times of the day. The layout is clear, with a focus on Shenzhen's geographical location and the surrounding region.","size":"1024x1024"} + + +![image1](https://filesystem.site/cdn/20240115/XD6EjyPDGCD4X3AQt3h3FijRmSb6fB.webp) + +![下载1](https://filesystem.site/cdn/download/20240115/XD6EjyPDGCD4X3AQt3h3FijRmSb6fB.webp) + +And here is another image link: ![another image](https://example.com/another-image.png). + + +这是根据今天深圳的天气情况制作的气象图。图中展示了白天晴朗、夜间部分多云的天气,以及相关的温度和风向信息。` + pattern := `!\[([^\]]*)]\(([^)]+)\)` + + // 编译正则表达式 + re := regexp.MustCompile(pattern) + + // 查找匹配的字符串 + matches := re.FindAllStringSubmatch(text, -1) + + // 提取链接并打印 + for _, match := range matches { + fmt.Println(match[2]) + } } diff --git a/web/src/views/mobile/Profile.vue b/web/src/views/mobile/Profile.vue index 1231fdf6..6215edc9 100644 --- a/web/src/views/mobile/Profile.vue +++ b/web/src/views/mobile/Profile.vue @@ -6,12 +6,11 @@