Skip to content
LlamaIndex 文档版本 跟随上游 源快照 88508224 更新于 2026-10-18

在本 notebook 中,我们将利用 Anthropic 的 Claude Opus 4.1 的高级编码能力创建一个可爱的网站,而且我们将在 LlamaIndex 中完成!

使用 Opus 4.1 构建基于 LLM 的助手

1. 安装所需的依赖项

! pip install -q llama-index-llms-anthropic get-code-from-markdown

让我们定义一个辅助函数,帮助我们从 Markdown 中获取代码:

from get_code_from_markdown import get_code_from_markdown

def fetch_code_from_markdown(markdown: str) -> str:
    return get_code_from_markdown(markdown, language="html")

现在让我们初始化 LLM:

import os
import getpass

os.environ["ANTHROPIC_API_KEY"] = getpass.getpass()
from llama_index.llms.anthropic import Anthropic

llm = Anthropic(model="claude-opus-4-1-20250805", max_tokens=12000)
res = llm.complete(
    "Can you build a llama-themed static HTML page, with cute little bouncing animations and blue/white/indigo as theme colors?"
)

现在让我们获取代码并将其写入 HTML 文件!

html_code = fetch_code_from_markdown(res.text)

with open("index.html", "w") as f:
    for block in html_code:
        f.write(block)

现在你可以下载 index.html 并查看结果 😃

Llama Paradise HTML

使用 Opus 4.1 构建智能体

我们还可以使用 Claude Opus 4.1 构建一个简单的计算器智能体

from llama_index.core.agent.workflow import FunctionAgent

def multiply(a: int, b: int) -> int:
    """Multiply two integers and return an integer"""
    return a * b

def add(a: int, b: int) -> int:
    """Sum two integers and return an integer"""
    return a + b

agent = FunctionAgent(
    name="CalculatorAgent",
    description="Useful to perform basic arithmetic operations",
    system_prompt="You are a calculator agent, you should perform arithmetic operations using the tools available to you.",
    tools=[multiply, add],
    llm=llm,
)

现在让我们运行该智能体并获取乘法结果:

from llama_index.core.agent.workflow import ToolCall, ToolCallResult

handler = agent.run("What is 60 multiplied by 95?")

async for event in handler.stream_events():
    if isinstance(event, ToolCallResult):
        print(
            f"Result from calling tool {event.tool_name}:\n\n{event.tool_output}"
        )
    if isinstance(event, ToolCall):
        print(
            f"Calling tool {event.tool_name} with arguments:\n\n{event.tool_kwargs}"
        )

response = await handler

print("Final response")
print(response)
Calling tool multiply with arguments:

{'a': 60, 'b': 95}
Result from calling tool multiply:

5700
Final response
60 multiplied by 95 equals 5,700.

我们再用求和运行一次!

from llama_index.core.agent.workflow import ToolCall, ToolCallResult

handler = agent.run("What is 1234 plus 5678?")

async for event in handler.stream_events():
    if isinstance(event, ToolCallResult):
        print(
            f"Result from calling tool {event.tool_name}:\n\n{event.tool_output}"
        )
    if isinstance(event, ToolCall):
        print(
            f"Calling tool {event.tool_name} with arguments:\n\n{event.tool_kwargs}"
        )

response = await handler

print("Final response")
print(response)
Calling tool add with arguments:

{'a': 1234, 'b': 5678}
Result from calling tool add:

6912
Final response
1234 plus 5678 equals 6912.

如果你想了解更多关于 Anthropic 的内容,请务必查看我们的通用示例 notebook

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。