Explorar o código

Merge branch 'main' of https://gogs.uu.jwwwww.com/mine/hexo-blog

gamehu hai 3 meses
pai
achega
bfe37ca35b

+ 83 - 0
source/_posts/从零到-RAG-系统:成功与失败.md

@@ -0,0 +1,83 @@
+---
+title: 从零到 RAG 系统:成功与失败
+author: Gamehu
+date: 2026-03-28 15:59:29
+categories:
+  - 技术
+tags:
+  - RAG
+  - LLM
+  - 向量数据库
+  - 工程实践
+---
+
+A few months ago I was tasked with creating an internal tool for the company's engineers: a Chat that used a local LLM. Nothing extraordinary so far. Then the requirements came in: it had to have a fast response, I insist... fast!, and... it also had to provide answers about every project the company has done throughout its entire history (almost a decade). They didn't want a traditional search engine, but a tool where you could ask questions in natural language and get answers with references to the original documents. With emphasis on providing information from OrcaFlex files (a simulation software for floating body dynamics, cables, etc., widely used in the offshore industry). It already seemed complex, but it was confirmed when I was given access to 1 TB of projects, mixed with technical documentation, reports, analyses, regulations, CSVs, etc. The emotional roller coaster had begun.
+
+I'll tell you upfront that it was neither a quick nor easy process, and that's why I'd like to share it. From the first attempts, mistakes, to the final architecture that ended up in production. I also want to highlight that I had never done anything similar before and didn't know how a RAG worked either.
+
+We'll go problem by problem, and the solution I applied to each one.
+
+## Problem 1: selecting the right technology
+
+The first step was to define the stack.
+
+I needed a local language model, without relying on external APIs, for confidentiality reasons. Ollama emerged as the most mature and easy-to-use option for running LLaMA models locally. I tried several embeddings, and nomic-embed-text offered good performance and quality for technical documents.
+
+Next was a RAG engine to orchestrate the document indexing process, embedding generation, vector database storage, and queries. Without it, no matter how fast the language model is, we couldn't retrieve relevant information from the documents. Think of it like a book's index: without it, you'd have to read the entire book to find the information you need. And with a good index, you can go straight to the right page. I'll call this process indexing for simplicity, although it's really a vectorization and indexing process.
+
+After some research, I found a mature open source framework called LlamaIndex.
+
+The language I'd use would be Python, I could list many reasons, but the most important one is that I feel comfortable and productive with it. Additionally, both Ollama and LlamaIndex have excellent Python SDKs.
+
+I was ready to start building the software. I wrote my first scripts to run vector tests on the RAG system and do some query experiments. It worked really well with very little code. I thought it would be a project of a few weeks. I couldn't have been more wrong.
+
+The next step was working with the actual documents. Hold on tight, it's going to be a bumpy ride!
+
+## Problem 2: the document chaos
+
+My file source was a folder on Azure with a massive amount of technical documents: hundreds of gigabytes, thousands of files, various formats, with no organization or structure beyond the folder hierarchy. Every data engineer's dream (note the irony).
+
+I cracked my knuckles, set the RAG output to save to disk, and launched my first script. LlamaIndex ended up overflowing my laptop's RAM within minutes, choking my OS until everything froze. I tried many configurations, caching systems, and other strategies, but at some point my machine always died.
+
+After debugging, I discovered it was processing huge files that contributed nothing: videos, simulations, backup files... Documents that added nothing to a RAG system, but that LlamaIndex tried to process as if they were text. If a file weighed several gigabytes, the system tried to load it entirely into memory for processing, which was suicide.
+
+I added a filtering system to the pipeline that excluded files by extension and by name patterns (simulation files, numerical results, etc.).
+
+| Category | Excluded extensions |
+|----------|---------------------|
+| Video | mp4, avi, mov, mkv, wmv, flv, webm, m4v, mpg, mpeg, 3gp, mts... |
+| Images | jpg, jpeg, png, gif, bmp, tiff, svg, ico, webp, heic, psd... |
+| Executables | exe, dll, msi, bat, sh, app, dmg, so, jar... |
+| Compressed | zip, rar, 7z, tar, gz, bz2, xz |
+| Simulation | sim, dat |
+| Temporary | tmp, temp, cache, log, swp, pyc, crdownload, partial... |
+| Backups | bak, 3dmbak, dwgbak, dxfbak, pdfbak, stlbak, old, bkp, original... |
+| Email | msg, pst, eml, oft |
+
+I also removed files that were expensive to process and didn't add value either, like CSVs, JSONs, among others. On the other hand, I converted PDF, DOCX, XLSX, PPTX, etc. files to plain text so LlamaIndex could process them without issues.
+
+The result was a 54% reduction in the number of files to index. And of course, my RAM stopped exploding.
+
+I could finally start indexing without fear.
+
+## Problem 3: indexing 451GB of documents without dying in the attempt
+
+A RAG involves creating a vector index file containing document embeddings. Vectors are numerical representations of documents that allow measuring their similarity. LlamaIndex has a simple system you can configure with a couple of lines. You just point it to the directory and it takes care of storing all the information inside in JSON format. It's really convenient, works well, unless you're dealing with hundreds of gigabytes of documents. The system became unmanageable: every time the service restarted, it had to reprocess all documents from scratch, which could take days. Also, the default format is not optimal for large searches (JSON).
+
+I added a checkpoint system to save indexing progress. Every time a problem occurred, I wouldn't lose all progress, but could resume from the last processed file. However, data got corrupted, it was error-prone, and very slow. I was facing a bottleneck I couldn't overcome.
+
+After many trials and errors, and reading more about it, I decided to make the leap to a dedicated vector database: ChromaDB. An open-source database (Apache-2.0 license) for storing and querying vectors. Not to be confused with the Chrome/Chromium browser. ChromaDB is an abstraction layer that stores on top of a traditional database, I configured SQLite, and offers specific functionalities like similarity searches, clustering, etc.
+
+The change was radical and instant. Indexing went from being a monolithic process that loaded everything into memory to a batch pipeline that processed 150 files at a time, generated their embeddings, and stored them directly in ChromaDB. This allowed indexing the 451GB of documents across multiple sessions, with checkpoints, without losing progress on interruptions, without corrupted data. Additionally, it was really easy to back up and restore the index in case of failures (just copy the SQLite file).
+
+The system was ready. With a quick benchmark, I discovered I would need several months to index all the content with my laptop. Now the bottleneck was neither the RAM, nor the indexing system, nor the files, but the GPU.
+
+## Problem 4: my graphics card is not a rocket
+
+My laptop has an integrated graphics card. Processing 500 MB of documents by CPU takes 4-5 hours, not good numbers. I absolutely needed a powerful GPU. In a follow-up meeting, it was decided to rent me a virtual machine with an NVIDIA RTX 4000 SFF Ada, which has 20GB of VRAM. These kinds of rentals are not exactly cheap. Now I was working under more pressure.
+
+I modified my containers and the system was optimized to take advantage of the GPU. I launched my script. After several weeks, between 2 and 3, the indexing process finished without failures. 738,470 vectors, 54GB of index in ChromaDB, and a RAG system ready to answer questions. I copied the ChromaDB database, a SQLite file, to my local machine and that was it. To the relief of my Sysadmin and Project Manager, we could finally shut down the virtual machine. The cost was 184 euros on Hetzner, not cheap.
+
+It was time to build the backend and frontend.
+
+##

