From 48188ca3f999e6f9beece00d9459a7896a471146 Mon Sep 17 00:00:00 2001 From: Thuc Pham <51660321+thucpn@users.noreply.github.com> Date: Fri, 24 May 2024 14:40:44 +0700 Subject: [PATCH] feat: construct resource url from backend --- .../express/src/controllers/stream-helper.ts | 12 ++++++++++++ .../streaming/fastapi/app/api/routers/chat.py | 14 +++++++++++++- .../streaming/nextjs/app/api/chat/stream-helper.ts | 12 ++++++++++++ .../nextjs/app/components/ui/chat/chat-sources.tsx | 12 +++++------- .../nextjs/app/components/ui/chat/index.ts | 1 + .../streaming/nextjs/app/components/ui/lib/url.ts | 11 ----------- 6 files changed, 43 insertions(+), 19 deletions(-) delete mode 100644 templates/types/streaming/nextjs/app/components/ui/lib/url.ts diff --git a/templates/types/streaming/express/src/controllers/stream-helper.ts b/templates/types/streaming/express/src/controllers/stream-helper.ts index 9f1a8864..574738e8 100644 --- a/templates/types/streaming/express/src/controllers/stream-helper.ts +++ b/templates/types/streaming/express/src/controllers/stream-helper.ts @@ -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( data: StreamData, sourceNodes?: NodeWithScore<Metadata>[], @@ -29,6 +40,7 @@ export function appendSourceData( ...node.node.toMutableJSON(), id: node.node.id_, score: node.score ?? null, + url: getNodeUrl(node.node.metadata), })), }, }); diff --git a/templates/types/streaming/fastapi/app/api/routers/chat.py b/templates/types/streaming/fastapi/app/api/routers/chat.py index c92ca3d4..1c022b4b 100644 --- a/templates/types/streaming/fastapi/app/api/routers/chat.py +++ b/templates/types/streaming/fastapi/app/api/routers/chat.py @@ -1,3 +1,4 @@ +import os from pydantic import BaseModel from typing import List, Any, Optional, Dict, Tuple from fastapi import APIRouter, Depends, HTTPException, Request, status @@ -38,14 +39,25 @@ class _SourceNodes(BaseModel): metadata: Dict[str, Any] score: Optional[float] text: str + url: Optional[str] @classmethod 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( id=source_node.node.node_id, - metadata=source_node.node.metadata, + metadata=metadata, score=source_node.score, text=source_node.node.text, # type: ignore + url=url ) @classmethod diff --git a/templates/types/streaming/nextjs/app/api/chat/stream-helper.ts b/templates/types/streaming/nextjs/app/api/chat/stream-helper.ts index 9f1a8864..574738e8 100644 --- a/templates/types/streaming/nextjs/app/api/chat/stream-helper.ts +++ b/templates/types/streaming/nextjs/app/api/chat/stream-helper.ts @@ -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( data: StreamData, sourceNodes?: NodeWithScore<Metadata>[], @@ -29,6 +40,7 @@ export function appendSourceData( ...node.node.toMutableJSON(), id: node.node.id_, score: node.score ?? null, + url: getNodeUrl(node.node.metadata), })), }, }); diff --git a/templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx b/templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx index 893541b0..fa32930a 100644 --- a/templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx +++ b/templates/types/streaming/nextjs/app/components/ui/chat/chat-sources.tsx @@ -2,7 +2,6 @@ import { Check, Copy } from "lucide-react"; import { useMemo } from "react"; import { Button } from "../button"; import { HoverCard, HoverCardContent, HoverCardTrigger } from "../hover-card"; -import { getStaticFileDataUrl } from "../lib/url"; import { SourceData, SourceNode } from "./index"; import { useCopyToClipboard } from "./use-copy-to-clipboard"; import PdfDialog from "./widgets/PdfDialog"; @@ -33,12 +32,11 @@ type NodeInfo = { function getNodeInfo(node: SourceNode): NodeInfo { if (typeof node.metadata["URL"] === "string") { - const url = node.metadata["URL"]; return { id: node.id, type: NODE_TYPE.URL, - path: url, - url, + path: node.url, + url: node.url, }; } if (typeof node.metadata["file_path"] === "string") { @@ -47,8 +45,8 @@ function getNodeInfo(node: SourceNode): NodeInfo { return { id: node.id, type: NODE_TYPE.FILE, - path: node.metadata["file_path"], - url: getStaticFileDataUrl(filePath), + path: filePath, + url: node.url, }; } @@ -125,7 +123,7 @@ function NodeInfo({ nodeInfo }: { nodeInfo: NodeInfo }) { <span>{nodeInfo.path}</span> </a> <Button - onClick={() => copyToClipboard(nodeInfo.path!)} + onClick={() => copyToClipboard(nodeInfo.url!)} size="icon" variant="ghost" className="h-12 w-12 shrink-0" diff --git a/templates/types/streaming/nextjs/app/components/ui/chat/index.ts b/templates/types/streaming/nextjs/app/components/ui/chat/index.ts index 106f6294..cb7e9272 100644 --- a/templates/types/streaming/nextjs/app/components/ui/chat/index.ts +++ b/templates/types/streaming/nextjs/app/components/ui/chat/index.ts @@ -21,6 +21,7 @@ export type SourceNode = { metadata: Record<string, unknown>; score?: number; text: string; + url?: string; }; export type SourceData = { diff --git a/templates/types/streaming/nextjs/app/components/ui/lib/url.ts b/templates/types/streaming/nextjs/app/components/ui/lib/url.ts deleted file mode 100644 index 5e2c90e5..00000000 --- a/templates/types/streaming/nextjs/app/components/ui/lib/url.ts +++ /dev/null @@ -1,11 +0,0 @@ -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; -}; -- GitLab