Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demo: RAG/docling/llama-index service with an Instructlab frontend #287

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading