我有这段代码,可以在 Opensearch Elasticsearch 中创建索引:
def openes_initiate(file):
endpoint = getenv("OPENSEARCH_ENDPOINT", "http://localhost:9200")
# index to demonstrate the VectorStore impl
idx = getenv("OPENSEARCH_INDEX", "llama-osindex-demo")
UnstructuredReader = download_loader("UnstructuredReader")
loader = UnstructuredReader()
documents = loader.load_data(file=Path(file))
# OpensearchVectorClient stores text in this field by default
text_field = "content"
# OpensearchVectorClient stores embeddings in this field by default
embedding_field = "embedding"
# OpensearchVectorClient encapsulates logic for a
# single opensearch index with vector search enabled
client = OpensearchVectorClient(endpoint, idx, 1536, embedding_field=embedding_field, text_field=text_field)
# initialize vector store
vector_store = OpensearchVectorStore(client)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
# initialize an index using our sample data and the client we just created
index = GPTVectorStoreIndex.from_documents(documents=documents,storage_context=storage_context)
我遇到的问题是,一旦我对数据建立了索引,我就无法重新加载它并对其进行查询。我尝试这样做:
def query(index,question):
query_engine = index.as_query_engine()
res = query_engine.query(question)
print(res.response)
其中
index
是我在第一段代码中创建的,但它返回 None
您需要创建开放搜索客户端并使用 VectorStoreIndex.from_vector_store() 加载索引,然后才能对其运行查询,
index 对象为 null,不会生成 null 结果。
数据嵌入后,要检索它,您需要从
OpensearchVectorClient
获取矢量存储。这是一个可以帮助您的片段:
鉴于:
client = OpensearchVectorClient(endpoint, idx, 1536,
embedding_field=embedding_field,
text_field=text_field)
vector_store = OpensearchVectorStore(client)
从
VectorStoreIndex
获取vector_store
:
service_context = ServiceContext.from_defaults(
llm=None, # or use your LLM
embed_model= your_embedding_model
)
vsi = VectorStoreIndex.from_vector_store(vector_store,
service_context=service_context)
query_engine = vsi.as_query_engine()
res = query_engine.query("your question")
print(res)
这应该可以帮助您检索和查询嵌入的数据。