跳转到内容

Tablestore 演示

本指南展示如何直接使用我们基于Tablestore的DocumentStore抽象。通过将节点放入文档存储中,您可以在相同底层文档存储上定义多个索引,而无需在索引间复制数据。

%pip install llama-index-storage-docstore-tablestore
%pip install llama-index-storage-index-store-tablestore
%pip install llama-index-vector-stores-tablestore
%pip install llama-index-llms-dashscope
%pip install llama-index-embeddings-dashscope
%pip install llama-index
%pip install matplotlib
import nest_asyncio
nest_asyncio.apply()
import logging
import sys
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
from llama_index.core import SimpleDirectoryReader, StorageContext
from llama_index.core import VectorStoreIndex, SimpleKeywordTableIndex
from llama_index.core import SummaryIndex
from llama_index.core.response.notebook_utils import display_response
from llama_index.core import Settings

接下来,我们使用表格存储的文档存储功能来执行一个演示。

import getpass
import os
os.environ["tablestore_end_point"] = getpass.getpass("tablestore end_point:")
os.environ["tablestore_instance_name"] = getpass.getpass(
"tablestore instance_name:"
)
os.environ["tablestore_access_key_id"] = getpass.getpass(
"tablestore access_key_id:"
)
os.environ["tablestore_access_key_secret"] = getpass.getpass(
"tablestore access_key_secret:"
)

配置DashScope大语言模型

Section titled “Config DashScope LLM”

接下来,我们使用dashscope的LLM来执行一个演示。

import os
import getpass
os.environ["DASHSCOPE_API_KEY"] = getpass.getpass("DashScope api key:")
!mkdir -p 'data/paul_graham/'
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'
reader = SimpleDirectoryReader("./data/paul_graham/")
documents = reader.load_data()
from llama_index.core.node_parser import SentenceSplitter
nodes = SentenceSplitter().get_nodes_from_documents(documents)
from llama_index.storage.docstore.tablestore import TablestoreDocumentStore
from llama_index.storage.index_store.tablestore import TablestoreIndexStore
from llama_index.vector_stores.tablestore import TablestoreVectorStore
from llama_index.embeddings.dashscope import (
DashScopeEmbedding,
DashScopeTextEmbeddingModels,
DashScopeTextEmbeddingType,
)
from llama_index.llms.dashscope import DashScope, DashScopeGenerationModels
embedder = DashScopeEmbedding(
model_name=DashScopeTextEmbeddingModels.TEXT_EMBEDDING_V3, # default demiension is 1024
text_type=DashScopeTextEmbeddingType.TEXT_TYPE_DOCUMENT,
)
dashscope_llm = DashScope(
model_name=DashScopeGenerationModels.QWEN_MAX,
api_key=os.environ["DASHSCOPE_API_KEY"],
)
Settings.llm = dashscope_llm
docstore = TablestoreDocumentStore.from_config(
endpoint=os.getenv("tablestore_end_point"),
instance_name=os.getenv("tablestore_instance_name"),
access_key_id=os.getenv("tablestore_access_key_id"),
access_key_secret=os.getenv("tablestore_access_key_secret"),
)
index_store = TablestoreIndexStore.from_config(
endpoint=os.getenv("tablestore_end_point"),
instance_name=os.getenv("tablestore_instance_name"),
access_key_id=os.getenv("tablestore_access_key_id"),
access_key_secret=os.getenv("tablestore_access_key_secret"),
)
vector_store = TablestoreVectorStore(
endpoint=os.getenv("tablestore_end_point"),
instance_name=os.getenv("tablestore_instance_name"),
access_key_id=os.getenv("tablestore_access_key_id"),
access_key_secret=os.getenv("tablestore_access_key_secret"),
vector_dimension=1024, # embedder dimension is 1024
)
vector_store.create_table_if_not_exist()
vector_store.create_search_index_if_not_exist()
storage_context = StorageContext.from_defaults(
docstore=docstore, index_store=index_store, vector_store=vector_store
)
storage_context.docstore.add_documents(nodes)

每个索引使用相同的基础节点。

# https://gpt-index.readthedocs.io/en/latest/api_reference/indices/list.html
summary_index = SummaryIndex(nodes, storage_context=storage_context)
# https://gpt-index.readthedocs.io/en/latest/api_reference/indices/vector_store.html
vector_index = VectorStoreIndex(
nodes,
insert_batch_size=20,
embed_model=embedder,
storage_context=storage_context,
)
# https://gpt-index.readthedocs.io/en/latest/api_reference/indices/table.html
keyword_table_index = SimpleKeywordTableIndex(
nodes=nodes,
storage_context=storage_context,
llm=dashscope_llm,
)
# NOTE: the docstore still has the same nodes
len(storage_context.docstore.docs)
44
# NOTE: docstore and index_store is persisted in Tablestore by default
# NOTE: here only need to persist simple vector store to disk
storage_context.persist()
# note down index IDs
list_id = summary_index.index_id
vector_id = vector_index.index_id
keyword_id = keyword_table_index.index_id
print(list_id, vector_id, keyword_id)
c05fec2a-ac87-4761-beeb-0901f9e6530e d0b021ed-3427-46ad-927d-12d72752dbc4 2e9bfc3a-5e69-408a-9430-7b0c8baf3d77
from llama_index.core import load_index_from_storage
# re-create storage context
storage_context = StorageContext.from_defaults(
docstore=docstore, index_store=index_store, vector_store=vector_store
)
summary_index = load_index_from_storage(
storage_context=storage_context,
index_id=list_id,
)
keyword_table_index = load_index_from_storage(
llm=dashscope_llm,
storage_context=storage_context,
index_id=keyword_id,
)
# You need to add "vector_store=xxx" to StorageContext to load vector index from Tablestore
vector_index = load_index_from_storage(
insert_batch_size=20,
embed_model=embedder,
storage_context=storage_context,
index_id=vector_id,
)
Settings.llm = dashscope_llm
Settings.chunk_size = 1024
query_engine = summary_index.as_query_engine()
list_response = query_engine.query("What is a summary of this document?")
display_response(list_response)
query_engine = vector_index.as_query_engine()
vector_response = query_engine.query("What did the author do growing up?")
display_response(vector_response)

Final Response: 作者在成长过程中,在校外参与了写作和编程活动。起初,他们写短篇小说,现在回想起来觉得这些作品并不出色,因为缺乏情节,更侧重于角色的情感。在编程方面,作者最初是在初中时使用IBM 1401计算机,尝试用Fortran语言通过打孔卡编写基础程序。后来,在获得一台TRS-80微型计算机后,作者更深入地钻研编程,创作了简单游戏、预测模型火箭飞行高度的程序,甚至开发了一个供父亲使用的文字处理器。

query_engine = keyword_table_index.as_query_engine()
keyword_response = query_engine.query(
"What did the author do after his time at YC?"
)
display_response(keyword_response)

Final Response: 在离开YC之后,作者决定投身绘画,全身心投入以探索自己能达到何种水平。他将2014年大部分时间都专注于此。然而到了11月,他失去了兴趣并停止了创作。此后,他重新开始撰写文章,甚至涉足创业之外的主题。2015年3月,他也再次开始研究Lisp。