File size: 1,964 Bytes
93762d1 |
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 |
from llama_index.core import (
VectorStoreIndex,
SimpleDirectoryReader,
get_response_synthesizer,
ServiceContext,
)
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.core.postprocessor import SentenceTransformerRerank
from typing import Optional, List
from llama_index.llms.groq import Groq
class RAG:
def __init__(
self, filepaths: List[str], rerank: Optional[SentenceTransformerRerank] = None
) -> None:
documents = SimpleDirectoryReader(input_files=filepaths).load_data()
response_synthesizer = get_response_synthesizer(
response_mode="tree_summarize",
use_async=True,
)
self.index = VectorStoreIndex.from_documents(
documents=documents,
response_synthesizer=response_synthesizer,
)
if not rerank:
self.query_engine = self.index.as_query_engine(
response_mode="tree_summarize",
use_async=True,
streaming=True,
similarity_top_k=10,
)
else:
self.query_engine = self.index.as_query_engine(
response_mode="tree_summarize",
use_async=True,
streaming=True,
similarity_top_k=10,
node_postprocessors=[rerank],
)
def run_query_engine(self, prompt):
response = self.query_engine.query(prompt)
response.print_response_stream()
return str(response)
class ServiceContextModule:
def __init__(self, api_key, model_name) -> None:
self._llm = Groq(model=model_name, api_key=api_key)
self._embedding_model = HuggingFaceEmbedding(
"Snowflake/snowflake-arctic-embed-m-long", trust_remote_code=True
)
self.service_context = ServiceContext.from_defaults(
llm=self._llm,
embed_model=self._embedding_model,
)
|