With the advent of ChatGPT in November 2022, the history of Chatbots took a turning point, as Google Trends shows from the last 5 years.

https://trends.google.com/

Looking at the past year the interest shows almost a flat trend but anyway at a high level of interest.

https://trends.google.com/

Thanks to ChatGPT, now we can speak to a chatbot that generates text like a human, because we have an AI Chatbot, that has under the hood a Generative AI, powered by Large Language Models like GPT, Mixtral, Llama, Gemma, Claude and so on. What does it mean? These types of models exploit the decoder side of Transformer architecture to predict the next word (token) in a sequence based on patterns of their training data, by the probability distribution of the most likely next word. Easily speaking, these language models are trained on huge amounts of data and in a chatbot context we can speak with them like a human.

So, how to build your chatbot?

Let’s get started!!!

Just keep 3 ingredients: API keys, some lines of Python code and Streamlit, that’s it!!!

On the left side, there is a widget to select a model you prefer, and a space to write the API key to work with the model.

GPT-3.5-turbo -> retrieve your OpenAI Key -> OpenAI PlatformMixtral 8x7B -> retrieve your Mistral Key -> Mistral AI | Frontier AI in your handsLlama-3B-70B -> retrieve your Llama Key -> Llama AI | API (llama-api.com)

On the right side, there is the GUI Interface to talk with the chatbot, thanks to Streamlit which is an open-source framework designed to create interactive web apps with minimal effort using Python.

Here’s the schema of the interface:

There’s a chat input at the bottom of the screen that contains your input text.When you enter a message, it appears as a chat message in the container above with an avatar.The assistant’s responses are streamed and displayed with a default avatar.# Chat input
if prompt := st.chat_input():
if not api_key:
st.info(“Please add your API_KEY to go ahead.”)
st.stop()

# Append user message to session state
st.session_state.messages.append({“role”: “user”, “content”: prompt})
st.chat_message(“user”).write(prompt)

# Client initialization based on selected model
if option == ‘MiXtral 8x7B’:
client = MistralClient(api_key=api_key)
response = client.chat(model=”open-mixtral-8x7b”, messages=st.session_state.messages, stream=True)
elif option == ‘GPT-3.5-turbo’:
client = OpenAI(api_key=api_key)
response = client.chat.completions.create(model=”gpt-3.5-turbo”, messages=st.session_state.messages, stream=True)
elif option == ‘Llama-3-70B’:
client = OpenAI(api_key=api_key,base_url=”https://api.llama-api.com”)
response = client.chat.completions.create(model=”llama3-70b”, messages=st.session_state.messages, stream=True)
else:
st.error(“Selected model is not supported.”)
st.stop()

# Process response and update session state
msg = response.choices[0].message.content
st.session_state.messages.append({“role”: “assistant”, “content”: msg})
st.chat_message(“assistant”).write(msg)

The key components of the above code are:

st.chat_imput(): a function used to type in a user prompt.st.chat_message(): a function used to display messages in the chat interface.st.session_state(): a function used to keep track of the conversation history.Client Initialization: Each “if else” block creates a client object for the corresponding AI model and sends the chat messages to the model’s API. Moreover, I can exploit the same OpenAI client for both ChatGPT 3.5 and LLama 3 models.Response Processing: this snippet’s code is used to retrieve the content of the first message from the AI’s response, append it to the session state messages, and finally display it in the chat.

Let’s try if it works with a general question: “Tell me about Bono Vox”

ChatGPT responseMixtral responseLlama response

I haven’t checked completely the correctness of the answers, but it works.

Then you can run it locally on your browser or you can deploy it on the Streamlit cloud as you prefer.

This project is inspired by a visionary man, Bill Gates, who said last year that everyone will have an AI Personal Assistant within 5 years, I think so and I’ve started to equip myself in this way.

References:

Build a basic LLM chat app — Streamlit Docs

Build your Personal AI Assistant with Streamlit and LLMs was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.

​ Level Up Coding – Medium

about Infinite Loop Digital

We support businesses by identifying requirements and helping clients integrate AI seamlessly into their operations.

Gartner
Gartner Digital Workplace Summit Generative Al

GenAI sessions:

  • 4 Use Cases for Generative AI and ChatGPT in the Digital Workplace
  • How the Power of Generative AI Will Transform Knowledge Management
  • The Perils and Promises of Microsoft 365 Copilot
  • How to Be the Generative AI Champion Your CIO and Organization Need
  • How to Shift Organizational Culture Today to Embrace Generative AI Tomorrow
  • Mitigate the Risks of Generative AI by Enhancing Your Information Governance
  • Cultivate Essential Skills for Collaborating With Artificial Intelligence
  • Ask the Expert: Microsoft 365 Copilot
  • Generative AI Across Digital Workplace Markets
10 – 11 June 2024

London, U.K.