Spaces:
Sleeping
Sleeping
File size: 1,703 Bytes
f0bc472 892e5b7 f0bc472 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | 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.")
|