Hologres¶
Hologres是一站式实时数据仓库,可支持高性能OLAP分析和高QPS在线服务。
要运行此笔记本,您需要在云中运行一个Hologres实例。您可以按照此链接获取一个实例。
创建实例后,您应该能够通过Hologres控制台找出以下配置。
In [ ]:
Copied!
test_hologres_config = {
"host": "<host>",
"port": 80,
"user": "<user>",
"password": "<password>",
"database": "<database>",
"table_name": "<table_name>",
}
test_hologres_config = {
"host": "",
"port": 80,
"user": "",
"password": "",
"database": "",
"table_name": "",
}
顺便提一句,您需要确保已安装了 llama-index
:
In [ ]:
Copied!
%pip install llama-index-vector-stores-hologres
%pip install llama-index-vector-stores-hologres
In [ ]:
Copied!
!pip install llama-index
!pip install llama-index
导入所需的包依赖项:¶
In [ ]:
Copied!
from llama_index.core import (
VectorStoreIndex,
SimpleDirectoryReader,
StorageContext,
)
from llama_index.vector_stores.hologres import HologresVectorStore
from llama_index.core import (
VectorStoreIndex,
SimpleDirectoryReader,
StorageContext,
)
from llama_index.vector_stores.hologres import HologresVectorStore
加载一些示例数据:¶
In [ ]:
Copied!
!mkdir -p 'data/paul_graham/'
!curl 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -o 'data/paul_graham/paul_graham_essay.txt'
!mkdir -p 'data/paul_graham/'
!curl 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -o 'data/paul_graham/paul_graham_essay.txt'
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 75042 100 75042 0 0 31985 0 0:00:02 0:00:02 --:--:-- 31987
读取数据:¶
In [ ]:
Copied!
# 加载文档documents = SimpleDirectoryReader("./data/paul_graham/").load_data()print(f"总文档数:{len(documents)}")print(f"第一个文档,id:{documents[0].doc_id}")print(f"第一个文档,哈希值:{documents[0].hash}")print( "第一个文档,文本" f"({len(documents[0].text)} 个字符):\n{'='*20}\n{documents[0].text[:360]} ...")
# 加载文档documents = SimpleDirectoryReader("./data/paul_graham/").load_data()print(f"总文档数:{len(documents)}")print(f"第一个文档,id:{documents[0].doc_id}")print(f"第一个文档,哈希值:{documents[0].hash}")print( "第一个文档,文本" f"({len(documents[0].text)} 个字符):\n{'='*20}\n{documents[0].text[:360]} ...")
Total documents: 1 First document, id: 824dafc0-0aa1-4c80-b99c-33895cfc606a First document, hash: 8430b3bdb65ee0a7853463b71e7e1e20beee3a3ce15ef3ec714919f8653b2eb9 First document, text (75014 characters): ==================== What I Worked On February 2021 Before college the two main things I worked on, outside of school, were writing and programming. I didn't write essays. I wrote what beginning writers were supposed to write then, and probably still are: short stories. My stories were awful. They had hardly any plot, just characters with strong feelings, which I imagined ma ...
创建 AnalyticDB Vector 存储对象:¶
In [ ]:
Copied!
hologres_store = HologresVectorStore.from_param(
host=test_hologres_config["host"],
port=test_hologres_config["port"],
user=test_hologres_config["user"],
password=test_hologres_config["password"],
database=test_hologres_config["database"],
table_name=test_hologres_config["table_name"],
embedding_dimension=1536,
pre_delete_table=True,
)
hologres_store = HologresVectorStore.from_param(
host=test_hologres_config["host"],
port=test_hologres_config["port"],
user=test_hologres_config["user"],
password=test_hologres_config["password"],
database=test_hologres_config["database"],
table_name=test_hologres_config["table_name"],
embedding_dimension=1536,
pre_delete_table=True,
)
从文档中构建索引:¶
In [ ]:
Copied!
storage_context = StorageContext.from_defaults(vector_store=hologres_store)
index = VectorStoreIndex.from_documents(
documents, storage_context=storage_context
)
storage_context = StorageContext.from_defaults(vector_store=hologres_store)
index = VectorStoreIndex.from_documents(
documents, storage_context=storage_context
)
使用索引进行查询:¶
In [ ]:
Copied!
query_engine = index.as_query_engine()
response = query_engine.query("Why did the author choose to work on AI?")
print(response.response)
query_engine = index.as_query_engine()
response = query_engine.query("Why did the author choose to work on AI?")
print(response.response)
The author was inspired to work on AI due to the influence of a science fiction novel, "The Moon is a Harsh Mistress," which featured an intelligent computer named Mike, and a PBS documentary showcasing Terry Winograd's use of the SHRDLU program. These experiences led the author to believe that creating intelligent machines was an imminent reality and sparked their interest in the field of AI.