-
Notifications
You must be signed in to change notification settings - Fork 46
/
chatbotUI.py
53 lines (37 loc) · 1.58 KB
/
chatbotUI.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
import streamlit as st
import os
os.environ["OPENAI_API_KEY"] = "sk-"
import openai
openai.api_key = "sk-"
from llama_index import VectorStoreIndex
from llama_index.vector_stores import ChromaVectorStore
from llama_index.storage.storage_context import StorageContext
import chromadb
db2 = chromadb.PersistentClient(path="./storage/chroma")
chroma_collection = db2.get_or_create_collection("andrew_sleep_db")
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_vector_store(
vector_store=vector_store,
storage_context=storage_context,
)
chat_engine = index.as_chat_engine(chat_mode="condense_question")
st.title("Chat with Andrew")
if "messages" not in st.session_state.keys():
st.session_state.messages = [{"role": "assistant", "content": "How may I help you?"}]
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.write(message["content"])
if prompt := st.chat_input():
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.write(prompt)
def generate_response(prompt_input):
return chat_engine.chat(prompt_input).response
if st.session_state.messages[-1]["role"] != "assistant":
with st.chat_message("assistant"):
with st.spinner("Thinking ... "):
response = generate_response(prompt)
st.write(response)
message = {"role": "assistant", "content": response}
st.session_state.messages.append(message)