Skip to content

Commit

Permalink
Demo: RAG/Docling/llama-index
Browse files Browse the repository at this point in the history
Signed-off-by: Brent Salisbury <[email protected]>
  • Loading branch information
nerdalert committed Oct 21, 2024
1 parent 67eefbb commit a2b56a0
Show file tree
Hide file tree
Showing 8 changed files with 980 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use server';

import { NextRequest, NextResponse } from 'next/server';
import fetch from 'node-fetch';

export async function DELETE(req: NextRequest, { params }: { params: { collectionName: string } }) {
const { collectionName } = params;

try {
console.log(`Deleting collection: ${collectionName}`);

// Make the API request to the backend to delete the collection
const response = await fetch(`http://127.0.0.1:8000/collections/${encodeURIComponent(collectionName)}`, {
method: 'DELETE'
});

// Check if the response was successful
if (!response.ok) {
const errorText = await response.text();
console.error(`Failed to delete collection: ${errorText}`);
throw new Error(`Failed to delete collection: ${errorText}`);
}

// Return a success response to the client
console.log(`Collection ${collectionName} deleted successfully.`);
return NextResponse.json({ message: `Collection ${collectionName} deleted successfully.` }, { status: 200 });
} catch (error: any) {
console.error('Error deleting collection:', error.message);
return NextResponse.json({ error: error.message }, { status: 500 });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// src/app/api/playground/ragchat/collections/[collectionName]/documents/file/route.ts
'use server';

import { NextRequest, NextResponse } from 'next/server';
import fetch from 'node-fetch';
import FormData from 'form-data';

export async function POST(req: NextRequest, { params }: { params: { collectionName: string } }) {
const { collectionName } = params;

try {
// Parse the form data from the incoming request
const formData = await req.formData();
const file = formData.get('files') as File | null;

if (!file) {
throw new Error('File is required for upload');
}

// Create FormData for the backend request
const backendFormData = new FormData();

// Convert the file to a Buffer for the Node.js environment
const buffer = Buffer.from(await file.arrayBuffer());

// Append the file buffer to FormData
backendFormData.append('file', buffer, file.name);

// Send the file to the backend service
const backendResponse = await fetch(`http://127.0.0.1:8000/collections/${encodeURIComponent(collectionName)}/documents/file`, {
method: 'POST',
body: backendFormData,
headers: backendFormData.getHeaders()
});

const backendResponseText = await backendResponse.text();

if (!backendResponse.ok) {
throw new Error(`Failed to upload file to backend: ${backendResponseText}`);
}

return NextResponse.json({ message: 'File uploaded successfully', data: backendResponseText }, { status: 200 });
} catch (error: any) {
console.error('Error during file upload:', error.message);
return NextResponse.json({ error: error.message }, { status: 500 });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// src/app/api/playground/ragchat/collections/[collectionName]/documents/url/route.ts
`use server`;

import { NextRequest, NextResponse } from 'next/server';

export async function POST(req: NextRequest, { params }: { params: { collectionName: string } }) {
const { collectionName } = params;

try {
const { http_source } = await req.json();

const response = await fetch(`http://localhost:8000/collections/${encodeURIComponent(collectionName)}/documents/url`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ http_source })
});

const responseText = await response.text();

if (!response.ok) {
throw new Error(`Failed to upload URL: ${responseText}`);
}

return NextResponse.json({ message: 'URL uploaded successfully', data: responseText }, { status: 200 });
} catch (error: any) {
console.error('Error uploading URL:', error.message);
return NextResponse.json({ error: error.message }, { status: 500 });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// src/app/api/playground/ragchat/collections/[collectionName]/query/route.ts
'use server';

import { NextRequest, NextResponse } from 'next/server';
import fetch from 'node-fetch';

export async function POST(req: NextRequest, { params }: { params: { collectionName: string } }) {
const { collectionName } = params;

try {
const { question } = await req.json();

console.log(`Received question: ${question} for collection: ${collectionName}`);

const response = await fetch(`http://127.0.0.1:8000/collections/${encodeURIComponent(collectionName)}/query`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ question })
});

// Check if the response was successful
if (!response.ok) {
const errorText = await response.text();
console.error(`Failed to query collection: ${errorText}`);
throw new Error(`Failed to query collection: ${errorText}`);
}

// Parse the backend response
const responseData = await response.json();
console.log('Backend response data:', responseData);

// Extract the 'answer' and 'sources' fields
const { answer, sources } = responseData;

// Return the answer and sources to the client
return NextResponse.json({ answer, sources }, { status: 200 });
} catch (error: any) {
console.error('Error querying collection:', error.message);
return NextResponse.json({ error: error.message }, { status: 500 });
}
}
31 changes: 31 additions & 0 deletions src/app/api/playground/ragchat/collections/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// src/app/api/playground/ragchat/collections/route.ts
'use server';

import { NextRequest, NextResponse } from 'next/server';
import fetch from 'node-fetch';

export async function GET(req: NextRequest) {
console.log('Received request to fetch collections');

try {
console.log('Making fetch call to backend service...');

const response = await fetch('http://127.0.0.1:8000/collections', {
method: 'GET',
headers: {
Accept: 'application/json' // Ensure Accept header is set properly
}
});

const rawText = await response.text();
console.log('Raw response text from backend:', rawText);

const data = JSON.parse(rawText);
console.log('Parsed collections data:', data);

return NextResponse.json(data, { status: 200 });
} catch (error: any) {
console.error('Error fetching collections:', error.message);
return NextResponse.json({ error: error.message }, { status: 500 });
}
}
Loading

0 comments on commit a2b56a0

Please sign in to comment.