Skip to content

Commit

Permalink
fix: add error handling for model_not_found (#8 by @jasongaare)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasongaare authored Oct 10, 2023
1 parent df7dfc8 commit a8b34e0
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/ai/openai/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { countTokens, estimatedCost } from '../../utils/countTokens'
import { ChatCompletion, ChatRequest, MessageCompletion } from '../../types'

let _openAI: OpenAI | null = null
const openAIModel = 'gpt-4'
export async function openAI() {
if (!_openAI) {
let api_key = process.env.OPENAI_API_KEY
Expand Down Expand Up @@ -43,7 +44,7 @@ export const chatGPTPrompt = async (options: Partial<ChatRequest>): Promise<Mess
const ai = await openAI()

const mergedOptions = {
model: 'gpt-4',
model: openAIModel,
messages: [],
functions: [],
stream: false,
Expand Down Expand Up @@ -78,6 +79,13 @@ export const chatGPTPrompt = async (options: Partial<ChatRequest>): Promise<Mess
}
}

if (e?.error?.code === 'model_not_found') {
return {
content: `ERROR: I'm sorry, I couldn't find the model. Are you sure you have access to model ${openAIModel}?\n\ncode: ${e?.error?.code}\n`,
role: 'assistant',
}
}

console.log('---PROMPT---')
console.dir({ mergedOptions })
console.log('---ERROR---')
Expand Down

0 comments on commit a8b34e0

Please sign in to comment.