Open haozhuoyuan opened 1 month ago
@haozhuoyuan Hello there! I'm here to help you with any bugs, questions, or contributions while you wait for a human maintainer.
To solve the error encountered while trying to insert objects into a Weaviate collection using a batch process, ensure that the batch object is correctly used within the add_node
function. Here is the relevant code snippet that shows how to add a node using a batch process:
def add_node(
client: "WeaviateClient",
node: BaseNode,
class_name: str,
batch: Optional[Any] = None,
text_key: str = DEFAULT_TEXT_KEY,
) -> None:
"""Add node."""
metadata = {}
metadata[text_key] = node.get_content(metadata_mode=MetadataMode.NONE) or ""
additional_metadata = node_to_metadata_dict(
node, remove_text=True, flat_metadata=False
)
metadata.update(additional_metadata)
vector = node.get_embedding()
id = node.node_id
# if batch object is provided (via a context manager), use that instead
if batch is not None:
batch.add_data_object(metadata, class_name, id, vector)
else:
client.batch.add_data_object(metadata, class_name, id, vector)
Ensure that the batch
object is correctly instantiated and passed to the add_node
function. If the batch object is not provided, the function will fall back to inserting the object directly into the collection.
Additionally, you can refer to the following test case to understand how to mock and test the batch process in Weaviate:
import sys
from unittest.mock import MagicMock
from llama_index.legacy.schema import NodeRelationship, RelatedNodeInfo, TextNode
from llama_index.legacy.vector_stores.weaviate import WeaviateVectorStore
def test_weaviate_add() -> None:
# mock import
sys.modules["weaviate"] = MagicMock()
weaviate_client = MagicMock()
batch_context_manager = MagicMock()
weaviate_client.batch.__enter__.return_value = batch_context_manager
vector_store = WeaviateVectorStore(weaviate_client=weaviate_client)
vector_store.add(
[
TextNode(
text="test node text",
id_="test node id",
relationships={
NodeRelationship.SOURCE: RelatedNodeInfo(node_id="test doc id")
},
embedding=[0.5, 0.5],
)
]
)
args, _ = batch_context_manager.add_data_object.call_args
assert args[-1] == [0.5, 0.5]
This test case demonstrates how to mock the Weaviate client and batch context manager to ensure that the batch process is correctly handled.
Furthermore, the add
method in the WeaviateVectorStore
class shows how to use the batch process within a context manager:
def add(
self,
nodes: List[BaseNode],
**add_kwargs: Any,
) -> List[str]:
"""Add nodes to index.
Args:
nodes: List[BaseNode]: list of nodes with embeddings
"""
ids = [r.node_id for r in nodes]
with self._client.batch.dynamic() as batch:
for node in nodes:
add_node(
self._client,
node,
self.index_name,
batch=batch,
text_key=self.text_key,
)
return ids
This method ensures that the batch process is correctly managed and that nodes are added to the Weaviate collection efficiently [1][2][3][4].
To continue talking to Dosu, mention @dosu.
Question Validation
Question
Error message is: {'message': 'Failed to send 1 objects in a batch of 1. Please inspect client.batch.failed_objects or collection.batch.failed_objects for the failed objects.'}
How should I solve it?Thank you