Chromadb中Collection.add()传include参数报错,求查看嵌入向量方法
解决Chromadb
add()方法的include参数错误及查看嵌入向量的方法 错误原因
Collection.add()方法不支持include参数,这个参数是用于get()或query()方法来指定返回哪些数据字段的,你误将其用在了添加数据的方法中,才会抛出TypeError。
修正后的添加代码
首先移除include参数,正确完成数据添加:
collection.add( documents=["This is a document", "This is another document"], metadatas=[{"source": "test_source", "page": 1}, {"source": "test_source2", "page": 2}], ids=["id1", "id2"] )
查看嵌入向量的方法
要查看已添加数据的嵌入向量,使用Collection.get()方法并通过include参数指定返回embeddings字段即可:
# 获取指定ID对应的嵌入向量及其他字段 retrieved_data = collection.get( ids=["id1", "id2"], # 可选,不指定则返回所有数据 include=['embeddings', 'metadatas', 'documents'] # 指定需要返回的字段 ) # 打印嵌入向量 print("嵌入向量:") for idx, emb in enumerate(retrieved_data['embeddings']): print(f"ID {retrieved_data['ids'][idx]}: {emb[:5]}...") # 只打印前5个元素避免输出过长
如果需要通过查询相似内容来获取嵌入向量,也可以使用query()方法:
query_result = collection.query( query_texts=["This is a test query"], n_results=2, include=['embeddings'] ) print("查询返回的嵌入向量:", query_result['embeddings'])
内容的提问来源于stack exchange,提问作者raju
相关产品推荐
相关产品推荐

