一、项目概述

Claude Cookbooks 是 Anthropic 官方推出的开发者资源库,提供了一系列可复制的代码片段和实战指南,帮助开发者快速构建基于 Claude API 的应用程序。该项目本质上是一个精心策划的"食谱集合",涵盖了从基础 API 调用到高级多模态处理的完整开发场景。

核心特性

  • 即用型代码样本:所有示例都是可以直接集成到项目中的完整代码片段
  • 多领域覆盖:包含分类、检索增强生成(RAG)、摘要、工具调用、多模态处理等核心能力
  • Python 优先:代码示例主要使用 Python,但概念可迁移到任何支持 Claude API 的编程语言
  • Jupyter Notebook 格式:交互式文档,便于学习和实验
  • 第三方集成示例:提供 Pinecone、Voyage AI、Wikipedia 等主流工具的集成方案

二、技术原理

架构设计

Claude Cookbooks 采用模块化组织方式,按照功能领域划分为多个独立目录:

anthropics/claude-cookbooks/
├── capabilities/          # 核心能力示例
│   ├── classification/   # 分类任务
│   ├── retrieval_augmented_generation/  # RAG
│   └── summarization/    # 摘要生成
├── tool_use/             # 工具调用与集成
├── third_party/          # 第三方服务集成
│   ├── Pinecone/         # 向量数据库
│   ├── Wikipedia/        # 知识库集成
│   └── VoyageAI/         # Embedding 服务
├── multimodal/           # 多模态能力
│   ├── getting_started_with_vision.ipynb
│   ├── best_practices_for_vision.ipynb
│   └── reading_charts_graphs_powerpoints.ipynb
└── misc/                 # 高级技巧
    ├── pdf_upload_summarization.ipynb
    ├── prompt_caching.ipynb
    └── building_evals.ipynb

核心技术栈与选型理由

项目使用现代化的 Python 数据科学生态:

# pyproject.toml 中的核心依赖
dependencies = [
    "anthropic>=0.77.0",           # Claude API 官方 SDK
    "claude-agent-sdk>=0.1.50",    # Claude Agent SDK
    "ipykernel>=7.1.0",            # Jupyter 内核
    "numpy>=2.3.4",                # 数值计算
    "pandas>=2.3.3",               # 数据处理
    "matplotlib>=3.10.8",          # 数据可视化
    "voyageai>=0.3.5",             # Embedding 服务
]

选型理由:

  1. anthropic SDK:官方维护,支持最新 Claude 模型特性(如 Tool Use、Vision)
  2. Jupyter Notebook:交互式文档格式,适合教学和技术演示
  3. Pandas + NumPy:处理结构化数据和进行数值分析的标准组合
  4. Matplotlib:可视化图表生成,配合多模态示例展示 Claude 的图表理解能力

关键算法与设计模式

1. 检索增强生成(RAG)模式

# third_party/Pinecone/rag_using_pinecone.ipynb 中的核心逻辑
from anthropic import Anthropic
import voyageai

# 1. 使用 Voyage AI 生成查询的 embedding
vo = voyageai.Client(api_key="your-voyage-key")
query_embedding = vo.embed(texts=[user_query], model="voyage-2").embeddings[0]

# 2. 在 Pinecone 中检索相关文档
index = pinecone.Index("your-index")
results = index.query(vector=query_embedding, top_k=5, include_metadata=True)

# 3. 构建增强 prompt 并调用 Claude
context = "\n".join([match['metadata']['text'] for match in results['matches']])
prompt = f"""Use the following context to answer the question:

Context: {context}

Question: {user_query}

Answer:"""

client = Anthropic(api_key="your-anthropic-key")
response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    messages=[{"role": "user", "content": prompt}]
)

设计亮点:

  • 使用专门的 embedding 模型(Voyage AI)而非 Claude 自带的 embedding
  • 分层清晰:检索、增强、生成三个步骤解耦
  • 支持动态上下文注入,实现知识库扩展

2. 工具调用(Tool Use)模式

# tool_use/calculator_tool.ipynb 中的工具定义与调用
tools = [
    {
        "name": "calculator",
        "description": "Perform mathematical calculations",
        "input_schema": {
            "type": "object",
            "properties": {
                "expression": {
                    "type": "string",
                    "description": "The mathematical expression to evaluate"
                }
            },
            "required": ["expression"]
        }
    }
]

# Claude 决定调用工具
response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    tools=tools,
    messages=[{"role": "user", "content": "What is 1234 * 5678?"}]
)

