主题
Agent SDK 实战项目
项目: 代码审查 Agent
构建一个自动审查代码的 Agent
python
import os
from anthropic import Anthropic
client = Anthropic()
def review_code(file_path: str) -> dict:
"""审查代码文件"""
with open(file_path, 'r') as f:
content = f.read()
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
system=[{
"type": "text",
"text": f"""请审查以下代码并提供反馈:
文件: {file_path}
内容:{content} }], messages=[{"role": "user", "content": f"Review this code:\n\n{content}"}] ) return { "feedback": response.content[0].text, "file_path": file_path, "issues": [] # 可以解析响应提取具体问题 }
## 使用示例
```python
result = review_code("app.js")
print(f"反馈: {result['feedback']}")
print(f"问题: {result['issues']}")下一节: 高级功能