返回顶部
s

strandsStrands SDK

Build and run Python-based AI agents using the AWS Strands SDK. Use when you need to create autonomous agents, multi-agent workflows, custom tools, or integrate with MCP servers. Supports Ollama (local), Anthropic, OpenAI, Bedrock, and other model providers. Use for agent scaffolding, tool creation, and running agent tasks programmatically.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 2.0.3
安全检测
已通过
1,720
下载量
免费
免费
0
收藏
概述
安装方式
版本历史

strands

Strands Agents SDK

使用 Strands SDK(Apache-2.0 许可,来自 AWS)在 Python 中构建 AI 代理。

已验证版本:strands-agents==1.23.0,strands-agents-tools==0.2.19

前置条件

bash

安装 SDK + 工具(推荐使用 pipx 进行隔离安装)


pipx install strands-agents-builder # 包含 strands-agents + strands-agents-tools + CLI

或直接安装

pip install strands-agents strands-agents-tools

核心概念:Bedrock 为默认

Agent() 在不指定 model= 参数时,默认使用 Amazon Bedrock——具体为 us-west-2 区域的 us.anthropic.claude-sonnet-4-20250514-v1:0。这需要 AWS 凭证。要使用其他提供商,请显式传递 model= 参数。

默认模型常量:strands.models.bedrock.DEFAULTBEDROCKMODEL_ID

快速入门——本地代理(Ollama)

python
from strands import Agent
from strands.models.ollama import OllamaModel

host 是必需的位置参数

