From d1ef966e651046083732dff330faefbddad67cd8 Mon Sep 17 00:00:00 2001 From: abhishekbhakat Date: Tue, 25 Mar 2025 17:12:45 +0000 Subject: [PATCH] Add initial project structure with .gitignore, pyproject.toml, and main application files --- .gitignore | 17 +++++++++++++++++ pyproject.toml | 15 +++++++++++++++ src/app.py | 39 +++++++++++++++++++++++++++++++++++++++ src/openai_client.py | 23 +++++++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 .gitignore create mode 100644 pyproject.toml create mode 100644 src/app.py create mode 100644 src/openai_client.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2c4f6ff --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +# Python +__pycache__/ +*.py[cod] +*$py.class + +# Virtual environment +env/ + +# Configuration +config/config.ini + +# IDE +.vscode/ +.idea/ + +# Streamlit +.streamlit/ diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..e24a5b3 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,15 @@ +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[project] +name = "streamlit-chat-app" +version = "0.1.0" +dependencies = [ + "streamlit", + "python-dotenv", + "openai" +] + +[tool.hatch.build.targets.wheel] +packages = ["src"] diff --git a/src/app.py b/src/app.py new file mode 100644 index 0000000..429f908 --- /dev/null +++ b/src/app.py @@ -0,0 +1,39 @@ +import streamlit as st +from openai_client import OpenAIClient + +def init_session_state(): + if "messages" not in st.session_state: + st.session_state.messages = [] + +def display_chat_messages(): + for message in st.session_state.messages: + with st.chat_message(message["role"]): + st.markdown(message["content"]) + +def handle_user_input(): + if prompt := st.chat_input("Type your message..."): + st.session_state.messages.append({"role": "user", "content": prompt}) + with st.chat_message("user"): + st.markdown(prompt) + + with st.chat_message("assistant"): + response_placeholder = st.empty() + full_response = "" + + client = OpenAIClient() + for chunk in client.get_chat_response(st.session_state.messages): + if chunk.choices[0].delta.content: + full_response += chunk.choices[0].delta.content + response_placeholder.markdown(full_response + "▌") + + response_placeholder.markdown(full_response) + st.session_state.messages.append({"role": "assistant", "content": full_response}) + +def main(): + st.title("Streamlit Chat App") + init_session_state() + display_chat_messages() + handle_user_input() + +if __name__ == "__main__": + main() diff --git a/src/openai_client.py b/src/openai_client.py new file mode 100644 index 0000000..80b4a3d --- /dev/null +++ b/src/openai_client.py @@ -0,0 +1,23 @@ +import os +import configparser +from openai import OpenAI + +class OpenAIClient: + def __init__(self): + self.config = configparser.ConfigParser() + self.config.read('config/config.ini') + self.client = OpenAI( + api_key=self.config['openai']['api_key'], + base_url=self.config['openai']['base_url'] + ) + + def get_chat_response(self, messages): + try: + response = self.client.chat.completions.create( + model=self.config['openai']['model'], + messages=messages, + stream=True + ) + return response + except Exception as e: + raise Exception(f"OpenAI API error: {str(e)}")