{"date": "<data article posted - 2022-09-13>", "url": "<car_review_url>", "section": "SUV", "source": "Car and Driver", "title": "<title of the article>"}
class Configuration: def __init__(self): self.initialize() def initialize(self): self.llm = Ollama(model="mixtral:8x7b-instruct-v0.1-q6_K", base_url="http://192.168.0.105:1234") self.embed_model = MistralAIEmbedding(model_name="mistral-embed", api_key=MISTRAL_API_KEY, embed_batch_size=8) self.client = chromadb.PersistentClient(path="./dbs/vector_dbs_test/cars/") self.chroma_collection = self.client.get_or_create_collection(name="cars_rag_test") self.vector_store = ChromaVectorStore(chroma_collection=self.chroma_collection) self.storage_context = StorageContext.from_defaults(vector_store=self.vector_store) self.service_context = ServiceContext.from_defaults(llm=self.llm, chunk_size=1024, chunk_overlap=25, embed_model=self.embed_model)
def extract_and_store_articles_info(db_path): conn = sqlite3.connect(db_path) cursor = conn.cursor() cursor.execute('SELECT a.metadata, a.original_summary FROM articles_processed a') rows = cursor.fetchall() conn.close() return rows def load_data(metadata, text): document = Document(text=text, metadata=metadata) return [document] def main(): config = Configuration() document_list = [] rows = extract_and_store_articles_info("./dbs/processed_data_test.db") for row in rows[:500]: metadata, text = json.loads(row[0]), row[1] metadata = {key.lower(): value.lower() if isinstance(value, str) else value for key, value in metadata.items()} text = text.lower() documents = load_data(metadata, text) document_list.append(documents) for document in document_list: #document[0].excluded_llm_metadata_keys = ["url"] #print(document[0].get_content(metadata_mode=MetadataMode.LLM)) try: VectorStoreIndex.from_documents(documents=document, service_context=config.service_context, storage_context=config.storage_context, show_progress=True) except: print("Error:", document[0].get_content(metadata_mode=MetadataMode.LLM)) continue if __name__ == "__main__": main()
index = VectorStoreIndex.from_vector_store(vector_store=config.vector_store, service_context=config.service_context) query_engine = index.as_query_engine(verbose=True) USER_PROMPT = """ Can you give me the Pricing and Specs for the 2024 Toyota RAV4 Review article. Cite the URL references you used to determine your answer. Think through the steps before responding. """ response = query_engine.query(USER_PROMPT) print(response)
Pricing and Specs for the 2024 Toyota RAV4 Review article
-> tile of the article which is also in the metadata (and summary): 2024 Toyota RAV4 Review, Pricing, and Specs
pip install llama-index-postprocessor-flag-embedding-reranker
from llama_index.postprocessor.flag_embedding_reranker import ( FlagEmbeddingReranker, ) rerank = FlagEmbeddingReranker(model="BAAI/bge-reranker-base", top_n=3) query_engine = index.as_query_engine(similarity_top_k=20, node_postprocessors=[rerank])