Hands-on, course-style experiments for building generative AI applications with Python.
This repository focuses on practical demos across:
- pre-trained models,
- hosted and local LLM integration,
- prompt engineering patterns,
- embeddings/vector databases,
- retrieval-augmented generation (RAG), and
- agentic workflows.
c02_pretrained_model/: transformer-based tasks (NER, QA, summarization, translation, text-to-image/audio, etc.)c03_large_language_model/: LLM provider integration and LangChain patterns (routing, parallel chains, safety/topic control)c04_prompt_engineering/: few-shot, self-consistency, prompt chaining, self-feedbackc05_vector_databases/: chunking, embeddings, loaders, storage, retrieval, capstone appc06_RAG/: BM25/TF-IDF, hybrid search, query expansion, RAG evaluationc07_agentic_system/: agentic RAG experimentsmiscellany/: utilities and standalone experimentstests/: pytest suite mirroring core modules
Note: some folder/file names intentionally contain typos from course progression (for example
data_loder/,sematic_chunking.py,structed_chunking.py).
- Python
>=3.11 uvrecommended for dependency/environment management
uv syncAlternative:
pip install -e .Create .env at repository root. Common keys used by scripts include:
OPENAI_API_KEY=...
GROQ_API_KEY=...
GOOGLE_API_KEY=...
ANTHROPIC_API_KEY=...
HUGGINGFACEHUB_API_TOKEN=...
PINECONE_API_KEY=...Many scripts call load_dotenv(find_dotenv(usecwd=True)), so keeping .env at the root is important.
From the repository root:
uv run python c03_large_language_model/chat_with_openai.py
uv run python c05_vector_databases/chunking/fixed_size.py
uv run python c06_RAG/simple_RAG.pyMost demo scripts are notebook-style Python files with # %% cells and are also suitable for step-by-step execution in VS Code.
Run full test suite:
uv run pytestRun a specific area:
uv run pytest tests/c05_vector_databases/chunkingOptional syntax check:
uv run python -m compileall .- Keep heavy model imports lazy when possible.
- Add new examples to the matching
c0X_module. - Preserve
# %%/# %% [markdown]cell structure for tutorial-style execution. - Add or update tests in
tests/when changing reusable utilities.
Core dependencies are managed in pyproject.toml and include:
- LangChain ecosystem (
langchain-*) - model/tooling (
transformers,torch,tensorflow,diffusers) - retrieval/vector stack (
chromadb,pinecone,sentence-transformers,rank-bm25) - evaluation/utilities (
ragas,pytest,datasets,wikipedia)
Educational/practice repository.
Last updated: March 2026
这是一个动手实践、课程风格的实验仓库,用于构建 Python 生成式 AI 应用。
本仓库聚焦以下方向的实战示例:
- 预训练模型,
- 托管与本地大语言模型集成,
- 提示词工程模式,
- 向量嵌入/向量数据库,
- 检索增强生成(RAG),以及
- 智能体工作流。
c02_pretrained_model/:基于 Transformer 的任务(NER、QA、摘要、翻译、文生图/音频等)c03_large_language_model/:LLM 服务商集成与 LangChain 模式(路由、并行链、安全/话题控制)c04_prompt_engineering/:少样本、自一致性、提示链、自反馈c05_vector_databases/:分块、嵌入、加载、存储、检索、综合应用c06_RAG/:BM25/TF-IDF、混合检索、查询扩展、RAG 评估c07_agentic_system/:智能体式 RAG 实验miscellany/:工具脚本与独立实验tests/:与核心模块对应的 pytest 测试套件
注意:部分目录/文件名由于课程演进原因,保留了拼写错误(例如
data_loder/、sematic_chunking.py、structed_chunking.py)。
- Python
>=3.11 - 推荐使用
uv进行依赖与环境管理
uv sync备选方式:
pip install -e .在仓库根目录创建 .env。常见环境变量包括:
OPENAI_API_KEY=...
GROQ_API_KEY=...
GOOGLE_API_KEY=...
ANTHROPIC_API_KEY=...
HUGGINGFACEHUB_API_TOKEN=...
PINECONE_API_KEY=...许多脚本会调用 load_dotenv(find_dotenv(usecwd=True)),因此将 .env 放在根目录很重要。
在仓库根目录执行:
uv run python c03_large_language_model/chat_with_openai.py
uv run python c05_vector_databases/chunking/fixed_size.py
uv run python c06_RAG/simple_RAG.py大多数示例脚本是带 # %% 单元的 notebook 风格 Python 文件,也适合在 VS Code 中分步执行。
运行完整测试套件:
uv run pytest运行指定模块测试:
uv run pytest tests/c05_vector_databases/chunking可选语法检查:
uv run python -m compileall .- 尽量对重量级模型采用惰性导入。
- 新示例请添加到对应的
c0X_模块。 - 保留
# %%/# %% [markdown]单元结构,便于教程式执行。 - 修改可复用工具时,请在
tests/中新增或更新测试。
核心依赖在 pyproject.toml 中管理,主要包括:
- LangChain 生态(
langchain-*) - 模型与工具链(
transformers、torch、tensorflow、diffusers) - 检索/向量技术栈(
chromadb、pinecone、sentence-transformers、rank-bm25) - 评估/通用工具(
ragas、pytest、datasets、wikipedia)
仅用于教育/实践。
最后更新:2026 年 3 月