Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC for indexing images using LLM vision #220

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@

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
Loading