import { kv } from '@vercel/kv';
import { pc } from '@/lib/ai/pinecone';
import { Ratelimit } from '@upstash/ratelimit';
import { NextRequest, NextResponse } from 'next/server';
// Allow responses up to 30 seconds
export const maxDuration = 30;
// Create Rate limit
const ratelimit = new Ratelimit({
redis: kv,
limiter: Ratelimit.fixedWindow(5, '30s'),
});
export async function POST(req: NextRequest) {
// call ratelimit with request ip
const { success, remaining } = await ratelimit.limit(req.ip ?? 'ip');
// block the request if unsuccessful
if (!success) return new Response('Rate limit exceeded', { status: 429 });
const { vector, topK } = await req.json();
try {
// Assuming you have an index named 'my-index'
const index = pc.Index(process.env.PINECONE_INDEX);
const queryResponse = await index.query({
vector,
topK,
includeMetadata: true,
});
return NextResponse.json({ results: queryResponse.matches });
} catch (error) {
console.error('Error querying Pinecone:', error);
return NextResponse.json({ error: 'Failed to query Pinecone' }, { status: 500 });
}
}