File size: 1,239 Bytes
595a71e
9ca752a
595a71e
e626f8a
 
bac4727
e626f8a
595a71e
 
bac4727
9ca752a
595a71e
 
bac4727
595a71e
9ca752a
595a71e
9ca752a
bac4727
 
9ca752a
595a71e
fd36045
 
595a71e
 
 
 
bac4727
595a71e
bac4727
 
595a71e
bac4727
 
 
 
 
595a71e
bac4727
 
 
 
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
import streamlit as st
from openai import OpenAI
import os
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

# Set up OpenAI API key
api_key = os.getenv("OPENAI_API_KEY")  # Make sure to have your OpenAI API key in .env
client = OpenAI(api_key=api_key)

# Function to query OpenAI
def query_openai(question, model="gpt-4o-mini"):
    try:
        response = client.chat.completions.create(
            model=model,
            messages=[
                {"role": "system", "content": "You are a helpful assistant. Please answer in 30 words"},
                {"role": "user", "content": question}
            ]
        )
        # Correct way to access message content
        return response.choices[0].message.content
    except Exception as e:
        return f"Error: {e}"

# Streamlit app
st.title("Simple Q&A Application")

# User input for asking questions
question = st.text_input("Ask your question:")

# Handle user input and display the answer
if st.button("Get Answer"):
    if question.strip():
        # Get answer from OpenAI API
        answer = query_openai(question)

        # Display the answer
        st.write(f"**Answer:** {answer}")
    else:
        st.warning("Please enter a question.")