问题验证
- 我已在文档和discord中寻找答案。
问题
以下是用于创建嵌入的代码,
代码:
"""
from llama_index.core import SQLDatabase
from llama_index.core.objects import (
SQLTableNodeMapping,
ObjectIndex,
SQLTableSchema,
)
import chromadb
from llama_index.vector_stores.chroma import ChromaVectorStore
from llama_index.core import StorageContext, VectorStoreIndex
sql_database = SQLDatabase(engine)
table_schema_objs = [
SQLTableSchema(table_name=t.table_name, context_str=t.table_summary)
for t in table_infos
]
print("Creating chromadb", chroma_path)
# create and save chroma
db = chromadb.PersistentClient(path=chroma_path)
chroma_collection = db.get_or_create_collection("table_schema")
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
object_index = ObjectIndex.from_objects(
table_schema_objs,
table_node_mapping,
storage_context=storage_context)
obj_retriever = object_index.as_retriever(similarity_top_k=3)
"""
所以一旦我们创建了这个嵌入,如果我们需要添加更多的表模式,我们该如何实现呢?请帮忙。
3条答案
按热度按时间ycl3bljg1#
要将一个新的
SQLTableSchema
添加到现有的 ChromaDB 嵌入中,您可以使用ChromaVectorStore
类的add
方法。以下是基于您提供的代码的示例:此代码片段演示了如何使用
ChromaVectorStore
类的add
方法创建一个新的SQLTableSchema
并将其添加到现有的 ChromaDB 嵌入中。mbzjlibv2#
你好,谢谢你的回复。但是你能帮我解决一个问题吗?如果只是列发生了变化,如何更新该表模式中现有的嵌入?请帮忙解答一下。
vwoqyblh3#
要更新现有的嵌入,如果仅更改现有
SQLTableSchema
中的列,您需要为更新后的模式重新生成TextNode
,然后在ChromaVectorStore
中更新嵌入。以下是操作步骤:TextNode
:ChromaVectorStore
中更新嵌入:这种方法确保旧的嵌入被移除,新的嵌入被添加到集合中,反映出更新后的模式变化 1(https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/objects/table_node_mapping.py)2(https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/vector_stores/chroma.py)。