Spaces:
Sleeping
Sleeping
| # Q&A Chatbot | |
| import openai | |
| from dotenv import load_dotenv | |
| import streamlit as st | |
| import os | |
| # Load the enviroment variables from .env file | |
| load_dotenv() | |
| # Set the API key from environment variable | |
| openai.api_key = os.getenv("OPENAI_API_KEY") | |
| # Function to load OpenAI model to get response | |
| def get_openai_response(prompt): | |
| response = openai.ChatCompletion.create( | |
| model="gpt-3.5-turbo", # You can change this to gpt-4 if you prefer | |
| messages=[ | |
| {"role": "system", "content": "You are a helpful assistant."}, | |
| {"role": "user", "content": prompt} | |
| ] | |
| ) | |
| # Return the assistant's reply | |
| return response['choices'][0]['message']['content'] | |
| # Initialize streamlit page | |
| st.set_page_config(page_title="Q&A Chatbot") | |
| st.header("Langchain Application") | |
| input= st.text_input("Enter your prompt:") | |
| submit=st.button("Submit") | |
| # If button is clicked, get and display the response | |
| if submit: | |
| response = get_openai_response(input) | |
| st.subheader("Response") | |
| st.write(response) | |