Cannot use for RAG embedding using Llama-Index but "vietgpt/vietnamese-sbert" and others work pretty well

#1
by ngtrphuong - opened

Can you double check why this issue happens?
I tried with Llama-Index and issue above happens,
What did I miss with this?

Code snippet:

from llama_index.core import Settings
from llama_index.embeddings.huggingface import HuggingFaceEmbedding

Settings.llm = llm
Settings.embed_model = HuggingFaceEmbedding(
   #model_name="dangvantuan/vietnamese-embedding", <-- This does not work, threw out of index or similar error "IndexError: index out of range in self"
    model_name="vietgpt/vietnamese-sbert",  # <-- This is working perfectly
)

Llama-Index notebook I tried is available here -

https://docs.llamaindex.ai/en/latest/examples/benchmarks/phi-3-mini-4k-instruct/

@ngtrphuong I think you need to check the version when installing because it works fine on my side

Can you specify the exact version of what tools, modules? I tried upgrading pytorch/torch to the lastest version but issue is still the same,

See the full error message below:
```

IndexError Traceback (most recent call last)
in <cell line: 3>()
1 from llama_index.core import VectorStoreIndex
2
----> 3 vector_index = VectorStoreIndex.from_documents(documents)

26 frames
/usr/local/lib/python3.10/dist-packages/llama_index/core/indices/base.py in from_documents(cls, documents, storage_context, show_progress, callback_manager, transformations, service_context, **kwargs)
143 )
144
--> 145 return cls(
146 nodes=nodes,
147 storage_context=storage_context,

/usr/local/lib/python3.10/dist-packages/llama_index/core/indices/vector_store/base.py in init(self, nodes, use_async, store_nodes_override, embed_model, insert_batch_size, objects, index_struct, storage_context, callback_manager, transformations, show_progress, service_context, **kwargs)
73
74 self._insert_batch_size = insert_batch_size
---> 75 super().init(
76 nodes=nodes,
77 index_struct=index_struct,

/usr/local/lib/python3.10/dist-packages/llama_index/core/indices/base.py in init(self, nodes, objects, index_struct, storage_context, callback_manager, transformations, show_progress, service_context, **kwargs)
92 if index_struct is None:
93 nodes = nodes or []
---> 94 index_struct = self.build_index_from_nodes(
95 nodes + objects # type: ignore
96 )

/usr/local/lib/python3.10/dist-packages/llama_index/core/indices/vector_store/base.py in build_index_from_nodes(self, nodes, **insert_kwargs)
306 )
307
--> 308 return self._build_index_from_nodes(nodes, **insert_kwargs)
309
310 def _insert(self, nodes: Sequence[BaseNode], **insert_kwargs: Any) -> None:

/usr/local/lib/python3.10/dist-packages/llama_index/core/indices/vector_store/base.py in _build_index_from_nodes(self, nodes, **insert_kwargs)
278 run_async_tasks(tasks)
279 else:
--> 280 self._add_nodes_to_index(
281 index_struct,
282 nodes,

/usr/local/lib/python3.10/dist-packages/llama_index/core/indices/vector_store/base.py in _add_nodes_to_index(self, index_struct, nodes, show_progress, **insert_kwargs)
231
232 for nodes_batch in iter_batch(nodes, self._insert_batch_size):
--> 233 nodes_batch = self._get_node_with_embedding(nodes_batch, show_progress)
234 new_ids = self._vector_store.add(nodes_batch, **insert_kwargs)
235

/usr/local/lib/python3.10/dist-packages/llama_index/core/indices/vector_store/base.py in _get_node_with_embedding(self, nodes, show_progress)
139
140 """
--> 141 id_to_embed_map = embed_nodes(
142 nodes, self._embed_model, show_progress=show_progress
143 )

/usr/local/lib/python3.10/dist-packages/llama_index/core/indices/utils.py in embed_nodes(nodes, embed_model, show_progress)
136 id_to_embed_map[node.node_id] = node.embedding
137
--> 138 new_embeddings = embed_model.get_text_embedding_batch(
139 texts_to_embed, show_progress=show_progress
140 )

/usr/local/lib/python3.10/dist-packages/llama_index/core/instrumentation/dispatcher.py in wrapper(func, instance, args, kwargs)
272 )
273 try:
--> 274 result = func(*args, **kwargs)
275 except BaseException as e:
276 self.event(SpanDropEvent(span_id=id_, err_str=str(e)))

/usr/local/lib/python3.10/dist-packages/llama_index/core/base/embeddings/base.py in get_text_embedding_batch(self, texts, show_progress, **kwargs)
329 payload={EventPayload.SERIALIZED: self.to_dict()},
330 ) as event:
--> 331 embeddings = self._get_text_embeddings(cur_batch)
332 result_embeddings.extend(embeddings)
333 event.on_end(

/usr/local/lib/python3.10/dist-packages/llama_index/embeddings/huggingface/base.py in _get_text_embeddings(self, texts)
147 def _get_text_embeddings(self, texts: List[str]) -> List[List[float]]:
148 """Get text embeddings."""
--> 149 return self._embed(texts, prompt_name="text")
150
151

/usr/local/lib/python3.10/dist-packages/llama_index/embeddings/huggingface/base.py in _embed(self, sentences, prompt_name)
122 ) -> List[List[float]]:
123 """Embed sentences."""
--> 124 return self._model.encode(
125 sentences,
126 batch_size=self.embed_batch_size,

/usr/local/lib/python3.10/dist-packages/sentence_transformers/SentenceTransformer.py in encode(self, sentences, prompt_name, prompt, batch_size, show_progress_bar, output_value, precision, convert_to_numpy, convert_to_tensor, device, normalize_embeddings)
369
370 with torch.no_grad():
--> 371 out_features = self.forward(features)
372 out_features["sentence_embedding"] = truncate_embeddings(
373 out_features["sentence_embedding"], self.truncate_dim

/usr/local/lib/python3.10/dist-packages/torch/nn/modules/container.py in forward(self, input)
215 def forward(self, input):
216 for module in self:
--> 217 input = module(input)
218 return input
219

/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in _wrapped_call_impl(self, *args, **kwargs)
1509 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]
1510 else:
-> 1511 return self._call_impl(*args, **kwargs)
1512
1513 def _call_impl(self, *args, **kwargs):

/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in _call_impl(self, *args, **kwargs)
1518 or _global_backward_pre_hooks or _global_backward_hooks
1519 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1520 return forward_call(*args, **kwargs)
1521
1522 try:

/usr/local/lib/python3.10/dist-packages/sentence_transformers/models/Transformer.py in forward(self, features)
96 trans_features["token_type_ids"] = features["token_type_ids"]
97
---> 98 output_states = self.auto_model(**trans_features, return_dict=False)
99 output_tokens = output_states[0]
100

/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in _wrapped_call_impl(self, *args, **kwargs)
1509 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]
1510 else:
-> 1511 return self._call_impl(*args, **kwargs)
1512
1513 def _call_impl(self, *args, **kwargs):

/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in _call_impl(self, *args, **kwargs)
1518 or _global_backward_pre_hooks or _global_backward_hooks
1519 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1520 return forward_call(*args, **kwargs)
1521
1522 try:

/usr/local/lib/python3.10/dist-packages/transformers/models/roberta/modeling_roberta.py in forward(self, input_ids, attention_mask, token_type_ids, position_ids, head_mask, inputs_embeds, encoder_hidden_states, encoder_attention_mask, past_key_values, use_cache, output_attentions, output_hidden_states, return_dict)
819 head_mask = self.get_head_mask(head_mask, self.config.num_hidden_layers)
820
--> 821 embedding_output = self.embeddings(
822 input_ids=input_ids,
823 position_ids=position_ids,

/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in _wrapped_call_impl(self, *args, **kwargs)
1509 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]
1510 else:
-> 1511 return self._call_impl(*args, **kwargs)
1512
1513 def _call_impl(self, *args, **kwargs):

/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in _call_impl(self, *args, **kwargs)
1518 or _global_backward_pre_hooks or _global_backward_hooks
1519 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1520 return forward_call(*args, **kwargs)
1521
1522 try:

/usr/local/lib/python3.10/dist-packages/transformers/models/roberta/modeling_roberta.py in forward(self, input_ids, token_type_ids, position_ids, inputs_embeds, past_key_values_length)
121 embeddings = inputs_embeds + token_type_embeddings
122 if self.position_embedding_type == "absolute":
--> 123 position_embeddings = self.position_embeddings(position_ids)
124 embeddings += position_embeddings
125 embeddings = self.LayerNorm(embeddings)

/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in _wrapped_call_impl(self, *args, **kwargs)
1509 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]
1510 else:
-> 1511 return self._call_impl(*args, **kwargs)
1512
1513 def _call_impl(self, *args, **kwargs):

/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in _call_impl(self, *args, **kwargs)
1518 or _global_backward_pre_hooks or _global_backward_hooks
1519 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1520 return forward_call(*args, **kwargs)
1521
1522 try:

/usr/local/lib/python3.10/dist-packages/torch/nn/modules/sparse.py in forward(self, input)
161
162 def forward(self, input: Tensor) -> Tensor:
--> 163 return F.embedding(
164 input, self.weight, self.padding_idx, self.max_norm,
165 self.norm_type, self.scale_grad_by_freq, self.sparse)

/usr/local/lib/python3.10/dist-packages/torch/nn/functional.py in embedding(input, weight, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse)
2235 # remove once script supports set_grad_enabled
2236 no_grad_embedding_renorm(weight, input, max_norm, norm_type)
-> 2237 return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
2238
2239

IndexError: index out of range in self
```

Sign up or log in to comment