LlamaIndexs 將大語言模型和外部數據連接在一起的工具。大模型prompt有一個長度限制,當外部知識的內容超過這個長度,無法同時將有效信息傳遞給大模型,因此就誕生了 LlamaIndex。
具體操作就是通過多輪對話的方式不斷提純外部數據,達到在有限的輸入長度限制下,傳達更多的信息給大模型。
本文在mac平臺驗證ollama llamaindex,假設ollama已安裝,mac安裝ollama安裝參考
在mac m1基于ollama運行deepseek r1_m1 mac deepseek-r1-CSDN博客
1 llama-index運行環境搭建
環境向量搭建
conda create -n llama-index python=3.12
conda activte llama-index
pip install llama-index
#?chromadb依賴
pip install llama-index-llms-ollama
pip install llama-index-embeddings-ollama
pip install llama_index-vector_stores-chroma
# 開源向量存儲
pip install chromadb
ollama embedding模型下載
由于mac本地計算能力有限,所以使用qwen3:1.7b小模型。
ollama pull yxl/m3e
ollama pull qwen3:1.7b
ollama list
2 向量本地化 & 自定義查詢
以pdf文件"長安的荔枝- 馬伯庸.pdf"為例(可以替換為其他PDF書籍),通過llama_index讀取為documents,為減少計算量,取前10個子document。
docs/長安的荔枝- 馬伯庸.pdf
documents向量本地目錄為./chromadb_v0
rm -rf?./chromadb_v0
mkdir -p ./chromadb_v0
向量集合名稱為"llama_index_test"
本地向量化代碼如下
import chromadb
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, get_response_synthesizer, Settings
from llama_index.llms.ollama import Ollama
from llama_index.embeddings.ollama import OllamaEmbedding
from llama_index.core.node_parser import SentenceSplitter
from llama_index.vector_stores.chroma import ChromaVectorStore
from llama_index.core import StorageContextSettings.embed_model = OllamaEmbedding(model_name="yxl/m3e:latest")
Settings.llm = Ollama(model="qwen3:1.7b", request_timeout=360)documents = SimpleDirectoryReader("docs").load_data()
documents = documents[:10]
print(f"documents: {len(documents)}")db = chromadb.PersistentClient(path="./chromadb_v0")
chroma_collection = db.get_or_create_collection("llama_index_test")
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
storage_context = StorageContext.from_defaults(vector_store=vector_store)index = VectorStoreIndex.from_documents(documents, storage_context=storage_context, transformations=[SentenceSplitter(chunk_size=256)]
)print(index)
然后是自定義查詢,prompt=“李善德”
import chromadb
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, get_response_synthesizer, Settings
from llama_index.vector_stores.chroma import ChromaVectorStore
from llama_index.core import StorageContext
from llama_index.llms.ollama import Ollama
from llama_index.embeddings.ollama import OllamaEmbedding
from llama_index.core.retrievers import VectorIndexRetriever
from llama_index.core.query_engine import RetrieverQueryEngineSettings.embed_model = OllamaEmbedding(model_name="yxl/m3e:latest")
Settings.llm = Ollama(model="qwen3:1.7b", request_timeout=720) db = chromadb.PersistentClient(path="./chromadb_v1")
chroma_collection = db.get_or_create_collection("llama_index_test")
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
storage_context = StorageContext.from_defaults(vector_store=vector_store)index = VectorStoreIndex.from_vector_store(vector_store, storage_context=storage_context
)retriever = VectorIndexRetriever(index=index,similarity_top_k=5, # 返回最相似的前 n 個文檔片段
)response_synthesizer = get_response_synthesizer()query_engine = RetrieverQueryEngine(retriever=retriever,response_synthesizer=response_synthesizer,
)response = query_engine.query("李善德")
print(response) # 輸出查詢結果
llama_index的回復如下。
<think>
</think>李善德是《長安的荔枝》一書中的一位重要角色,他因在官場中表現出的忠誠與謹慎,而逐漸被世人所知。他曾在多個衙署任職,包括司農寺和上林署,負責處理各種政務事務。在一次重要的政務活動中,他被圣人指派為荔枝使,負責運輸珍貴的荔枝,這一職位對他來說具有極大的意義。他的經歷展現了他在官場中的沉穩與擔當,也體現了他在復雜的政治環境中所展現出的智慧與忠誠。
可見,llamaindex,借助外部知識庫chromadb,和向量檢索,找到知識庫中最相關內容,然后通過大模型將這些內容提純為最終答案。
reference
---
ollama - qwen3:1.7b
https://www.ollama.com/library/qwen3:1.7b
Ollama LLM llamaindex
https://docs.llamaindex.ai/en/stable/examples/llm/ollama/
RAG+Agent 實戰 llama-index+ollama 本地環境構建rag、agent
https://blog.csdn.net/yierbubu1212/article/details/142718139