Make AI answer the questions using the context from your website

Having a chat with AI is possible via the OpenAI Integration using this approach: Complete chat with OpenAI but how do you provide the scope of the conversation so that the user is chatting with your website, instead of the general knowledge?

This use case can be implemented by providing the AI model additional context when asking it to complete the chat. While we’re working on a more sophisticated approach that includes embeddings, and a vector database, it is possible to mimic the chat completion with the retrieval plugin enabled. We can use either the existing website or knowledge base search functionality to find the relevant content that we can add to the final prompt. The proposed flow can be described in such a diagram:

Given the content is relevant, even with the minimum added amount of it, this improves the completion result dramatically, as the model just needs a little direction.

Here are the steps to take to implement this approach.

Step 1. Create and set up an OpenAI integration, as described here.

Step 2. Add bot variable of type Chat Completion:

{
        "model": "gpt-4-32k",
        "messages": [
          {
            "role": "system",
            "content": "You are a helpful assistant for a Quriobot chatbot platform"
          },
          {
            "role": "system",
            "content": "Note that you need to help our customer with a question regarding Quriobot chatbot platform, and if you cannot provide such help, just say so"
          },
          {
            "role": "system",
            "content": "Consider such results from the Quriobot knowledge base for the user prompt:"
          },
          {
            "role": "system",
            "content": "{{qb_search}}"
          },
          {
            "role": "user",
            "content": "{{chat_prompt}}"
          }
        ]
}

This variable will create a response from a bot based on the user input, which is represented by the chat_prompt variable, and the search results, represented by the qb_search variable

Step 3. Add bot variable of type Script (Developer Mode has to be enabled):

function(callback, variables) {
 var searchInput = variables.chat_prompt.value;
 var url = 'https://api.academy.quriobot.com/0.0.1/frontend/knowledge_bases/search'
 quriobot.ajax(url, function(responseText) {
   var response = JSON.parse(responseText);
   var text = response.items && response.items.map(function(item){
     return item.text
   }).join('\n') || ''
   callback(text)
 }, {
   knowledge_base: '69nJaZmpx9mpvxPR',
   search: searchInput,
   page_size: 25
 }, {'Organisation': 'Oa01M4EW8bxN9J75'})
}

This variable will call the search endpoint in the knowledge base and then concatenate the results into one string. This variable uses the previously mentioned user input variable chat_prompt to find the related content. In our example, it uses our knowledge base search functionality, but it can be your own website’s search endpoint.

If your website doesn’t have such a search endpoint, you can use Google Site Search API to find the related content. The Google Site Search usage is also described in this article. Note that for this use case, the API key has to have NO application restriction on the website usage, so the application restriction has to be turned OFF. In the case of using Google Site Search, getting the related content will be more complicated, as Google doesn’t return the text content of the found result directly, so we have to fetch it.

Add a script variable:

function(callback, variables) {
  var searchInput = variables.chat_prompt.value;
  var url = new quriobot.URL("https://www.googleapis.com/customsearch/v1", true)
  url.query.key = 'AIzaSoTww'
  url.query.cx = '464aef26f14634ae9'
  url.query.q = searchInput
  quriobot.ajax(url.toString(), function(responseText) {
    try {
        var response = JSON.parse(responseText);
        var item = response.items && response.items.length && response.items[0]
        callback(item.link)
    } catch (error) {
        callback('error:' + error)
    }
    //callback(url.toString() + '\n' + responseText)
  })
}

This variable will perform a site search using the user input and get the URL of the first page from the search results

Add another script variable:

function(callback, variables) {
   variables.site_search_link.value(function(value) {
     quriobot.ajax(value, 
     function(responseText) {
      var text = quriobot.htmlToText(
        responseText)
      callback(text.slice(0, 4096))
     }
    )
   })
}

This variable will fetch the found website page, convert HTML to plain text, and get the first 4096 characters of its content (this you can tune for your own needs) If you use the Google Site Search option, then you’ll need to replace the usage of the qb_search variable in Step 2, with the site_search variable.

Step 3. Add a step for the user input:

Note that the code name for this step is chat_prompt - this automatically creates a bot variable with the same name.

Step 4. Add a step response with the chat completion:

This response uses the completed_chat variable created in Step 2.

Edit this page

Categories
Tags
See also