Skip to content

Commit

Permalink
feat: ask-docs (#653)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Aug 7, 2024
1 parent 0fd8064 commit 3328af0
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 1 deletion.
25 changes: 25 additions & 0 deletions apps/docs/app/api/entelligence/route.ts
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,
}),
});
};
8 changes: 7 additions & 1 deletion apps/docs/app/docs/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { DocsLayout } from "fumadocs-ui/layout";
import type { ReactNode } from "react";
import { docsOptions } from "./layout.config";
import "fumadocs-ui/twoslash.css";
import { DocsChat } from "@/components/docs-chat/DocsChat";

export default function Layout({ children }: { children: ReactNode }) {
return <DocsLayout {...docsOptions}>{children}</DocsLayout>;
return (
<DocsLayout {...docsOptions}>
{children}
<DocsChat />
</DocsLayout>
);
}
86 changes: 86 additions & 0 deletions apps/docs/components/docs-chat/DocsChat.tsx
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 } }}
/>
);
};

0 comments on commit 3328af0

Please sign in to comment.