Skip to content

Commit a2b56a0

Browse files
committed
Demo: RAG/Docling/llama-index
Signed-off-by: Brent Salisbury <[email protected]>
1 parent 67eefbb commit a2b56a0

File tree

8 files changed

+980
-1
lines changed

8 files changed

+980
-1
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use server';
2+
3+
import { NextRequest, NextResponse } from 'next/server';
4+
import fetch from 'node-fetch';
5+
6+
export async function DELETE(req: NextRequest, { params }: { params: { collectionName: string } }) {
7+
const { collectionName } = params;
8+
9+
try {
10+
console.log(`Deleting collection: ${collectionName}`);
11+
12+
// Make the API request to the backend to delete the collection
13+
const response = await fetch(`http://127.0.0.1:8000/collections/${encodeURIComponent(collectionName)}`, {
14+
method: 'DELETE'
15+
});
16+
17+
// Check if the response was successful
18+
if (!response.ok) {
19+
const errorText = await response.text();
20+
console.error(`Failed to delete collection: ${errorText}`);
21+
throw new Error(`Failed to delete collection: ${errorText}`);
22+
}
23+
24+
// Return a success response to the client
25+
console.log(`Collection ${collectionName} deleted successfully.`);
26+
return NextResponse.json({ message: `Collection ${collectionName} deleted successfully.` }, { status: 200 });
27+
} catch (error: any) {
28+
console.error('Error deleting collection:', error.message);
29+
return NextResponse.json({ error: error.message }, { status: 500 });
30+
}
31+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// src/app/api/playground/ragchat/collections/[collectionName]/documents/file/route.ts
2+
'use server';
3+
4+
import { NextRequest, NextResponse } from 'next/server';
5+
import fetch from 'node-fetch';
6+
import FormData from 'form-data';
7+
8+
export async function POST(req: NextRequest, { params }: { params: { collectionName: string } }) {
9+
const { collectionName } = params;
10+
11+
try {
12+
// Parse the form data from the incoming request
13+
const formData = await req.formData();
14+
const file = formData.get('files') as File | null;
15+
16+
if (!file) {
17+
throw new Error('File is required for upload');
18+
}
19+
20+
// Create FormData for the backend request
21+
const backendFormData = new FormData();
22+
23+
// Convert the file to a Buffer for the Node.js environment
24+
const buffer = Buffer.from(await file.arrayBuffer());
25+
26+
// Append the file buffer to FormData
27+
backendFormData.append('file', buffer, file.name);
28+
29+
// Send the file to the backend service
30+
const backendResponse = await fetch(`http://127.0.0.1:8000/collections/${encodeURIComponent(collectionName)}/documents/file`, {
31+
method: 'POST',
32+
body: backendFormData,
33+
headers: backendFormData.getHeaders()
34+
});
35+
36+
const backendResponseText = await backendResponse.text();
37+
38+
if (!backendResponse.ok) {
39+
throw new Error(`Failed to upload file to backend: ${backendResponseText}`);
40+
}
41+
42+
return NextResponse.json({ message: 'File uploaded successfully', data: backendResponseText }, { status: 200 });
43+
} catch (error: any) {
44+
console.error('Error during file upload:', error.message);
45+
return NextResponse.json({ error: error.message }, { status: 500 });
46+
}
47+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// src/app/api/playground/ragchat/collections/[collectionName]/documents/url/route.ts
2+
`use server`;
3+
4+
import { NextRequest, NextResponse } from 'next/server';
5+
6+
export async function POST(req: NextRequest, { params }: { params: { collectionName: string } }) {
7+
const { collectionName } = params;
8+
9+
try {
10+
const { http_source } = await req.json();
11+
12+
const response = await fetch(`http://localhost:8000/collections/${encodeURIComponent(collectionName)}/documents/url`, {
13+
method: 'POST',
14+
headers: {
15+
'Content-Type': 'application/json'
16+
},
17+
body: JSON.stringify({ http_source })
18+
});
19+
20+
const responseText = await response.text();
21+
22+
if (!response.ok) {
23+
throw new Error(`Failed to upload URL: ${responseText}`);
24+
}
25+
26+
return NextResponse.json({ message: 'URL uploaded successfully', data: responseText }, { status: 200 });
27+
} catch (error: any) {
28+
console.error('Error uploading URL:', error.message);
29+
return NextResponse.json({ error: error.message }, { status: 500 });
30+
}
31+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// src/app/api/playground/ragchat/collections/[collectionName]/query/route.ts
2+
'use server';
3+
4+
import { NextRequest, NextResponse } from 'next/server';
5+
import fetch from 'node-fetch';
6+
7+
export async function POST(req: NextRequest, { params }: { params: { collectionName: string } }) {
8+
const { collectionName } = params;
9+
10+
try {
11+
const { question } = await req.json();
12+
13+
console.log(`Received question: ${question} for collection: ${collectionName}`);
14+
15+
const response = await fetch(`http://127.0.0.1:8000/collections/${encodeURIComponent(collectionName)}/query`, {
16+
method: 'POST',
17+
headers: {
18+
'Content-Type': 'application/json'
19+
},
20+
body: JSON.stringify({ question })
21+
});
22+
23+
// Check if the response was successful
24+
if (!response.ok) {
25+
const errorText = await response.text();
26+
console.error(`Failed to query collection: ${errorText}`);
27+
throw new Error(`Failed to query collection: ${errorText}`);
28+
}
29+
30+
// Parse the backend response
31+
const responseData = await response.json();
32+
console.log('Backend response data:', responseData);
33+
34+
// Extract the 'answer' and 'sources' fields
35+
const { answer, sources } = responseData;
36+
37+
// Return the answer and sources to the client
38+
return NextResponse.json({ answer, sources }, { status: 200 });
39+
} catch (error: any) {
40+
console.error('Error querying collection:', error.message);
41+
return NextResponse.json({ error: error.message }, { status: 500 });
42+
}
43+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// src/app/api/playground/ragchat/collections/route.ts
2+
'use server';
3+
4+
import { NextRequest, NextResponse } from 'next/server';
5+
import fetch from 'node-fetch';
6+
7+
export async function GET(req: NextRequest) {
8+
console.log('Received request to fetch collections');
9+
10+
try {
11+
console.log('Making fetch call to backend service...');
12+
13+
const response = await fetch('http://127.0.0.1:8000/collections', {
14+
method: 'GET',
15+
headers: {
16+
Accept: 'application/json' // Ensure Accept header is set properly
17+
}
18+
});
19+
20+
const rawText = await response.text();
21+
console.log('Raw response text from backend:', rawText);
22+
23+
const data = JSON.parse(rawText);
24+
console.log('Parsed collections data:', data);
25+
26+
return NextResponse.json(data, { status: 200 });
27+
} catch (error: any) {
28+
console.error('Error fetching collections:', error.message);
29+
return NextResponse.json({ error: error.message }, { status: 500 });
30+
}
31+
}

0 commit comments

Comments
 (0)