|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "start-feishubot/initialization" |
| 8 | + "start-feishubot/services" |
| 9 | + "start-feishubot/services/openai" |
| 10 | + "start-feishubot/utils" |
| 11 | + |
| 12 | + larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1" |
| 13 | +) |
| 14 | + |
| 15 | +type VisionAction struct { /*图片推理*/ |
| 16 | +} |
| 17 | + |
| 18 | +func (va *VisionAction) Execute(a *ActionInfo) bool { |
| 19 | + if !AzureModeCheck(a) { |
| 20 | + return true |
| 21 | + } |
| 22 | + |
| 23 | + if isVisionCommand(a) { |
| 24 | + initializeVisionMode(a) |
| 25 | + sendVisionInstructionCard(*a.ctx, a.info.sessionId, a.info.msgId) |
| 26 | + return false |
| 27 | + } |
| 28 | + |
| 29 | + mode := a.handler.sessionCache.GetMode(*a.info.sessionId) |
| 30 | + |
| 31 | + if a.info.msgType == "image" { |
| 32 | + if mode != services.ModeVision { |
| 33 | + sendVisionModeCheckCard(*a.ctx, a.info.sessionId, a.info.msgId) |
| 34 | + return false |
| 35 | + } |
| 36 | + |
| 37 | + return va.handleVisionImage(a) |
| 38 | + } |
| 39 | + |
| 40 | + if a.info.msgType == "post" && mode == services.ModeVision { |
| 41 | + return va.handleVisionPost(a) |
| 42 | + } |
| 43 | + |
| 44 | + return true |
| 45 | +} |
| 46 | + |
| 47 | +func isVisionCommand(a *ActionInfo) bool { |
| 48 | + _, foundPic := utils.EitherTrimEqual(a.info.qParsed, "/vision", "图片推理") |
| 49 | + return foundPic |
| 50 | +} |
| 51 | + |
| 52 | +func initializeVisionMode(a *ActionInfo) { |
| 53 | + a.handler.sessionCache.Clear(*a.info.sessionId) |
| 54 | + a.handler.sessionCache.SetMode(*a.info.sessionId, services.ModeVision) |
| 55 | + a.handler.sessionCache.SetVisionDetail(*a.info.sessionId, services.VisionDetailHigh) |
| 56 | +} |
| 57 | + |
| 58 | +func (va *VisionAction) handleVisionImage(a *ActionInfo) bool { |
| 59 | + detail := a.handler.sessionCache.GetVisionDetail(*a.info.sessionId) |
| 60 | + base64, err := downloadAndEncodeImage(a.info.imageKey, a.info.msgId) |
| 61 | + if err != nil { |
| 62 | + replyWithErrorMsg(*a.ctx, err, a.info.msgId) |
| 63 | + return false |
| 64 | + } |
| 65 | + |
| 66 | + return va.processImageAndReply(a, base64, detail) |
| 67 | +} |
| 68 | + |
| 69 | +func (va *VisionAction) handleVisionPost(a *ActionInfo) bool { |
| 70 | + detail := a.handler.sessionCache.GetVisionDetail(*a.info.sessionId) |
| 71 | + var base64s []string |
| 72 | + |
| 73 | + for _, imageKey := range a.info.imageKeys { |
| 74 | + if imageKey == "" { |
| 75 | + continue |
| 76 | + } |
| 77 | + base64, err := downloadAndEncodeImage(imageKey, a.info.msgId) |
| 78 | + if err != nil { |
| 79 | + replyWithErrorMsg(*a.ctx, err, a.info.msgId) |
| 80 | + return false |
| 81 | + } |
| 82 | + base64s = append(base64s, base64) |
| 83 | + } |
| 84 | + |
| 85 | + if len(base64s) == 0 { |
| 86 | + replyMsg(*a.ctx, "🤖️:请发送一张图片", a.info.msgId) |
| 87 | + return false |
| 88 | + } |
| 89 | + |
| 90 | + return va.processMultipleImagesAndReply(a, base64s, detail) |
| 91 | +} |
| 92 | + |
| 93 | +func downloadAndEncodeImage(imageKey string, msgId *string) (string, error) { |
| 94 | + f := fmt.Sprintf("%s.png", imageKey) |
| 95 | + defer os.Remove(f) |
| 96 | + |
| 97 | + req := larkim.NewGetMessageResourceReqBuilder().MessageId(*msgId).FileKey(imageKey).Type("image").Build() |
| 98 | + resp, err := initialization.GetLarkClient().Im.MessageResource.Get(context.Background(), req) |
| 99 | + if err != nil { |
| 100 | + return "", err |
| 101 | + } |
| 102 | + |
| 103 | + resp.WriteFile(f) |
| 104 | + return openai.GetBase64FromImage(f) |
| 105 | +} |
| 106 | + |
| 107 | +func replyWithErrorMsg(ctx context.Context, err error, msgId *string) { |
| 108 | + replyMsg(ctx, fmt.Sprintf("🤖️:图片下载失败,请稍后再试~\n 错误信息: %v", err), msgId) |
| 109 | +} |
| 110 | + |
| 111 | +func (va *VisionAction) processImageAndReply(a *ActionInfo, base64 string, detail string) bool { |
| 112 | + msg := createVisionMessages("解释这个图片", base64, detail) |
| 113 | + completions, err := a.handler.gpt.GetVisionInfo(msg) |
| 114 | + if err != nil { |
| 115 | + replyWithErrorMsg(*a.ctx, err, a.info.msgId) |
| 116 | + return false |
| 117 | + } |
| 118 | + sendVisionTopicCard(*a.ctx, a.info.sessionId, a.info.msgId, completions.Content) |
| 119 | + return false |
| 120 | +} |
| 121 | + |
| 122 | +func (va *VisionAction) processMultipleImagesAndReply(a *ActionInfo, base64s []string, detail string) bool { |
| 123 | + msg := createMultipleVisionMessages(a.info.qParsed, base64s, detail) |
| 124 | + completions, err := a.handler.gpt.GetVisionInfo(msg) |
| 125 | + if err != nil { |
| 126 | + replyWithErrorMsg(*a.ctx, err, a.info.msgId) |
| 127 | + return false |
| 128 | + } |
| 129 | + sendVisionTopicCard(*a.ctx, a.info.sessionId, a.info.msgId, completions.Content) |
| 130 | + return false |
| 131 | +} |
| 132 | + |
| 133 | +func createVisionMessages(query, base64Image, detail string) []openai.VisionMessages { |
| 134 | + return []openai.VisionMessages{ |
| 135 | + { |
| 136 | + Role: "user", |
| 137 | + Content: []openai.ContentType{ |
| 138 | + {Type: "text", Text: query}, |
| 139 | + {Type: "image_url", ImageURL: &openai.ImageURL{ |
| 140 | + URL: "data:image/jpeg;base64," + base64Image, |
| 141 | + Detail: detail, |
| 142 | + }}, |
| 143 | + }, |
| 144 | + }, |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +func createMultipleVisionMessages(query string, base64Images []string, detail string) []openai.VisionMessages { |
| 149 | + content := []openai.ContentType{{Type: "text", Text: query}} |
| 150 | + for _, base64Image := range base64Images { |
| 151 | + content = append(content, openai.ContentType{ |
| 152 | + Type: "image_url", |
| 153 | + ImageURL: &openai.ImageURL{ |
| 154 | + URL: "data:image/jpeg;base64," + base64Image, |
| 155 | + Detail: detail, |
| 156 | + }, |
| 157 | + }) |
| 158 | + } |
| 159 | + return []openai.VisionMessages{{Role: "user", Content: content}} |
| 160 | +} |
0 commit comments