llama_index 如何将新的SQLTableSchema添加到现有的ChromaDB嵌入中?

r6vfmomb  于 2个月前  发布在  其他
关注(0)|答案(3)|浏览(21)

问题验证

  • 我已在文档和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)

"""
所以一旦我们创建了这个嵌入,如果我们需要添加更多的表模式,我们该如何实现呢?请帮忙。

ycl3bljg

ycl3bljg1#

要将一个新的 SQLTableSchema 添加到现有的 ChromaDB 嵌入中,您可以使用 ChromaVectorStore 类的 add 方法。以下是基于您提供的代码的示例:

from llama_index.core.objects import SQLTableSchema
from llama_index.vector_stores.chroma import ChromaVectorStore

# Assuming you have an existing ChromaVectorStore instance
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)

# Define your new SQLTableSchema
new_sql_table_schema = SQLTableSchema(
    table_name="new_table",
    columns=[
        {"name": "column1", "type": "TEXT"},
        {"name": "column2", "type": "INTEGER"},
    ],
    primary_key="column1"
)

# Add the new SQLTableSchema to the existing ChromaDB embedding
vector_store.add([new_sql_table_schema])

此代码片段演示了如何使用 ChromaVectorStore 类的 add 方法创建一个新的 SQLTableSchema 并将其添加到现有的 ChromaDB 嵌入中。

  • 要继续与 Dosu 对话,请提及 @dosu。*
mbzjlibv

mbzjlibv2#

你好,谢谢你的回复。但是你能帮我解决一个问题吗?如果只是列发生了变化,如何更新该表模式中现有的嵌入?请帮忙解答一下。

vwoqyblh

vwoqyblh3#

要更新现有的嵌入,如果仅更改现有 SQLTableSchema 中的列,您需要为更新后的模式重新生成 TextNode ,然后在 ChromaVectorStore 中更新嵌入。以下是操作步骤:

  1. 为更新后的模式重新生成 TextNode:
from llama_index.core.objects import SQLTableNodeMapping, SQLTableSchema

# Assuming you have an instance of SQLTableNodeMapping and an updated SQLTableSchema
sql_table_node_mapping = SQLTableNodeMapping(sql_database)
updated_schema = SQLTableSchema(table_name="your_table_name", context_str="new_context")

# Generate the updated TextNode
updated_node = sql_table_node_mapping.to_node(updated_schema)
  1. ChromaVectorStore 中更新嵌入:
from llama_index.vector_stores.chroma import ChromaVectorStore

# Assuming you have an existing ChromaVectorStore instance
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)

# Step 1: Delete the existing node
vector_store.delete(ref_doc_id=updated_node.node_id)

# Step 2: Add the updated node
vector_store.add(nodes=[updated_node])

这种方法确保旧的嵌入被移除,新的嵌入被添加到集合中,反映出更新后的模式变化 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)。

  • 要继续与 Dosu 对话,请提及 @dosu。*

相关问题