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

clean up csv prompt + frontend #3393

Merged
merged 5 commits into from
Dec 11, 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
43 changes: 10 additions & 33 deletions backend/danswer/llm/utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import copy
import io
import json
from collections.abc import Callable
from collections.abc import Iterator
from typing import Any
from typing import cast

import litellm # type: ignore
import pandas as pd
import tiktoken
from langchain.prompts.base import StringPromptValue
from langchain.prompts.chat import ChatPromptValue
Expand Down Expand Up @@ -100,53 +98,32 @@ def litellm_exception_to_error_msg(
return error_msg


# Processes CSV files to show the first 5 rows and max_columns (default 40) columns
def _process_csv_file(file: InMemoryChatFile, max_columns: int = 40) -> str:
df = pd.read_csv(io.StringIO(file.content.decode("utf-8")))

csv_preview = df.head().to_string(max_cols=max_columns)

file_name_section = (
f"CSV FILE NAME: {file.filename}\n"
if file.filename
else "CSV FILE (NO NAME PROVIDED):\n"
)
return f"{file_name_section}{CODE_BLOCK_PAT.format(csv_preview)}\n\n\n"


def _build_content(
message: str,
files: list[InMemoryChatFile] | None = None,
) -> str:
"""Applies all non-image files."""
text_files = (
[file for file in files if file.file_type == ChatFileType.PLAIN_TEXT]
if files
else None
)
if not files:
return message

csv_files = (
[file for file in files if file.file_type == ChatFileType.CSV]
if files
else None
)
text_files = [
file
for file in files
if file.file_type in (ChatFileType.PLAIN_TEXT, ChatFileType.CSV)
]

if not text_files and not csv_files:
if not text_files:
return message

final_message_with_files = "FILES:\n\n"
for file in text_files or []:
for file in text_files:
file_content = file.content.decode("utf-8")
file_name_section = f"DOCUMENT: {file.filename}\n" if file.filename else ""
final_message_with_files += (
f"{file_name_section}{CODE_BLOCK_PAT.format(file_content.strip())}\n\n\n"
)
for file in csv_files or []:
final_message_with_files += _process_csv_file(file)

final_message_with_files += message

return final_message_with_files
return final_message_with_files + message


def build_content_with_imgs(
Expand Down
21 changes: 15 additions & 6 deletions web/src/app/chat/ChatPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1079,10 +1079,17 @@ export function ChatPage({
updateCanContinue(false, frozenSessionId);

if (currentChatState() != "input") {
setPopup({
message: "Please wait for the response to complete",
type: "error",
});
if (currentChatState() == "uploading") {
setPopup({
message: "Please wait for the content to upload",
type: "error",
});
} else {
setPopup({
message: "Please wait for the response to complete",
type: "error",
});
}

return;
}
Expand Down Expand Up @@ -1557,7 +1564,7 @@ export function ChatPage({
}
};

const handleImageUpload = (acceptedFiles: File[]) => {
const handleImageUpload = async (acceptedFiles: File[]) => {
const [_, llmModel] = getFinalLLM(
llmProviders,
liveAssistant,
Expand Down Expand Up @@ -1597,8 +1604,9 @@ export function ChatPage({
(file) => !tempFileDescriptors.some((newFile) => newFile.id === file.id)
);
};
updateChatState("uploading", currentSessionId());

uploadFilesForChat(acceptedFiles).then(([files, error]) => {
await uploadFilesForChat(acceptedFiles).then(([files, error]) => {
if (error) {
setCurrentMessageFiles((prev) => removeTempFiles(prev));
setPopup({
Expand All @@ -1609,6 +1617,7 @@ export function ChatPage({
setCurrentMessageFiles((prev) => [...removeTempFiles(prev), ...files]);
}
});
updateChatState("input", currentSessionId());
};
const [showHistorySidebar, setShowHistorySidebar] = useState(false); // State to track if sidebar is open

Expand Down
7 changes: 6 additions & 1 deletion web/src/app/chat/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
export type FeedbackType = "like" | "dislike";
export type ChatState = "input" | "loading" | "streaming" | "toolBuilding";
export type ChatState =
| "input"
| "loading"
| "streaming"
| "toolBuilding"
| "uploading";
export interface RegenerationState {
regenerating: boolean;
finalMessageIndex: number;
Expand Down
6 changes: 2 additions & 4 deletions web/src/components/tools/CSVContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ const CsvContent: React.FC<ContentComponentProps> = ({
}`}
>
<div
className={`overflow-y-hidden flex relative ${
expanded ? "max-h-2/3" : "max-h-[300px]"
}`}
className={`flex relative ${expanded ? "max-h-2/3" : "max-h-[300px]"}`}
>
<Table>
<TableHeader className="sticky z-[1000] top-0">
Expand All @@ -108,7 +106,7 @@ const CsvContent: React.FC<ContentComponentProps> = ({
</TableRow>
</TableHeader>

<TableBody className="h-[300px] overflow-y-scroll">
<TableBody className="h-[300px] overflow-y-hidden ">
{data.length > 0 ? (
data.map((row, rowIndex) => (
<TableRow key={rowIndex}>
Expand Down
6 changes: 3 additions & 3 deletions web/src/components/tools/ExpandableContentWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ const ExpandableContentWrapper: React.FC<ExpandableContentWrapperProps> = ({
</div>
</CardHeader>
<Card
className={`!rounded-none w-full ${
expanded ? "max-h-[600px]" : "max-h-[300px] h"
} p-0 relative overflow-x-scroll overflow-y-scroll mx-auto`}
className={`!rounded-none p-0 relative mx-auto w-full ${
expanded ? "max-h-[600px]" : "max-h-[300px] h-full"
} `}
>
<CardContent className="p-0">
<ContentComponent
Expand Down
Loading