-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
95 lines (73 loc) · 2.88 KB
/
app.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
import json
import openai
import streamlit as st
from pathlib import Path
from secret_key import OPENAI_API_KEY
from llama_index.core import StorageContext, load_index_from_storage
# Set OpenAI API key
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
# Initialize index path
index_path = Path('index')
# rebuild storage context and load index
storage_context = StorageContext.from_defaults(persist_dir=index_path)
index = load_index_from_storage(storage_context)
class Chatbot:
def __init__(self, api_key, index, user_id):
self.index = index
openai.api_key = api_key
self.user_id = user_id
self.chat_history = []
self.filename = f"{self.user_id}_history.json"
def generate_response(self, user_input):
prompt = "\n".join([f"{message['role']}: {message['content']}"
for message in self.chat_history[-5:]])
prompt += f"\nUser: {user_input}"
query_engine = index.as_query_engine()
response = query_engine.query(user_input)
message = {"role": "assistant", "content": response.response}
self.chat_history.append({"role": "user", "content": user_input})
self.chat_history.append(message)
return message
def load_chat_history(self):
try:
with open(self.filename, 'r') as f:
self.chat_history = json.load(f)
except FileNotFoundError:
pass
def save_chat_history(self):
with open(self.filename, 'w') as f:
json.dump(self.chat_history, f)
def main():
st.title("FAQ Chatbot")
# User ID
user_id = st.text_input("Your Name:")
# Check if user ID is provided
if user_id:
# Create chatbot instance for the user
bot = Chatbot(os.environ["OPENAI_API_KEY"], index, user_id)
# Load chat history
bot.load_chat_history()
# Display chat history
for message in bot.chat_history[-6:]:
st.write(f"{message['role']}: {message['content']}")
# User input
user_input = st.text_input("Type your questions here: ")
# Generate response
if user_input:
if user_input.lower() in ["bye", "goodbye"]:
st.write(f"{user_id}: {user_input}")
st.write(f"Bot: Goodbye!")
else:
bot_response = bot.generate_response(user_input)
bot_response_content = bot_response['content']
st.write(f"{user_id}: {user_input}")
st.write(f"Bot: {bot_response_content}")
# Appending user input and bot response to chat history
bot.chat_history.append(
{"role": "user", "content": user_input})
bot.chat_history.append(
{"role": "assistant", "content": bot_response_content})
bot.save_chat_history()
if __name__ == "__main__":
main()