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

Don't parse documents as markdown in retrieval bot #312

Merged
merged 1 commit into from
Apr 18, 2024
Merged
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
18 changes: 15 additions & 3 deletions frontend/src/components/Document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function isValidHttpUrl(str: string) {

function DocumentViewer(props: {
document: MessageDocument;
markdown?: boolean;
className?: string;
}) {
const [open, setOpen] = useState(false);
Expand Down Expand Up @@ -79,7 +80,10 @@ function DocumentViewer(props: {
</button>

<span className="flex flex-grow basis-0 flex-col gap-4">
<StringViewer value={props.document.page_content} />
<StringViewer
value={props.document.page_content}
markdown={props.markdown}
/>

<span className="flex flex-col flex-wrap items-start gap-2">
{metadata.map(({ key, value }, idx) => {
Expand All @@ -105,12 +109,20 @@ function DocumentViewer(props: {
);
}

export function DocumentList(props: { documents: MessageDocument[] }) {
export function DocumentList(props: {
documents: MessageDocument[];
markdown?: boolean;
}) {
return (
<div className="flex flex-col items-stretch gap-4 rounded-lg ring-1 ring-gray-300 overflow-hidden my-2">
<div className="grid divide-y empty:hidden">
{props.documents.map((document, idx) => (
<DocumentViewer document={document} key={idx} className="py-3" />
<DocumentViewer
document={document}
key={idx}
className="py-3"
markdown={props.markdown}
/>
))}
</div>
</div>
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/components/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ function isDocumentContent(

export function MessageContent(props: { content: MessageType["content"] }) {
if (typeof props.content === "string") {
return <StringViewer value={props.content} />;
if (!props.content.trim()) {
return null;
}
return <StringViewer value={props.content} markdown />;
} else if (isDocumentContent(props.content)) {
return <DocumentList documents={props.content} />;
} else if (
Expand All @@ -113,6 +116,7 @@ export function MessageContent(props: { content: MessageType["content"] }) {
) {
return (
<DocumentList
markdown
documents={props.content.map((it) => ({
page_content: it.content,
metadata: omit(it, "content"),
Expand Down
12 changes: 10 additions & 2 deletions frontend/src/components/String.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@ const OPTIONS: MarkedOptions = {
breaks: true,
};

export function StringViewer(props: { value: string; className?: string }) {
return (
export function StringViewer(props: {
value: string;
className?: string;
markdown?: boolean;
}) {
return props.markdown ? (
<div
className={cn("text-gray-900 prose", props.className)}
dangerouslySetInnerHTML={{
__html: DOMPurify.sanitize(marked(props.value, OPTIONS)).trim(),
}}
/>
) : (
<div className={cn("text-gray-900 prose", props.className)}>
{props.value}
</div>
);
}
Loading