teddyllm commited on
Commit
fe57ff5
1 Parent(s): 18d044a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +119 -0
app.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+
4
+ from langchain_community.vectorstores import FAISS
5
+ from langchain_nvidia_ai_endpoints import NVIDIAEmbeddings
6
+ from langchain.retrievers import ContextualCompressionRetriever
7
+ from langchain.retrievers.document_compressors import FlashrankRerank
8
+ from langchain_core.output_parsers import StrOutputParser
9
+ from langchain_core.runnables import RunnablePassthrough
10
+ from langchain_core.prompts import ChatPromptTemplate
11
+ from langchain_nvidia_ai_endpoints import ChatNVIDIA
12
+
13
+
14
+ def format_docs(docs):
15
+ print("-------- Documents ------------")
16
+ print(docs)
17
+ return "\n\n".join(doc.page_content for doc in docs)
18
+
19
+
20
+ embeddings = NVIDIAEmbeddings(model="nvidia/nv-embedqa-mistral-7b-v2")
21
+ db = FAISS.load_local("app/faiss_index", embeddings, allow_dangerous_deserialization=True)
22
+ retriever = db.as_retriever()
23
+ compressor = FlashrankRerank()
24
+ compression_retriever = ContextualCompressionRetriever(
25
+ base_compressor=compressor, base_retriever=retriever
26
+ )
27
+
28
+ st.title("KCE Chatbot")
29
+ with st.expander("Disclaimer", icon="ℹ️"):
30
+ st.info("""
31
+ We appreciate your engagement with our chatbot! We hope this chatbot can help you with the questions you have regarding with the KCE company.
32
+ This chatbot is a demonstration preview. While the system is designed to provide helpful and informative responses by retrieving and generating relevant information, it is important to note the following:
33
+ 1. Potential for Inaccuracies: The chatbot may sometimes produce incorrect or misleading information. The responses generated by the LLM are based on patterns in the data it has been trained on and the information retrieved, which might not always be accurate or up-to-date.
34
+ 2. Hallucinations: The LLM might generate responses that seem plausible but are entirely fabricated. These "hallucinations" are a known limitation of current LLM technology and can occur despite the retrieval mechanism.\n
35
+ By interacting with this chatbot, you acknowledge and accept these limitations and agree to use the information provided responsibly.
36
+ """)
37
+
38
+ models_dict = {
39
+ "meta/llama-3.1-405b": "meta/llama-3.1-405b-instruct",
40
+ "meta/llama-3.1-70b": "meta/llama-3.1-70b-instruct",
41
+ "meta/llama3.1-8b": "meta/llama-3.1-8b-instruct",
42
+ "google/gemma-2-27b": "google/gemma-2-27b-it",
43
+ "google/gemma-7b": "google/gemma-7b",
44
+ "microsoft/phi-3-mini-128k": "microsoft/phi-3-mini-128k-instruct",
45
+ "microsoft/phi-3-medium-4k": "microsoft/phi-3-medium-4k-instruct"
46
+ }
47
+
48
+
49
+ # openai_api_key = st.sidebar.text_input("OpenAI API Key", type="password")
50
+ model = st.sidebar.selectbox(
51
+ "Choose model",
52
+ tuple(models_dict.keys()),
53
+ label_visibility="visible",
54
+ )
55
+ st.sidebar.write(f"Selected model: {model}")
56
+
57
+
58
+
59
+ def response_generator(message):
60
+ llm = ChatNVIDIA(model=models_dict[model])
61
+ prompt = ChatPromptTemplate.from_messages([
62
+ ('system',
63
+ "You are a KCE chatbot, and you are assisting customers with the inquires about the company."
64
+ "Answer the questions witht the provided context. Do not include based on the context or based on the documents in your answer."
65
+ "Remember that your job is to represent KCE company."
66
+ "Please say you do not know if you do not know or cannot find the information needed."
67
+ "\n Question: {question} \nContext: {context}"),
68
+ ('user', "{question}")
69
+ ])
70
+
71
+ rag_chain = (
72
+ {"context": retriever | format_docs, "question": RunnablePassthrough()}
73
+ | prompt
74
+ | llm
75
+ | StrOutputParser()
76
+ )
77
+ # response = f"Response to: {message}"
78
+ # for word in response.split():
79
+ # yield word + " "
80
+ # time.sleep(0.5)
81
+ partial_message=""
82
+ for chunk in rag_chain.stream(message):
83
+ # partial_message = partial_message + chunk
84
+ yield partial_message + chunk
85
+ # response = random.choice(
86
+ # [
87
+ # 'Hello there! How can I asist you today?',
88
+ # 'Hi, human! Is there anything I can help you with?',
89
+ # 'Do you need any help?'
90
+ # ]
91
+ # )
92
+
93
+ # for word in response.split():
94
+ # yield word + " "
95
+ # time.sleep(0.05)
96
+
97
+
98
+ # Initialize chat history
99
+ if "messages" not in st.session_state:
100
+ st.session_state.messages = []
101
+
102
+ # Display chat messages from history on app rerun
103
+ for message in st.session_state.messages:
104
+ with st.chat_message(message["role"]):
105
+ st.markdown(message["content"])
106
+
107
+ # Accept user input
108
+ if prompt := st.chat_input("Please type your question here"):
109
+ # Add user message to chat history
110
+ st.session_state.messages.append({"role": "user", "content": prompt})
111
+ # Display user message in chat message container
112
+ with st.chat_message("user"):
113
+ st.markdown(prompt)
114
+
115
+ # Display assistant response in chat message container
116
+ with st.chat_message("assistant"):
117
+ response = st.write_stream(response_generator(prompt))
118
+ # Add assistant response to chat history
119
+ st.session_state.messages.append({"role": "assistant", "content": response})