Your own AI knowledge base chatbot with OpenAI without Fine-Tuning

Your own AI knowledge base chatbot with OpenAI without Fine-Tuning - Jan 22

Overview / Motivation

ChatGPT is a strong AI chatbot but it cannot answer based on our own knowledge base. Everyone is wondering if we had our own AI but it still difficult to make because we have to train machine learning models usually. I explain how to make our own AI chatbot quickly. The chatbot is not perfect but it should be a cost efficient way and it is a way that even people without ML experience can try.

How it works

The GPT-3 models available on OpenAI APIs, can find answers for questions from a document. By using that, we can extract answers from our documents. If we create an API that receives questions and answers retrieved from our documents, it should be our own AI chatbot.

Experiments on ChatGPT

The prompt template is like this.

Please give me answers for my questions based on the article.


Enter fullscreen mode Exit fullscreen mode

Image description

The point is giving all knowledges to GPT-3 model every time you ask questions. That's simple, Right?!

Build the system

To maintain our chatbot's knowledge, we have to have a way to update knowledge easily. For this chatbot, I used Google Document to store the data and used Google Apps Script as a web server.(Please check reference to make an API endpoint with Google Apps Script on Google Sheets.)

Step 1. Create a document

Please create a Google Document and write down your knowledge that AI uses. The content can be anything but I think what are difficult to find on web is appropriate. Because, if we can find on web easily, searching on the web can be faster than using your chatbot.

My ancle phone number

Your tasks that are not urgent
1. task 1
2. task 2
Enter fullscreen mode Exit fullscreen mode

Please note that OpenAI has limits of the length of prompt. So if the knowledge base document is too long, it fails. To avoid this, you have to call OpenAI API twice. The flow is like the below.

  • Find a doc that appropriate for a question.
  • Find answers on the doc I'm using the idea to increase knowledge that AI can search around but I do not explain in this article because it's a bit complicated.

Step 2. Call OpenAI API from Apps Script

Let's say we make a JavaScript function to find answers from a Google Document.

 * @param {string} docId ID of a Google Document.
 * @param {string} q Question text.
function searchDoc(docId, q) {
  // Opens a doc
  const doc = DocumentApp.openById(docId);
  // Gets the content as text not as structured data.
  const text = doc.getBody().getText();
  const data = {
    "model": "text-davinci-003",
    "prompt": `Please give me answers for my questions based on the article.\n\nArticle: ${text}\n\nQuestion: ${q}`,
    "max_tokens": 200,
    "temperature": 0.7
  const options = {
    "method" : "post",
    "payload" : JSON.stringify(data),
    headers: {
      "Content-Type": "application/json",
      "Authorization": "Bearer [YOUR API KEY]",
  const res = UrlFetchApp.fetch("", options).getContentText()
  return JSON.parse(res).choices[0].text
Enter fullscreen mode Exit fullscreen mode

It's basically easy because it's just calling the completion API. However, you have to use Apps Script specific function: UrlFetchApp instead of XMLHttpResponse in usual scripting. I'm using Davinci in the code but you can use other models if you want to lower the running cost. The price of other models are much lower than Davinci. Please check how many tokens you consumes per request on your account page of OpenAI. Your transaction history is available.

I do not talk how to receive text of questions as a parameter of GET requests. Please check a reference at the end of this article.

Step 3. Connect to Slack

I'm using this solution to check my companies rules like how to submit bill, how to apply vacation on our HR system. I'm using Slack so it is the best entry point for me. It depends on your own case.

To get a message from Slack and proxy to the Apps Script API server, it's easier to use Zapier than building a server by owrselves. The flow is like the below

  1. Set a trigger which fires on I send a message on a specific channel on Slack
  2. Get the message content and proxy to the API endponit
  3. Create a new message on the channel as a chatbot.

Image description

Please note that making HTTP requests from Zapier is a limited feature for subscribers. However, you can try the function for 7 days even with their free plan.


That's basically it. Easy right? I hope it makes your life easier!


Build An API in Google Sheets and Google Apps Script