Try with Settings once
from llama_index.core import Settings
Settings.llm = llm
Settings.embed_model = embed_model
# No need to define service context or to pass it anywhere
i still have the same error it say 'FaissVectorStore' object has no attribute 'callback_manager' i want to use faiss as a vector store and Mongodb to store files and chunk
Create a ServiceContext with the LLM and embedding model
service_context = ServiceContext(
llm=Settings.llm,
embed_model=Settings.embed_model,
llm_predictor=None,
prompt_helper=prompt_helper,
llama_logger=None,
callback_manager=CallbackManager([]),
transformations=None,
)
Initialize a VectorStoreIndex for document management
vector_store = FaissVectorStore(faiss_index=faiss_index)
Create a QueryEngine for handling queries
query_engine = RetrieverQueryEngine(service_context, vector_store)Thats not how you create a retriever query engine
Not sure where you saw that -- you need the retriever
index = ....
retriever = index.as_retriever(similarity_top_k=2)
query_engine = RetrieverQueryEngine.from_args(retriever, llm=llm)
but what can i do for rhis error AttributeError: 'FaissVectorStore' object has no attribute 'callback_manager' how can i define callback_manager on FaissVectorStore
Did you try changes that Logan suggested above?
Yea, the way you are making the query engine is not correct, that is why you have that error
thanks guys but i cant test the query engne yet i want to use mangodb to stock files data (the chunks and the page number for example ) and Faiss vector store to do the similarity search but i cant structure the code correctly can i have ur thoughts ?
setup your storage context with the stuff, and go to town (tbh I wouldn't recommend faiss unless you want to store/load from disk)
# create
storage_context = StorageContext.from_defaults(
index_store=MongoIndexStore.from_host_and_port(...),
docstore=MongoDocumentStore.from_host_and_port(...),
vector_store=FaissVectorStore(...)
)
index = VectorStoreIndex.from_documents(documents, storage_context=storage_context)
index.storage_context.persist(persist_dir="./storage")
# to load -- mongo is remove, vector store is on disk
storage_context = StorageContext.from_defaults(
index_store=MongoIndexStore.from_host_and_port(...),
docstore=MongoDocumentStore.from_host_and_port(...),
persist_dir="./storage"
)
index = load_index_from_storage(storage_context)
Other vector db integrations store everything in the vector store, and thats all you really need to worry about
is this correct service_context = ServiceContext.from_defaults(
llm=Settings.llm,
embed_model=Settings.embed_model,
prompt_helper=prompt_helper
)
vector_store = FaissVectorStore(faiss_index=faiss_index)
storage_context = StorageContext.from_defaults(docstore=docstore, index_store=index_store ,vector_store=vector_store)
documents = list(pdf_collection.find())
index = VectorStoreIndex.from_documents(documents, storage_context=storage_context)
index.storage_context.persist(persist_dir="./storage")
vector_store = FaissVectorStore.from_persist_dir("./storage")
storage_context = StorageContext.from_defaults(
vector_store=vector_store, persist_dir="./storage"
)
to load -- mongo is remove, vector store is on disk
storage_context = StorageContext.from_defaults( docstore=docstore, index_store=index_store ,
persist_dir="./storage"
)
index = load_index_from_storage(storage_context)
retriever = index.as_retriever(similarity_top_k=2)
query_engine = RetrieverQueryEngine.from_args(retriever, llm=Settings.llm)when i execute the code i have this error env\lib\site-packages\llama_index\core\indices\base.py", line 136, in from_documents
docstore.set_document_hash(doc.get_doc_id(), doc.hash)
AttributeError: 'dict' object has no attribute 'get_doc_id'
@Logan M @WhiteFang_Jr do i need to use docstore ? i update my code to this service_context = ServiceContext.from_defaults(llm=Settings.llm, embed_model=Settings.embed_model, prompt_helper=prompt_helper)
vector_store = FaissVectorStore(faiss_index=faiss_index)
storage_context = StorageContext.from_defaults(docstore=docstore, index_store=index_store, vector_store=vector_store)
Initialize MongoDB reader
reader = SimpleMongoReader(uri="mongodb://localhost:27017")
documents = reader.load_data(db.name, pdf_collection.name, field_names=["text"])
index = VectorStoreIndex.from_documents(documents, storage_context=storage_context)
index.storage_context.persist(persist_dir="./storage")
Load or create indices
index = load_index_from_storage(storage_context)
retriever = index.as_retriever(similarity_top_k=2) and now i have this error llama_index\core\indices\loading.py", line 40, in load_index_from_storage
raise ValueError(
ValueError: Expected to load a single index, but got 4 instead. Please specify index_id.No, I think four indexes have persisted in the same place. Try removing the storage folder and then try again.
This is for specifying index ID, else if you remember the index id you can provide that while loading up:
index = load_index_from_storage(
storage_context: StorageContext,
index_id: ADD_INDEX_ID_HERE
)
Also logan pretty much covered docstore point here!
I would also suggest that you drop using service_context
Just add llm and embed_model to Settings or just pass it
@WhiteFang_Jr thanku very much for ur answer i will modify as u said , can u tell me plz how can i implements a query engine i using this retriever = index.as_retriever(similarity_top_k=2)
query_engine = RetrieverQueryEngine.from_args(retriever, llm=Settings.llm) @app.route('/query', methods=['GET'])
def query():
query_text = request.args.get('query')
if not query_text:
return jsonify({'error': 'Query parameter is missing'}), 400
try:
response = query_engine.query(query_text)
return jsonify({'response': response})
except Exception as e:
logging.error(f"Error processing query: {e}")
return jsonify({'error': str(e)}), 500
but its not working ERROR:root:Error processing query: '3'
The query_engine looks okay! is there more error traceback to this?
this is the consol when i execute and my pc is overloading
@WhiteFang_Jr this is the traceback for the query engine