feat: download image which ai generated in dialog and replace the image url

This commit is contained in:
RockYang 2024-02-20 18:38:03 +08:00
parent 13bfb14107
commit 8a452c3072
4 changed files with 80 additions and 13 deletions

View File

@ -6,6 +6,7 @@ import (
"chatplus/core/types" "chatplus/core/types"
"chatplus/handler" "chatplus/handler"
logger2 "chatplus/logger" logger2 "chatplus/logger"
"chatplus/service/oss"
"chatplus/store/model" "chatplus/store/model"
"chatplus/store/vo" "chatplus/store/vo"
"chatplus/utils" "chatplus/utils"
@ -16,6 +17,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"net/url" "net/url"
"regexp"
"strings" "strings"
"time" "time"
@ -33,14 +35,16 @@ var logger = logger2.GetLogger()
type ChatHandler struct { type ChatHandler struct {
handler.BaseHandler handler.BaseHandler
db *gorm.DB db *gorm.DB
redis *redis.Client 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{ h := ChatHandler{
db: db, db: db,
redis: redis, redis: redis,
uploadManager: manager,
} }
h.App = app h.App = app
return &h return &h
@ -546,3 +550,29 @@ func (h *ChatHandler) incUserTokenFee(userId uint, tokens int) {
h.db.Model(&model.User{}).Where("id = ?", userId). h.db.Model(&model.User{}).Where("id = ?", userId).
UpdateColumn("tokens", gorm.Expr("tokens + ?", tokens)) 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
}

View File

@ -233,7 +233,7 @@ func (h *ChatHandler) sendOpenAiMessage(
RoleId: role.Id, RoleId: role.Id,
Type: types.ReplyMsg, Type: types.ReplyMsg,
Icon: role.Icon, Icon: role.Icon,
Content: message.Content, Content: h.extractImgUrl(message.Content),
Tokens: totalTokens, Tokens: totalTokens,
UseContext: useContext, UseContext: useContext,
Model: req.Model, Model: req.Model,

View File

@ -2,10 +2,48 @@ package main
import ( import (
"fmt" "fmt"
"net/url" "regexp"
) )
func main() { func main() {
u, err := url.Parse("https://api.chat-plus.net/mj/image/1706368258238514?aaa=bbb") text := `
fmt.Println(u.Path, u.RawQuery, err) > 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])
}
} }

View File

@ -6,12 +6,11 @@
<van-form @submit="save"> <van-form @submit="save">
<van-cell-group inset v-model="form"> <van-cell-group inset v-model="form">
<van-field <van-field
v-model="form.mobile" v-model="form.username"
name="手机号" name="号"
label="手机号" label="号"
readonly readonly
disabled disabled
placeholder="手机号"
/> />
<van-field label="头像"> <van-field label="头像">
<template #input> <template #input>