From 3985c9e90a189edcc3b3437d4b4981b17c1442d4 Mon Sep 17 00:00:00 2001 From: Willy Douhard Date: Fri, 1 Sep 2023 17:18:52 +0200 Subject: [PATCH] Wd/0.6.401 (#349) * fix last message loading * make follow_symlink default to False and configurable * relax fastapi version requirement * fix local db client * try to stringify content instead of throwing * remove prints --- src/chainlit/client/local.py | 3 ++ src/chainlit/config.py | 5 +++ .../organisms/chat/message/messages.tsx | 34 +++++++++++++++---- src/chainlit/frontend/src/types/chat.ts | 1 + src/chainlit/message.py | 6 +++- src/chainlit/server.py | 3 +- src/pyproject.toml | 4 +-- 7 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/chainlit/client/local.py b/src/chainlit/client/local.py index fa3e7bfb78..4fb0817798 100644 --- a/src/chainlit/client/local.py +++ b/src/chainlit/client/local.py @@ -39,6 +39,9 @@ def before_write(self, variables: Dict): # Sqlite doesn't support list of primitives, so we need to serialize it. variables["forIds"] = json.dumps(variables["forIds"]) + if "streaming" in variables: + del variables["streaming"] + def after_read(self, variables: Dict): if "prompt" in variables: # Sqlite doesn't support json fields, so we need to parse it. diff --git a/src/chainlit/config.py b/src/chainlit/config.py index aeda9af675..104a139d30 100644 --- a/src/chainlit/config.py +++ b/src/chainlit/config.py @@ -51,6 +51,9 @@ # Enable third parties caching (e.g LangChain cache) cache = false +# Follow symlink for asset mount (see https://github.com/Chainlit/chainlit/issues/317) +# follow_symlink = false + # Chainlit server address # chainlit_server = "" @@ -203,6 +206,8 @@ class ProjectSettings(DataClassJsonMixin): session_timeout: int = 3600 # Enable third parties caching (e.g LangChain cache) cache: bool = False + # Follow symlink for asset mount (see https://github.com/Chainlit/chainlit/issues/317) + follow_symlink: bool = False # Chainlit server address chainlit_server: Optional[str] = None diff --git a/src/chainlit/frontend/src/components/organisms/chat/message/messages.tsx b/src/chainlit/frontend/src/components/organisms/chat/message/messages.tsx index 5786d62068..c382ec96f9 100644 --- a/src/chainlit/frontend/src/components/organisms/chat/message/messages.tsx +++ b/src/chainlit/frontend/src/components/organisms/chat/message/messages.tsx @@ -16,6 +16,22 @@ interface Props { isRunning?: boolean; } +function isLastMessage(messages: INestedMessage[], index: number) { + if (messages.length - 1 === index) { + return true; + } + + for (let i = index + 1; i < messages.length; i++) { + if (messages[i].streaming) { + continue; + } else { + return false; + } + } + + return true; +} + export default function Messages({ messages, elements, @@ -24,21 +40,25 @@ export default function Messages({ isRunning }: Props) { const loading = useRecoilValue(loadingState); + const isRoot = indent === 0; let previousAuthor = ''; + const filtered = messages.filter((m, i) => { const hasContent = !!m.content; const hasChildren = !!m.subMessages?.length; const isLast = i === messages.length - 1; - const _isRunning = + const messageRunning = isRunning === undefined ? loading && isLast : isRunning && isLast; - return hasContent || hasChildren || (!hasContent && _isRunning); + return hasContent || hasChildren || (!hasContent && messageRunning); }); return ( <> {filtered.map((m, i) => { - const isLast = i === filtered.length - 1; - const _isRunning = - isRunning === undefined ? loading && isLast : isRunning && isLast; + const isLast = isLastMessage(filtered, i); + let messageRunning = isRunning === undefined ? loading : isRunning; + if (isRoot) { + messageRunning = messageRunning && isLast; + } const showAvatar = m.author !== previousAuthor; const showBorder = false; previousAuthor = m.author; @@ -49,9 +69,9 @@ export default function Messages({ actions={actions} showAvatar={showAvatar} showBorder={showBorder} - key={i} + key={m.id} indent={indent} - isRunning={_isRunning} + isRunning={messageRunning} isLast={isLast} /> ); diff --git a/src/chainlit/frontend/src/types/chat.ts b/src/chainlit/frontend/src/types/chat.ts index 09c55bb425..2cf9fb2814 100644 --- a/src/chainlit/frontend/src/types/chat.ts +++ b/src/chainlit/frontend/src/types/chat.ts @@ -48,6 +48,7 @@ export interface IMessage { parentId?: string; isError?: boolean; prompt?: IPrompt; + streaming?: boolean; } export interface IMessageUpdate extends IMessage { diff --git a/src/chainlit/message.py b/src/chainlit/message.py index d12b6abfdb..08c326bd5c 100644 --- a/src/chainlit/message.py +++ b/src/chainlit/message.py @@ -155,7 +155,10 @@ def __init__( elif isinstance(content, str): self.content = content else: - raise TypeError(f"Unsupported type {type(content)} for message content") + logger.warn( + f"Unsupported type {type(content)} for message content. Attempting to stringify it" + ) + self.content = str(content) self.author = author self.prompt = prompt @@ -192,6 +195,7 @@ def to_dict(self): "language": self.language, "parentId": self.parent_id, "indent": self.indent, + "streaming": self.streaming, } if self.prompt: diff --git a/src/chainlit/server.py b/src/chainlit/server.py index 4e4dc72992..f1c243a55f 100644 --- a/src/chainlit/server.py +++ b/src/chainlit/server.py @@ -122,7 +122,8 @@ async def watch_files_for_changes(): app.mount( "/assets", StaticFiles( - packages=[("chainlit", os.path.join(build_dir, "assets"))], follow_symlink=True + packages=[("chainlit", os.path.join(build_dir, "assets"))], + follow_symlink=config.project.follow_symlink, ), name="assets", ) diff --git a/src/pyproject.toml b/src/pyproject.toml index c54ec25726..1094174d98 100644 --- a/src/pyproject.toml +++ b/src/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "chainlit" -version = "0.6.4" +version = "0.6.401" keywords = ['LLM', 'Agents', 'gen ai', 'chat ui', 'chatbot ui', 'langchain'] description = "A faster way to build chatbot UIs." authors = ["Chainlit"] @@ -22,7 +22,7 @@ chainlit = 'chainlit.cli:cli' python = "^3.8.1" dataclasses_json = "^0.5.7" uvicorn = "^0.23.2" -fastapi = "^0.103.0" +fastapi = "^0.99.0" fastapi-socketio = "^0.0.10" aiohttp = "^3.8.4" aiofiles = "^23.1.0"