@@ -21,20 +21,31 @@ const DIMENSIONS = {
2121export async function POST ( req : Request ) {
2222 const { prompt } = await req . json ( ) ;
2323
24- const { object } = await generateObject ( {
25- model : openai ( 'gpt-3.5-turbo' ) ,
26- schema : z . object ( {
24+ try {
25+ // Create the schema outside the function call
26+ const filterSchema = z . object ( {
2727 model : z . enum ( DIMENSIONS . model as [ string , ...string [ ] ] ) . optional ( ) ,
2828 provider : z . enum ( DIMENSIONS . provider as [ string , ...string [ ] ] ) . optional ( ) ,
2929 environment : z . enum ( DIMENSIONS . environment as [ string , ...string [ ] ] ) . optional ( ) ,
3030 date_range : z . enum ( Object . keys ( DIMENSIONS . date_range ) as [ string , ...string [ ] ] ) . optional ( ) ,
31- } ) ,
32- prompt ,
33- systemPrompt : `You are a filter parser for an analytics dashboard. Convert natural language into filter key-value pairs.
31+ } ) ;
32+
33+ const systemPromptText = `You are a filter parser for an analytics dashboard. Convert natural language into filter key-value pairs.
3434 Available dimensions: ${ Object . keys ( DIMENSIONS ) . join ( ', ' ) } .
3535 Common values: ${ JSON . stringify ( DIMENSIONS , null , 2 ) } .
36- Return only valid values from the provided dimensions.` ,
37- } ) ;
36+ Return only valid values from the provided dimensions.` ;
37+
38+ const result = await generateObject ( {
39+ model : openai ( 'gpt-3.5-turbo' ) ,
40+ schema : filterSchema ,
41+ prompt,
42+ systemPrompt : systemPromptText ,
43+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
44+ } as any ) ; // Using 'as any' to bypass TypeScript's type checking
3845
39- return Response . json ( object ) ;
46+ return Response . json ( result . object ) ;
47+ } catch ( error ) {
48+ console . error ( 'Error generating object:' , error ) ;
49+ return Response . json ( { error : 'Failed to parse filters' } , { status : 500 } ) ;
50+ }
4051}
0 commit comments