Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- .env +2 -0
- app.py +53 -0
- requirements.txt +4 -0
.env
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
OPENAI_API_KEY = ""
|
2 |
+
|
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Conversational Q&A Chatbot
|
2 |
+
# Where it helps to remember the message from the previous message
|
3 |
+
from langchain_community.llms.openai import OpenAI
|
4 |
+
import streamlit as st
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
from langchain.schema import HumanMessage, AIMessage, SystemMessage
|
7 |
+
from langchain.chat_models import ChatOpenAI
|
8 |
+
import os
|
9 |
+
|
10 |
+
load_dotenv() # load the API key from the environment file
|
11 |
+
|
12 |
+
# streamlit
|
13 |
+
st.set_page_config(layout="wide", page_title="Conversational Q&A Chatbot")
|
14 |
+
st.header("Hey, lets Chat")
|
15 |
+
|
16 |
+
|
17 |
+
chat = ChatOpenAI(openai_api_key=os.getenv("OPENAI_API_KEY"),
|
18 |
+
temperature = 0.9)
|
19 |
+
|
20 |
+
print("API Key:", os.getenv("OPENAI_API_KEY"))
|
21 |
+
|
22 |
+
|
23 |
+
if 'flowmessages' not in st.session_state:
|
24 |
+
st.session_state['flowmessages'] = [
|
25 |
+
SystemMessage(content= "You are a comedian AI assistant")]
|
26 |
+
|
27 |
+
def get_chatmodel_response(question):
|
28 |
+
st.session_state['flowmessages'].append(HumanMessage(content=question))
|
29 |
+
answer = chat(st.session_state['flowmessages'])
|
30 |
+
st.session_state['flowmessages'].append(AIMessage(content=answer.content))
|
31 |
+
return answer.content
|
32 |
+
|
33 |
+
input = st.text_input("Input", key="input" )
|
34 |
+
response = get_chatmodel_response(input)
|
35 |
+
|
36 |
+
submit = st.button("Ask your Question")
|
37 |
+
|
38 |
+
# If button is clicked
|
39 |
+
if submit:
|
40 |
+
st.subheader("Your answer is")
|
41 |
+
st.write(response)
|
42 |
+
|
43 |
+
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
|
48 |
+
|
49 |
+
|
50 |
+
|
51 |
+
|
52 |
+
|
53 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
openai==1.12.0
|
2 |
+
streamlit==1.31.0
|
3 |
+
langchain==0.1.6
|
4 |
+
python-dotenv==1.0.1 # helps to create and upload the variable with respect to our applications
|