# 解析工具调用并执行
if response.stop_reason == "tool_use":
    tool_use = response.content[0]
    if tool_use.name == "calculator":
        expression = tool_use.input["expression"]
        result = eval(expression)  # 注意:生产环境应使用安全的计算器
        
        # 将结果返回给 Claude
        response = client.messages.create(
            model="claude-3-5-sonnet-20241022",
            max_tokens=1024,
            tools=tools,
            messages=[
                {"role": "user", "content": "What is 1234 * 5678?"},
                {"role": "assistant", "content": response.content},
                {"role": "user", "content": [{
                    "type": "tool_result",
                    "tool_use_id": tool_use.id,
                    "content": str(result)
                }]}
            ]
        )

技术要点:

  • Claude 能够理解工具描述并自主决定何时调用
  • 支持多轮工具调用(连续推理)
  • 工具结果以结构化方式返回,便于 Claude 整合到最终答案中

3. 多模态视觉处理

# multimodal/getting_started_with_vision.ipynb
import base64
from anthropic import Anthropic

# 读取图片并编码为 base64
with open("chart.png", "rb") as f:
    image_data = base64.b64encode(f.read()).decode('utf-8')

client = Anthropic(api_key="your-key")
message = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "image",
                    "source": {
                        "type": "base64",
                        "media_type": "image/png",
                        "data": image_data
                    }
                },
                {
                    "type": "text",
                    "text": "Analyze this chart and summarize the key trends."
                }
            ]
        }
    ]
)

能力展示:

  • 图表理解与数据提取
  • 表单内容识别(OCR)
  • 技术图纸分析
  • 多图对比与推理

数据流分析

典型的数据流(以 RAG 为例):

用户输入
  ↓
[Query Embedding] → Voyage AI API
  ↓
[Vector Search] → Pinecone Index
  ↓
[Context Retrieval] ← Top-K 相关文档
  ↓
[Prompt Augmentation] → 构建增强提示词
  ↓
[Claude API Call] → Claude 3.5 Sonnet
  ↓
[Response Generation] → 基于上下文的答案
  ↓
用户输出

关键优化点:

  1. Embedding 缓存:对常见查询预先计算 embedding
  2. 上下文压缩:使用 Claude 对检索结果进行摘要,减少 token 消耗
  3. 并行检索:多个知识库同时查询,提高召回率

三、安装与快速开始

环境要求

  • Python 3.11 或 3.12(不支持 3.13+)
  • Claude API Key(在 Anthropic Console 免费注册获取)
  • 可选:Voyage AI API Key(用于 Embedding 示例)
  • 可选:Pinecone API Key(用于向量数据库示例)

安装步骤

# 1. 克隆仓库
git clone https://github.com/anthropics/claude-cookbooks.git
cd claude-cookbooks

# 2. 安装 uv(推荐的 Python 包管理器)
curl -LsSf https://astral.sh/uv/install.sh | sh

# 3. 安装依赖
uv sync --all-extras

# 4. 配置环境变量
cp .env.example .env
# 编辑 .env 文件,填入你的 API Key

最简运行示例

# quickstart.py - 最简单的 Claude API 调用
import os
from anthropic import Anthropic

client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))

message = client.messages.create(
    model="claude-3-5-haiku-20241022",  # 快速且经济的模型
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Explain RAG in one sentence."}
    ]
)

print(message.content[0].text)

运行:

export ANTHROPIC_API_KEY="your-key-here"
uv run python quickstart.py

四、使用方法与实战

基础用法:文本分类

# capabilities/classification/ 目录下的示例
from anthropic import Anthropic
import json

client = Anthropic()

def classify_email(email_text):
    prompt = f"""Classify the following email into one of these categories:
- Inquiry
- Complaint
- Feedback
- Other

Email: {email_text}

Respond with a JSON object like {{"category": "Inquiry"}}"""

    response = client.messages.create(
        model="claude-3-5-haiku-20241022",
        max_tokens=256,
        messages=[{"role": "user", "content": prompt}]
    )
    
    # 启用 JSON 模式确保结构化输出
    return json.loads(response.content[0].text)

# 测试
email = "Hi, I want to know if you support custom integrations."
result = classify_email(email)
print(result)  # {"category": "Inquiry"}

进阶用法:构建客服 Agent

# tool_use/customer_service_agent.ipynb 的简化版本
from anthropic import Anthropic
import json

client = Anthropic()

# 定义可用工具
tools = [
    {
        "name": "lookup_order",
        "description": "Look up an order by order ID",
        "input_schema": {
            "type": "object",
            "properties": {
                "order_id": {"type": "string"}
            },
            "required": ["order_id"]
        }
    },
    {
        "name": "cancel_order",
        "description": "Cancel an order",
        "input_schema": {
            "type": "object",
            "properties": {
                "order_id": {"type": "string"}
            },
            "required": ["order_id"]
        }
    }
]

# 模拟数据库
orders_db = {
    "ORD-123": {"status": "shipped", "item": "Widget A"},
    "ORD-456": {"status": "processing", "item": "Widget B"}
}

