@@ -5,10 +5,12 @@ import (
5
5
"errors"
6
6
"fmt"
7
7
"math/rand"
8
+ "strconv"
8
9
"strings"
9
10
10
11
"github.com/krau/ManyACG/adapter"
11
12
"github.com/krau/ManyACG/common"
13
+ "github.com/krau/ManyACG/config"
12
14
13
15
"github.com/krau/ManyACG/service"
14
16
"github.com/krau/ManyACG/sources"
@@ -76,3 +78,62 @@ func RandomPicture(ctx context.Context, bot *telego.Bot, message telego.Message)
76
78
}
77
79
}
78
80
}
81
+
82
+ func HybridSearchArtworks (ctx context.Context , bot * telego.Bot , message telego.Message ) {
83
+ if common .MeilisearchClient == nil {
84
+ utils .ReplyMessage (bot , message , "未启用混合搜索功能" )
85
+ return
86
+ }
87
+ _ , _ , args := telegoutil .ParseCommand (message .Text )
88
+ if len (args ) == 0 {
89
+ utils .ReplyMessage (bot , message , "使用方法: /query <搜索内容> [语义比例]\n 语义比例为0-1的浮点数, 应位于参数列表最后, 越大越趋向于基于语义搜索, 若不提供, 使用默认值0.8" )
90
+ return
91
+ }
92
+ var hybridSemanticRatio float64
93
+ var queryText string
94
+ hybridSemanticRatio , err := strconv .ParseFloat (args [len (args )- 1 ], 64 )
95
+ if err != nil {
96
+ hybridSemanticRatio = 0.8
97
+ queryText = strings .Join (args , " " )
98
+ } else {
99
+ if hybridSemanticRatio < 0 || hybridSemanticRatio > 1 {
100
+ utils .ReplyMessage (bot , message , "参数错误: 语义比例应为0-1的浮点数" )
101
+ return
102
+ }
103
+ queryText = strings .Join (args [:len (args )- 1 ], " " )
104
+ }
105
+ artworks , err := service .HybridSearchArtworks (ctx , queryText , hybridSemanticRatio , 10 )
106
+ if err != nil {
107
+ common .Logger .Errorf ("搜索失败: %s" , err )
108
+ utils .ReplyMessage (bot , message , "搜索失败, 请联系管理员检查搜索引擎设置与状态" )
109
+ return
110
+ }
111
+ if len (artworks ) == 0 {
112
+ utils .ReplyMessage (bot , message , "未找到相关图片" )
113
+ return
114
+ }
115
+
116
+ if len (artworks ) > 10 {
117
+ artworks = artworks [:10 ]
118
+ }
119
+
120
+ inputMedias := make ([]telego.InputMedia , 0 , len (artworks ))
121
+ for _ , artwork := range artworks {
122
+ picture := artwork .Pictures [0 ]
123
+ var file telego.InputFile
124
+ if picture .TelegramInfo != nil && picture .TelegramInfo .PhotoFileID != "" {
125
+ file = telegoutil .FileFromID (picture .TelegramInfo .PhotoFileID )
126
+ } else {
127
+ photoURL := fmt .Sprintf ("%s/?url=%s&w=2560&h=2560&we&output=jpg" , config .Cfg .WSRVURL , picture .Original )
128
+ file = telegoutil .FileFromURL (photoURL )
129
+ }
130
+ caption := fmt .Sprintf ("<a href=\" %s\" >%s</a>" , artwork .SourceURL , common .EscapeHTML (artwork .Title ))
131
+ inputMedias = append (inputMedias , telegoutil .MediaPhoto (file ).WithCaption (caption ).WithParseMode (telego .ModeHTML ))
132
+ }
133
+ mediaGroup := telegoutil .MediaGroup (message .Chat .ChatID (), inputMedias ... )
134
+ _ , err = bot .SendMediaGroup (mediaGroup )
135
+ if err != nil {
136
+ common .Logger .Errorf ("发送图片失败: %s" , err )
137
+ }
138
+
139
+ }
0 commit comments