Skip to content

Commit

Permalink
Update response format
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubknejzlik committed Aug 11, 2024
1 parent d16133a commit fbb384c
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/completions/completion-with-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,22 @@ export const functionToOpenAIChatCompletionTool = <T extends z.ZodRawShape>(
}
}

type Response<T extends z.ZodRawShape> = {
object: z.infer<z.ZodObject<T>>
rawResponse: OpenAI.Chat.Completions.ChatCompletionMessage
}

export const completionWithJsonResponse = async <T extends z.ZodRawShape>({
validator,
...opts
}: CompletionOptsWithJsonResponse<T>): Promise<z.infer<z.ZodObject<T>>> => {
}: CompletionOptsWithJsonResponse<T>): Promise<Response<T>> => {
const { responseObject, prompt, ...rest } = opts
const responseObjectSchema = JSON.stringify(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
zodToJsonSchema(responseObject)
)

const _prompt = `Output JSON must be single object (only one JSON object) conforming to the following JsonSchema7:\n${responseObjectSchema}\n\n${prompt ? `${prompt}\n\n` : ''}\n`
const _prompt = `JSON schema:\n${responseObjectSchema}\n\n${prompt ? `${prompt}\n\n` : ''}\nYou MUST answer with a JSON object that matches the JSON schema above.`
const res = await completionWithFunctions({
...rest,
response_format: { type: 'json_object' },
Expand All @@ -55,16 +60,16 @@ export const completionWithJsonResponse = async <T extends z.ZodRawShape>({
if (parsedContent.$schema && parsedContent.properties) {
parsedContent = parsedContent.properties
}
const parsed = responseObject.parse(parsedContent)
const object = responseObject.parse(parsedContent)

if (validator) {
const isValid = await validator(parsed)
const isValid = await validator(object)
if (isValid === false) {
throw new Error('Validation of the response failed. Please try again.')
}
}

return parsed
return { object, rawResponse: res }
} catch (err) {
throw new Error(`Failed to parse response: ${err}, json: '${res.content}'`)
}
Expand All @@ -75,7 +80,7 @@ export const completionWithJsonResponseWithRetry = async <
>(
props: CompletionOptsWithJsonResponse<T>,
retryCount = 2
): Promise<z.infer<z.ZodObject<T>>> => {
): Promise<Response<T>> => {
let latestErr: Error | undefined
try {
return await completionWithJsonResponse(props)
Expand Down

0 comments on commit fbb384c

Please sign in to comment.