-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
118 additions
and
1 deletion.
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,25 @@ | ||
export const POST = async (req: Request) => { | ||
const { messages } = (await req.json()) as { | ||
messages: { role: "user" | "assistant"; content: string }[]; | ||
}; | ||
|
||
// remove the most recent user question | ||
const { content: question, role } = messages.pop()!; | ||
if (role !== "user" || !question) throw new Error("No question provided"); | ||
|
||
return fetch(process.env["ENTELLIGENCE_API_URL"]!, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
history: messages.map((m) => ({ | ||
role: m.role, | ||
content: m.content, | ||
})), | ||
question, | ||
vectorDBUrl: "Yonom&assistant-ui", | ||
advancedAgent: false, | ||
}), | ||
}); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
"use client"; | ||
|
||
import { | ||
AssistantModal, | ||
ChatModelAdapter, | ||
useLocalRuntime, | ||
} from "@assistant-ui/react"; | ||
import { makeMarkdownText } from "@assistant-ui/react-markdown"; | ||
import { makePrismAsyncSyntaxHighlighter } from "@assistant-ui/react-syntax-highlighter"; | ||
import { coldarkDark } from "react-syntax-highlighter/dist/esm/styles/prism"; | ||
import remarkGfm from "remark-gfm"; | ||
|
||
co asAsyncIterable<T>(source: ReadableStream<T>): AsyncIterable<T> { | ||
return { | ||
[Symbol.asyncIterator]: () => { | ||
const reader = source.getReader(); | ||
return { | ||
async next(): Promise<IteratorResult<T, undefined>> { | ||
const { done, value } = await reader.read(); | ||
return done | ||
? { done: true, value: undefined } | ||
: { done: false, value }; | ||
}, | ||
}; | ||
}, | ||
}; | ||
} | ||
|
||
const MyCustomAdapter: ChatModelAdapter = { | ||
async *run({ messages, abortSignal }) { | ||
const messagesToSend = messages.map((m) => ({ | ||
role: m.role, | ||
content: m.content | ||
.filter((c) => c.type === "text") | ||
.map((c) => c.text) | ||
.join(" "), | ||
})); | ||
|
||
const response = await fetch("/api/entelligence", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
messages: messagesToSend, | ||
}), | ||
signal: abortSignal, | ||
}); | ||
|
||
let text = ""; | ||
for await (const chunk of asAsyncIterable( | ||
response.body!.pipeThrough(new TextDecoderStream()), | ||
)) { | ||
text += chunk; | ||
yield { content: [{ type: "text", text }] }; | ||
} | ||
}, | ||
}; | ||
|
||
const SyntaxHighlighter = makePrismAsyncSyntaxHighlighter({ | ||
style: coldarkDark, | ||
customStyle: { | ||
margin: 0, | ||
backgroundColor: "black", | ||
}, | ||
}); | ||
const MarkdownText = makeMarkdownText({ | ||
remarkPlugins: [remarkGfm], | ||
components: { | ||
SyntaxHighlighter, | ||
}, | ||
}); | ||
|
||
export const DocsChat = () => { | ||
const runtime = useLocalRuntime(MyCustomAdapter); | ||
|
||
return ( | ||
<AssistantModal | ||
runtime={runtime} | ||
welcome={{ | ||
message: "Ask me anything about the docs!", | ||
}} | ||
assistantMessage={{ components: { Text: MarkdownText } }} | ||
/> | ||
); | ||
}; |