Skip to content

Commit

Permalink
POC for indexing images using LLM vision
Browse files Browse the repository at this point in the history
  • Loading branch information
crspeller committed Jul 10, 2024
1 parent 90810c9 commit 42dc3c8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
1 change: 1 addition & 0 deletions server/ai/conversation.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
)

type File struct {
ID string
MimeType string
Size int64
Reader io.Reader
Expand Down
41 changes: 41 additions & 0 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ func (p *Plugin) MessageHasBeenPosted(c *plugin.Context, post *model.Post) {
p.pluginAPI.Log.Error(err.Error())
}
}

if len(post.FileIds) > 0 {
if err := p.handleImageIndexing(post); err != nil {
p.pluginAPI.Log.Error(err.Error())
}
}
}

const (
Expand Down Expand Up @@ -297,3 +303,38 @@ func (p *Plugin) handleDMs(bot *Bot, channel *model.Channel, postingUser *model.

return nil
}

func (p *Plugin) handleImageIndexing(post *model.Post) error {
bot := p.GetBotByUsernameOrFirst("copilot")
if bot == nil {
return fmt.Errorf("no bot found")
}

if !bot.cfg.EnableVision {
return fmt.Errorf("vision is disabled")
}

llm := p.getLLM(bot.cfg)

aiFiles := p.PostFilesToAIFiles(bot, post)
for _, file := range aiFiles {
result, err := llm.ChatCompletionNoStream(ai.BotConversation{
Posts: []ai.Post{{
Role: ai.PostRoleUser,
Message: "Write a descripton and a list of keywords that describe this image. Use plaintext only. Write only the description and the keywords. The keywords should be separted with commas.",

Check failure on line 324 in server/plugin.go

View workflow job for this annotation

GitHub Actions / plugin-ci / lint

`descripton` is a misspelling of `description` (misspell)
Files: []ai.File{file},
}},
})
if err != nil {
p.pluginAPI.Log.Warn("failed to generate image description", "error", err, "file_id", file.ID)
continue
}

if err := p.pluginAPI.File.SetSearchableContent(file.ID, result); err != nil {
p.pluginAPI.Log.Warn("failed to set searchable content", "error", err, "file_id", file.ID)
continue
}
}

return nil
}
9 changes: 7 additions & 2 deletions server/post_processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ type WorkerResult struct {
return nil
}*/

func (p *Plugin) PostToAIPost(bot *Bot, post *model.Post) ai.Post {
func (p *Plugin) PostFilesToAIFiles(bot *Bot, post *model.Post) []ai.File {
var files []ai.File
if bot.cfg.EnableVision {
files = make([]ai.File, 0, len(post.FileIds))
Expand All @@ -397,17 +397,22 @@ func (p *Plugin) PostToAIPost(bot *Bot, post *model.Post) ai.Post {
continue
}
files = append(files, ai.File{
ID: fileID,
Reader: file,
MimeType: fileInfo.MimeType,
Size: fileInfo.Size,
})
}
}

return files
}

func (p *Plugin) PostToAIPost(bot *Bot, post *model.Post) ai.Post {
return ai.Post{
Role: ai.GetPostRole(bot.mmBot.UserId, post),
Message: ai.FormatPostBody(post),
Files: files,
Files: p.PostFilesToAIFiles(bot, post),
}
}

Expand Down

0 comments on commit 42dc3c8

Please sign in to comment.