LLM 上下文协议:为什么你的 AI 助手需要记忆、推理和示例

发布于 2025 年 1 月 24 日 · 阅读时长 8 分钟


教机器人编码的问题

想象一下:你递给某人一本字典,让他们写诗。他们知道每个单词,包括它的拼写和定义——但他们知道单词是如何组合在一起的吗?为什么某些组合能唤起情感,而其他组合却毫无效果?当我们把 API 文档交给 AI 助手,并期待它能带来奇迹时,我们实际上正在做的事情正是如此。

我花了无数时间看着我的人工智能编程助手费力地使用我自己的库 Crawl4AI。尽管我提供了详尽的文档,它还是会生成一些技术上正确但实际上毫无用处的代码。就像一个游客照着常用语手册说话一样——语法上没问题,但文化上却完全听不懂。

进入三维上下文协议

如果我们不是倾倒信息,而是提供智慧,会怎么样?不仅仅是“是什么”,还有“如何”和“为什么”?这促使我开发了一种名为“LLM 语境协议”的方法——一种反映人类实际掌握图书馆方式的结构化方法。

可以将其视为 AI 环境中的 HTTP。正如 HTTP 不会规定您的网站内容,但会提供可靠的通信结构一样,该协议也不会规定您如何编写文档——它为您的 AI 真正理解代码所需的内容提供了一个框架。

图书馆智慧的三大支柱

#### 🧠 **Memory: The Foundation**
# AsyncWebCrawler.arun() - Memory Context

## Signature
async def arun(
    url: str,
    config: CrawlerConfig = None,
    session_id: str = None,
    **kwargs
) -> CrawlResult

## Parameters
- url: Target URL to crawl
- config: Optional configuration object
- session_id: Optional session identifier for caching
...
This is your API reference—the facts, the parameters, the return types. It's the easiest part to generate and, ironically, the least useful in isolation. It's like memorizing a dictionary without understanding grammar.
#### 🎯 **Reasoning: The Soul**
# AsyncWebCrawler Design Philosophy - Reasoning Context

## Why Async-First Architecture?

Crawl4AI uses AsyncWebCrawler as its primary interface because modern web 
scraping demands concurrency. Here's the thinking:

1. **Network I/O is slow**: Waiting synchronously wastes 90% of execution time
2. **Modern sites are complex**: Multiple resources load in parallel
3. **Scale matters**: You're rarely crawling just one page

## When to Use Session Management

Session management isn't just about performance—it's about appearing human:
- Use sessions when crawling multiple pages from the same domain
- Reuse browser contexts to maintain cookies and local storage
- But don't overdo it: too long sessions look suspicious

## The Cache Strategy Decision Tree
if static_content and infrequent_updates:
    use_cache_mode('read_write')
elif dynamic_content and real_time_needed:
    use_cache_mode('bypass')
else:
    use_cache_mode('read_only')  # Safe default
This is where the library creator's philosophy lives. It's not just *what* the library does, but *why* it does it that way. This is the hardest part to write because it requires genuine understanding—and it's a red flag when a library lacks it.
#### 💻 **Examples: The Practice**
# Crawling with JavaScript execution
result = await crawler.arun(
    url="https://example.com",
    js_code="window.scrollTo(0, document.body.scrollHeight);",
    wait_for="css:.lazy-loaded-content"
)

# Extracting structured data with CSS selectors
result = await crawler.arun(
    url="https://shop.example.com",
    extraction_strategy=CSSExtractionStrategy({
        "prices": "span.price::text",
        "titles": "h2.product-title::text"
    })
)

# Session-based crawling with custom headers
async with crawler:
    result1 = await crawler.arun(url1, session_id="product_scan")
    result2 = await crawler.arun(url2, session_id="product_scan")
Pure code. No fluff. Just patterns in action. Because sometimes, you just need to see how it's done.

为什么这很重要(特别是对于小型法学硕士)

AI 助手的一大特点是:小机器人无法凭空想象。它们就像充满热情的实习生——潜力无限,但需要明确的指导。当你依赖大型语言模型从原始 API 文档中“摸索”时,你就是在要求它从头开始重塑你的库的理念。每一次都是如此。

通过提供涵盖这三个维度的结构化语境,我们不仅仅是在记录,更是在教学。我们传递的不仅仅是知识,更是智慧。

图书馆的文化DNA

🧬
Your library's reasoning is its cultural DNA.
It reflects your taste, your architectural decisions, your opinions about how things should be done. A library without reasoning is like a recipe without techniques—sure, you have the ingredients, but good luck making something edible.

想一想:当你学习一个新的库时,你真正想要的是什么?你想要精通。而精通源于理解:记忆告诉你什么是可能的;推理告诉你什么是合理的;示例告诉你什么是实用的。

他们共同创造智慧。

超越手动文档

现在,事情变得有趣起来。我没有为 Crawl4AI 手工编写数千行结构化文档。谁有那么多时间?相反,我构建了一个工具,它:

  1. 分析你的代码库
  2. 提取 API 签名和结构(内存)
  3. 识别模式和架构决策(推理)
  4. 从测试和示例中收集实际使用情况(示例)
  5. 生成结构化的 LLM 上下文文件

多么美妙?这个工具正在成为 Crawl4AI 本身的一部分。因为如果我们要彻底改变 AI 理解代码的方式,不妨将其自动化。

协议,而不是处方

请记住:这是一个协议,而不是规定。正如 HTTP 不会告诉你要构建什么样的网站一样,LLM 上下文协议也不会规定你的文档风格。它只是说:

“如果你想让人工智能真正理解你的图书馆,它需要三样东西:事实、哲学和模式。”

如何传递这些信息取决于你。该协议只是确保重要的信息在翻译过程中不会丢失。

亲自尝试

想在自己的库中实现这个功能吗?上下文生成工具将作为 Crawl4AI 的一部分开源。如果您有兴趣抢先体验或想讨论一下这个方法,请给我发送私信 X @unclecode

因为让我们面对现实:如果我们要生活在一个由人工智能编写一半代码的世界里,我们不妨正确地教它。


最后的想法

Memory + Reasoning + Examples = Wisdom
And wisdom, not information, is what makes great developers—human or artificial.

想看看实际效果吗?查看Crawl4AI LLM 上下文生成器,体验结构化上下文带来的不同。


> Feedback