Add AI Chatbox to your VitePress document site
This article will guide you how to quickly empower your VitePress
documentation site with AI conversational capabilities using an open-source tool Documate. This will allow it to answer your users' questions based on the content of your documentation, supporting streaming outputs.
The complete source code of the project can be viewed at https://github.com/AirCodeLabs/documate.
Integration Steps
If you want to create a brand-new VitePress project with AI conversational capabilities, use the command below:
bash1npm create documate@latest --template vitepress
After creation, jump directly to step 3 "Build, Upload, and Configure the Search Backend API".
To add AI conversational capabilities to an existing VitePress project, follow these steps:
1. Initialization
Run the following command in your VitePress project root directory to initialize:
bash1npx @documate/documate init --framework vue
This command will create a documate.json
configuration file.
json1{ 2 "root": ".", 3 "include": [ 4 "**/*.md" 5 ], 6 "backend": "" 7}
A documate:upload
command will also be added, which is used to upload documents to create a knowledge base. I will explain its specific usage later.
json1{ 2 "scripts": { 3 "docs:dev": "vitepress dev", 4 "docs:build": "vitepress build", 5 "docs:preview": "vitepress preview", 6 "documate:upload": "documate upload" 7 }, 8 "dependencies": { 9 "@documate/vue": "^0.2.3" 10 }, 11 "devDependencies": { 12 "@documate/documate": "^0.1.0" 13 } 14}
2. Add UI Entry to the Project
Add the following code to the file .vitepress/theme/index.js
. If it doesn't exist, you need to create it manually. VitePress introduces how to customize your own theme in the Extending the Default Theme documentation.
jsx1import { h } from 'vue' 2import DefaultTheme from 'vitepress/theme' 3 4// Load component and style 5import Documate from '@documate/vue' 6import '@documate/vue/dist/style.css' 7 8export default { 9 ...DefaultTheme, 10 // Add Documate UI to the slot 11 Layout: h(DefaultTheme.Layout, null, { 12 'nav-bar-content-before': () => h(Documate, { 13 endpoint: '', 14 }), 15 }), 16}
The above code will add an AI chat box UI to the navbar. After launching the service locally using npm run docs:dev
, you can find the Ask AI
button in the top left corner. If you don't see the Ask AI button, check to ensure all the above code has been added correctly and that you've imported the CSS style file from @documate/vue/dist/style.css
.
Now, you have successfully integrated the UI. Next, we'll add the interface capability for the chat box to answer questions.
3. Build, Upload, and Configure the Search Backend API
Documate's backend code is used to upload document content to create a knowledge base and to receive user questions and return streaming answers.
Go to the backend folder on GitHub and click on 「Deploy to AirCode」 to quickly copy and deploy your own backend code.
If you are using AirCode (an online platform for writing and deploying Node.js applications) for the first time, you will be redirected to the login page. It is recommended to log in with GitHub for faster access.
After creating the app, set the OPENAI_API_KEY
environment variable in the Environments
tab to your OpenAI Key value. You can obtain the API Key from the OpenAI platform.
Click the「Deploy」button on the top bar to deploy all functions online. Once deployed successfully, you will receive URLs to call each function.
Here we will use the upload.js
and ask.js
functions, one for uploading document content and the other for answering questions.
4. Set Up the API Endpoint
In the AirCode Dashboard, select the deployed upload.js file, copy its URL, and add it to the backend field in documate.json.
json1// documate.json 2{ 3 "root": ".", 4 "include": [ "**/*.md" ], 5 "backend": "替换为你的 upload.js 的 URL" 6}
Similarly, in AirCode, select the deployed ask.js
file, copy its URL, and modify the endpoint value in .vitepress/theme/index.js
.
js1// .vitepress/theme/index.js 2import { h } from 'vue' 3import DefaultTheme from 'vitepress/theme' 4import Documate from '@documate/vue' 5import '@documate/vue/dist/style.css' 6 7export default { 8 ...DefaultTheme, 9 Layout: h(DefaultTheme.Layout, null, { 10 'nav-bar-content-before': () => h(Documate, { 11 // Replace the URL with your own one 12 endpoint: '替换为你的 ask.js 的 URL', 13 }, 14 ), 15 }), 16}
5. Run the Project
Use the command below to upload the content to the backend and generate the documentation knowledge base:
bash1npm run documate:upload
Once the command completes, launch the project locally, click the Ask AI
button in the top left corner, enter a question in the pop-up dialog box, and receive answers based on your documentation content.
bash1npm run docs:dev
For more usage and configuration methods, please refer to the Documate project on GitHub, Comments and discussions are welcome.
GitHub -> https://github.com/AirCodeLabs/documate.