Qdrant 向量存储¶
创建 Qdrant 客户端¶
In [ ]
已复制!
%pip install llama-index-vector-stores-qdrant llama-index-readers-file llama-index-embeddings-fastembed llama-index-llms-openai
%pip install llama-index-vector-stores-qdrant llama-index-readers-file llama-index-embeddings-fastembed llama-index-llms-openai
In [ ]
已复制!
import logging
import sys
import os
import qdrant_client
from IPython.display import Markdown, display
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.core import StorageContext
from llama_index.vector_stores.qdrant import QdrantVectorStore
from llama_index.embeddings.fastembed import FastEmbedEmbedding
from llama_index.core import Settings
Settings.embed_model = FastEmbedEmbedding(model_name="BAAI/bge-base-en-v1.5")
import logging import sys import os import qdrant_client from IPython.display import Markdown, display from llama_index.core import VectorStoreIndex, SimpleDirectoryReader from llama_index.core import StorageContext from llama_index.vector_stores.qdrant import QdrantVectorStore from llama_index.embeddings.fastembed import FastEmbedEmbedding from llama_index.core import Settings Settings.embed_model = FastEmbedEmbedding(model_name="BAAI/bge-base-en-v1.5")
如果首次运行,使用以下命令安装依赖项
!pip install -U qdrant_client fastembed
设置您的 OpenAI 密钥以验证 LLM
按照以下步骤将 OpenAI API 密钥设置为 OPENAI_API_KEY 环境变量 -
- 使用终端
In [ ]
已复制!
export OPENAI_API_KEY=your_api_key_here
export OPENAI_API_KEY=your_api_key_here
- 在 Jupyter Notebook 中使用 IPython 魔术命令
In [ ]
已复制!
%env OPENAI_API_KEY=<YOUR_OPENAI_API_KEY>
%env OPENAI_API_KEY=
- 使用 Python 脚本
In [ ]
已复制!
import os
os.environ["OPENAI_API_KEY"] = "your_api_key_here"
import os os.environ["OPENAI_API_KEY"] = "your_api_key_here"
注意:通常建议将敏感信息(如 API 密钥)设置为环境变量,而不是硬编码到脚本中。
In [ ]
已复制!
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
logging.basicConfig(stream=sys.stdout, level=logging.INFO) logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
下载数据
In [ ]
已复制!
!mkdir -p 'data/paul_graham/'
!wget '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/' !wget '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'
加载文档¶
In [ ]
已复制!
# load documents
documents = SimpleDirectoryReader("./data/paul_graham/").load_data()
# load documents documents = SimpleDirectoryReader("./data/paul_graham/").load_data()
构建 VectorStoreIndex¶
In [ ]
已复制!
client = qdrant_client.QdrantClient(
# you can use :memory: mode for fast and light-weight experiments,
# it does not require to have Qdrant deployed anywhere
# but requires qdrant-client >= 1.1.1
# location=":memory:"
# otherwise set Qdrant instance address with:
# url="http://<host>:<port>"
# otherwise set Qdrant instance with host and port:
host="localhost",
port=6333
# set API KEY for Qdrant Cloud
# api_key="<qdrant-api-key>",
)
client = qdrant_client.QdrantClient( # you can use :memory: mode for fast and light-weight experiments, # it does not require to have Qdrant deployed anywhere # but requires qdrant-client >= 1.1.1 # location=":memory:" # otherwise set Qdrant instance address with: # url="http://:" # otherwise set Qdrant instance with host and port: host="localhost", port=6333 # set API KEY for Qdrant Cloud # api_key="",
)
In [ ]
已复制!
vector_store = QdrantVectorStore(client=client, collection_name="paul_graham")
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(
documents,
storage_context=storage_context,
)
vector_store = QdrantVectorStore(client=client, collection_name="paul_graham") storage_context = StorageContext.from_defaults(vector_store=vector_store) index = VectorStoreIndex.from_documents( documents, storage_context=storage_context, )
查询索引¶
In [ ]
已复制!
# set Logging to DEBUG for more detailed outputs
query_engine = index.as_query_engine()
response = query_engine.query("What did the author do growing up?")
# set Logging to DEBUG for more detailed outputs query_engine = index.as_query_engine() response = query_engine.query("What did the author do growing up?")
In [ ]
已复制!
display(Markdown(f"<b>{response}</b>"))
display(Markdown(f"{response}"))
作者在大学之前从事写作和编程工作。
In [ ]
已复制!
# set Logging to DEBUG for more detailed outputs
query_engine = index.as_query_engine()
response = query_engine.query(
"What did the author do after his time at Viaweb?"
)
# set Logging to DEBUG for more detailed outputs query_engine = index.as_query_engine() response = query_engine.query( "What did the author do after his time at Viaweb?" )
In [ ]
已复制!
display(Markdown(f"<b>{response}</b>"))
display(Markdown(f"{response}"))
作者在 Viaweb 工作后,安排为一个为客户做项目的团队做自由职业。
异步构建 VectorStoreIndex¶
In [ ]
已复制!
# To connect to the same event-loop,
# allows async events to run on notebook
import nest_asyncio
nest_asyncio.apply()
# To connect to the same event-loop, # allows async events to run on notebook import nest_asyncio nest_asyncio.apply()
In [ ]
已复制!
aclient = qdrant_client.AsyncQdrantClient(
# you can use :memory: mode for fast and light-weight experiments,
# it does not require to have Qdrant deployed anywhere
# but requires qdrant-client >= 1.1.1
location=":memory:"
# otherwise set Qdrant instance address with:
# uri="http://<host>:<port>"
# set API KEY for Qdrant Cloud
# api_key="<qdrant-api-key>",
)
aclient = qdrant_client.AsyncQdrantClient( # you can use :memory: mode for fast and light-weight experiments, # it does not require to have Qdrant deployed anywhere # but requires qdrant-client >= 1.1.1 location=":memory:" # otherwise set Qdrant instance address with: # uri="http://:" # set API KEY for Qdrant Cloud # api_key="",
)
In [ ]
已复制!
vector_store = QdrantVectorStore(
collection_name="paul_graham",
client=client,
aclient=aclient,
prefer_grpc=True,
)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(
documents,
storage_context=storage_context,
use_async=True,
)
vector_store = QdrantVectorStore( collection_name="paul_graham", client=client, aclient=aclient, prefer_grpc=True, ) storage_context = StorageContext.from_defaults(vector_store=vector_store) index = VectorStoreIndex.from_documents( documents, storage_context=storage_context, use_async=True, )
异步查询索引¶
In [ ]
已复制!
query_engine = index.as_query_engine(use_async=True)
response = await query_engine.aquery("What did the author do growing up?")
query_engine = index.as_query_engine(use_async=True) response = await query_engine.aquery("What did the author do growing up?")
In [ ]
已复制!
display(Markdown(f"<b>{response}</b>"))
display(Markdown(f"{response}"))
作者从小从事短篇小说写作和编程,特别是在九年级时使用早期版本的 Fortran 在 IBM 1401 计算机上进行编程。后来,作者转向使用微型计算机,大约在 1980 年从 TRS-80 开始,在那里他们编写了简单的游戏、程序和文字处理器。
In [ ]
已复制!
# set Logging to DEBUG for more detailed outputs
query_engine = index.as_query_engine(use_async=True)
response = await query_engine.aquery(
"What did the author do after his time at Viaweb?"
)
# set Logging to DEBUG for more detailed outputs query_engine = index.as_query_engine(use_async=True) response = await query_engine.aquery( "What did the author do after his time at Viaweb?" )
In [ ]
已复制!
display(Markdown(f"<b>{response}</b>"))
display(Markdown(f"{response}"))
作者在 Viaweb 工作后,继续共同创立了 Y Combinator。
混合搜索¶
创建 Qdrant 索引时可以启用混合搜索。这里,我们使用 Qdrant 的 BM25 功能快速创建一个稀疏和稠密索引用于混合检索。
In [ ]
已复制!
from qdrant_client import QdrantClient, AsyncQdrantClient
from llama_index.core import VectorStoreIndex
from llama_index.core import StorageContext
from llama_index.vector_stores.qdrant import QdrantVectorStore
client = QdrantClient(host="localhost", port=6333)
aclient = AsyncQdrantClient(host="localhost", port=6333)
vector_store = QdrantVectorStore(
client=client,
aclient=aclient,
collection_name="paul_graham_hybrid",
enable_hybrid=True,
fastembed_sparse_model="Qdrant/bm25",
)
index = VectorStoreIndex.from_documents(
documents,
storage_context=StorageContext.from_defaults(vector_store=vector_store),
)
# retrieve 2 sparse, 2 dense, and filter down to 3 total hybrid results
query_engine = index.as_query_engine(
vector_store_query_mode="hybrid",
sparse_top_k=2,
similarity_top_k=2,
hybrid_top_k=3,
)
response = query_engine.query("What did the author do growing up?")
display(Markdown(f"<b>{response}</b>"))
from qdrant_client import QdrantClient, AsyncQdrantClient from llama_index.core import VectorStoreIndex from llama_index.core import StorageContext from llama_index.vector_stores.qdrant import QdrantVectorStore client = QdrantClient(host="localhost", port=6333) aclient = AsyncQdrantClient(host="localhost", port=6333) vector_store = QdrantVectorStore( client=client, aclient=aclient, collection_name="paul_graham_hybrid", enable_hybrid=True, fastembed_sparse_model="Qdrant/bm25", ) index = VectorStoreIndex.from_documents( documents, storage_context=StorageContext.from_defaults(vector_store=vector_store), ) # retrieve 2 sparse, 2 dense, and filter down to 3 total hybrid results query_engine = index.as_query_engine( vector_store_query_mode="hybrid", sparse_top_k=2, similarity_top_k=2, hybrid_top_k=3, ) response = query_engine.query("What did the author do growing up?") display(Markdown(f"{response}"))
保存和加载¶
要恢复索引,在大多数情况下,您只需使用向量存储对象本身即可恢复。索引由 Qdrant 自动保存。
In [ ]
已复制!
loaded_index = VectorStoreIndex.from_vector_store(
vector_store,
# Embedding model should match the original embedding model
# embed_model=Settings.embed_model
)
loaded_index = VectorStoreIndex.from_vector_store( vector_store, # Embedding model should match the original embedding model # embed_model=Settings.embed_model )