red1xe commited on
Commit
5f6b2a6
1 Parent(s): 4b3013a

similarity search

Browse files
Files changed (1) hide show
  1. app.py +15 -15
app.py CHANGED
@@ -28,19 +28,19 @@ if files:
28
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150)
29
  chunks = text_splitter.split_text(full_text)
30
  embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
31
- vectorstore = FAISS.from_texts(chunks, embeddings)
32
  memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True,)
33
- llm = AutoModelForCausalLM.from_pretrained("red1xe/Llama-2-7B-codeGPT")
34
- chain = RetrievalQA.from_llm(
35
- llm=llm,
36
- retriever=vectorstore.as_retriever(),
37
- memory=memory,
38
- )
39
- st.success("Done!")
40
- st.header("Start Chat")
41
- st.subheader("Ask a question")
42
- question = st.text_input("Question")
43
- if st.button("Ask"):
44
- with st.spinner("Thinking..."):
45
- answer = chain.query(question)
46
- st.success(answer)
 
28
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150)
29
  chunks = text_splitter.split_text(full_text)
30
  embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
31
+ db = FAISS.from_texts(chunks, embeddings)
32
  memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True,)
33
+
34
+ def retrieve_info(query):
35
+ similar_response = db.similarity_search(query, k=3)
36
+ page_contents_array = [doc.page_contents for doc in similar_response]
37
+ page_contents = " ".join(page_contents_array)
38
+ return page_contents
39
+
40
+ st.header("Chatbot")
41
+ st.subheader("Ask a question")
42
+ question = st.text_input("Question")
43
+ if question:
44
+ st.subheader("Answer")
45
+ answer = retrieve_info(question)
46
+ st.write(answer)