Documentation is a crucial part of any software product, but making it easily accessible can be challenging. This is where documentAI and conversational chatbots can help. In this post we will go over how you can integrate documentAI with your existing documentation to allow your users to converse with your docs.
Make sure to check out our integration for our documentation.
A chat interface for documentation has several advantages:
First get your API key from the console. Authenticate requests by passing this key in the X-API-KEY header. Learn more about authentication here.
There are 2 options to ingest the documentation, you can either crawl your existing documentation or you can upload it directly.
The easiest way to ingest docs is via crawling. This entails programmatically visiting each page and processing its content. See documentation for details.
curl -X POST https://api.documentai.dev/v1/collections/[COLLECTION ID]/crawl \ -H 'X-API-KEY: [YOUR API KEY]' \ -d '{"url": "[YOUR DOCUMENTATION ROOT URL]", "maxDepth": 5, "maxDocuments": 100}'
Where:
Crawling is asynchronous, the response will contain a crawlId, using which you can check status as per documentation.
{ "collectionId": "[COLLECTION ID]", "crawlId": "f08594bd-4486-4918-b237-0017b1fd2d6c", "status": { "date": "2023-09-03T12:22:36.291790Z", "status": "CRAWLING" } }
If your documentation is in offline files you can also upload them directly. To do this you can use the upload API, full reference here.
curl -X PUT https://api.documentai.dev/v1/collections/[COLLECTION ID]/upload \ -H 'X-API-KEY: [YOUR API KEY]' \ --form file=doc1.pdf \ --form file=doc2.docx
Where:
You can upload multiple files if needed in a single request. The upload is asynchronous and you can monitor the status of your upload job using check upload API. The response will contain an uploadId.
{ "collectionId": "[COLLECTION ID]", "uploadId": "6f207f16-c30b-47ef-9a58-efea9df9ae73" }
After the crawl is completed you can integrate with the chat API. Full reference is available here. The chat interface exposes a conversational UX for your users to ask questions about your documentation and they can also leverage LLM's knowledge to easily understand your product and how to use it.
curl -X POST https://api.documentai.dev/v1/collections/[COLLECTION ID]/chat/[CHAT ID] \ -H 'X-API-KEY: [YOUR API KEY}' \ -d '{"message": [YOUR MESSAGE]}'
Where:
{ "sender": "ASSISTANT", "message": { "id": "553beb15-9f2b-4c97-857f-94963bbce84f", "date": "2023-09-03T12:24:09.026440Z", "content": "To upload a document using TypeScript, you can utilize the `axios` library to make HTTP requests to the documentAI API. Here's an example of how you can upload a document:\n\n```typescript\nimport axios from 'axios';\n\nasync function uploadDocument(collectionId: string, apiKey: string, filePath: string): Promise<void> {\n try {\n const url = `https://api.documentai.dev/v1/collections/${collectionId}/upload`;\n const formData = new FormData();\n formData.append('file', filePath);\n\n const response = await axios.put(url, formData, {\n headers: {\n 'Content-Type': 'multipart/form-data',\n 'X-API-KEY': apiKey,\n },\n });\n\n const uploadId = response.data.uploadId;\n console.log(`Document uploaded successfully. Upload ID: ${uploadId}`);\n } catch (error) {\n console.error('Failed to upload document:', error.response.data.error);\n }\n}\n\n// Usage\nconst collectionId = 'mycollection';\nconst apiKey = 'your-api-key';\nconst filePath = '/path/to/your/document.pdf';\n\nuploadDocument(collectionId, apiKey, filePath);\n```\n\nMake sure to replace `'your-api-key'` with your actual API key and `'mycollection'` with the ID of the collection you want to upload the document to. Also, update `'your/document.pdf'` with the path to your actual document file.\n\nPlease note that you need to install the `axios` library if it's not already installed. You can install it by running `npm install axios` or `yarn add axios`.\n\nRemember to handle any errors that may occur during the upload process.", "context": [ { "collectionId": "[COLECTION ID]", "documentId": "b0dd63b1-f78c-4989-94f9-f73768bb12dd", "chunkId": "348a8973-4fc6-42ba-a557-0d89ea8aef54", "content": "To upload a document use multipart/form-data. The maximum file size is 50MB. Documents can be uploaded in a batch.\n\nPUT/v1/collections/:collectionId/upload\n\ncurl -X PUT /v1/collections/mycollection/upload \\\n -H 'X-API-KEY: 46n1Zwy48X95mIfbOjIFO99Dg613KjRu8iFA4bAr' \\\n --form file=may_report.pdf \\\n --form file=june_report.pdf\nResponse\n\n{\n\t\"collectionId\": \"mycollection\",\n\t\"uploadId\": \"6f207f16-c30b-47ef-9a58-efea9df9ae73\"\n}\nCheck Upload\n\nRetrieves the current status of an upload.\n\nGET/v1/collections/:collectionId/upload/:uploadId\n\ncurl -X GET /v1/collections/mycollection/upload/6f207f16-c30b-47ef-9a58-efea9df9ae73 \\\n -H 'X-API-KEY: 46n1Zwy48X95mIfbOjIFO99Dg613KjRu8iFA4bAr'\nResponse", "metadata": {} }, { "collectionId": "[COLECTION ID]", "documentId": "b0dd63b1-f78c-4989-94f9-f73768bb12dd", "chunkId": "bf3d056f-073b-46e2-b08b-7914fc511ba8", "content": "API Reference | documentAI\nIntroduction\n\nWelcome to the documentAI API! documentAI provides a retrieval augmented generation service that allows you to generate high-quality content using a combination of large language models and an information retrieval system.\n\nOur API provides easy access to documentAI's state-of-the-art AI models through a simple REST interface. You can integrate documentAI directly into your application to add powerful natural language generation capabilities.\n\nThis documentation provides complete reference material for using the documentAI API. We recommend reading the quick start guide to get up and running quickly. From there you can explore the available endpoints for generating text, managing knowledge sources, and more.\n\nAuthentication\n\nTo use the documentAI API, you'll need an API key. API keys can be created and managed from your documentAI console.", "metadata": {} }, { "collectionId": "[COLECTION ID]", "documentId": "b0dd63b1-f78c-4989-94f9-f73768bb12dd", "chunkId": "858ec3d6-770f-40ac-a203-5dd219c1482d", "content": "\"metadata\": {\n \"url\": \"https://documentai.dev/docs/documents\"\n }\n }\n ]\n}\nGet Document", "metadata": {} } ] } }
You can also retrive the whole conversation using get chat API.
curl -X GET https://api.documentai.dev/v1/collections/[COLLECTION ID]/chat/[CHAT ID] \ -H 'X-API-KEY: [YOUR API KEY}'
Where:
The response will include both your messages and assistant's messages with the relevant context so you can link back to the source.
{ "messages": [ { "sender": "USER", "id": "d595de0e-4664-43a6-b03b-396bb05933a4", "date": "2023-09-03T11:24:09.026440Z", "content": "How can I upload a document using TypeScript?", "context": [] }, { "sender": "ASSISTANT", "message": { "id": "553beb15-9f2b-4c97-857f-94963bbce84f", "date": "2023-09-03T12:24:09.026440Z", "content": "To upload a document using TypeScript, you can utilize the `axios` library to make HTTP requests to the documentAI API. Here's an example of how you can upload a document:\n\n```typescript\nimport axios from 'axios';\n\nasync function uploadDocument(collectionId: string, apiKey: string, filePath: string): Promise<void> {\n try {\n const url = `https://api.documentai.dev/v1/collections/${collectionId}/upload`;\n const formData = new FormData();\n formData.append('file', filePath);\n\n const response = await axios.put(url, formData, {\n headers: {\n 'Content-Type': 'multipart/form-data',\n 'X-API-KEY': apiKey,\n },\n });\n\n const uploadId = response.data.uploadId;\n console.log(`Document uploaded successfully. Upload ID: ${uploadId}`);\n } catch (error) {\n console.error('Failed to upload document:', error.response.data.error);\n }\n}\n\n// Usage\nconst collectionId = 'mycollection';\nconst apiKey = 'your-api-key';\nconst filePath = '/path/to/your/document.pdf';\n\nuploadDocument(collectionId, apiKey, filePath);\n```\n\nMake sure to replace `'your-api-key'` with your actual API key and `'mycollection'` with the ID of the collection you want to upload the document to. Also, update `'your/document.pdf'` with the path to your actual document file.\n\nPlease note that you need to install the `axios` library if it's not already installed. You can install it by running `npm install axios` or `yarn add axios`.\n\nRemember to handle any errors that may occur during the upload process.", "context": [ { "collectionId": "[COLECTION ID]", "documentId": "b0dd63b1-f78c-4989-94f9-f73768bb12dd", "chunkId": "348a8973-4fc6-42ba-a557-0d89ea8aef54", "content": "To upload a document use multipart/form-data. The maximum file size is 50MB. Documents can be uploaded in a batch.\n\nPUT/v1/collections/:collectionId/upload\n\ncurl -X PUT /v1/collections/mycollection/upload \\\n -H 'X-API-KEY: 46n1Zwy48X95mIfbOjIFO99Dg613KjRu8iFA4bAr' \\\n --form file=may_report.pdf \\\n --form file=june_report.pdf\nResponse\n\n{\n\t\"collectionId\": \"mycollection\",\n\t\"uploadId\": \"6f207f16-c30b-47ef-9a58-efea9df9ae73\"\n}\nCheck Upload\n\nRetrieves the current status of an upload.\n\nGET/v1/collections/:collectionId/upload/:uploadId\n\ncurl -X GET /v1/collections/mycollection/upload/6f207f16-c30b-47ef-9a58-efea9df9ae73 \\\n -H 'X-API-KEY: 46n1Zwy48X95mIfbOjIFO99Dg613KjRu8iFA4bAr'\nResponse", "metadata": {} }, { "collectionId": "[COLECTION ID]", "documentId": "b0dd63b1-f78c-4989-94f9-f73768bb12dd", "chunkId": "bf3d056f-073b-46e2-b08b-7914fc511ba8", "content": "API Reference | documentAI\nIntroduction\n\nWelcome to the documentAI API! documentAI provides a retrieval augmented generation service that allows you to generate high-quality content using a combination of large language models and an information retrieval system.\n\nOur API provides easy access to documentAI's state-of-the-art AI models through a simple REST interface. You can integrate documentAI directly into your application to add powerful natural language generation capabilities.\n\nThis documentation provides complete reference material for using the documentAI API. We recommend reading the quick start guide to get up and running quickly. From there you can explore the available endpoints for generating text, managing knowledge sources, and more.\n\nAuthentication\n\nTo use the documentAI API, you'll need an API key. API keys can be created and managed from your documentAI console.", "metadata": {} }, { "collectionId": "[COLECTION ID]", "documentId": "b0dd63b1-f78c-4989-94f9-f73768bb12dd", "chunkId": "858ec3d6-770f-40ac-a203-5dd219c1482d", "content": "\"metadata\": {\n \"url\": \"https://documentai.dev/docs/documents\"\n }\n }\n ]\n}\nGet Document", "metadata": {} } ] } } ] }
Hopefully this post demonstrated the benefits of using documentAI to make your documantation easier and more accessible only with a few API calls. Remember you can checkout an example running on our documentation.