Skip to content

Commit

Permalink
google attachment fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nbonamy committed May 15, 2024
1 parent d796fda commit 413d491
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 15 deletions.
11 changes: 11 additions & 0 deletions src/models/attachment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

export const textFormats = ['pdf', 'txt', 'docx', 'pptx', 'xlsx']
export const imageFormats = ['jpeg', 'jpg', 'png', 'webp']

export default class Attachment {

url: string
Expand All @@ -20,6 +23,14 @@ export default class Attachment {
}
}

isText(): boolean {
return textFormats.includes(this.format)
}

isImage(): boolean {
return imageFormats.includes(this.format)
}

extractText(): void {
const rawText = window.api.file.extractText(this.contents, this.format)
//console.log('Raw text:', rawText)
Expand Down
7 changes: 3 additions & 4 deletions src/services/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import Plugin from '../plugins/plugin'
import BrowsePlugin from '../plugins/browse'
import TavilyPlugin from '../plugins/tavily'
import PythonPlugin from '../plugins/python'
import { textFormats, imageFormats } from './llm'
import { PluginParameter } from '../types/plugin.d'
import { minimatch } from 'minimatch'

Expand Down Expand Up @@ -98,7 +97,7 @@ export default class LlmEngine {
}

// check if amy of the messages in the thread have an attachment
return thread.some((msg) => msg.attachment && imageFormats.includes(msg.attachment.format))
return thread.some((msg) => msg.attachment && msg.attachment.isImage())

}

Expand Down Expand Up @@ -152,12 +151,12 @@ export default class LlmEngine {
}

// text formats
if (textFormats.includes(msg.attachment.format)) {
if (msg.attachment.isText()) {
payload.content += `\n\n${msg.attachment.contents}`
}

// image formats
if (imageFormats.includes(msg.attachment.format)) {
if (msg.attachment.isImage()) {
if (!imageAttached && this.isVisionModel(model)) {
this.addImageToPayload(msg, payload)
imageAttached = true
Expand Down
16 changes: 10 additions & 6 deletions src/services/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,16 @@ export default class extends LlmEngine {
}

// add inline
prompt.push({
inlineData: {
mimeType: 'image/png',
data: lastMessage.attachment.contents,
}
})
if (lastMessage.attachment.isImage()) {
prompt.push({
inlineData: {
mimeType: 'image/png',
data: lastMessage.attachment.contents,
}
})
} else if (lastMessage.attachment.isText()) {
prompt.push(lastMessage.attachment.contents)
}

}

Expand Down
3 changes: 1 addition & 2 deletions src/services/llm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import { Model, EngineConfig, Configuration } from '../types/config.d'
import { imageFormats, textFormats } from '../models/attachment'
import { store } from './store'
import OpenAI, { isOpenAIReady } from './openai'
import Ollama, { isOllamaReady } from './ollama'
Expand All @@ -10,8 +11,6 @@ import Groq, { isGroqReady } from './groq'
import LlmEngine from './engine'

export const availableEngines = ['openai', 'ollama', 'anthropic', 'mistralai', 'google', 'groq']
export const textFormats = ['pdf', 'txt', 'docx', 'pptx', 'xlsx']
export const imageFormats = ['jpeg', 'jpg', 'png', 'webp']

export const isEngineReady = (engine: string) => {
if (engine === 'openai') return isOpenAIReady(store.config.engines.openai)
Expand Down
2 changes: 2 additions & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ interface Attachment {
format: string
contents: string
downloaded: boolean
isText(): boolean
isImage(): boolean
}

interface Command {
Expand Down
15 changes: 12 additions & 3 deletions tests/unit/engine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { isEngineReady, igniteEngine, hasVisionModels, isVisionModel, loadOpenAI
import { store } from '../../src/services/store'
import defaults from '../../defaults/settings.json'
import Message from '../../src/models/message'
import Attachment from '../../src/models/attachment'
import OpenAI from '../../src/services/openai'
import Ollama from '../../src/services/ollama'
import MistralAI from '../../src/services/mistralai'
import Anthropic from '../../src/services/anthropic'
import Google from '../../src/services/google'
import Groq from '../../src/services/groq'
import { Model } from '../../src/types/config.d'
import { text } from 'stream/consumers'

const model = [{ id: 'llava:latest', name: 'llava:latest', meta: {} }]

Expand All @@ -31,6 +31,15 @@ vi.mock('openai', async() => {
return { default: OpenAI }
})

window.api = {
base64: {
decode: (data: string) => data
},
file: {
extractText: (contents) => contents
}
}

beforeEach(() => {
store.config = defaults
})
Expand Down Expand Up @@ -166,7 +175,7 @@ test('Build payload with text attachment', async () => {
new Message('system', { role: 'system', type: 'text', content: 'instructions' }),
new Message('user', { role: 'user', type: 'text', content: 'prompt1' }),
]
messages[1].attachFile({ format: 'txt', contents: 'attachment', downloaded: true, url: '' })
messages[1].attachFile(new Attachment('', 'txt', 'attachment', true))
expect(openai.buildPayload(messages, 'gpt-model1')).toStrictEqual([
{ role: 'system', content: 'instructions' },
{ role: 'user', content: 'prompt1\n\nattachment' },
Expand All @@ -179,7 +188,7 @@ test('Build payload with image attachment', async () => {
new Message('system', { role: 'system', type: 'text', content: 'instructions' }),
new Message('user', { role: 'user', type: 'text', content: 'prompt1' }),
]
messages[1].attachFile({ format: 'png', contents: 'attachment', downloaded: true, url: '' })
messages[1].attachFile(new Attachment('', 'png', 'attachment', true))
expect(openai.buildPayload(messages, 'gpt-model1')).toStrictEqual([
{ role: 'system', content: 'instructions' },
{ role: 'user', content: 'prompt1' },
Expand Down

0 comments on commit 413d491

Please sign in to comment.