Update vector_store_retriever.py
Browse files- vector_store_retriever.py +25 -45
vector_store_retriever.py
CHANGED
@@ -1,59 +1,39 @@
|
|
1 |
import gradio as gr
|
2 |
-
from langchain.document_loaders import DirectoryLoader, PyPDFLoader
|
3 |
from langchain.vectorstores import Chroma
|
4 |
-
from langchain.
|
5 |
from langchain.embeddings import HuggingFaceInstructEmbeddings
|
6 |
-
from langchain.agents import Tool
|
7 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
8 |
-
from langchain.llms import HuggingFacePipeline
|
9 |
-
from transformers import LlamaTokenizer, LlamaForCausalLM, pipeline
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
texts = text_splitter.split_documents(documents)
|
18 |
-
|
19 |
-
# HF Instructor Embeddings
|
20 |
-
instructor_embeddings = HuggingFaceInstructEmbeddings(model_name="hkunlp/instructor-xl", model_kwargs={"device": "cuda"})
|
21 |
-
|
22 |
-
# Embed and store the texts
|
23 |
-
persist_directory = 'db'
|
24 |
-
embedding = instructor_embeddings
|
25 |
-
vectordb = Chroma.from_documents(documents=texts, embedding=embedding, persist_directory=persist_directory)
|
26 |
-
|
27 |
-
# Make a retriever
|
28 |
-
retriever = vectordb.as_retriever(search_kwargs={"k": 3})
|
29 |
-
|
30 |
-
# Setup LLM for text generation
|
31 |
-
tokenizer = LlamaTokenizer.from_pretrained("TheBloke/wizardLM-7B-HF")
|
32 |
-
model = LlamaForCausalLM.from_pretrained("TheBloke/wizardLM-7B-HF", load_in_8bit=True, device_map='auto', torch_dtype=torch.float16, low_cpu_mem_usage=True)
|
33 |
-
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_length=1024, temperature=0, top_p=0.95, repetition_penalty=1.15)
|
34 |
-
local_llm = HuggingFacePipeline(pipeline=pipe)
|
35 |
|
36 |
-
#
|
37 |
-
|
|
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
description = "This tool uses LangChain's RetrievalQA to find relevant answers from a vector store based on a given query."
|
42 |
|
43 |
-
|
44 |
-
|
|
|
45 |
|
46 |
-
def __call__(self, query
|
47 |
-
# Run the query through the
|
48 |
-
|
49 |
-
return
|
50 |
|
51 |
-
# Create the Gradio interface using the
|
52 |
tool = gr.Interface(
|
53 |
-
|
|
|
|
|
54 |
live=True,
|
55 |
-
title="
|
56 |
-
description="This tool
|
57 |
)
|
58 |
|
59 |
# Launch the Gradio interface
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from langchain.vectorstores import Chroma
|
3 |
+
from langchain.document_loaders import PyPDFLoader
|
4 |
from langchain.embeddings import HuggingFaceInstructEmbeddings
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
# Initialize the HuggingFaceInstructEmbeddings
|
7 |
+
hf = HuggingFaceInstructEmbeddings(
|
8 |
+
model_name="hkunlp/instructor-large",
|
9 |
+
embed_instruction="Represent the document for retrieval: ",
|
10 |
+
query_instruction="Represent the query for retrieval: "
|
11 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
# Load and process the PDF files
|
14 |
+
loader = PyPDFLoader('./new_papers/new_papers/', glob="./*.pdf")
|
15 |
+
documents = loader.load()
|
16 |
|
17 |
+
# Create a Chroma vector store from the PDF documents
|
18 |
+
db = Chroma.from_documents(documents, hf, collection_name="my-collection")
|
|
|
19 |
|
20 |
+
class VectoreStoreRetrievalTool:
|
21 |
+
def __init__(self):
|
22 |
+
self.retriever = db.as_retriever(search_kwargs={"k": 1})
|
23 |
|
24 |
+
def __call__(self, query):
|
25 |
+
# Run the query through the retriever
|
26 |
+
response = self.retriever.run(query)
|
27 |
+
return response['result']
|
28 |
|
29 |
+
# Create the Gradio interface using the PDFRetrievalTool
|
30 |
tool = gr.Interface(
|
31 |
+
PDFRetrievalTool(),
|
32 |
+
inputs=gr.Textbox(),
|
33 |
+
outputs=gr.Textbox(),
|
34 |
live=True,
|
35 |
+
title="PDF Retrieval Tool",
|
36 |
+
description="This tool indexes PDF documents and retrieves relevant answers based on a given query.",
|
37 |
)
|
38 |
|
39 |
# Launch the Gradio interface
|