Created
December 1, 2024 10:26
-
-
Save Miguel07Alm/f5dc8bf3bb5f453296b42b9e032b46a3 to your computer and use it in GitHub Desktop.
This code defines a serverless API endpoint for generating custom structured data using AI models. The API leverages Zod schemas to validate and enforce structured output from a given prompt. Key features include: Input Validation: Accepts a user prompt, a system prompt, and a JSON Schema. The JSON Schema is dynamically converted to a Zod schema…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { google } from "@ai-sdk/google"; | |
import { convertToCoreMessages, generateObject } from "ai"; | |
import { NextRequest, NextResponse } from "next/server"; | |
import { Schema, z } from 'zod'; | |
import { JsonSchema, jsonSchemaToZod } from 'json-schema-to-zod'; | |
export const maxDuration = 60; | |
export async function POST(req: NextRequest) { | |
try { | |
const { | |
prompt, | |
zodSchema, | |
systemPrompt, | |
}: { | |
prompt: string; | |
zodSchema: JsonSchema; | |
systemPrompt: string; | |
} = await req.json(); | |
let schema: Schema<any, z.ZodTypeDef, any> | Schema<any>; | |
try { | |
schema = new Function('z', `return ${jsonSchemaToZod(zodSchema)}`)(z); | |
} catch (error) { | |
return NextResponse.json( | |
{ error: 'Invalid schema format' }, | |
{ status: 400 }, | |
); | |
} | |
const result = await generateObject({ | |
messages: convertToCoreMessages([ | |
{ | |
role: 'system', | |
content: systemPrompt, | |
}, | |
{ | |
role: 'user', | |
content: prompt, | |
}, | |
]), | |
model: google('gemini-1.5-flash-latest', { | |
safetySettings: [ | |
{ | |
category: 'HARM_CATEGORY_HATE_SPEECH', | |
threshold: 'BLOCK_NONE', | |
}, | |
{ | |
category: 'HARM_CATEGORY_DANGEROUS_CONTENT', | |
threshold: 'BLOCK_NONE', | |
}, | |
{ | |
category: 'HARM_CATEGORY_HARASSMENT', | |
threshold: 'BLOCK_NONE', | |
}, | |
{ | |
category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT', | |
threshold: 'BLOCK_NONE', | |
}, | |
], | |
structuredOutputs: false, | |
}), | |
schema: schema, | |
}); | |
return NextResponse.json(result.object, { status: 200 }); | |
} catch (error) { | |
console.error(error); | |
return NextResponse.json( | |
{ error: 'Failed to generate response' }, | |
{ status: 500 }, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment