TestChatBot / app.py
vince1313's picture
update app.py
892e5b7 verified
Raw History Blame Contribute Delete
1.7 kB
import os
# Streamlit link: https://streamlit.io/
# Streamlit Docs link: https://docs.streamlit.io/
import streamlit as st
from openai import OpenAI
# Adding dotenv package: https://pypi.org/project/python-dotenv/
# pip install python-dotenv
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Set the OpenAI API key from .env file
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# Settings for the OpenAI model
settings = {
"model": "gpt-4o",
"temperature": 0,
"max_tokens": 100,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0
}
def get_openai_response(user_message):
response = client.chat.completions.create(
model=settings["model"],
messages=[
{
"role": "system",
# "content": "You are a helpful assistant in real-estate."
"content": "You are a helpful bot, you always provide programming solutions in python in German, English or Spanish",
},
{
"role": "user",
"content": user_message,
},
],
temperature=settings["temperature"])
return response.choices[0].message.content
# Streamlit App
st.title("Chat with TechMaloo ChatBot") # Defining the app title in streamlit
user_message = st.text_input("Enter your message:")
if st.button("Send"):
if user_message:
with st.spinner("Generating response..."):
try:
response = get_openai_response(user_message)
st.write(response)
except Exception as e:
st.write(f"Error: {e}")
else:
st.write("Please enter a message.")