File size: 1,227 Bytes
6ef4166
60d9461
6ef4166
4d90cc8
60d9461
6ef4166
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
## Q&A Chatbot
#from langchain import OpenAI  # Updated import for OpenAI
from dotenv import load_dotenv
from langchain.llms import OpenAI
from langchain_community.chat_models import ChatOpenAI

load_dotenv()  # take environment variables from .env.

import streamlit as st
import os
import openai  # Import the OpenAI library

# Set the OpenAI API key globally
openai.api_key = os.getenv("OPENAI_API_KEY")
if not openai.api_key:
    raise ValueError("OpenAI API key is not set. Please set the OPENAI_API_KEY environment variable.")

# Function to load OpenAI model and get response
def get_openai_response(question):
    # Create an instance of the OpenAI model
    llm = OpenAI(model_name="gpt-3.5-turbo", temperature=0.5)
    response = llm(question)
    return response

# Initialize our Streamlit app
st.set_page_config(page_title="Q&A Demo")

st.header("Langchain Application")

input_text = st.text_input("Input:", key="input")
submit = st.button("Ask the question")

# If ask button is clicked
if submit and input_text:
    try:
        response = get_openai_response(input_text)
        st.subheader("The Response is:")
        st.write(response)
    except Exception as e:
        st.error(f"An error occurred: {e}")