Tech Thoughts

A GenAI Chatbot to enhance User Experience

03.06.2024


Chatbot conversation example

A client approached us with a request to build an intelligent chatbot for their website, aimed at efficiently answering user questions related to the site’s public content.


The first step in building the chatbot was to extract the website content. A custom Python script was written using the BeautifulSoup library to navigate the site’s menu and extract the needed content from all linked pages. The extracted content was compiled into a single PDF document to serve as the knowledge base for the next step.


Using Llama Index and OpenAI APIs, a Python script was created to:


  1. Split the PDF document containing the knowledge base into smaller pieces;
  2. Map each piece into a latent space using OpenAI embedding models;
  3. Take user questions as input and map them into the same latent space;
  4. Extract the PDF pieces whose vectors are closest to the question vector, indicating they cover similar topics;
  5. Pass the user's question and the extracted context (PDF pieces) to OpenAI API, including a system message to guide the output.

The chosen model generates a coherent answer to the user's question using the provided context (relevant PDF pieces). This answer is then returned to the user.


To make the demo more visually appealing, Streamlit was used, a library that makes it easy to create interactive graphical interfaces that communicate with the previously written Python script. Special attention was given to managing the chat interactively, with response tokens displayed as they are received from OpenAI and the conversation history always visible on screen (using Streamlit's session_state).


In conclusion, the combination of BeautifulSoup, Llama Index, OpenAI API, and Streamlit allowed for the creation of a sophisticated and user-friendly chatbot. This project showcased the ability to integrate various tools and technologies to build an effective solution. The demo is currently undergoing approval from the client, further enhancements and expansions of this chatbot's capabilities will be considered based on their feedback.

GPT Scraping OpenAI API Generative AI Chatbot OpenAI RAG GenAI Generation Llama Index Streamlit Conversational AI AI BeautifulSoup