Training AI Chatbot with Files using MyBotChat API
In this step-by-step tutorial, we look at how to use API to upload files (pdf, docx, txt, xlsx, pptx) for training your AI Chatbot.
Prerequisite
You need to have an account with MyBot and created a chatbot. If you have not done this step yet, visit this get-started turorial page to start.
1. Generate API Key
- Login to your account, go to My Chatbots then select a bot of your choice and go to API Key tab.
- click on Add New to see a popup form to enter a name. Name is optional. Finally click Save.
- From the list of your API Key, copy the key.
2. Upload and Manage your training files via API
Current API only support training your Chatbot with Files; more support coming soon.
Upload File and Train Chatbot: Make sure you only upload supported file type, the server will return error code and message when it cannot accept your file. After uploading, your Chatbot will be automatically trained to understand content in your file, you can test it immediately. We are going to use
/api/Train/File/{ChatbotId}
to upload file, assuming there is a file on your desktop nametourism.txt
and the full path to access your file is:/Users/borey/Desktop/tourism.txt
.Curl Example:
curl --location --request POST 'https://mybot.chat/api/Training/File/--your-chatbotId--' \ --header 'Authorization: Bearer --your-api-key--' \ --form 'file=@"/Users/borey/Desktop/tourism.txt"'
Javascript Example:
var myHeaders = new Headers(); myHeaders.append("Authorization", "Bearer --your-api-key--"); var formdata = new FormData(); formdata.append("file", fileInput.files[0], "tourism.txt"); var requestOptions = { method: 'POST', headers: myHeaders, body: formdata, redirect: 'follow' }; fetch("https://mybot.chat/api/Training/File/--your-chatbotId--", requestOptions) .then(response => response.text()) .then(result => console.log(result)) .catch(error => console.log('error', error));
Example Reponse from Server:
{ "chatbotId": "772af073-234f-465a-b44c-5a72fddce458", "id": 4456 }
Get all your files: by using GET request to this endpoint:
/api/Train/File/{ChatbotId}
Example in Curl:
curl --location --request GET 'https://mybot.chat/api/Training/File/--your-chatbotId--' \ --header 'Authorization: Bearer --your-api-key--'
Example in Javascript:
var myHeaders = new Headers(); myHeaders.append("Authorization", "Bearer --your-api-key--"); var requestOptions = { method: 'GET', headers: myHeaders, redirect: 'follow' }; fetch("https://mybot.chat/api/Training/File/--your-chatbotId--", requestOptions) .then(response => response.text()) .then(result => console.log(result)) .catch(error => console.log('error', error));
Example Response from the bot:
[ { "id": 46, "chatbotId": "772af073-234f-465a-b44c-5a72fddce458", "fileName": "moon.docx", "contentLength": 8195, "isTrained": true, "createdDate": "2024-11-10T07:03:13.042198Z", "modifiedDate": "2024-11-10T07:03:13.042198Z" }, { "id": 45, "chatbotId": "772af073-234f-465a-b44c-5a72fddce458", "fileName": "customer-data.txt", "contentLength": 379, "isTrained": true, "createdDate": "2024-11-10T07:02:57.192112Z", "modifiedDate": "2024-11-10T07:02:57.192148Z" } ]
Delete File: using API endpoint
api/Training/File/{ChatbotId}/{id}
For this example, we are using the result from the file list, ChatbotId = 772af073-234f-465a-b44c-5a72fddce458 and File Id = 46Example using Curl:
curl --location --request DELETE 'https://mybot.chat/api/Training/File/772af073-234f-465a-b44c-5a72fddce458/46' \ --header 'Authorization: Bearer --your-api-key--'
Javascript Example:
var myHeaders = new Headers(); myHeaders.append("Authorization", "Bearer --your-api-key--"); var formdata = new FormData(); var requestOptions = { method: 'DELETE', headers: myHeaders, body: formdata, redirect: 'follow' }; fetch("https://mybot.chat/api/Training/File/772af073-234f-465a-b44c-5a72fddce458/46", requestOptions) .then(response => response.text()) .then(result => console.log(result)) .catch(error => console.log('error', error));