Skip to content

Commit

Permalink
assistant errors tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nbonamy committed May 1, 2024
1 parent 47daf8a commit a14bca2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/services/assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,13 @@ export default class {
if (error.name !== 'AbortError') {
if (error.status === 401 || error.message.includes('401') || error.message.toLowerCase().includes('apikey')) {
message.setText('You need to enter your API key in the Models tab of <a href="#settings">Settings</a> in order to chat.')
callback?.call(null, { done: true })
} else if (error.status === 400 && (error.message.includes('credit') || error.message.includes('balance'))) {
message.setText('Sorry, it seems you have run out of credits. Check the balance of your LLM provider account.')
callback?.call(null, { done: true })
} else if (message.content === '') {
message.setText('Sorry, I could not generate text for that prompt.')
callback?.call(null, { done: true })
} else {
message.appendText({ text: '\n\nSorry, I am not able to continue here.', done: true })
}
Expand Down
22 changes: 22 additions & 0 deletions tests/mocks/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ import { Configuration } from '../../src/types/config.d'
import LlmEngine from '../../src/services/engine'
import RandomChunkStream from './stream'

class LlmError extends Error {

name: string
status: number
message: string

constructor(name: string, status: number, message: string) {
super()
this.name = name
this.status = status
this.message = message
}
}

export default class LlmMock extends LlmEngine {

constructor(config: Configuration) {
Expand Down Expand Up @@ -53,6 +67,14 @@ export default class LlmMock extends LlmEngine {

async stream(thread: Message[], opts: LlmCompletionOpts): Promise<LlmStream> {

// errors
if (thread[thread.length-1].content.includes('no api key')) {
throw new LlmError('NoApiKeyError', 401, 'Missing apiKey')
}
if (thread[thread.length-1].content.includes('no credit')) {
throw new LlmError('LowBalanceError', 400, 'Your balance is too low')
}

// model: switch to vision if needed
const model = this.selectModel(thread, opts?.model || this.getChatModel())

Expand Down
14 changes: 13 additions & 1 deletion tests/unit/assistant.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,16 @@ test('Conversation language', async () => {
const instructions = await assistant.chat.messages[0].content
expect(instructions).toMatch(/French/)

})
})

test('No API Key', async () => {
await prompt('no api key')
const content = assistant.chat.lastMessage().content
expect(content).toBe('You need to enter your API key in the Models tab of <a href="#settings">Settings</a> in order to chat.')
})

test('Low balance', async () => {
await prompt('no credit left')
const content = assistant.chat.lastMessage().content
expect(content).toBe('Sorry, it seems you have run out of credits. Check the balance of your LLM provider account.')
})

0 comments on commit a14bca2

Please sign in to comment.