AI项目:用RAG做智能问答助手
非常入门级的项目,适合出入AI的小白跟做
一、这个项目能做什么?
你输入一个 GitHub 仓库的 URL,系统会:
- 用 gitingest 把整个仓库转成 Markdown 文本
- 用 LlamaIndex 对文本做向量化索引(Embedding + VectorStore)
- 用本地 Ollama 模型回答你关于这个仓库的任何问题
比如你可以问:
- "这个项目的核心架构是什么?"
- "main.py 里的 process 函数做了什么?"
- "这个项目用了哪些依赖?"
二、技术栈一览
| 组件 | 工具 | 作用 |
|---|---|---|
| 前端 | Streamlit | 聊天界面 |
| RAG 框架 | LlamaIndex | 文档索引+检索+问答 |
| 仓库解析 | gitingest | GitHub 仓库→Markdown |
| 本地大模型 | Ollama + Llama 3.2 | 推理引擎,无需 API |
| 向量模型 | BAAI/bge-large-en-v1.5 | 文本嵌入 |
三、环境搭建(10 分钟)
Step 1:安装 Ollama
去 https://ollama.com 下载安装,然后拉模型:
ollama pull llama3.2
验证:
ollama list
# 应该看到 llama3.2
Step 2:安装 Python 依赖
pip install gitingest llama-index llama-index-llms-ollama \
llama-index-embeddings-huggingface streamlit python-dotenv
Step 3:克隆项目
git clone https://github.com/patchy631/ai-engineering-hub.git
cd ai-engineering-hub/github-rag
四、核心代码解析
整个项目只有一个核心文件 app.py(约 180 行),我们拆解关键部分:
(1)仓库解析——把 GitHub 仓库变成文本
from gitingest import ingest
summary, tree, content = ingest(github_url)
# summary: 仓库概要
# tree: 文件目录树
# content: 所有代码拼接成的 Markdown
一行代码就能把整个仓库抓下来转成可索引的文本,这是整个 RAG 管线的数据源。
(2)向量索引——让 AI "记住" 代码
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.core.node_parser import MarkdownNodeParser
# 加载文档
loader = SimpleDirectoryReader(input_dir=content_path)
docs = loader.load_data()
# 用 Markdown 格式切分
node_parser = MarkdownNodeParser()
# 创建向量索引
index = VectorStoreIndex.from_documents(
documents=docs,
transformations=[node_parser],
show_progress=True
)
这里做了三件事:读文件 → 按 Markdown 标题切块 → 用 Embedding 模型编码成向量存入索引。
(3)问答引擎——检索 + 生成
qa_prompt = """
You are an AI assistant specialized in analyzing GitHub repositories.
Repository structure:
{tree}
Context information:
{context_str}
Query: {query_str}
Answer: """
query_engine = index.as_query_engine(streaming=True)
query_engine.update_prompts({
"response_synthesizer:text_qa_template": PromptTemplate(qa_prompt)
})
LlamaIndex 会自动:用户提问 → 向量检索最相关的代码片段 → 拼上 Prompt → 发给 Ollama 生成回答。
(4)Streamlit 聊天界面
if prompt := st.chat_input("What's up?"):
response = query_engine.query(prompt)
for chunk in response.response_gen:
full_response += chunk # 流式输出
五、运行效果
streamlit run app_local.py
浏览器会打开 localhost:8501,在侧边栏输入仓库 URL,点 "Load Repository",然后就可以聊天了。
六、可以怎么改进?(面试加分项)
如果你想在这个基础上做项目展示或面试作品,推荐几个方向:
- 换更强的模型:把 llama3.2 换成 qwen3 或 deepseek-v4,效果显著提升
- 加 Reranker:在检索后加一层重排序(
llama-index-postprocessor-cohere-rerank),提升回答精准度 - 支持多仓库对比:同时加载两个仓库,问"A 和 B 的架构有什么区别"
- 加缓存:用 Redis 缓存已解析的仓库,避免重复下载
- 部署上线:用 Streamlit Cloud 或 HuggingFace Spaces 免费部署
七、学到了什么?
通过这个项目,你实际练习了:
- RAG 全流程:数据采集 → 文本切分 → 向量化 → 检索 → 生成
- LlamaIndex 核心 API:VectorStoreIndex、QueryEngine、PromptTemplate
- 本地大模型部署:Ollama 安装和调用
- Streamlit 快速原型:聊天界面搭建
这些都是 AI 工程师面试的高频考点,动手做一遍比看十篇八股文有用。
项目地址:https://github.com/patchy631/ai-engineering-hub/tree/main/github-rag
#AI项目实战#