Spaces:
Sleeping
Sleeping
import { NextRequest, NextResponse } from 'next/server' | |
import { HfInference } from '@huggingface/inference' | |
const hf = new HfInference(process.env.API_TOKEN) | |
interface RequestBody { | |
query: string | |
documents: Array<{ text: string }> | |
} | |
export async function POST(request: NextRequest) { | |
try { | |
const body: RequestBody = await request.json() | |
const { query, documents } = body | |
if (!query.trim()) { | |
return NextResponse.json({ | |
answer: 'Please enter a question.' | |
}) | |
} | |
const context = documents | |
.map(doc => { | |
return doc.text | |
.replace(/\s+/g, ' ') | |
.trim() | |
}) | |
.filter(text => text.length > 0) | |
.join('\n\n') | |
const prompt = `[INST] You are a helpful AI assistant. Please answer the following question based on the provided context. If the answer cannot be found in the context, say "I cannot find the answer in the provided documents." Answer in the same language as the question. Do not include any instruction tags in your response. | |
Context: | |
${context} | |
Question: | |
${query} [/INST]` | |
const response = await hf.textGeneration({ | |
model: 'nvidia/Llama-3.1-Nemotron-70B-Instruct-HF', | |
inputs: prompt, | |
parameters: { | |
max_new_tokens: 512, | |
temperature: 0.7, | |
top_p: 0.95, | |
repetition_penalty: 1.15, | |
return_full_text: false | |
} | |
}) | |
const answer = response.generated_text?.trim() | |
.replace(/\[INST\]/g, '') | |
.replace(/\[\/INST\]/g, '') | |
.trim() || 'Failed to generate an answer' | |
return NextResponse.json({ answer }) | |
} catch (error) { | |
console.error('Error processing QA request:', error) | |
return NextResponse.json( | |
{ error: 'Failed to process request' }, | |
{ status: 500 } | |
) | |
} | |
} |