-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_rag_dense_retrival.py
More file actions
63 lines (41 loc) · 2.36 KB
/
Copy pathbasic_rag_dense_retrival.py
File metadata and controls
63 lines (41 loc) · 2.36 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
os.environ["COHERE_API_KEY"] = "w7f2pzS4Ku5JSkVidvDeyDtZJWLlxZHwI5YmGZZP"
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_cohere import CohereEmbeddings
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import Chroma
from langchain.chains import RetrievalQA
from langchain_cohere import ChatCohere
llm = ChatCohere(model="command-r", temperature=0)
embeddings = CohereEmbeddings(model="embed-english-v3.0")
urls = [
"https://lilianweng.github.io/posts/2023-06-23-agent/",
"https://lilianweng.github.io/posts/2023-03-15-prompt-engineering/",
"https://lilianweng.github.io/posts/2023-10-25-adv-attack-llm/",
# bez tego llm nie radzi sobie z odpowiedzenim na ptyanie o autorow
"https://www.sciencedirect.com/science/article/pii/S0378437119303528",
# "https://milvus.io/ai-quick-reference/what-is-the-difference-between-sparse-and-dense-retrieval?utm_source=chatgpt.com"
]
# tutaj uzywamy dokumentow zaladowanych z neta dlatego uzywamy WebBaseLoader
docs = [WebBaseLoader(url).load() for url in urls]
# Tutaj tworzy sie jedna plaska lista, potencjalne miejsce gdzie mozna by strukturyzowac dane do grafu
docs_list = [item for sublist in docs for item in sublist]
text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(
chunk_size=512,
chunk_overlap=0,
# chunk_size=50, chunk_overlap=0
)
chunks = text_splitter.split_documents(docs_list)
# here we add to vector_store
chroma_vectorstore = Chroma.from_documents(documents=chunks, embedding=embeddings)
retriever = chroma_vectorstore.as_retriever(search_kwargs={"k": 4})
# query = "What are authors of: 'A review of cellular automata models for crowd evacuation' paper?"
# query = "What are authors of A review of cellular automata evacuation paper?"
# query = "How do simulation methods replicate panic-driven group behavior in confined areas?"
# query = "Which computational framework divides the environment into discrete cells to simulate evacuation patterns?"
# query = "What kind of spatial logic is used to simulate individual flow in emergency exits?"
query = "Who wrote the evacuation review?"
retrieved = retriever.get_relevant_documents(query)
qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=retriever, chain_type="stuff")
response = qa_chain.invoke(query)
print("Answer: ", response)