binqiangliu commited on
Commit
878d57a
1 Parent(s): 4503441

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py CHANGED
@@ -85,3 +85,56 @@ vectordb = Chroma.from_documents(documents=all_splits, embedding=embeddings, per
85
 
86
  # specify the retriever
87
  retriever = vectordb.as_retriever()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
  # specify the retriever
87
  retriever = vectordb.as_retriever()
88
+
89
+ # build huggingface pipeline for using zephyr-7b-alpha
90
+ pipeline = pipeline(
91
+ "text-generation",
92
+ model=model,
93
+ tokenizer=tokenizer,
94
+ use_cache=True,
95
+ device_map="auto",
96
+ max_length=2048,
97
+ do_sample=True,
98
+ top_k=5,
99
+ num_return_sequences=1,
100
+ eos_token_id=tokenizer.eos_token_id,
101
+ pad_token_id=tokenizer.eos_token_id,
102
+ )
103
+
104
+ # specify the llm
105
+ llm = HuggingFacePipeline(pipeline=pipeline)
106
+
107
+ # build conversational retrieval chain with memory (rag) using langchain
108
+ def create_conversation(query: str, chat_history: list) -> tuple:
109
+ try:
110
+
111
+ memory = ConversationBufferMemory(
112
+ memory_key='chat_history',
113
+ return_messages=False
114
+ )
115
+ qa_chain = ConversationalRetrievalChain.from_llm(
116
+ llm=llm,
117
+ retriever=retriever,
118
+ memory=memory,
119
+ get_chat_history=lambda h: h,
120
+ )
121
+
122
+ result = qa_chain({'question': query, 'chat_history': chat_history})
123
+ chat_history.append((query, result['answer']))
124
+ return '', chat_history
125
+
126
+
127
+ except Exception as e:
128
+ chat_history.append((query, e))
129
+ return '', chat_history
130
+
131
+ # build gradio ui
132
+ with gr.Blocks() as demo:
133
+
134
+ chatbot = gr.Chatbot(label='Chat with your data (Zephyr 7B Alpha)')
135
+ msg = gr.Textbox()
136
+ clear = gr.ClearButton([msg, chatbot])
137
+
138
+ msg.submit(create_conversation, [msg, chatbot], [msg, chatbot])
139
+
140
+ demo.launch()