-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamlitbot.py
61 lines (49 loc) · 1.98 KB
/
streamlitbot.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
import streamlit as st
import os
import openai
import json
from llama_index import GPTSimpleVectorIndex
key = "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"
api_key = os.environ["OPENAI_API_KEY"] = key
index = GPTSimpleVectorIndex.load_from_disk("TomalinLE_HorvathS_SinclairDA.JSON")
class Chatbot:
def __init__(self, api_key, index):
self.index = index
openai.api_key = api_key
self.chat_history = []
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}"
response = index.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, filename):
try:
with open(filename, 'r') as f:
self.chat_history = json.load(f)
except FileNotFoundError:
pass
def save_chat_history(self, filename):
with open(filename, 'w') as f:
json.dump(self.chat_history, f)
def main():
# Set up the chatbot
chatbot = Chatbot(api_key, index)
# Load chat history from file (if it exists)
chat_history_file = "chat_history.json"
chatbot.load_chat_history(chat_history_file)
# Set up the Streamlit UI
st.set_page_config(page_title="GeneBot", page_icon=":robot_face:")
st.title("GeneBot")
# Define the chat UI
st.write("Welcome to GeneBot! Please enter your message below:")
user_input = st.text_input("User:")
if st.button("Send"):
message = chatbot.generate_response(user_input)
st.write(f"{message['role']}: {message['content']}")
# Save chat history to file
chatbot.save_chat_history(chat_history_file)
if __name__ == "__main__":
main()