Spaces:
Sleeping
Sleeping
Commit
•
35b843d
1
Parent(s):
27bc40a
Upload 4 files
Browse files- faiss_index/index.faiss +0 -0
- faiss_index/index.pkl +3 -0
- main.py +64 -0
- requirements.txt +12 -0
faiss_index/index.faiss
ADDED
Binary file (230 kB). View file
|
|
faiss_index/index.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:73becf24b1238d656c0f8261be6a0666f422200bc5eabc5ff0d9b46b553f2045
|
3 |
+
size 47382
|
main.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
# from langchain_helper import get_qa_chain, create_vector_db
|
3 |
+
|
4 |
+
|
5 |
+
|
6 |
+
from langchain.vectorstores import FAISS
|
7 |
+
from langchain.llms import GooglePalm
|
8 |
+
from langchain.document_loaders.csv_loader import CSVLoader
|
9 |
+
from langchain.embeddings import HuggingFaceInstructEmbeddings
|
10 |
+
from langchain.prompts import PromptTemplate
|
11 |
+
from langchain.chains import RetrievalQA
|
12 |
+
from dotenv import load_dotenv
|
13 |
+
load_dotenv()
|
14 |
+
import os
|
15 |
+
# get this free api key from https://makersuite.google.com/
|
16 |
+
llm = GooglePalm(google_api_key=os.environ["GOOGLE_API_KEY"], temperature=0.1)
|
17 |
+
|
18 |
+
instructor_embeddings = HuggingFaceInstructEmbeddings(model_name="hkunlp/instructor-large")
|
19 |
+
vectordb_file_path = "faiss_index"
|
20 |
+
|
21 |
+
|
22 |
+
def get_qa_chain():
|
23 |
+
# Load the vector database from the local folder
|
24 |
+
vectordb = FAISS.load_local(vectordb_file_path, instructor_embeddings)
|
25 |
+
|
26 |
+
# Create a retriever for querying the vector database
|
27 |
+
retriever = vectordb.as_retriever(score_threshold=0.7)
|
28 |
+
|
29 |
+
prompt_template = """Given the following context and a question, generate an answer based on this context only.
|
30 |
+
In the answer try to provide as much text as possible from "response" section in the source document context without making much changes.
|
31 |
+
If the answer is not found in the context, kindly state "I don't know." Don't try to make up an answer.
|
32 |
+
|
33 |
+
CONTEXT: {context}
|
34 |
+
|
35 |
+
QUESTION: {question}"""
|
36 |
+
|
37 |
+
PROMPT = PromptTemplate(
|
38 |
+
template=prompt_template, input_variables=["context", "question"]
|
39 |
+
)
|
40 |
+
|
41 |
+
chain = RetrievalQA.from_chain_type(llm=llm,
|
42 |
+
chain_type="stuff",
|
43 |
+
retriever=retriever,
|
44 |
+
input_key="query",
|
45 |
+
return_source_documents=True,
|
46 |
+
chain_type_kwargs={"prompt": PROMPT})
|
47 |
+
|
48 |
+
return chain
|
49 |
+
|
50 |
+
|
51 |
+
|
52 |
+
st.title("Q&A for My Courses ")
|
53 |
+
# btn = st.button("Create Knowledgebase")
|
54 |
+
# if btn:
|
55 |
+
# create_vector_db()
|
56 |
+
|
57 |
+
question = st.text_input("Question: ")
|
58 |
+
|
59 |
+
if question:
|
60 |
+
chain = get_qa_chain()
|
61 |
+
response = chain(question)
|
62 |
+
|
63 |
+
st.header("Answer")
|
64 |
+
st.write(response["result"])
|
requirements.txt
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain==0.0.284
|
2 |
+
python-dotenv==1.0.0
|
3 |
+
streamlit==1.22.0
|
4 |
+
tiktoken==0.4.0
|
5 |
+
faiss-cpu==1.7.4
|
6 |
+
protobuf~=3.19.0
|
7 |
+
huggingface
|
8 |
+
transformers
|
9 |
+
InstructorEmbedding
|
10 |
+
torch
|
11 |
+
sentence_transformers
|
12 |
+
google-generativeai
|