From be3a4ed2568b227a01f02c254ea275fce49ed75f Mon Sep 17 00:00:00 2001 From: n4ze3m Date: Sat, 3 Feb 2024 17:51:11 +0530 Subject: [PATCH] Add @langchain/core dependency and update imports*** ***Update SidepanelRouting to use dark mode*** ***Add image support to PlaygroundMessage component --- package.json | 1 + src/chain/chat-with-website.ts | 2 +- src/components/Common/Playground/Message.tsx | 25 ++++-- src/components/Sidepanel/Chat/body.tsx | 1 + src/components/Sidepanel/Chat/form.tsx | 91 ++++++++++++++++--- src/hooks/useMessage.tsx | 93 +++++++++++++++----- src/libs/to-base64.ts | 7 ++ src/loader/html.ts | 2 +- src/routes/index.tsx | 21 +++-- src/routes/sidepanel-chat.tsx | 74 +++++++++++++++- src/services/ollama.ts | 13 ++- src/store/index.tsx | 4 +- yarn.lock | 17 ++++ 13 files changed, 294 insertions(+), 57 deletions(-) create mode 100644 src/libs/to-base64.ts diff --git a/package.json b/package.json index d216a8c..bc29c86 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "@headlessui/react": "^1.7.18", "@heroicons/react": "^2.1.1", "@langchain/community": "^0.0.21", + "@langchain/core": "^0.1.22", "@mantine/form": "^7.5.0", "@plasmohq/storage": "^1.9.0", "@tailwindcss/forms": "^0.5.7", diff --git a/src/chain/chat-with-website.ts b/src/chain/chat-with-website.ts index 0d0acf0..9a2793a 100644 --- a/src/chain/chat-with-website.ts +++ b/src/chain/chat-with-website.ts @@ -1,5 +1,5 @@ import { BaseLanguageModel } from "langchain/base_language"; -import { Document } from "langchain/document"; +import { Document } from "@langchain/core/documents"; import { ChatPromptTemplate, MessagesPlaceholder, diff --git a/src/components/Common/Playground/Message.tsx b/src/components/Common/Playground/Message.tsx index 324635c..c75869a 100644 --- a/src/components/Common/Playground/Message.tsx +++ b/src/components/Common/Playground/Message.tsx @@ -1,7 +1,4 @@ -import { - CheckIcon, - ClipboardIcon, -} from "@heroicons/react/24/outline" +import { CheckIcon, ClipboardIcon } from "@heroicons/react/24/outline" import Markdown from "../../Common/Markdown" import React from "react" @@ -12,6 +9,7 @@ type Props = { userAvatar?: JSX.Element isBot: boolean name: string + images?: string[] } export const PlaygroundMessage = (props: Props) => { @@ -48,13 +46,11 @@ export const PlaygroundMessage = (props: Props) => {
- { - props.isBot && ( + {props.isBot && ( {props.name} - ) - } + )}
@@ -81,6 +77,19 @@ export const PlaygroundMessage = (props: Props) => { )}
+ {props.images && ( +
+ {props.images.map((image, index) => ( +
+ Uploaded +
+ ))} +
+ )} ) } diff --git a/src/components/Sidepanel/Chat/body.tsx b/src/components/Sidepanel/Chat/body.tsx index dffb389..6624b43 100644 --- a/src/components/Sidepanel/Chat/body.tsx +++ b/src/components/Sidepanel/Chat/body.tsx @@ -20,6 +20,7 @@ export const SidePanelBody = () => { isBot={message.isBot} message={message.message} name={message.name} + images={message.images || []} /> ))}
diff --git a/src/components/Sidepanel/Chat/form.tsx b/src/components/Sidepanel/Chat/form.tsx index b5d9081..34982ec 100644 --- a/src/components/Sidepanel/Chat/form.tsx +++ b/src/components/Sidepanel/Chat/form.tsx @@ -3,9 +3,17 @@ import { useMutation } from "@tanstack/react-query" import React from "react" import useDynamicTextareaSize from "~hooks/useDynamicTextareaSize" import { useMessage } from "~hooks/useMessage" +import PhotoIcon from "@heroicons/react/24/outline/PhotoIcon" +import XMarkIcon from "@heroicons/react/24/outline/XMarkIcon" +import { toBase64 } from "~libs/to-base64" -export const SidepanelForm = () => { +type Props = { + dropedFile: File | undefined +} + +export const SidepanelForm = ({ dropedFile }: Props) => { const textareaRef = React.useRef(null) + const inputRef = React.useRef(null) const resetHeight = () => { const textarea = textareaRef.current @@ -15,16 +23,34 @@ export const SidepanelForm = () => { } const form = useForm({ initialValues: { - message: "" + message: "", + image: "" } }) - useDynamicTextareaSize( - textareaRef, - form.values.message, - ) + const onInputChange = async ( + e: React.ChangeEvent | File + ) => { + if (e instanceof File) { + const base64 = await toBase64(e) + form.setFieldValue("image", base64) + } else { + if (e.target.files) { + const base64 = await toBase64(e.target.files[0]) + form.setFieldValue("image", base64) + } + } + } - const { onSubmit, selectedModel } = useMessage() + React.useEffect(() => { + if (dropedFile) { + onInputChange(dropedFile) + } + }, [dropedFile]) + + useDynamicTextareaSize(textareaRef, form.values.message, 120) + + const { onSubmit, selectedModel, chatMode } = useMessage() const { mutateAsync: sendMessage, isPending: isSending } = useMutation({ mutationFn: onSubmit @@ -33,6 +59,24 @@ export const SidepanelForm = () => { return (
+ {chatMode === "normal" && form.values.image && ( +
+
+ Uploaded + +
+
+ )}
{ @@ -42,10 +86,34 @@ export const SidepanelForm = () => { } form.reset() resetHeight() - await sendMessage(value.message) + await sendMessage({ + image: value.image, + message: value.message.trim() + }) })} className="shrink-0 flex-grow flex items-center ">
+ + +