Skip to main content

How to create a chat interface for your LLM with Streamlit

Published 3 min readMarkdown

In an age where AI is highly advanced, and with tools like ChatGPT and DeepSeek, privacy and accessibility of models are major concerns. Ollama simplifies this by allowing you to easily download the model and run it from your terminal. However, using the terminal isn't easy for everyone, especially with long prompts. This is where Streamlit comes in handy! With just a few lines of code, you can create a simple UI for your model. In this article, I will show you how to set up your UI and connect it to your Ollama server.

  1. Create a virtual environment using pip or conda. It is advisable to run your scripts in virtual environments to avoid altering your already installed packages. Next, activate your environment.

  2. Install Streamlit: In your virtual environment, run pip install streamlit.

  3. Set up your chat interface. The chat_input UI component of Streamlit will be used to enter our prompts for the chatbot. We will use session state to store the messages. This input will have an 'on_submit' event listener, which will prompt the model when you submit your prompt.

     import streamlit as st
     st.chat_input("Say something...", on_submit=handle_change, key='prompt')
    

    The handle_change function takes the prompt and makes a request to the model.

     def handle_change():
         prompt = st.session_state.prompt
    
         if(prompt):
             response = prompt_model(prompt)
             st.session_state.response = response
    

    The result of the response is saved to the session_state along with your prompt. This session_state is then displayed on the screen using Streamlit’s chat_message UI component.

    The chat_message component allows you to create your UI in the format of a chat application for msg in st.session_state.messages:

     for msg in st.session_state.messages:  
         with st.chat_message(msg["role"]):  
             st.write(msg["content"])
    

    Finally, we conditionally add the messages to the session_state if there is a prompt:

     if st.session_state.prompt:  
         prompt = st.session_state.prompt
         st.session_state.messages.append({"role": "user", "content": prompt})  
         with st.chat_message("user"):  
             st.write(prompt)  
    
     if st.session_state.response:
         bot_response = st.session_state.response  
         st.session_state.messages.append({"role": "bot", "content": bot_response})  
         with st.chat_message("bot"):  
             st.write(bot_response)
    

    Part two: Setup Ollama and your script for the request.

    Download and install Ollama from their official website.

    Select a model you would like to work with. Keep in mind that your model should not be too heavy for your PC’s resources. The model details are available there. Install the model by running the command: ollama pull <model-name>

    Start the Ollama server: Run ollama serve.

    Finally, create a script that will make a request to the Ollama server and fetch a response. This script is what is imported into our Streamlit app for model prompting.

     import requests
    
     def prompt_model(prompt='what day is it today?'):
         response_list = []
    
         url = 'http://localhost:11434/api/generate'
         data = {
             "model": "llama3.2:1b", # this is what I downloaded
             "prompt": prompt
         }
         headers = {'Content-Type': 'application/json'}
         response = requests.post(url, data=json.dumps(data), headers=headers, stream=True)
         try:
             for line in response.iter_lines():
                 if line:
                     decoded_line = json.loads(line.decode('utf-8'))
                     response_list.append(decoded_line['response'])
             return ' '.join(response_list)
    
         finally:
             response.close()
    

    Finally, your simple chat UI is ready to use!

The link to the repo: https://github.com/Faith-Nchifor/ollama_streamlit_chat_ui

Ollama website: https://ollama.com/

I will like to know your thoughts!

No comments yet. Be the first to comment.