Skip to content

Commit

Permalink
improve error handling (including collectionName not found)
Browse files Browse the repository at this point in the history
  • Loading branch information
fegloff committed Oct 3, 2023
1 parent 8c62aa2 commit 4c72c4f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 53 deletions.
31 changes: 11 additions & 20 deletions src/modules/llms/api/llmApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios, { AxiosError } from 'axios'
import config from '../../../config'
import { type ChatConversation } from '../../types'

const API_ENDPOINT = config.llms.apiEndpoint // config.llms.apiEndpoint // 'http://127.0.0.1:5000'
const API_ENDPOINT = config.llms.apiEndpoint // 'http://localhost:8080' // http://127.0.0.1:5000' // config.llms.apiEndpoint // config.llms.apiEndpoint // 'http://127.0.0.1:5000'

export interface LlmCompletion {
completion: ChatConversation | undefined
Expand Down Expand Up @@ -67,25 +67,16 @@ interface QueryUrlDocumentOutput {
}

export const queryUrlDocument = async (args: QueryUrlDocument): Promise<QueryUrlDocumentOutput> => {
try {
const data = { collectionName: args.collectioName, prompt: args.prompt, conversation: args.conversation }
console.log(data.conversation)
const endpointUrl = `${API_ENDPOINT}/collections/query`
const response = await axios.post(endpointUrl, data)
if (response) {
return response.data
}
return {
completion: '',
price: 0
}
} catch (error: any) {
if (error instanceof AxiosError) {
console.log(error.code)
console.log(error.message)
console.log(error.stack)
}
throw error
const data = { collectionName: args.collectioName, prompt: args.prompt, conversation: args.conversation }
console.log(data.conversation)
const endpointUrl = `${API_ENDPOINT}/collections/query`
const response = await axios.post(endpointUrl, data)
if (response) {
return response.data
}
return {
completion: '',
price: 0
}
}

Expand Down
55 changes: 23 additions & 32 deletions src/modules/llms/api/vertex.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,37 @@
import axios, { AxiosError } from 'axios'
import axios from 'axios'
import config from '../../../config'
import { type ChatConversation } from '../../types'
import { type LlmCompletion } from './llmApi'

const API_ENDPOINT = config.llms.apiEndpoint
const API_ENDPOINT = config.llms.apiEndpoint // http://localhost:8080' // config.llms.apiEndpoint

export const vertexCompletion = async (
conversation: ChatConversation[],
model = config.llms.model
): Promise<LlmCompletion> => {
try {
const data = {
model, // chat-bison@001 'chat-bison', //'gpt-3.5-turbo',
stream: false,
messages: conversation
}
const url = `${API_ENDPOINT}/vertex/completions`
const response = await axios.post(url, data)
if (response) {
const totalInputTokens = 4 // response.data.usage.prompt_tokens;
const totalOutputTokens = 5 // response.data.usage.completion_tokens;
return {
completion: {
content: response.data._prediction_response[0][0].candidates[0].content,
author: 'bot',
model
},
usage: totalOutputTokens + totalInputTokens,
price: 0
}
}
const data = {
model, // chat-bison@001 'chat-bison', //'gpt-3.5-turbo',
stream: false,
messages: conversation
}
const url = `${API_ENDPOINT}/vertex/completions`
const response = await axios.post(url, data)
if (response) {
const totalInputTokens = 4 // response.data.usage.prompt_tokens;
const totalOutputTokens = 5 // response.data.usage.completion_tokens;
return {
completion: undefined,
usage: 0,
completion: {
content: response.data._prediction_response[0][0].candidates[0].content,
author: 'bot',
model
},
usage: totalOutputTokens + totalInputTokens,
price: 0
}
} catch (error: any) {
if (error instanceof AxiosError) {
console.log(error.code)
console.log(error.message)
console.log(error.stack)
}
throw error
}
return {
completion: undefined,
usage: 0,
price: 0
}
}
13 changes: 12 additions & 1 deletion src/modules/open-ai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,18 @@ export class OpenAIBot implements PayableBot {
}
}
} catch (e: any) {
await this.onError(ctx, e)
if (e instanceof AxiosError) {
if (e.message.includes('404')) {
ctx.session.collections.activeCollections =
[...ctx.session.collections.activeCollections.filter(c => c.url !== url)]
console.log(ctx.session.collections.activeCollections)
await sendMessage(ctx, 'Collection not found, please try again')
} else {
await this.onError(ctx, e)
}
} else {
await this.onError(ctx, e)
}
}
}

Expand Down

0 comments on commit 4c72c4f

Please sign in to comment.