使用 FAISS + Ollama LLM 构建一个简单的本地浦那旅行 AI - POC
Source: Dev.to
有没有想过如何为城市观光或旅行推荐创建自己的本地 AI 助手?在这个概念验证中,我们使用 FAISS 向量数据库进行嵌入,并使用 Ollama LLM 生成答案,构建了一个 Pune Grand Tour AI。无需 Docker,也没有云费用——仅使用本地 Python 和嵌入。
🔹 Purpose of this POC
- 本地 AI 助手 – 一个专注于浦那旅游的迷你 ChatGPT。
- 快速检索 – 嵌入向量实现对精心策划数据集的相似度搜索。
- 成本高效 – 无需云端向量数据库;FAISS 完全在本地运行。
- 动手 AI 探索 – 学习实用流水线:嵌入向量 → 向量数据库 → LLM。
🔹 为什么使用 FAISS?
FAISS(Facebook AI Similarity Search)是一个高性能库,用于:
- 存储向量嵌入。
- 执行快速相似度(最近邻)搜索。
- 在本地运行,无需任何云基础设施。
关键点:所有 Pune 数据都能轻松放入内存,因此 FAISS 提供了闪电般的检索速度。
🔹 数据集
我们使用一个简单的文本文件(pune_places_chunks.txt),其中包含浦那的:
- 历史堡垒
- 纪念碑
- 博物馆
- 旅游景点
每一行或每个块代表一个文档。示例:
[PLACE] Shaniwar Wada
Shaniwar Wada is a historic fort located in Pune, built in 1732 by Peshwa Bajirao I.
It served as the administrative center of the Maratha Empire.
[PLACE] Aga Khan Palace
The Aga Khan Palace is known for its association with Mahatma Gandhi and history.
🔹 第一步 – 创建嵌入和 FAISS 向量库
文件: ingest.py
from langchain_community.document_loaders import TextLoader
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
# 1️⃣ Load processed Pune data
loader = TextLoader("../data/processed/pune_places_chunks.txt")
documents = loader.load()
# 2️⃣ Create embeddings
embeddings = HuggingFaceEmbeddings(
model_name="sentence-transformers/all-MiniLM-L6-v2"
)
# 3️⃣ Build FAISS vector store
vectorstore = FAISS.from_documents(documents, embeddings)
# 4️⃣ Persist the DB locally
vectorstore.save_local("../embeddings/pune_faiss")
print("Pune embeddings created successfully")
运行:
python ingest.py
预期输出
Pune embeddings created successfully
解释
TextLoader读取文本块。HuggingFaceEmbeddings将每个块转换为向量。FAISS.from_documents构建可搜索的向量库。save_local将 FAISS 索引持久化,以便后续使用。
Source: …
🔹 第 2 步 – 使用 Ollama LLM 查询 FAISS
文件: chat.py
from langchain_community.vectorstores import FAISS
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_ollama import OllamaLLM
# 1️⃣ 加载嵌入模型
embeddings = HuggingFaceEmbeddings(
model_name="sentence-transformers/all-MiniLM-L6-v2"
)
print("Embeddings loaded")
# 2️⃣ 加载 FAISS 数据库(允许 pickle 反序列化)
vectordb = FAISS.load_local(
"../embeddings/pune_faiss",
embeddings,
allow_dangerous_deserialization=True
)
print("FAISS Vector DB loaded")
# 3️⃣ 提出问题
question = "Tell me famous places to visit in Pune"
docs = vectordb.similarity_search(question, k=3)
if not docs:
print("No documents retrieved. Check embeddings folder.")
exit(1)
context = "\n".join([d.page_content for d in docs])
print(f"Retrieved docs count: {len(docs)}")
print("Context preview (first 300 chars):")
print(context[:300])
# 4️⃣ 初始化 Ollama LLM
llm = OllamaLLM(model="llama3")
print("Ollama LLM loaded")
# 5️⃣ 构建提示词
prompt = f"""
You are a Pune travel guide AI.
Answer using only the context below.
Context:
{context}
Question:
{question}
"""
# 6️⃣ 生成响应
response = llm.invoke(prompt)
print("\nPune AI says:\n")
print(response)
运行:
python chat.py
示例输出
Embeddings loaded
FAISS Vector DB loaded
Retrieved docs count: 3
Context preview (first 300 chars):
[PLACE] Shaniwar Wada
Shaniwar Wada is a historic fort located in Pune, built in 1732...
Ollama LLM loaded
Pune AI says:
Pune is famous for Shaniwar Wada, Sinhagad Fort, Aga Khan Palace, and Dagdusheth Ganpati Temple.
说明
similarity_search获取最相关的前 k 条文档。- 将检索到的上下文拼接后发送给 Ollama LLM。
- LLM 仅基于提供的上下文生成类似人类的答案。
Source: …
🔹 第 3 步 – 使用 Streamlit 让它交互式
文件: app.py
import streamlit as st
from langchain_community.vectorstores import FAISS
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_ollama import OllamaLLM
st.title("Pune Grand Tour AI")
st.write("Ask about Pune's forts, monuments, and travel tips!")
@st.cache_resource
def load_vectorstore():
embeddings = HuggingFaceEmbeddings(
model_name="sentence-transformers/all-MiniLM-L6-v2"
)
return FAISS.load_local(
"../embeddings/pune_faiss",
embeddings,
allow_dangerous_deserialization=True,
)
@st.cache_resource
def load_llm():
return OllamaLLM(model="llama3")
vectordb = load_vectorstore()
llm = load_llm()
question = st.text_input("Ask a question about Pune:")
if question:
docs = vectordb.similarity_search(question, k=3)
if not docs:
st.warning("No documents found!")
else:
context = "\n".join([d.page_content for d in docs])
prompt = f"""
You are a Pune travel guide AI.
Context:
{context}
Question:
{question}
"""
response = llm.invoke(prompt)
st.subheader("Retrieved Context")
st.text(context[:500] + ("..." if len(context) > 500 else ""))
st.subheader("AI Answer")
st.write(response)
运行应用:
streamlit run app.py
现在你拥有了一个完整的交互式网页 UI,用户可以输入任何与浦那(Pune)相关的查询,并获得由本地嵌入和 Ollama LLM 提供支持的答案。
🎉 完成!
你已经为浦那旅游构建了一个完整的本地 AI 助手:
- 摄取 原始文本 → 嵌入 → FAISS 索引。
- 检索 通过相似度搜索获取相关片段。
- 生成 使用 Ollama LLM 的答案。
- 交互 通过 Streamlit 网页应用。
所有这些都在你的机器上运行——无需云服务费用、无需 Docker,仅需 Python。祝你旅途愉快!
运行应用程序
pip install streamlit
streamlit run app.py
结果
打开一个浏览器 UI,您可以提出任何与浦那相关的问题,AI 将进行交互式回答。
🔹 本概念验证的关键优势
- 完全本地 – 无需云或 Docker 依赖。
- 快速检索 – FAISS 提供即时相似度搜索。
- 上下文感知 AI – Ollama LLM 基于精选的浦那知识进行回答。
- 可扩展 – 可添加更多文档、图片或旅行提示。
- 交互式 UI – Streamlit 让任何人都能轻松使用 AI。
🔹 常见问题与解决方案

🔹 使用场景
- 本地城市指南 AI,适用于旅游应用
- 地理/历史课程的教育助理
- 针对任何精选数据集的个人知识助理
- RAG(检索增强生成)项目的原型
🔹 结论
使用 FAISS + Ollama LLM + Streamlit,您可以构建快速、本地、上下文感知的 AI 助手,无需依赖云服务或 Docker。此浦那 AI 概念验证展示了专门的知识库如何驱动聊天机器人,提供准确且具上下文针对性的答案。