如果您在 Colab 上打开此 Notebook,您可能需要安装 LlamaIndex 🦙。
输入 [ ]
已复制!
%pip install llama-index-vector-stores-faiss
%pip install llama-index-vector-stores-faiss
!pip install llama-index
已复制!
import logging import sys logging.basicConfig(stream=sys.stdout, level=logging.INFO) logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
已复制!
%pip install llama-index-vector-stores-faiss
import logging
import sys
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
import faiss # text-ada-embedding-002 的维度 d = 1536 faiss_index = faiss.IndexFlatL2(d)
已复制!
%pip install llama-index-vector-stores-faiss
import faiss
# dimensions of text-ada-embedding-002
d = 1536
faiss_index = faiss.IndexFlatL2(d)
加载文档,构建 VectorStoreIndex¶
from llama_index.core import ( SimpleDirectoryReader, load_index_from_storage, VectorStoreIndex, StorageContext, ) from llama_index.vector_stores.faiss import FaissVectorStore from IPython.display import Markdown, display
已复制!
%pip install llama-index-vector-stores-faiss
from llama_index.core import (
SimpleDirectoryReader,
load_index_from_storage,
VectorStoreIndex,
StorageContext,
)
from llama_index.vector_stores.faiss import FaissVectorStore
from IPython.display import Markdown, display
下载数据
!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'
已复制!
%pip install llama-index-vector-stores-faiss
!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'
# 加载文档 documents = SimpleDirectoryReader("./data/paul_graham/").load_data()
已复制!
%pip install llama-index-vector-stores-faiss
# load documents
documents = SimpleDirectoryReader("./data/paul_graham/").load_data()
vector_store = FaissVectorStore(faiss_index=faiss_index) storage_context = StorageContext.from_defaults(vector_store=vector_store) index = VectorStoreIndex.from_documents( documents, storage_context=storage_context )
已复制!
%pip install llama-index-vector-stores-faiss
vector_store = FaissVectorStore(faiss_index=faiss_index)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(
documents, storage_context=storage_context
)
# 将索引保存到磁盘 index.storage_context.persist()
已复制!
%pip install llama-index-vector-stores-faiss
# save index to disk
index.storage_context.persist()
# 从磁盘加载索引 vector_store = FaissVectorStore.from_persist_dir("./storage") storage_context = StorageContext.from_defaults( vector_store=vector_store, persist_dir="./storage" ) index = load_index_from_storage(storage_context=storage_context)
已复制!
%pip install llama-index-vector-stores-faiss
# load index from disk
vector_store = FaissVectorStore.from_persist_dir("./storage")
storage_context = StorageContext.from_defaults(
vector_store=vector_store, persist_dir="./storage"
)
index = load_index_from_storage(storage_context=storage_context)
查询索引¶
# 将日志级别设置为 DEBUG 以获得更详细的输出 query_engine = index.as_query_engine() response = query_engine.query("What did the author do growing up?")
已复制!
%pip install llama-index-vector-stores-faiss
# 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?")
display(Markdown(f"{response}"))
已复制!
%pip install llama-index-vector-stores-faiss
display(Markdown(f"<b>{response}</b>"))
# 将日志级别设置为 DEBUG 以获得更详细的输出 query_engine = index.as_query_engine() response = query_engine.query( "What did the author do after his time at Y Combinator?" )
已复制!
%pip install llama-index-vector-stores-faiss
# 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 Y Combinator?"
)
回到顶部
已复制!
%pip install llama-index-vector-stores-faiss
display(Markdown(f"<b>{response}</b>"))
# 将日志级别设置为 DEBUG 以获得更详细的输出 query_engine = index.as_query_engine() response = query_engine.query( "What did the author do after his time at Y Combinator?" )