-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate llamaindex chat-ui (#399)
--------- Co-authored-by: Marcus Schiesser <[email protected]>
- Loading branch information
Showing
34 changed files
with
290 additions
and
2,019 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"create-llama": patch | ||
--- | ||
|
||
feat: use llamaindex chat-ui for nextjs frontend |
53 changes: 11 additions & 42 deletions
53
templates/types/streaming/nextjs/app/components/chat-section.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,26 @@ | ||
"use client"; | ||
|
||
import { ChatSection as ChatSectionUI } from "@llamaindex/chat-ui"; | ||
import "@llamaindex/chat-ui/styles/code.css"; | ||
import "@llamaindex/chat-ui/styles/katex.css"; | ||
import { useChat } from "ai/react"; | ||
import { useState } from "react"; | ||
import { ChatInput, ChatMessages } from "./ui/chat"; | ||
import CustomChatInput from "./ui/chat/chat-input"; | ||
import CustomChatMessages from "./ui/chat/chat-messages"; | ||
import { useClientConfig } from "./ui/chat/hooks/use-config"; | ||
|
||
export default function ChatSection() { | ||
const { backend } = useClientConfig(); | ||
const [requestData, setRequestData] = useState<any>(); | ||
const { | ||
messages, | ||
input, | ||
isLoading, | ||
handleSubmit, | ||
handleInputChange, | ||
reload, | ||
stop, | ||
append, | ||
setInput, | ||
} = useChat({ | ||
body: { data: requestData }, | ||
const handler = useChat({ | ||
api: `${backend}/api/chat`, | ||
headers: { | ||
"Content-Type": "application/json", // using JSON because of vercel/ai 2.2.26 | ||
}, | ||
onError: (error: unknown) => { | ||
if (!(error instanceof Error)) throw error; | ||
const message = JSON.parse(error.message); | ||
alert(message.detail); | ||
alert(JSON.parse(error.message).detail); | ||
}, | ||
sendExtraMessageFields: true, | ||
}); | ||
|
||
return ( | ||
<div className="space-y-4 w-full h-full flex flex-col"> | ||
<ChatMessages | ||
messages={messages} | ||
isLoading={isLoading} | ||
reload={reload} | ||
stop={stop} | ||
append={append} | ||
/> | ||
<ChatInput | ||
input={input} | ||
handleSubmit={handleSubmit} | ||
handleInputChange={handleInputChange} | ||
isLoading={isLoading} | ||
messages={messages} | ||
append={append} | ||
setInput={setInput} | ||
requestParams={{ params: requestData }} | ||
setRequestData={setRequestData} | ||
/> | ||
</div> | ||
<ChatSectionUI handler={handler} className="w-full h-full"> | ||
<CustomChatMessages /> | ||
<CustomChatInput /> | ||
</ChatSectionUI> | ||
); | ||
} |
1 change: 0 additions & 1 deletion
1
templates/types/streaming/nextjs/app/components/ui/README-template.md
This file was deleted.
Oops, something went wrong.
28 changes: 0 additions & 28 deletions
28
templates/types/streaming/nextjs/app/components/ui/chat/chat-actions.tsx
This file was deleted.
Oops, something went wrong.
6 changes: 4 additions & 2 deletions
6
...ents/ui/chat/chat-message/chat-avatar.tsx → ...js/app/components/ui/chat/chat-avatar.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
templates/types/streaming/nextjs/app/components/ui/chat/chat-message-content.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { | ||
ChatMessage, | ||
ContentPosition, | ||
getSourceAnnotationData, | ||
useChatMessage, | ||
} from "@llamaindex/chat-ui"; | ||
import { Markdown } from "./custom/markdown"; | ||
import { ToolAnnotations } from "./tools/chat-tools"; | ||
|
||
export function ChatMessageContent() { | ||
const { message } = useChatMessage(); | ||
const customContent = [ | ||
{ | ||
// override the default markdown component | ||
position: ContentPosition.MARKDOWN, | ||
component: ( | ||
<Markdown | ||
content={message.content} | ||
sources={getSourceAnnotationData(message.annotations)?.[0]} | ||
/> | ||
), | ||
}, | ||
{ | ||
// add the tool annotations after events | ||
position: ContentPosition.AFTER_EVENTS, | ||
component: <ToolAnnotations message={message} />, | ||
}, | ||
]; | ||
return <ChatMessage.Content content={customContent} />; | ||
} |
Oops, something went wrong.