model = OllamaModel(http://localhost:11434, model_id=qwen3:latest) agent = Agent(model=model) result = agent(法国的首都是什么?) print(result)

注意: 并非所有开源模型都支持工具调用。经过消融处理的模型通常在消融过程中丢失了函数调用能力。请先使用原始模型(qwen3、llama3.x、mistral)进行测试。

快速入门——Bedrock(默认提供商)

python
from strands import Agent

未指定模型 → BedrockModel(Claude Sonnet 4,us-west-2)

需要 AWS 凭证(~/.aws/credentials 或环境变量)

agent = Agent() result = agent(解释量子计算)

显式指定 Bedrock 模型:

from strands.models import BedrockModel model = BedrockModel(model_id=us.anthropic.claude-sonnet-4-20250514-v1:0) agent = Agent(model=model)

快速入门——Anthropic(直接 API)

python
from strands import Agent
from strands.models.anthropic import AnthropicModel

max_tokens 是 Required[int]——必须提供

model = AnthropicModel(modelid=claude-sonnet-4-20250514, maxtokens=4096) agent = Agent(model=model) result = agent(解释量子计算)

需要 ANTHROPICAPIKEY 环境变量。

快速入门——OpenAI

python
from strands import Agent
from strands.models.openai import OpenAIModel

model = OpenAIModel(model_id=gpt-4.1)
agent = Agent(model=model)

需要 OPENAIAPIKEY 环境变量。

创建自定义工具

使用 @tool 装饰器。类型提示成为模式;文档字符串成为描述:

python
from strands import Agent, tool

@tool
def read_file(path: str) -> str:
读取指定路径的文件内容。

参数:
path:要读取的文件系统路径。

with open(path) as f:
return f.read()

@tool
def write_file(path: str, content: str) -> str:
将内容写入文件。

参数:
path:要写入的文件系统路径。
content:要写入的文本内容。

with open(path, w) as f:
f.write(content)
return f已将 {len(content)} 字节写入 {path}

agent = Agent(model=model, tools=[readfile, writefile])
agent(读取 /tmp/test.txt 并总结它)

ToolContext

工具可以通过 ToolContext 访问代理状态:

python
from strands import tool
from strands.types.tools import ToolContext

@tool
def statefultool(query: str, toolcontext: ToolContext) -> str:
一个访问代理状态的工具。

参数:
query:输入查询。

# 访问共享的代理状态
count = toolcontext.state.get(callcount, 0) + 1
toolcontext.state[callcount] = count
return f第 #{count} 次调用:{query}

内置工具(46 个可用)

strands-agents-tools 提供预构建的工具:

python
from strandstools import calculator, fileread, filewrite, shell, httprequest
agent = Agent(model=model, tools=[calculator, file_read, shell])

完整列表:calculator、fileread、filewrite、shell、httprequest、editor、imagereader、pythonrepl、currenttime、think、stop、sleep、environment、retrieve、searchvideo、chatvideo、speak、generateimage、generateimagestability、diagram、journal、memory、agentcorememory、elasticsearchmemory、mongodbmemory、mem0memory、rss、cron、batch、workflow、useagent、usellm、useaws、usecomputer、loadtool、handofftouser、slack、swarm、graph、a2aclient、mcpclient、exa、tavily、brightdata、nova_reels。

热重载:Agent(loadtoolsfrom_directory=True) 会监视 ./tools/ 目录的变化。

MCP 集成

连接到任何模型上下文协议服务器。MCPClient 实现了 ToolProvider——直接在工具列表中传递它:

python
from strands import Agent
from strands.tools.mcp import MCPClient
from mcp import stdio_client, StdioServerParameters

MCPClient 接受一个返回传输层的可调用对象

mcp = MCPClient(lambda: stdio_client(StdioServerParameters( command=uvx, args=[some-mcp-server@latest] )))

作为上下文管理器使用——MCPClient 是一个 ToolProvider

with mcp: agent = Agent(model=model, tools=[mcp]) agent(使用 MCP 工具做点什么)

SSE 传输:
python
from mcp.client.sse import sse_client
mcp = MCPClient(lambda: sse_client(http://localhost:8080/sse))

多代理模式

代理作为工具

嵌套代理——内部代理成为外部代理的工具:

python
researcher = Agent(model=model, system_prompt=你是一个研究助手。)
writer = Agent(model=model, system_prompt=你是一个写作者。)

orchestrator = Agent(
model=model,
tools=[researcher, writer],
system_prompt=你协调研究和写作任务。
)
orchestrator(研究量子计算并写一篇博客文章)

群体模式

具有共享上下文和自主交接协调的自组织代理团队:

python
from strands.multiagent.swarm import Swarm

代理需要 name + description 用于交接识别

researcher = Agent( model=model, name=researcher, description=查找和总结信息 ) writer = Agent( model=model, name=writer, description=创建精炼的内容 )

swarm = Swarm(
nodes=[researcher, writer],
entry_point=researcher, # 可选——默认为第一个代理
max_handoffs=20, # 默认值
max_iterations=20, # 默认值
execution_timeout=900.0, # 默认 15 分钟
node_timeout=300.0 # 每个节点默认 5 分钟
)
result = swarm(研究 AI 代理,然后交给写作者写一篇博客文章)

群体自动注入一个 handofftoagent 工具。代理通过使用目标代理的名称调用它来进行交接。支持中断/恢复、会话持久化和重复交接检测。

图模式(DAG)

通过 GraphBuilder 进行基于依赖关系的确定性执行:

python
from strands.multiagent.graph import GraphBuilder

builder = GraphBuilder()
researchnode = builder.addnode(researcher, node_id=research)
writingnode = builder.addnode(writer, node_id=writing)
builder.add_edge(research, writing)
builder.setentrypoint(research)

可选:条件边

builder.add_edge(research, writing,

condition=lambda state: complete in str(state.completed_nodes))

graph = builder.build()
result = graph(写一篇关于 AI 代理的博客文章)

支持

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 strands-1776362017 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 strands-1776362017 技能

通过命令行安装

skillhub install strands-1776362017

下载

⬇ 下载 strands v2.0.3(免费)

文件大小: 13.62 KB | 发布时间: 2026-4-17 15:26

v2.0.3 最新 2026-4-17 15:26
v2.0.3: Debug publish

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部