/
rag.py
executable file
·53 lines (44 loc) · 1.79 KB
/
rag.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
41
42
43
44
45
46
47
48
49
50
51
52
### Loading the embedder
from llama_index import VectorStoreIndex, SimpleDirectoryReader
from llama_index import ServiceContext
from llama_index import ServiceContext
from llama_index import VectorStoreIndex
from llama_index import StorageContext
from llama_index.vector_stores.qdrant import QdrantVectorStore
from llama_index.embeddings import LangchainEmbedding
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
import qdrant_client
import yaml
class RAG:
def __init__(self, config_file, llm):
self.config = config_file
self.qdrant_client = qdrant_client.QdrantClient(
url=self.config['qdrant_url']
)
self.llm = llm # ollama llm
def load_embedder(self):
embed_model = LangchainEmbedding(
HuggingFaceEmbeddings(model_name=self.config['embedding_model'])
)
return embed_model
def qdrant_index(self):
client = qdrant_client.QdrantClient(url=self.config["qdrant_url"])
qdrant_vector_store = QdrantVectorStore(
client=client, collection_name=self.config['collection_name']
)
# service_context = ServiceContext.from_defaults(
# llm=self.llm, embed_model="local:BAAI/bge-small-en-v1.5"
# )
service_context = ServiceContext.from_defaults(
llm=self.llm, embed_model=self.load_embedder(), chunk_size=self.config["chunk_size"]
)
index = VectorStoreIndex.from_vector_store(
vector_store=qdrant_vector_store, service_context=service_context
)
return index
# def _load_config(self, config_file):
# with open(config_file, "r") as stream:
# try:
# self.config = yaml.safe_load(stream)
# except yaml.YAMLError as e:
# print(e)