Skip to content
Snippets Groups Projects
Commit 48188ca3 authored by Thuc Pham's avatar Thuc Pham
Browse files

feat: construct resource url from backend

parent 1fde1dc5
No related branches found
No related tags found
No related merge requests found
...@@ -17,6 +17,17 @@ export function appendImageData(data: StreamData, imageUrl?: string) { ...@@ -17,6 +17,17 @@ export function appendImageData(data: StreamData, imageUrl?: string) {
}); });
} }
function getNodeUrl(metadata: Metadata) {
const url = metadata["URL"];
if (url) return url;
const fileName = metadata["file_name"];
if (fileName) {
const fileServerUrlPrefix = process.env.FILESERVER_URL_PREFIX || "";
return `${fileServerUrlPrefix}/data/${fileName}`;
}
return undefined;
}
export function appendSourceData( export function appendSourceData(
data: StreamData, data: StreamData,
sourceNodes?: NodeWithScore<Metadata>[], sourceNodes?: NodeWithScore<Metadata>[],
...@@ -29,6 +40,7 @@ export function appendSourceData( ...@@ -29,6 +40,7 @@ export function appendSourceData(
...node.node.toMutableJSON(), ...node.node.toMutableJSON(),
id: node.node.id_, id: node.node.id_,
score: node.score ?? null, score: node.score ?? null,
url: getNodeUrl(node.node.metadata),
})), })),
}, },
}); });
......
import os
from pydantic import BaseModel from pydantic import BaseModel
from typing import List, Any, Optional, Dict, Tuple from typing import List, Any, Optional, Dict, Tuple
from fastapi import APIRouter, Depends, HTTPException, Request, status from fastapi import APIRouter, Depends, HTTPException, Request, status
...@@ -38,14 +39,25 @@ class _SourceNodes(BaseModel): ...@@ -38,14 +39,25 @@ class _SourceNodes(BaseModel):
metadata: Dict[str, Any] metadata: Dict[str, Any]
score: Optional[float] score: Optional[float]
text: str text: str
url: Optional[str]
@classmethod @classmethod
def from_source_node(cls, source_node: NodeWithScore): def from_source_node(cls, source_node: NodeWithScore):
metadata = source_node.node.metadata
url = metadata.get("URL")
if not url:
file_name = metadata.get("file_name")
if file_name:
file_server_url_prefix = os.getenv("FILESERVER_URL_PREFIX", "")
url = f"{file_server_url_prefix}/data/{file_name}"
return cls( return cls(
id=source_node.node.node_id, id=source_node.node.node_id,
metadata=source_node.node.metadata, metadata=metadata,
score=source_node.score, score=source_node.score,
text=source_node.node.text, # type: ignore text=source_node.node.text, # type: ignore
url=url
) )
@classmethod @classmethod
......
...@@ -17,6 +17,17 @@ export function appendImageData(data: StreamData, imageUrl?: string) { ...@@ -17,6 +17,17 @@ export function appendImageData(data: StreamData, imageUrl?: string) {
}); });
} }
function getNodeUrl(metadata: Metadata) {
const url = metadata["URL"];
if (url) return url;
const fileName = metadata["file_name"];
if (fileName) {
const fileServerUrlPrefix = process.env.FILESERVER_URL_PREFIX || "";
return `${fileServerUrlPrefix}/data/${fileName}`;
}
return undefined;
}
export function appendSourceData( export function appendSourceData(
data: StreamData, data: StreamData,
sourceNodes?: NodeWithScore<Metadata>[], sourceNodes?: NodeWithScore<Metadata>[],
...@@ -29,6 +40,7 @@ export function appendSourceData( ...@@ -29,6 +40,7 @@ export function appendSourceData(
...node.node.toMutableJSON(), ...node.node.toMutableJSON(),
id: node.node.id_, id: node.node.id_,
score: node.score ?? null, score: node.score ?? null,
url: getNodeUrl(node.node.metadata),
})), })),
}, },
}); });
......
...@@ -2,7 +2,6 @@ import { Check, Copy } from "lucide-react"; ...@@ -2,7 +2,6 @@ import { Check, Copy } from "lucide-react";
import { useMemo } from "react"; import { useMemo } from "react";
import { Button } from "../button"; import { Button } from "../button";
import { HoverCard, HoverCardContent, HoverCardTrigger } from "../hover-card"; import { HoverCard, HoverCardContent, HoverCardTrigger } from "../hover-card";
import { getStaticFileDataUrl } from "../lib/url";
import { SourceData, SourceNode } from "./index"; import { SourceData, SourceNode } from "./index";
import { useCopyToClipboard } from "./use-copy-to-clipboard"; import { useCopyToClipboard } from "./use-copy-to-clipboard";
import PdfDialog from "./widgets/PdfDialog"; import PdfDialog from "./widgets/PdfDialog";
...@@ -33,12 +32,11 @@ type NodeInfo = { ...@@ -33,12 +32,11 @@ type NodeInfo = {
function getNodeInfo(node: SourceNode): NodeInfo { function getNodeInfo(node: SourceNode): NodeInfo {
if (typeof node.metadata["URL"] === "string") { if (typeof node.metadata["URL"] === "string") {
const url = node.metadata["URL"];
return { return {
id: node.id, id: node.id,
type: NODE_TYPE.URL, type: NODE_TYPE.URL,
path: url, path: node.url,
url, url: node.url,
}; };
} }
if (typeof node.metadata["file_path"] === "string") { if (typeof node.metadata["file_path"] === "string") {
...@@ -47,8 +45,8 @@ function getNodeInfo(node: SourceNode): NodeInfo { ...@@ -47,8 +45,8 @@ function getNodeInfo(node: SourceNode): NodeInfo {
return { return {
id: node.id, id: node.id,
type: NODE_TYPE.FILE, type: NODE_TYPE.FILE,
path: node.metadata["file_path"], path: filePath,
url: getStaticFileDataUrl(filePath), url: node.url,
}; };
} }
...@@ -125,7 +123,7 @@ function NodeInfo({ nodeInfo }: { nodeInfo: NodeInfo }) { ...@@ -125,7 +123,7 @@ function NodeInfo({ nodeInfo }: { nodeInfo: NodeInfo }) {
<span>{nodeInfo.path}</span> <span>{nodeInfo.path}</span>
</a> </a>
<Button <Button
onClick={() => copyToClipboard(nodeInfo.path!)} onClick={() => copyToClipboard(nodeInfo.url!)}
size="icon" size="icon"
variant="ghost" variant="ghost"
className="h-12 w-12 shrink-0" className="h-12 w-12 shrink-0"
......
...@@ -21,6 +21,7 @@ export type SourceNode = { ...@@ -21,6 +21,7 @@ export type SourceNode = {
metadata: Record<string, unknown>; metadata: Record<string, unknown>;
score?: number; score?: number;
text: string; text: string;
url?: string;
}; };
export type SourceData = { export type SourceData = {
......
const staticFileAPI = "/api/files";
export const getStaticFileDataUrl = (filePath: string) => {
const isUsingBackend = !!process.env.NEXT_PUBLIC_CHAT_API;
const fileUrl = `${staticFileAPI}/${filePath}`;
if (isUsingBackend) {
const backendOrigin = new URL(process.env.NEXT_PUBLIC_CHAT_API!).origin;
return `${backendOrigin}${fileUrl}`;
}
return fileUrl;
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment