Skip to content

feat(UI): WIP Enhancing Rag chat playground #3148

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
14 changes: 14 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,20 @@ repos:
pass_filenames: false
require_serial: true
files: ^.github/workflows/.*$
- id: ui-prettier
name: Format UI code with Prettier
entry: bash -c 'cd llama_stack/ui && npm run format'
language: system
files: ^llama_stack/ui/.*\.(ts|tsx)$
pass_filenames: false
require_serial: true
- id: ui-eslint
name: Lint UI code with ESLint
entry: bash -c 'cd llama_stack/ui && npm run lint -- --fix --quiet'
language: system
files: ^llama_stack/ui/.*\.(ts|tsx)$
pass_filenames: false
require_serial: true

ci:
autofix_commit_msg: 🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
Expand Down
9 changes: 9 additions & 0 deletions llama_stack/ui/.prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# Ignore artifacts:
build
coverage
.next
node_modules
dist
*.lock
*.log

# Generated files
*.min.js
*.min.css
11 changes: 10 additions & 1 deletion llama_stack/ui/.prettierrc
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
{}
{
"semi": true,
"trailingComma": "es5",
"singleQuote": false,
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"bracketSpacing": true,
"arrowParens": "avoid"
}
56 changes: 56 additions & 0 deletions llama_stack/ui/app/api/fetch-url/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { NextRequest, NextResponse } from "next/server";

export async function POST(request: NextRequest) {
try {
const { url } = await request.json();

if (!url || typeof url !== "string") {
return NextResponse.json({ error: "URL is required" }, { status: 400 });
}

// Fetch the URL content
const response = await fetch(url, {
headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
},
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const contentType = response.headers.get("content-type") || "";
let content: string;

if (contentType.includes("application/json")) {
const json = await response.json();
content = JSON.stringify(json, null, 2);
} else if (contentType.includes("text/html")) {
const html = await response.text();
// Basic HTML to text conversion - remove tags and decode entities
content = html
.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, "")
.replace(/<style\b[^<]*(?:(?!<\/style>)<[^<]*)*<\/style>/gi, "")
.replace(/<[^>]*>/g, "")
.replace(/&nbsp;/g, " ")
.replace(/&amp;/g, "&")
.replace(/&lt;/g, "<")
.replace(/&gt;/g, ">")
.replace(/&quot;/g, '"')
.replace(/&#39;/g, "'")
.replace(/\s+/g, " ")
.trim();
} else {
content = await response.text();
}

return NextResponse.json({ content });
} catch (error) {
console.error("Error fetching URL:", error);
return NextResponse.json(
{ error: "Failed to fetch URL content" },
{ status: 500 }
);
}
}
51 changes: 51 additions & 0 deletions llama_stack/ui/app/api/upload-document/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { NextRequest, NextResponse } from "next/server";

export async function POST(request: NextRequest) {
try {
const formData = await request.formData();
const file = formData.get("file") as File;
const vectorDbId = formData.get("vectorDbId") as string;

if (!file || !vectorDbId) {
return NextResponse.json(
{ error: "File and vectorDbId are required" },
{ status: 400 }
);
}

// Read file content based on type
let content: string;
const mimeType = file.type || "application/octet-stream";

if (mimeType === "text/plain" || mimeType === "text/markdown") {
content = await file.text();
} else if (mimeType === "application/pdf") {
// For PDFs, convert to base64 on the server side
const arrayBuffer = await file.arrayBuffer();
const bytes = new Uint8Array(arrayBuffer);
let binary = "";
for (let i = 0; i < bytes.byteLength; i++) {
binary += String.fromCharCode(bytes[i]);
}
const base64 = btoa(binary);
content = `data:${mimeType};base64,${base64}`;
} else {
// Try to read as text
content = await file.text();
}

// Return the processed content for the client to send to RagTool
return NextResponse.json({
content,
mimeType,
fileName: file.name,
fileSize: file.size,
});
} catch (error) {
console.error("Error processing file upload:", error);
return NextResponse.json(
{ error: "Failed to process file upload" },
{ status: 500 }
);
}
}
4 changes: 2 additions & 2 deletions llama_stack/ui/app/api/v1/[...path]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async function proxyRequest(request: NextRequest, method: string) {
const responseText = await response.text();

console.log(
`Response from FastAPI: ${response.status} ${response.statusText}`,
`Response from FastAPI: ${response.status} ${response.statusText}`
);

// Create response with same status and headers
Expand All @@ -74,7 +74,7 @@ async function proxyRequest(request: NextRequest, method: string) {
backend_url: BACKEND_URL,
timestamp: new Date().toISOString(),
},
{ status: 500 },
{ status: 500 }
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions llama_stack/ui/app/auth/signin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ export default function SignInPage() {
onClick={() => {
console.log("Signing in with GitHub...");
signIn("github", { callbackUrl: "/auth/signin" }).catch(
(error) => {
error => {
console.error("Sign in error:", error);
},
}
);
}}
className="w-full"
Expand Down
Loading
Loading