1)doc
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
from llama_index.llms.ollama import Ollama
from llama_index.embeddings.ollama import OllamaEmbedding
# 加载数据
documents = SimpleDirectoryReader("llama-index/docs").load_data()
# Emb
Settings.embed_model = OllamaEmbedding(model_name="llama3.1")
# Post processing
Settings.llm = Ollama(model="llama3.1", request_timeout=360)
index = VectorStoreIndex.from_documents(
documents
)
query_engine = index.as_query_engine()
# 执行查询并获取响应
response = query_engine.query("黑猫叫什么")
print(response)
2)pdf
# ref:
# https://llamahub.ai/l/readers/llama-index-readers-file?from=
import os
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings, StorageContext, load_index_from_storage
from llama_index.llms.ollama import Ollama
from llama_index.embeddings.ollama import OllamaEmbedding
from llama_index.readers.file import PDFReader
# load pdf
parser = PDFReader()
file_extractor = {".pdf": parser}
documents = SimpleDirectoryReader(
"llama-index/pdf", file_extractor=file_extractor
).load_data()
# emb
Settings.embed_model = OllamaEmbedding(model_name="wangshenzhi/llama3-8b-chinese-chat-ollama-q8")
# llm
Settings.llm = Ollama(model="wangshenzhi/llama3-8b-chinese-chat-ollama-q8", request_timeout=360)
if not os.path.exists("llama-index/db/pdf-db"):
index = VectorStoreIndex.from_documents(
documents
)
index.storage_context.persist(persist_dir="llama-index/db/pdf-db")
else:
storage_context = StorageContext.from_defaults(persist_dir="llama-index/db/pdf-db")
index = load_index_from_storage(storage_context)
query_engine = index.as_query_engine()
# 执行查询并获取响应
response = query_engine.query("如果我想获得澳洲500签证, TOEFL最少要考多少分?")
print(response)
3)query展示了检索的细节,可以选择几条相似的数据
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, get_response_synthesizer, Settings
from llama_index.core.retrievers import VectorIndexRetriever
from llama_index.core.query_engine import RetrieverQueryEngine
from llama_index.llms.ollama import Ollama
from llama_index.embeddings.ollama import OllamaEmbedding
# Emb
Settings.embed_model = OllamaEmbedding(model_name="wangshenzhi/llama3-8b-chinese-chat-ollama-q8")
# Post processing
Settings.llm = Ollama(model="wangshenzhi/llama3-8b-chinese-chat-ollama-q8", request_timeout=360)
# build index
documents = SimpleDirectoryReader("llama-index/docs").load_data()
index = VectorStoreIndex.from_documents(documents)
# configure retriever
retriever = VectorIndexRetriever(
index=index,
similarity_top_k=2,
)
# configure response synthesizer
response_synthesizer = get_response_synthesizer()
# assemble query engine
query_engine = RetrieverQueryEngine(
retriever=retriever,
response_synthesizer=response_synthesizer,
)
# query
response = query_engine.query("霸王茶姬香港店什么时候开业?店长薪酬多少?")
print(response)