+ 112 - 0
source/_posts/慢下来:关于-AI-Agent-编程的反思.md

@@ -0,0 +1,112 @@
+---
+title: 慢下来:关于 AI Agent 编程的反思
+author: Gamehu
+date: 2026-03-27 04:06:34
+categories:
+  - 思考
+tags:
+  - AI
+  - Agent
+  - 编程哲学
+  - 软件工程
+---
+
+Mario Zechner(libGDX 作者)最新文章《Thoughts on slowing the fuck down》对当前 AI Agent 编程热潮提出了尖锐反思。文章指出:**我们正用 Agent 以惊人的速度给自己挖坑**。
+
+## 现状:一切都在崩坏
+
+作者观察到,软件质量正在加速下滑:
+- 98% 正常运行时间成为常态(而非例外)
+- 用户界面出现最奇怪的 bug
+- AWS 被曝 AI 导致故障,随后发布 90 天整改令
+- Windows 质量下滑,微软官方承认并承诺改进
+
+**核心问题**:声称 100% 代码由 AI 生成的公司,持续产出质量最差的产品。
+
+---
+
+## 错误的工作方式
+
+### 1. 复合错误(Compounding Booboos)
+
+**人类 vs Agent 的关键差异:**
+
+| 人类 | Agent |
+|------|-------|
+| 犯错几次后学会不再犯 | 持续重复相同错误 |
+| 是瓶颈,每天只能引入有限错误 | 无瓶颈,几小时生成 2 万行代码 |
+| 痛苦到达阈值会修复 | 无痛感,直到为时已晚 |
+
+**结果**:无害的小错误以不可持续的速度复合,形成代码库怪物。
+
+### 2. 习得性复杂性的商人
+
+Agent 是"复杂性商人":
+- 训练数据中见过大量糟糕的架构决策
+- 被委托架构应用,结果就是 immense complexity
+- 决策永远是局部的,导致代码重复、抽象过度
+
+**惊人对比**:人类企业代码库需要数年才能达到的复杂度,2 人团队 + Agent 几周内就能达成。
+
+### 3. Agentic 搜索召回率低
+
+当代码库膨胀后,Agent 无法找到所有需要修改的代码:
+- 无论是 ripgrep、LSP 还是向量数据库
+- 代码库越大,召回率越低
+- 导致重复代码、不一致性,最终绽放为"美丽的屎花"
+
+---
+
+## 正确的工作方式(作者建议)
+
+### 适合 Agent 的任务
+
+好的 Agent 任务具备以下特征:
+- ✅ 范围可限定,无需理解完整系统
+- ✅ 可闭环评估(有明确的评估函数)
+- ✅ 非关键任务(内部工具、原型)
+- ✅ 人类是最终质量关卡
+
+**反例**:Karpathy 的 auto-research 用于优化启动时间?很好!但产出的代码绝非生产就绪——评估函数只捕获狭窄指标,忽略代码质量、复杂度、正确性。
+
+### 核心建议:慢下来
+
+> "Slowing the fuck down is the way to go."
+
+**具体做法:**
+
+1. **给自己时间思考** —— 真正在构建什么、为什么
+2. **设定代码生成上限** —— 与代码审查能力匹配
+3. **手写架构定义** —— API、系统整体结构亲手写
+4. **与 Agent 结对编程** —— 而非完全委托
+5. **保持在场** —— 看到代码逐步构建,理解系统"感觉"
+
+**为什么**:
+- 摩擦让你更好理解想构建什么
+- 经验和品味在此发挥作用(当前 SOTA 模型无法替代)
+- 慢下来才能学习和成长
+
+### 最终收益
+
+- 系统和代码库保持可维护性
+- 产品带来愉悦而非 slop
+- 构建更少但正确的功能
+- 学会说"不"本身就是功能
+- 理解系统,保持 agency
+- 能修复问题,能重构优化
+
+---
+
+## 关键金句
+
+> "Coding agents are sirens, luring you in with their speed of code generation and jagged intelligence."
+
+> "Your agents never see each other's runs, never get to see all of your codebase... an agent's decisions are always local."
+
+> "All of this requires discipline and agency. All of this requires humans."
+
+---
+
+**原文**:https://mariozechner.at/posts/2026-03-25-thoughts-on-slowing-the-fuck-down/
+
+**作者**:Mario Zechner(libGDX 创始人)