def execute_tool(tool_name, tool_input):
    if tool_name == "lookup_order":
        order_id = tool_input["order_id"]
        return orders_db.get(order_id, {"error": "Order not found"})
    elif tool_name == "cancel_order":
        order_id = tool_input["order_id"]
        if order_id in orders_db and orders_db[order_id]["status"] == "processing":
            del orders_db[order_id]
            return {"success": True, "message": f"Order {order_id} cancelled"}
        else:
            return {"success": False, "message": "Cannot cancel this order"}

# 多轮对话处理
conversation = []

while True:
    user_input = input("Customer: ")
    if user_input.lower() in ['quit', 'exit']:
        break
    
    conversation.append({"role": "user", "content": user_input})
    
    response = client.messages.create(
        model="claude-3-5-sonnet-20241022",
        max_tokens=1024,
        tools=tools,
        messages=conversation
    )
    
    # 处理工具调用
    while response.stop_reason == "tool_use":
        tool_results = []
        assistant_content = []
        
        for content_block in response.content:
            if content_block.type == "text":
                assistant_content.append(content_block)
                print("Agent:", content_block.text)
            elif content_block.type == "tool_use":
                assistant_content.append(content_block)
                tool_name = content_block.name
                tool_input = content_block.input
                
                # 执行工具
                result = execute_tool(tool_name, tool_input)
                tool_results.append({
                    "type": "tool_result",
                    "tool_use_id": content_block.id,
                    "content": json.dumps(result)
                })
        
        # 将工具结果返回给 Claude
        conversation.append({"role": "assistant", "content": assistant_content})
        conversation.append({"role": "user", "content": tool_results})
        
        response = client.messages.create(
            model="claude-3-5-sonnet-20241022",
            max_tokens=1024,
            tools=tools,
            messages=conversation
        )
    
    # 最终文本回复
    print("Agent:", response.content[0].text)
    conversation.append({"role": "assistant", "content": response.content})

实战场景:

  • 电商客服:查询订单、取消订单、退款处理
  • 技术支持:查询文档、创建工单、路由到人工
  • 预约系统:检查日历、预约、取消预约

实际项目示例:PDF 文档问答系统

# misc/pdf_upload_summarization.ipynb 的实战应用
import PyPDF2
from anthropic import Anthropic
import voyageai

class PDFQASystem:
    def __init__(self, anthropic_key, voyage_key):
        self.anthropic = Anthropic(api_key=anthropic_key)
        self.voyage = voyageai.Client(api_key=voyage_key)
        self.document_chunks = []
    
    def load_pdf(self, pdf_path):
        """解析 PDF 并分块"""
        with open(pdf_path, 'rb') as f:
            reader = PyPDF2.PdfReader(f)
            text = ""
            for page in reader.pages:
                text += page.extract_text()
        
        # 简单分块(实际项目应使用语义分块)
        chunk_size = 1000
        self.document_chunks = [
            text[i:i+chunk_size] 
            for i in range(0, len(text), chunk_size)
        ]
    
    def answer_question(self, question):
        """基于 PDF 内容回答问题"""
        # 1. 为问题生成 embedding
        query_emb = self.voyage.embed(
            texts=[question], 
            model="voyage-2"
        ).embeddings[0]
        
        # 2. 为文档块生成 embedding(实际应预先计算并存储)
        chunk_embs = self.voyage.embed(
            texts=self.document_chunks,
            model="voyage-2"
        ).embeddings
        
        # 3. 计算相似度并检索最相关的块
        import numpy as np
        similarities = np.dot(chunk_embs, query_emb)
        top_k_indices = np.argsort(similarities)[-3:][::-1]
        relevant_chunks = [self.document_chunks[i] for i in top_k_indices]
        
        # 4. 构建 RAG prompt
        context = "\n---\n".join(relevant_chunks)
        prompt = f"""Answer the question based on the following context:

Context: {context}

Question: {question}

If the context doesn't contain the answer, say "I don't have enough information to answer this question."""

        # 5. 调用 Claude
        response = self.anthropic.messages.create(
            model="claude-3-5-sonnet-20241022",
            max_tokens=1024,
            messages=[{"role": "user", "content": prompt}]
        )
        
        return response.content[0].text

# 使用示例
qa = PDFQASystem(
    anthropic_key="your-anthropic-key",
    voyage_key="your-voyage-key"
)
qa.load_pdf("technical_manual.pdf")
answer = qa.answer_question("How to troubleshoot error code 500?")
print(answer)

五、常见问题与解决方案

安装失败

问题:uv sync 失败,提示 Python 版本不兼容

解决方案:

# 检查 Python 版本
python3 --version  # 应确保是 3.11 或 3.12

# 如果版本不对,使用 pyenv 安装正确版本
brew install pyenv
pyenv install 3.12.0
pyenv local 3.12.0

