diff --git a/templates/simple/nextjs/README-template.md b/templates/simple/nextjs/README-template.md index c4033664f80d3cb9cb687fb5facbc82aedb302f6..1509ded7c3be489d369b94d6d6a286d496f488d8 100644 --- a/templates/simple/nextjs/README-template.md +++ b/templates/simple/nextjs/README-template.md @@ -1,17 +1,17 @@ -This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app). +This is a [LlamaIndex](https://www.llamaindex.ai/) project using [Next.js](https://nextjs.org/) bootstrapped with [`create-llama`](https://github.com/run-llama/LlamaIndexTS/tree/main/packages/create-llama). ## Getting Started -First, run the development server: +First, install the dependencies: -```bash +``` +npm install +``` + +Second, run the development server: + +``` npm run dev -# or -yarn dev -# or -pnpm dev -# or -bun dev ``` Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. @@ -22,15 +22,9 @@ This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-opti ## Learn More -To learn more about Next.js, take a look at the following resources: - -- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. -- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. - -You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome! - -## Deploy on Vercel +To learn more about LlamaIndex, take a look at the following resources: -The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. +- [LlamaIndex Documentation](https://docs.llamaindex.ai) - learn about LlamaIndex (Python features). +- [LlamaIndexTS Documentation](https://ts.llamaindex.ai) - learn about LlamaIndex (Typescript features). -Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details. +You can check out [the LlamaIndexTS GitHub repository](https://github.com/run-llama/LlamaIndexTS) - your feedback and contributions are welcome! diff --git a/templates/simple/nextjs/app/components/chat-avatar.tsx b/templates/simple/nextjs/app/components/chat-avatar.tsx new file mode 100644 index 0000000000000000000000000000000000000000..2f79edaea62798b11096d280f1491ac1b55b75eb --- /dev/null +++ b/templates/simple/nextjs/app/components/chat-avatar.tsx @@ -0,0 +1,34 @@ +"use client"; + +import { ChatMessage } from "llamaindex"; +import Image from "next/image"; + +export default function ChatAvatar(chatMessage: ChatMessage) { + if (chatMessage.role === "user") { + return ( + <div className="flex h-8 w-8 shrink-0 select-none items-center justify-center rounded-md border shadow bg-background"> + <svg + xmlns="http://www.w3.org/2000/svg" + viewBox="0 0 256 256" + fill="currentColor" + className="h-4 w-4" + > + <path d="M230.92 212c-15.23-26.33-38.7-45.21-66.09-54.16a72 72 0 1 0-73.66 0c-27.39 8.94-50.86 27.82-66.09 54.16a8 8 0 1 0 13.85 8c18.84-32.56 52.14-52 89.07-52s70.23 19.44 89.07 52a8 8 0 1 0 13.85-8ZM72 96a56 56 0 1 1 56 56 56.06 56.06 0 0 1-56-56Z"></path> + </svg> + </div> + ); + } + + return ( + <div className="flex h-8 w-8 shrink-0 select-none items-center justify-center rounded-md border bg-black text-white"> + <Image + className="rounded-md" + src="/llama.png" + alt="Llama Logo" + width={24} + height={24} + priority + /> + </div> + ); +} diff --git a/templates/simple/nextjs/app/components/chat-history.tsx b/templates/simple/nextjs/app/components/chat-history.tsx new file mode 100644 index 0000000000000000000000000000000000000000..2eafff4c17b35177b86dc4a1a5482caf803dfd07 --- /dev/null +++ b/templates/simple/nextjs/app/components/chat-history.tsx @@ -0,0 +1,34 @@ +"use client"; + +import ChatItem from "@/app/components/chat-item"; +import { useChat } from "@/app/components/chat-section"; +import { useEffect, useRef } from "react"; + +export default function ChatHistory() { + const scrollableChatContainerRef = useRef<HTMLDivElement>(null); + const { chatHistory } = useChat(); + + const scrollToBottom = () => { + if (scrollableChatContainerRef.current) { + scrollableChatContainerRef.current.scrollTop = + scrollableChatContainerRef.current.scrollHeight; + } + }; + + useEffect(() => { + scrollToBottom(); + }, [chatHistory.length]); + + return ( + <div className="w-full max-w-5xl p-4 bg-white rounded-xl shadow-xl"> + <div + className="flex flex-col gap-5 divide-y h-[50vh] overflow-auto" + ref={scrollableChatContainerRef} + > + {chatHistory.map((chatMessage, index) => ( + <ChatItem key={index} {...chatMessage} /> + ))} + </div> + </div> + ); +} diff --git a/templates/simple/nextjs/app/components/chat-item.tsx b/templates/simple/nextjs/app/components/chat-item.tsx new file mode 100644 index 0000000000000000000000000000000000000000..5d32c64e7d4f1d4d0f3c2ee09f48d7c36aab3954 --- /dev/null +++ b/templates/simple/nextjs/app/components/chat-item.tsx @@ -0,0 +1,13 @@ +"use client"; + +import ChatAvatar from "@/app/components/chat-avatar"; +import { ChatMessage } from "llamaindex"; + +export default function ChatItem(chatMessage: ChatMessage) { + return ( + <div className="flex items-start gap-4 pt-5"> + <ChatAvatar {...chatMessage} /> + <p className="break-words">{chatMessage.content}</p> + </div> + ); +} diff --git a/templates/simple/nextjs/app/components/chat-section.tsx b/templates/simple/nextjs/app/components/chat-section.tsx new file mode 100644 index 0000000000000000000000000000000000000000..416c56cd24932d3d7bede631eadfa8acfdf147e6 --- /dev/null +++ b/templates/simple/nextjs/app/components/chat-section.tsx @@ -0,0 +1,45 @@ +"use client"; + +import ChatHistory from "@/app/components/chat-history"; +import MessageForm from "@/app/components/message-form"; +import ChatStorageService from "@/app/services/chatStorage.service"; +import { ChatMessage } from "llamaindex"; +import { createContext, useContext, useEffect, useState } from "react"; + +const ChatSectionContext = createContext<{ + chatHistory: ChatMessage[]; + loadChat: () => void; +}>({ + chatHistory: [], + loadChat: () => {}, +}); + +const ChatSectionContextProvider = (props: { children: JSX.Element[] }) => { + const [chatHistory, setChatHistory] = useState<ChatMessage[]>([]); + + const loadChat = () => { + const data = ChatStorageService.getChatHistory(); + setChatHistory(data); + }; + + useEffect(() => { + loadChat(); + }, []); + + return ( + <ChatSectionContext.Provider value={{ chatHistory, loadChat }}> + {props.children} + </ChatSectionContext.Provider> + ); +}; + +export default function ChatSection() { + return ( + <ChatSectionContextProvider> + <ChatHistory /> + <MessageForm /> + </ChatSectionContextProvider> + ); +} + +export const useChat = () => useContext(ChatSectionContext); diff --git a/templates/simple/nextjs/app/components/header.tsx b/templates/simple/nextjs/app/components/header.tsx new file mode 100644 index 0000000000000000000000000000000000000000..2b0e488f769eff6700a282c2a6a77dd8d0a4dac8 --- /dev/null +++ b/templates/simple/nextjs/app/components/header.tsx @@ -0,0 +1,28 @@ +import Image from "next/image"; + +export default function Header() { + return ( + <div className="z-10 max-w-5xl w-full items-center justify-between font-mono text-sm lg:flex"> + <p className="fixed left-0 top-0 flex w-full justify-center border-b border-gray-300 bg-gradient-to-b from-zinc-200 pb-6 pt-8 backdrop-blur-2xl dark:border-neutral-800 dark:bg-zinc-800/30 dark:from-inherit lg:static lg:w-auto lg:rounded-xl lg:border lg:bg-gray-200 lg:p-4 lg:dark:bg-zinc-800/30"> + Get started by editing + <code className="font-mono font-bold">app/page.tsx</code> + </p> + <div className="fixed bottom-0 left-0 flex h-48 w-full items-end justify-center bg-gradient-to-t from-white via-white dark:from-black dark:via-black lg:static lg:h-auto lg:w-auto lg:bg-none"> + <a + href="https://www.llamaindex.ai/" + className="flex items-center justify-center font-nunito text-lg font-bold gap-2" + > + <span>Built by LlamaIndex</span> + <Image + className="rounded-xl" + src="/llama.png" + alt="Llama Logo" + width={40} + height={40} + priority + /> + </a> + </div> + </div> + ); +} diff --git a/templates/simple/nextjs/app/components/message-form.tsx b/templates/simple/nextjs/app/components/message-form.tsx index 59f42ce3aee9b2207724759b600b17eed1e1961d..fff39b9a490ca55bf381b92db9a826139ac8cd5d 100644 --- a/templates/simple/nextjs/app/components/message-form.tsx +++ b/templates/simple/nextjs/app/components/message-form.tsx @@ -1,40 +1,80 @@ "use client"; +import { useChat } from "@/app/components/chat-section"; +import ChatStorageService from "@/app/services/chatStorage.service"; import { ChatMessage } from "llamaindex"; +import { useState } from "react"; + +const LLM_API_ROUTE = "/api/llm"; export default function MessageForm() { - const testSendMessage = async (message: string) => { - const chatHistory: ChatMessage[] = []; - const apiRoute = "/api/llm"; - const response = await fetch(apiRoute, { + const { loadChat } = useChat(); + const [loading, setLoading] = useState(false); + + const getAssistantMessage = async (message: string) => { + const response = await fetch(LLM_API_ROUTE, { method: "POST", headers: { "Content-Type": "application/json", }, - body: JSON.stringify({ message, chatHistory }), + body: JSON.stringify({ + message, + chatHistory: ChatStorageService.getChatHistory(), + }), }); const data = await response.json(); - alert(JSON.stringify(data)); + const assistantMessage = data.result as ChatMessage; + return assistantMessage; + }; + + const sendMessage = async (message: string) => { + if (!message) return; + try { + setLoading(true); + const userMessage: ChatMessage = { content: message, role: "user" }; + ChatStorageService.addChatMessage(userMessage); + loadChat(); + const assistantMessage = await getAssistantMessage(message); + ChatStorageService.addChatMessage(assistantMessage); + setLoading(false); + loadChat(); + } catch (error: any) { + alert(JSON.stringify(error)); + } + }; + + const handleFormSubmit = async (e: React.FormEvent<HTMLFormElement>) => { + e.preventDefault(); + const message = e.currentTarget.message.value; + await sendMessage(message); + }; + + const handleKeyDown = async (e: React.KeyboardEvent<HTMLTextAreaElement>) => { + if (e.key === "Enter" && !e.shiftKey) { + e.preventDefault(); + const message = e.currentTarget.value; + if (message !== "") { + await sendMessage(message); + } + } }; return ( <form - onSubmit={(e) => { - e.preventDefault(); - const message = e.currentTarget.message.value; - testSendMessage(message); - }} - className="flex flex-col items-center justify-center w-full max-w-5xl p-4 space-y-4 bg-white rounded-xl shadow-xl" + onSubmit={handleFormSubmit} + className="flex items-start justify-between w-full max-w-5xl p-4 bg-white rounded-xl shadow-xl gap-4" > - <input + <textarea + rows={1} autoFocus - type="text" name="message" placeholder="Type a message" - className="w-full p-4 rounded-xl shadow-inner" + className="w-full p-4 rounded-xl shadow-inner flex-1" + onKeyDown={handleKeyDown} /> <button + disabled={loading} type="submit" - className="p-4 text-white rounded-xl shadow-xl bg-gradient-to-r from-cyan-500 to-sky-500 dark:from-cyan-600 dark:to-sky-600" + className="p-4 text-white rounded-xl shadow-xl bg-gradient-to-r from-cyan-500 to-sky-500 disabled:opacity-50 disabled:cursor-not-allowed" > Send message </button> diff --git a/templates/simple/nextjs/app/favicon.ico b/templates/simple/nextjs/app/favicon.ico index 718d6fea4835ec2d246af9800eddb7ffb276240c..a1eaef62f2dfa895f1bbffc6595bb53d9604963e 100644 Binary files a/templates/simple/nextjs/app/favicon.ico and b/templates/simple/nextjs/app/favicon.ico differ diff --git a/templates/simple/nextjs/app/globals.css b/templates/simple/nextjs/app/globals.css index fd81e885836d815b8019694a910a93d86a43cb66..d85e2eec9ab40d8bc2d8cbad401f414ae8cd0ab2 100644 --- a/templates/simple/nextjs/app/globals.css +++ b/templates/simple/nextjs/app/globals.css @@ -25,3 +25,15 @@ body { ) rgb(var(--background-start-rgb)); } + +.background-gradient { + background-color: #fff; + background-image: radial-gradient( + at 21% 11%, + rgba(186, 186, 233, 0.53) 0, + transparent 50% + ), + radial-gradient(at 85% 0, hsla(46, 57%, 78%, 0.52) 0, transparent 50%), + radial-gradient(at 91% 36%, rgba(194, 213, 255, 0.68) 0, transparent 50%), + radial-gradient(at 8% 40%, rgba(251, 218, 239, 0.46) 0, transparent 50%); +} diff --git a/templates/simple/nextjs/app/page.tsx b/templates/simple/nextjs/app/page.tsx index a6c7e79f09764f6900291fab0dd925939963e213..31f51facb248687cf7e7c6b34493778634a8c59c 100644 --- a/templates/simple/nextjs/app/page.tsx +++ b/templates/simple/nextjs/app/page.tsx @@ -1,34 +1,11 @@ -import MessageForm from "@/app/components/message-form"; -import Image from "next/image"; +import ChatSection from "@/app/components/chat-section"; +import Header from "@/app/components/header"; export default function Home() { return ( - <main className="flex min-h-screen flex-col items-center gap-10 p-24"> - <div className="z-10 max-w-5xl w-full items-center justify-between font-mono text-sm lg:flex"> - <p className="fixed left-0 top-0 flex w-full justify-center border-b border-gray-300 bg-gradient-to-b from-zinc-200 pb-6 pt-8 backdrop-blur-2xl dark:border-neutral-800 dark:bg-zinc-800/30 dark:from-inherit lg:static lg:w-auto lg:rounded-xl lg:border lg:bg-gray-200 lg:p-4 lg:dark:bg-zinc-800/30"> - Get started by editing - <code className="font-mono font-bold">app/page.tsx</code> - </p> - <div className="fixed bottom-0 left-0 flex h-48 w-full items-end justify-center bg-gradient-to-t from-white via-white dark:from-black dark:via-black lg:static lg:h-auto lg:w-auto lg:bg-none"> - <a - className="pointer-events-none flex place-items-center gap-2 p-8 lg:pointer-events-auto lg:p-0" - href="https://vercel.com?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app" - target="_blank" - rel="noopener noreferrer" - > - By{" "} - <Image - src="/vercel.svg" - alt="Vercel Logo" - className="dark:invert" - width={100} - height={24} - priority - /> - </a> - </div> - </div> - <MessageForm /> + <main className="flex min-h-screen flex-col items-center gap-10 p-24 background-gradient"> + <Header /> + <ChatSection /> </main> ); } diff --git a/templates/simple/nextjs/app/services/chatStorage.service.ts b/templates/simple/nextjs/app/services/chatStorage.service.ts new file mode 100644 index 0000000000000000000000000000000000000000..6f92ce86a276d5fb011327cf1001e7e605a485e8 --- /dev/null +++ b/templates/simple/nextjs/app/services/chatStorage.service.ts @@ -0,0 +1,35 @@ +import { ChatMessage } from "llamaindex"; + +export const CHAT_STORAGE_KEY = "chatHistory"; + +class _ChatStorageService { + public getChatHistory(): ChatMessage[] { + const chatHistory = localStorage.getItem(CHAT_STORAGE_KEY); + return chatHistory ? JSON.parse(chatHistory) : []; + } + + public saveChatHistory(chatHistory: ChatMessage[]) { + localStorage.setItem(CHAT_STORAGE_KEY, JSON.stringify(chatHistory)); + } + + public clearChatHistory() { + localStorage.removeItem(CHAT_STORAGE_KEY); + } + + public addChatMessage(chatMessage: ChatMessage) { + const chatHistory = this.getChatHistory(); + chatHistory.push(chatMessage); + this.saveChatHistory(chatHistory); + } + + constructor() { + if (typeof window !== "undefined") { + const chatHistory = localStorage.getItem(CHAT_STORAGE_KEY); + if (!chatHistory) this.saveChatHistory([]); + } + } +} + +const ChatStorageService = new _ChatStorageService(); + +export default ChatStorageService; diff --git a/templates/simple/nextjs/public/llama.png b/templates/simple/nextjs/public/llama.png new file mode 100644 index 0000000000000000000000000000000000000000..d4efba3b816bf765439c6d01b322b02684e946c3 Binary files /dev/null and b/templates/simple/nextjs/public/llama.png differ diff --git a/templates/simple/nextjs/public/next.svg b/templates/simple/nextjs/public/next.svg deleted file mode 100644 index 5174b28c565c285e3e312ec5178be64fbeca8398..0000000000000000000000000000000000000000 --- a/templates/simple/nextjs/public/next.svg +++ /dev/null @@ -1 +0,0 @@ -<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 394 80"><path fill="#000" d="M262 0h68.5v12.7h-27.2v66.6h-13.6V12.7H262V0ZM149 0v12.7H94v20.4h44.3v12.6H94v21h55v12.6H80.5V0h68.7zm34.3 0h-17.8l63.8 79.4h17.9l-32-39.7 32-39.6h-17.9l-23 28.6-23-28.6zm18.3 56.7-9-11-27.1 33.7h17.8l18.3-22.7z"/><path fill="#000" d="M81 79.3 17 0H0v79.3h13.6V17l50.2 62.3H81Zm252.6-.4c-1 0-1.8-.4-2.5-1s-1.1-1.6-1.1-2.6.3-1.8 1-2.5 1.6-1 2.6-1 1.8.3 2.5 1a3.4 3.4 0 0 1 .6 4.3 3.7 3.7 0 0 1-3 1.8zm23.2-33.5h6v23.3c0 2.1-.4 4-1.3 5.5a9.1 9.1 0 0 1-3.8 3.5c-1.6.8-3.5 1.3-5.7 1.3-2 0-3.7-.4-5.3-1s-2.8-1.8-3.7-3.2c-.9-1.3-1.4-3-1.4-5h6c.1.8.3 1.6.7 2.2s1 1.2 1.6 1.5c.7.4 1.5.5 2.4.5 1 0 1.8-.2 2.4-.6a4 4 0 0 0 1.6-1.8c.3-.8.5-1.8.5-3V45.5zm30.9 9.1a4.4 4.4 0 0 0-2-3.3 7.5 7.5 0 0 0-4.3-1.1c-1.3 0-2.4.2-3.3.5-.9.4-1.6 1-2 1.6a3.5 3.5 0 0 0-.3 4c.3.5.7.9 1.3 1.2l1.8 1 2 .5 3.2.8c1.3.3 2.5.7 3.7 1.2a13 13 0 0 1 3.2 1.8 8.1 8.1 0 0 1 3 6.5c0 2-.5 3.7-1.5 5.1a10 10 0 0 1-4.4 3.5c-1.8.8-4.1 1.2-6.8 1.2-2.6 0-4.9-.4-6.8-1.2-2-.8-3.4-2-4.5-3.5a10 10 0 0 1-1.7-5.6h6a5 5 0 0 0 3.5 4.6c1 .4 2.2.6 3.4.6 1.3 0 2.5-.2 3.5-.6 1-.4 1.8-1 2.4-1.7a4 4 0 0 0 .8-2.4c0-.9-.2-1.6-.7-2.2a11 11 0 0 0-2.1-1.4l-3.2-1-3.8-1c-2.8-.7-5-1.7-6.6-3.2a7.2 7.2 0 0 1-2.4-5.7 8 8 0 0 1 1.7-5 10 10 0 0 1 4.3-3.5c2-.8 4-1.2 6.4-1.2 2.3 0 4.4.4 6.2 1.2 1.8.8 3.2 2 4.3 3.4 1 1.4 1.5 3 1.5 5h-5.8z"/></svg> \ No newline at end of file diff --git a/templates/simple/nextjs/public/vercel.svg b/templates/simple/nextjs/public/vercel.svg deleted file mode 100644 index d2f84222734f27b623d1c80dda3561b04d1284af..0000000000000000000000000000000000000000 --- a/templates/simple/nextjs/public/vercel.svg +++ /dev/null @@ -1 +0,0 @@ -<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 283 64"><path fill="black" d="M141 16c-11 0-19 7-19 18s9 18 20 18c7 0 13-3 16-7l-7-5c-2 3-6 4-9 4-5 0-9-3-10-7h28v-3c0-11-8-18-19-18zm-9 15c1-4 4-7 9-7s8 3 9 7h-18zm117-15c-11 0-19 7-19 18s9 18 20 18c6 0 12-3 16-7l-8-5c-2 3-5 4-8 4-5 0-9-3-11-7h28l1-3c0-11-8-18-19-18zm-10 15c2-4 5-7 10-7s8 3 9 7h-19zm-39 3c0 6 4 10 10 10 4 0 7-2 9-5l8 5c-3 5-9 8-17 8-11 0-19-7-19-18s8-18 19-18c8 0 14 3 17 8l-8 5c-2-3-5-5-9-5-6 0-10 4-10 10zm83-29v46h-9V5h9zM37 0l37 64H0L37 0zm92 5-27 48L74 5h10l18 30 17-30h10zm59 12v10l-3-1c-6 0-10 4-10 10v15h-9V17h9v9c0-5 6-9 13-9z"/></svg> \ No newline at end of file