Llamma index Saving and Loading
I create a llamma2 model and could query with it with no problems and then i saved it like this.index.storage_context.persist(persist_dir=“<persist_dir_xara>”)After loading i encountered a weird errorrebuild storage contextstorage_context = StorageContext.from_defaults(persist_dir=“<persist_dir_xara>”)load indexindex = load_index_from_storage(storage_context)query_engine = index.as_query_engine()response = query_engine.query(“random message”)ValueError                                Traceback (most recent call last)Cell In[12], line 21 query_engine = index.as_query_engine()----> 2 response = query_engine.query(“a”)File /srv/scratch3/zinonos/zin/lib/python3.10/site-packages/llama_index/core/base_query_engine.py:30, in BaseQueryEngine.query(self, str_or_query_bundle)28 if isinstance(str_or_query_bundle, str):29     str_or_query_bundle = QueryBundle(str_or_query_bundle)—> 30 return self._query(str_or_query_bundle)File /srv/scratch3/zinonos/zin/lib/python3.10/site-packages/llama_index/query_engine/retriever_query_engine.py:170, in RetrieverQueryEngine._query(self, query_bundle)166 “”“Answer a query.”“”167 with self.callback_manager.event(168     CBEventType.QUERY, payload={EventPayload.QUERY_STR: query_bundle.query_str}169 ) as query_event:→ 170     nodes = self.retrieve(query_bundle)171     response = self._response_synthesizer.synthesize(172         query=query_bundle,173         nodes=nodes,174     )176     query_event.on_end(payload={EventPayload.RESPONSE: response})File /srv/scratch3/zinonos/zin/lib/python3.10/site-packages/llama_index/query_engine/retriever_query_engine.py:126, in RetrieverQueryEngine.retrieve(self, query_bundle)125 def retrieve(self, query_bundle: QueryBundle) → List[NodeWithScore]:→ 126     nodes = self._retriever.retrieve(query_bundle)127     return self._apply_node_postprocessors(nodes, query_bundle=query_bundle)File /srv/scratch3/zinonos/zin/lib/python3.10/site-packages/llama_index/core/base_retriever.py:54, in BaseRetriever.retrieve(self, str_or_query_bundle)49 with self.callback_manager.as_trace(“query”):50     with self.callback_manager.event(51         CBEventType.RETRIEVE,52         payload={EventPayload.QUERY_STR: query_bundle.query_str},53     ) as retrieve_event:—> 54         nodes = self._retrieve(query_bundle)55         retrieve_event.on_end(56             payload={EventPayload.NODES: nodes},57         )58 return nodesFile /srv/scratch3/zinonos/zin/lib/python3.10/site-packages/llama_index/indices/vector_store/retrievers/retriever.py:88, in VectorIndexRetriever._retrieve(self, query_bundle)82     if query_bundle.embedding is None and len(query_bundle.embedding_strs) > 0:83         query_bundle.embedding = (84             self._service_context.embed_model.get_agg_embedding_from_queries(85                 query_bundle.embedding_strs86             )87         )—> 88 return self._get_nodes_with_embeddings(query_bundle)File /srv/scratch3/zinonos/zin/lib/python3.10/site-packages/llama_index/indices/vector_store/retrievers/retriever.py:164, in VectorIndexRetriever._get_nodes_with_embeddings(self, query_bundle_with_embeddings)160 def _get_nodes_with_embeddings(161     self, query_bundle_with_embeddings: QueryBundle162 ) → List[NodeWithScore]:163     query = self._build_vector_store_query(query_bundle_with_embeddings)→ 164     query_result = self._vector_store.query(query, **self._kwargs)165     return self._build_node_list_from_query_result(query_result)File /srv/scratch3/zinonos/zin/lib/python3.10/site-packages/llama_index/vector_stores/simple.py:274, in SimpleVectorStore.query(self, query, **kwargs)266     top_similarities, top_ids = get_top_k_mmr_embeddings(267         query_embedding,268         embeddings,(…)271         mmr_threshold=mmr_threshold,272     )273 elif query.mode == VectorStoreQueryMode.DEFAULT:→ 274     top_similarities, top_ids = get_top_k_embeddings(275         query_embedding,276         embeddings,277         similarity_top_k=query.similarity_top_k,278         embedding_ids=node_ids,279     )280 else:281     raise ValueError(f"Invalid query mode: {query.mode}")File /srv/scratch3/zinonos/zin/lib/python3.10/site-packages/llama_index/indices/query/embedding_utils.py:31, in get_top_k_embeddings(query_embedding, embeddings, similarity_fn, similarity_top_k, embedding_ids, similarity_cutoff)29 similarity_heap: List[Tuple[float, Any]] =30 for i, emb in enumerate(embeddings_np):—> 31     similarity = similarity_fn(query_embedding_np, emb)32     if similarity_cutoff is None or similarity > similarity_cutoff:33         heapq.heappush(similarity_heap, (similarity, embedding_ids[i]))File /srv/scratch3/zinonos/zin/lib/python3.10/site-packages/llama_index/embeddings/base.py:47, in similarity(embedding1, embedding2, mode)45     return np.dot(embedding1, embedding2)46 else:—> 47     product = np.dot(embedding1, embedding2)48     norm = np.linalg.norm(embedding1) * np.linalg.norm(embedding2)49     return product / normValueError: shapes (1536,) and (768,) not aligned: 1536 (dim 0) != 768 (dim 0)
2024-01-02T12:39:27Z
Solved. Have to adjust code like this else it loads default embedingsindex = load_index_from_storage(storage_context, service_context=service_context)