Skip to content

Commit

Permalink
Attempt fix for incorrect chunk handling
Browse files Browse the repository at this point in the history
  • Loading branch information
poteat committed Jul 3, 2023
1 parent e2c008f commit c29de03
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"semi": false,
"singleQuote": false
}
25 changes: 25 additions & 0 deletions apps/spellbound/src/api/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ export async function listModels(apiKey: string): Promise<string[]> {
}
}

type PossibleEndpointError = {
error?: {
message?: string | null
type?: string | null
param?: string | null
code?: string | null
} | null
} | null

const nonRetryErrorTypes = ["tokens", "invalid_request_error"]

function parseCompletionDataChunk(buffer: Buffer): string {
const contentDelimiter = "\n\n"
const jsonDataPrefix = "data: "
Expand All @@ -91,6 +102,20 @@ function parseCompletionDataChunk(buffer: Buffer): string {
}
} else {
if (chunk) {
let chunkObj: PossibleEndpointError = null

try {
chunkObj = JSON.parse(chunk)
} catch (error) {
console.error("Error parsing chunk:", chunk, error)
}

if (nonRetryErrorTypes.some((x) => x === chunkObj?.error?.type)) {
console.error("Fatal error, not retrying:", chunk)

throw chunkObj
}

console.error("Unexpected chunk:", chunk)
}
}
Expand Down

0 comments on commit c29de03

Please sign in to comment.