-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrag_falcon.py
40 lines (28 loc) · 1.25 KB
/
rag_falcon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!pip install langchain
#!pip install sentence-transformers
#!pip install faiss-gpu
#!pip install pypdf
from langchain.text_splitter import CharacterTextSplitter
from langchain_community.document_loaders import PyPDFLoader
from langchain_community.vectorstores import FAISS
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
loader = PyPDFLoader('langchain.pdf')
#loader = TextLoader("./sample_text_file.txt")
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(documents)
embeddings = HuggingFaceEmbeddings()
db = FAISS.from_documents(docs, embeddings)
from langchain import HuggingFaceHub
repo_id = "tiiuae/falcon-7b-instruct"
key = "YOUR_HUGGING_FACE_ACCESS_TOKEN" #Use your own API key
llm=HuggingFaceHub(huggingfacehub_api_token= key,
repo_id=repo_id,
model_kwargs={"temperature":0.1 ,"max_length":512})
from langchain.chains import RetrievalQA
from langchain.schema import retriever
retriever = db.as_retriever()
chain = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever)
query="When LangChain was lunched?"
out = chain.invoke(query)
print(out['result'])