-
Notifications
You must be signed in to change notification settings - Fork 10k
[Docs Site] Add DocsAI.tsx #22754
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
Merged
Merged
[Docs Site] Add DocsAI.tsx #22754
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
01f75ad
[Docs Site] Add Support AI
KianNH 8f3a677
support gfm
KianNH e0323d5
turn \n into linebreaks
KianNH cfaa7f8
remove debug
KianNH 20a396c
feedback
KianNH 33ac599
pointer and beta
KianNH d5cda72
sources target and rename
KianNH 691d9a2
use textarea
KianNH File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,175 @@ | ||
| import { useState } from "react"; | ||
| import Markdown from "react-markdown"; | ||
| import remarkBreaks from "remark-breaks"; | ||
| import remarkGfm from "remark-gfm"; | ||
| import { fetchEventSource } from "@microsoft/fetch-event-source"; | ||
| import { Ring } from "ldrs/react"; | ||
| import "ldrs/react/Ring.css"; | ||
|
|
||
| type Messages = { role: "user" | "assistant"; content: string }[]; | ||
| type Sources = { title: string; file_path: string }[]; | ||
|
|
||
| function Messages({ | ||
| messages, | ||
| loading, | ||
| }: { | ||
| messages: Messages; | ||
| loading: boolean; | ||
| }) { | ||
| const classes = { | ||
| base: "w-fit max-w-3/4 rounded p-4", | ||
| user: "bg-cl1-brand-orange text-cl1-black self-end", | ||
| assistant: "self-start bg-(--sl-color-bg-nav)", | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="flex flex-col justify-center gap-4"> | ||
| {messages | ||
| .filter((message) => Boolean(message.content)) | ||
| .map((message, index) => ( | ||
| <div | ||
| key={index} | ||
| className={`${classes.base} ${message.role === "user" ? classes.user : classes.assistant}`} | ||
| > | ||
| <Markdown remarkPlugins={[remarkGfm, remarkBreaks]}> | ||
| {message.content} | ||
| </Markdown> | ||
| </div> | ||
| ))} | ||
| {loading && ( | ||
| <div className={`${classes.base} ${classes.assistant}`}> | ||
| <Ring size={16} speed={1} color="var(--color-cl1-brand-orange)" /> | ||
| </div> | ||
| )} | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| export default function SupportAI() { | ||
| const [threadId, setThreadId] = useState<string | undefined>(); | ||
| const [question, setQuestion] = useState<string>(""); | ||
| const [loading, setLoading] = useState<boolean>(false); | ||
|
|
||
| const [messages, setMessages] = useState<Messages>([]); | ||
|
|
||
| async function handleSubmit() { | ||
| setLoading(true); | ||
| setMessages((messages) => [ | ||
| ...messages, | ||
| { role: "user", content: question }, | ||
| { role: "assistant", content: "" }, | ||
| ]); | ||
| setQuestion(""); | ||
|
|
||
| const controller = new AbortController(); | ||
| const { signal } = controller; | ||
|
|
||
| let chunkedAnswer = ""; | ||
| let sources: Sources = []; | ||
|
|
||
| await fetchEventSource( | ||
| // "http://localhost:8010/proxy/devdocs/ask", | ||
| "https://support-ai.cloudflaresupport.workers.dev/devdocs/ask", | ||
| { | ||
| method: "POST", | ||
| body: JSON.stringify({ | ||
| question, | ||
| threadId, | ||
| }), | ||
| signal, | ||
| openWhenHidden: true, | ||
| async onopen(response) { | ||
| if (!response.ok) { | ||
| throw new Error(response.status.toString()); | ||
| } | ||
|
|
||
| return; | ||
| }, | ||
| onerror(error) { | ||
| if (error instanceof Error) { | ||
| setLoading(false); | ||
| setMessages((messages) => [ | ||
| ...messages, | ||
| { | ||
| role: "assistant", | ||
| content: | ||
| "I'm unable to provide an answer to that at the moment. Please rephrase your query and I'll try again.", | ||
| }, | ||
| ]); | ||
| throw error; | ||
KianNH marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| }, | ||
| onmessage(ev) { | ||
| if (ev.data === "[DONE]") { | ||
| controller.abort(); | ||
|
|
||
| setMessages((messages) => { | ||
| const newMessages = [...messages]; | ||
| newMessages[newMessages.length - 1].content += [ | ||
| "\n\n", | ||
| "I used these sources to answer your question, please review them if you need more information:", | ||
| "\n\n", | ||
| sources | ||
| .map((source) => `- [${source.title}](${source.file_path})`) | ||
| .join("\n"), | ||
| ].join("\n"); | ||
|
|
||
| return newMessages; | ||
| }); | ||
| } | ||
|
|
||
| const { threadId, response, botResponse } = JSON.parse(ev.data); | ||
|
|
||
| if (botResponse?.sources) { | ||
| sources = botResponse.sources; | ||
| } | ||
|
|
||
| if (threadId) { | ||
| setThreadId(threadId); | ||
| } | ||
|
|
||
| if (!response) return; | ||
|
|
||
| chunkedAnswer += response; | ||
|
|
||
| setLoading(false); | ||
KianNH marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| setMessages((messages) => { | ||
| const newMessages = [...messages]; | ||
| newMessages[newMessages.length - 1].content = chunkedAnswer; | ||
| return newMessages; | ||
| }); | ||
| }, | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <div> | ||
| <Messages messages={messages} loading={loading} /> | ||
| <div className="flex items-center justify-center gap-4"> | ||
| <input | ||
| className="w-full rounded p-2" | ||
| type="text" | ||
| placeholder="Ask a question..." | ||
| value={question} | ||
| disabled={loading} | ||
| onChange={(e) => setQuestion(e.target.value)} | ||
| onKeyDown={async (e) => { | ||
| if (e.key === "Enter" && !loading) { | ||
| e.preventDefault(); | ||
| await handleSubmit(); | ||
| } | ||
| }} | ||
| /> | ||
| </div> | ||
| <p className="text-center text-xs"> | ||
| Use of Support AI is subject to the Cloudflare Website and Online | ||
| Services{" "} | ||
| <a href="https://www.cloudflare.com/website-terms/">Terms of Use</a>. | ||
| You acknowledge and agree that the output generated by Support AI has | ||
| not been verified by Cloudflare for accuracy and does not represent | ||
| Cloudflare's views. | ||
| </p> | ||
| </div> | ||
| ); | ||
| } | ||
This file contains hidden or 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,10 @@ | ||
| --- | ||
| title: Support AI | ||
kodster28 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| tableOfContents: false | ||
| sidebar: | ||
| order: 8 | ||
| --- | ||
|
|
||
| import SupportAI from "~/components/SupportAI.tsx"; | ||
|
|
||
| <SupportAI client:load /> | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.