Part 5 - Frontend Flourish: Craft Immersive AI Experiences Using Streamlit

Intro

As we approach the finale, we shift our focus to the frontend. Discover how Streamlit, the dynamic Python library, can breathe life into your AI applications. Build an engaging user interface that brings vector-powered insights to life, visually enhancing user interactions and experiences. Unlock the potential to captivate users with visually appealing dashboards that showcase the transformative impact of vector storage.

The Streamlit code on GitHub.

Understanding the Scenario


Imagine you’re tasked with creating a chatbot that helps users retrieve relevant information from a knowledge base. We’ll integrate Azure Cognitive Search for document retrieval and OpenAI for natural language understanding to achieve this. The chatbot will be presented using a Streamlit web application, providing an interactive interface for users to ask questions and receive instant responses.  The backend has already been covered in the previous sections.

Setting Up the Environment

Before we dive into the code, let’s make sure our development environment is set up correctly:

  • Install Streamlit using `pip install streamlit`.
  • Ensure you have an Azure subscription and access to Azure Cognitive Search.
  • Obtain API keys and endpoint URLs for both Azure Cognitive Search and OpenAI.

Key Components Explained

The code provided in this part comprises several key functions and elements. Let’s break them down:

Displaying Company Logo and Welcome Text

The add_company_logo_and_welcome_text() function is responsible for showcasing the company logo and a warm welcome message at the top of the web application. This function uses CSS styling to center the logo and applies the Streamlit write() and markdown() methods to display the welcome message.

Image to Base64 Conversion

The image_to_base64() function converts an image (in this case, the company logo) to its base64 representation. This conversion is essential for embedding the image in the HTML code.

Chatbot Interaction

The askdocuments() function facilitates communication between the chatbot and external APIs. It sends a user’s question to the API, retrieves and processes the response, and returns the answer. This function actually calls the backend REST API which was explained in the previous part 4.

Streamlit Web Application

The main() function sets up the Streamlit web application. It invokes the add_company_logo_and_welcome_text() function to display the company logo and welcome message. Users can select different indexes, input their questions, and receive responses from the chatbot backend we developed in previous sections.

Building the Streamlit Application

The heart of our solution lies in the Streamlit web application. Users can interact with the chatbot through a simple and intuitive interface. Here’s a breakdown of how the application works:
Users are presented with a selection of indexes to choose from, enabling them to target specific domains or topics.
The sidebar allows users to input their Azure Cognitive Search and OpenAI credentials.
Upon submitting a question, the chatbot queries the APIs and returns responses, displayed as chat messages.
Users can engage in a back-and-forth conversation with the chatbot, asking follow-up questions and receiving immediate answers.

The full code can be found here

Conclusion

In this blog post, we’ve explored the process of building an interactive AI-powered chatbot using Streamlit, Azure Cognitive Search, and OpenAI. By combining these technologies, we’ve created a user-friendly interface that enables users to access information effortlessly. The chatbot’s ability to understand natural language and retrieve relevant documents from a knowledge base demonstrates the power of AI in enhancing user experiences.

The code provided is a foundation for creating customized chatbot applications. As you continue to explore the capabilities of Streamlit, Azure Cognitive Search, and OpenAI, you’ll be able to develop chatbots that cater to specific industries, domains, and user needs.

Feel free to modify and expand upon the code to suit your requirements, and let your creativity shine as you embark on your journey to create intelligent and engaging chatbots. Happy coding!

Want to know more?

This insight is part of a series where we go through the necessary steps to create and optimize Chat & AI Applications.

Below, you can find the full overview and the links to the different parts of the series:

If you want to get started with creating your own AI-powered chatbot, contact us