# 重新安装
uv sync --all-extras

运行时错误

问题:ImportError: cannot import name 'Anthropic' from 'anthropic'

原因:anthropic SDK 版本过旧

解决方案:

# 更新到最新版本
uv pip install --upgrade anthropic

# 或使用 uv sync 重新同步
uv sync --all-extras --refresh

API 限流问题

问题:RateLimitError: Too many requests

解决方案:

  1. 实现指数退避重试机制
  2. 使用批处理 API(如果支持)
  3. 申请提高 API quota
import time
from anthropic import Anthropic, RateLimitError

client = Anthropic()

def call_with_retry(prompt, max_retries=3):
    for attempt in range(max_retries):
        try:
            response = client.messages.create(
                model="claude-3-5-haiku-20241022",
                max_tokens=1024,
                messages=[{"role": "user", "content": prompt}]
            )
            return response.content[0].text
        except RateLimitError:
            wait_time = 2 ** attempt  # 指数退避:1s, 2s, 4s
            print(f"Rate limited. Waiting {wait_time}s...")
            time.sleep(wait_time)
    
    raise Exception("Max retries exceeded")

# 使用
result = call_with_retry("Explain quantum computing.")

笔记本执行失败

问题:Jupyter Notebook 中的代码单元格报错

诊断步骤:

  1. 检查 .env 文件是否配置了所有必需的 API Key
  2. 确认已安装所有依赖:uv run pytest tests/notebook_tests/ -m "not slow"
  3. 检查 Python 内核是否选择正确的虚拟环境

解决方案:

# 在笔记本中检查环境
import sys
print(sys.executable)  # 应确保指向 uv 创建的虚拟环境

# 手动安装缺失的包
uv pip install missing-package-name

性能问题

问题:RAG 查询速度慢

优化方案:

  1. 使用 Prompt Caching(Claude 3.5 支持):
# misc/prompt_caching.ipynb
response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    system={
        "type": "text",
        "text": "You are a helpful assistant. Here is the documentation: ...",
        "cache_control": {"type": "ephemeral"}  # 缓存系统提示词
    },
    messages=[{"role": "user", "content": question}]
)
  1. 并行 Embedding 计算:
from concurrent.futures import ThreadPoolExecutor

def batch_embed(texts, batch_size=32):
    with ThreadPoolExecutor() as executor:
        futures = []
        for i in range(0, len(texts), batch_size):
            batch = texts[i:i+batch_size]
            future = executor.submit(
                voyage.embed, texts=batch, model="voyage-2"
            )
            futures.append(future)
        results = [f.result() for f in futures]
    return results
  1. 使用更轻量的 Embedding 模型:考虑 voyage-lite 或本地模型

兼容性问题

问题:代码在 Python 3.13 上运行失败

原因:项目明确不支持 Python 3.13+(pyproject.toml 中限制)

解决方案:

# 使用 pyenv 切换到兼容版本
pyenv install 3.12.0
pyenv local 3.12.0

# 或使用 Docker 容器
docker run -it --rm -v $(pwd):/app python:3.12 bash

六、总结

Anthropic Claude Cookbooks 是一个极其宝贵的学习资源,特别适合以下人群:

  1. AI 应用开发者:快速掌握 Claude API 的各种能力和最佳实践
  2. 技术团队 Leader:评估 Claude 是否适合集成到产品中
  3. AI 产品经理:理解 Claude 的功能边界和适用场景
  4. 学习者:通过实战代码深入理解 LLM 应用开发

项目亮点

  • ✅ 官方维护:Anthropic 团队持续更新,跟随最新模型特性
  • ✅ 生产级代码:不是简单的 demo,而是可以直接改造使用的实战代码
  • ✅ 覆盖全面:从基础到进阶,从文本到多模态,从单次调用到 Agent 系统
  • ✅ 社区活跃:欢迎贡献,有完善的测试和代码规范

学习路径建议

  1. 初学者:从 anthropic_api_fundamentals 课程开始 → 阅读 getting_started_with_vision.ipynb
  2. 进阶开发者:直接跳到 tool_use/ 目录,学习如何构建 Agent
  3. 高级用户:研究 multimodal/using_sub_agents.ipynb,学习多模型协作

实际应用价值

通过这些 Cookbook,你可以快速实现:

  • 智能文档问答系统(RAG)
  • 多模态内容分析工具(图表、PDF、图片)
  • 自动化客服 Agent
  • 内容审核与分类系统
  • 代码分析与生成工具

项目链接:

  • GitHub:https://github.com/anthropics/claude-cookbooks
  • Anthropic 文档:https://docs.anthropic.com
  • Discord 社区:https://www.anthropic.com/discord

如果你正在构建基于 Claude 的应用,这个项目绝对值得深入研究和实践! EOF echo “Blog post created successfully!”