-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.py
80 lines (62 loc) · 2.19 KB
/
chat.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
# File: LangChainchatOpenAI.py
# Author: Denys L
# Date: October 8, 2023
# Description:
import streamlit as st
from streamlit_extras.stateful_chat import chat, add_message
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationalRetrievalChain
from langchain.vectorstores import Qdrant
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.embeddings import HuggingFaceInstructEmbeddings
from dotenv import load_dotenv
import qdrant_client
import os
import time
def get_qdrant_client():
return qdrant_client.QdrantClient(
url=os.getenv("QDRANT_HOST"),
port=os.getenv("QDRANT_PORT"),
api_key=os.getenv("QDRANT_API_KEY"),
https=True)
def get_vector_store():
client = get_qdrant_client()
embeddings = OpenAIEmbeddings()
# embeddings = HuggingFaceInstructEmbeddings(model_name="hkunlp/instructor-xl")
return Qdrant(
client=client,
collection_name=os.getenv("QDRANT_COLLECTION_NAME"),
embeddings=embeddings,
)
def get_conversation_chain():
llm = ChatOpenAI()
memory = ConversationBufferMemory(
memory_key='chat_history', return_messages=True)
conversation_chain = ConversationalRetrievalChain.from_llm(
llm=llm,
retriever=get_vector_store().as_retriever(),
memory=memory
)
return conversation_chain
def main():
load_dotenv()
st.set_page_config(
page_title="Chat with multiple Book's",
page_icon=":books:",
layout="centered",
)
st.header("Ask your remote database 💬")
if "conversation" not in st.session_state:
st.session_state.conversation = get_conversation_chain()
with chat(key="my_chat"):
if prompt := st.chat_input():
add_message("user", prompt, avatar="🧑💻")
response = st.session_state.conversation({'question': prompt})
def stream_echo():
for word in response['answer'].split():
yield word + " "
time.sleep(0.10)
add_message("assistant", stream_echo, avatar="🤖")
if __name__ == '__main__':
main()