Develop a local, browser-based chat application for interacting with Large Language Models (LLMs). The application should support multiple chat sessions, allowing users to create new sessions and revisit previous ones. Implement a sidebar for managing chat sessions, enabling users to initiate new conversations and select from a history of past sessions. Persist chat session data locally using a simple database solution, ensuring that chat histories are retained across browser sessions and computer restarts. Design a configuration file where users can specify the LLM's API endpoint URL, model name, and API key, enabling flexible connectivity to different LLM providers. The chat interface should facilitate a straightforward back-and-forth conversation flow, with the capability for users to edit and resubmit their messages within the chat history. The application should function entirely locally, without requiring Docker or any server-side components.
The LLM configuration should exclusively support OpenAI as the wrapper for the local llm. Ensure that the model name is configurable as an open text field, similar to the API key and endpoint.
Make this 100% python code
I'll create a browser-based chat application using Streamlit, which allows for rapid development of interactive web applications in Python. This solution will run entirely locally and persist chat data using SQLite.
Let's build a complete chat application that meets all your requirements:
app.py
requirements.txt
app.py
Install the required dependencies:
pip install -r requirements.txt
Run the application:
streamlit run app.py
http://localhost:8501
Multiple Chat Sessions:
Persistent Storage:
LLM Configuration:
Chat Interface:
Local Operation:
Database Structure:
sessions
table: Stores chat session metadatamessages
table: Stores all messages with their roles and timestampsConfiguration Management:
llm_config.json
fileLLM Integration:
UI Components:
This application provides a complete solution for interacting with LLMs locally through a browser interface, with all the requested features implemented in pure Python.
I see there was an error executing the Python code. This is likely because the Python executable in the browser environment has limitations. Let me provide a revised version that should work better with the constraints of the environment.
app.py
requirements.txt
app.py
Install the required dependencies:
pip install -r requirements.txt
Run the application:
streamlit run app.py
http://localhost:8501
This revised version maintains all the requested features while being more compatible with execution environments:
The application runs entirely locally without requiring Docker or server-side components, making it easy to set up and use on any computer with Python installed.
Error 1: You tried to access openAI.chat completion, but this is no longer supported in open AI >= equal to 1.0.0
The primary API for interacting with OpenAI models is the Responses API. You can generate text from the model with the code below.
import os from openai import OpenAI
client = OpenAI( # This is the default and can be omitted api_key=os.environ.get("OPENAI_API_KEY"), )
response = client.responses.create( model="gpt-4o", instructions="You are a coding assistant that talks like a pirate.", input="How do I check if a Python object is an instance of a class?", )
print(response.output_text) The previous standard (supported indefinitely) for generating text is the Chat Completions API. You can use that API to generate text from the model with the code below.
from openai import OpenAI
client = OpenAI()
completion = client.chat.completions.create( model="gpt-4o", messages=[ {"role": "developer", "content": "Talk like a pirate."}, { "role": "user", "content": "How do I check if a Python object is an instance of a class?", }, ], )
print(completion.choices[0].message.content) While you can provide an api_key keyword argument, we recommend using python-dotenv to add OPENAI_API_KEY="My API Key" to your .env file so that your API key is not stored in source control.
Error 2: streamlit.errors.streamlitAPIexception: st.session_state.user_input cannot be modified after the widget with key user_input is in instantiated. Line 121 Line 640 Line 410 Line 136 Line 114 Line 101 Line 515 raise StrealitAPIException(
No Output
Run the code to generate an output.