i’m using langgraph,but now i’ve solved this problem,
async def load_documents(
self,
documents: List[Any],
method: str = “from_documents”,
metadatas: Optional[List[Dict]] = None
) → None:
“”"
加载文档到Weaviate集合(使用insert_many批量插入)
Args:
documents: 文档列表(LangChain Document 对象)
method: 保留参数(兼容旧接口,实际不再使用)
metadatas: 保留参数(兼容旧接口,实际不再使用)
"""
# 获取Weaviate集合
collection = self.client.collections.get(self.config.collection_name)
# 准备批量插入数据
objects_to_insert = []
for doc in documents:
obj = {
"content": doc.page_content,
"source": doc.metadata.get("source", ""),
"page": doc.metadata.get("page", 0),
# 可以添加其他metadata字段
"document_type": doc.metadata.get("document_type", "unknown")
}
objects_to_insert.append(obj)
# 批量插入数据
try:
response = await collection.data.insert_many(objects_to_insert)
print(f"成功插入 {len(response.all_responses)} 个文档块")
print(f"失败 {len(response.errors)} 个(如果有)")
return response
except Exception as e:
print(f"文档插入失败: {str(e)}")
raise
i’ve got another question。
async def aenter(self):
“”“初始化 Weaviate 客户端”“”
try:
# self.client =weaviate.use_async_with_local(
# port=self.config.weaviate_port,
# headers={
# “X-OpenAI-Api-Key”: self.config.openai_api_key,
# “X-OpenAI-Baseurl”: self.config.openai_base_url,
# },
# )
self.client = WeaviateAsyncClient(
connection_params=ConnectionParams.from_params(
http_host=self.config.http_host,
http_port=self.config.http_port,
http_secure=self.config.http_secure,
grpc_host=self.config.grpc_host,
grpc_port=self.config.grpc_port,
grpc_secure=self.config.grpc_secure,
),
# auth_client_secret=Auth.api_key(“secr3tk3y”),
additional_headers={
“X-OpenAI-Api-Key”: self.config.openai_api_key,
“X-OpenAI-Baseurl”: self.config.openai_base_url,
},
additional_config=AdditionalConfig(
timeout=Timeout(init=30, query=60, insert=120), # Values in seconds
),
skip_init_checks=False
)
await self.client.connect() # 确保 Weaviate 连接
self.vectorstore = await WeaviateVectorStore(
client=self.client,
index_name=self.config.collection_name,
text_key="text",
embedding=self.embeddings
)
print(f"Weaviate 连接状态: {await self.client.is_ready()}")
return self
except Exception as e:
print(f"Weaviate 初始化失败: {e}")
raise
error
Weaviate 初始化失败: ‘coroutine’ object has no attribute ‘multi_tenancy_config’
Traceback (most recent call last):
File “/home/wyk/zxz_test_0327/answer_weaviate_async.py”, line 68, in
asyncio.run(main())
File “/usr/lib/python3.11/asyncio/runners.py”, line 188, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File “/usr/lib/python3.11/asyncio/runners.py”, line 120, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/lib/python3.11/asyncio/base_events.py”, line 650, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File “/home/wyk/zxz_test_0327/answer_weaviate_async.py”, line 31, in main
async with VectorStoreManager(config) as vector_manager:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/wyk/zxz_test_0327/utils/VectorStoreManager_weaviate_v1_async.py”, line 90, in aenter
self.vectorstore = await WeaviateVectorStore(
^^^^^^^^^^^^^^^^^^^^
File “/home/wyk/.local/lib/python3.11/site-packages/langchain_weaviate/vectorstores.py”, line 126, in init
).multi_tenancy_config.enabled
^^^^^^^^^^^^^^^^^^^^
AttributeError: ‘coroutine’ object has no attribute ‘multi_tenancy_config’
sys:1: RuntimeWarning: coroutine ‘_ConfigCollectionAsync.get’ was never awaited
This doesn’t mean I can’t wrap an asynchronous client with await WeaviateVectorStore().