Skip to content

feat: construct resource url from backend #98

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

Merged
merged 5 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions templates/types/streaming/express/src/controllers/stream-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || "";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if process.env.FILESERVER_URL_PREFIX is not set, how about logging a warning and return undefined? (also in python)

return `${fileServerUrlPrefix}/data/${fileName}`;
}
return undefined;
}

export function appendSourceData(
data: StreamData,
sourceNodes?: NodeWithScore<Metadata>[],
Expand All @@ -29,6 +40,7 @@ export function appendSourceData(
...node.node.toMutableJSON(),
id: node.node.id_,
score: node.score ?? null,
url: getNodeUrl(node.node.metadata),
})),
},
});
Expand Down
14 changes: 13 additions & 1 deletion templates/types/streaming/fastapi/app/api/routers/chat.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions templates/types/streaming/nextjs/app/api/chat/stream-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>[],
Expand All @@ -29,6 +40,7 @@ export function appendSourceData(
...node.node.toMutableJSON(),
id: node.node.id_,
score: node.score ?? null,
url: getNodeUrl(node.node.metadata),
})),
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -33,12 +32,11 @@ type NodeInfo = {

function getNodeInfo(node: SourceNode): NodeInfo {
if (typeof node.metadata["URL"] === "string") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code can be simplified as we render NODE_TYPE.URL and NODE_TYPE.FILE the same way, we can merge them - I guess we also don't need to check the metadata here and probably even can remove path

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") {
Expand All @@ -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,
};
}

Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type SourceNode = {
metadata: Record<string, unknown>;
score?: number;
text: string;
url?: string;
};

export type SourceData = {
Expand Down
11 changes: 0 additions & 11 deletions templates/types/streaming/nextjs/app/components/ui/lib/url.ts

This file was deleted.

Loading