Spaces:
Sleeping
Sleeping
Update src/app/api/qa/route.ts
Browse files- src/app/api/qa/route.ts +7 -11
src/app/api/qa/route.ts
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import { NextRequest, NextResponse } from 'next/server'
|
2 |
import { HfInference } from '@huggingface/inference'
|
3 |
|
4 |
-
const hf = new HfInference(process.env.
|
5 |
|
6 |
interface RequestBody {
|
7 |
query: string
|
@@ -13,25 +13,21 @@ export async function POST(request: NextRequest) {
|
|
13 |
const body: RequestBody = await request.json()
|
14 |
const { query, documents } = body
|
15 |
|
16 |
-
// If query is empty, return early
|
17 |
if (!query.trim()) {
|
18 |
return NextResponse.json({
|
19 |
answer: 'Please enter a question.'
|
20 |
})
|
21 |
}
|
22 |
|
23 |
-
// Clean and format the document texts
|
24 |
const context = documents
|
25 |
.map(doc => {
|
26 |
-
// Clean up the text and ensure proper spacing
|
27 |
return doc.text
|
28 |
-
.replace(/\s+/g, ' ')
|
29 |
-
.trim()
|
30 |
})
|
31 |
.filter(text => text.length > 0)
|
32 |
-
.join('\n\n')
|
33 |
|
34 |
-
// Create a prompt following Llama's instruction format
|
35 |
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.
|
36 |
|
37 |
Context:
|
@@ -48,13 +44,13 @@ ${query} [/INST]`
|
|
48 |
temperature: 0.7,
|
49 |
top_p: 0.95,
|
50 |
repetition_penalty: 1.15,
|
51 |
-
return_full_text: false
|
52 |
}
|
53 |
})
|
54 |
|
55 |
const answer = response.generated_text?.trim()
|
56 |
-
.replace(/\[INST\]/g, '')
|
57 |
-
.replace(/\[\/INST\]/g, '')
|
58 |
.trim() || 'Failed to generate an answer'
|
59 |
|
60 |
return NextResponse.json({ answer })
|
|
|
1 |
import { NextRequest, NextResponse } from 'next/server'
|
2 |
import { HfInference } from '@huggingface/inference'
|
3 |
|
4 |
+
const hf = new HfInference(process.env.API_TOKEN)
|
5 |
|
6 |
interface RequestBody {
|
7 |
query: string
|
|
|
13 |
const body: RequestBody = await request.json()
|
14 |
const { query, documents } = body
|
15 |
|
|
|
16 |
if (!query.trim()) {
|
17 |
return NextResponse.json({
|
18 |
answer: 'Please enter a question.'
|
19 |
})
|
20 |
}
|
21 |
|
|
|
22 |
const context = documents
|
23 |
.map(doc => {
|
|
|
24 |
return doc.text
|
25 |
+
.replace(/\s+/g, ' ')
|
26 |
+
.trim()
|
27 |
})
|
28 |
.filter(text => text.length > 0)
|
29 |
+
.join('\n\n')
|
30 |
|
|
|
31 |
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.
|
32 |
|
33 |
Context:
|
|
|
44 |
temperature: 0.7,
|
45 |
top_p: 0.95,
|
46 |
repetition_penalty: 1.15,
|
47 |
+
return_full_text: false
|
48 |
}
|
49 |
})
|
50 |
|
51 |
const answer = response.generated_text?.trim()
|
52 |
+
.replace(/\[INST\]/g, '')
|
53 |
+
.replace(/\[\/INST\]/g, '')
|
54 |
.trim() || 'Failed to generate an answer'
|
55 |
|
56 |
return NextResponse.json({ answer })
|