Spaces:
Sleeping
Sleeping
# from dotenv import load_dotenv | |
import streamlit as st | |
import openai | |
import os | |
# Load environment variables | |
# load_dotenv() | |
# Set OpenAI API key | |
# openai.api_key = os.getenv("OPENAI_API_KEY") | |
# Function to get a response from OpenAI | |
def get_openai_response(question): | |
try: | |
response = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo", | |
messages=[ | |
{"role": "system", "content": "You are a helpful assistant."}, | |
{"role": "user", "content": question} | |
], | |
temperature=0.5 | |
) | |
return response['choices'][0]['message']['content'].strip() | |
except Exception as e: | |
return f"An error occurred: {e}" | |
# Initialize Streamlit app | |
st.set_page_config(page_title="Q&A Demo") | |
st.header("Langchain Application") | |
input = st.text_input("Input: ", key="input") | |
submit = st.button("Ask the question") | |
if submit: | |
if input.strip(): | |
response = get_openai_response(input) | |
st.subheader("The Response is") | |
st.write(response) | |
else: | |
st.warning("Please enter a question before submitting.") | |