-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
134 lines (103 loc) · 3.62 KB
/
Copy pathagent.py
File metadata and controls
134 lines (103 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""Chapter 2 reference: an Agent Loop with one read-only tool."""
from collections.abc import Callable, Iterable
import os
from pathlib import Path
from typing import Any
import anthropic
from tools import execute_tool, tool_definitions
DEFAULT_BASE_URL = "https://api.deepseek.com/anthropic"
DEFAULT_MODEL = "claude-sonnet-4.6"
SYSTEM_PROMPT = "You are a helpful coding assistant."
SCRIPT_DIR = Path(__file__).resolve().parent
def create_client() -> anthropic.Anthropic:
"""Create an Anthropic client configured for DeepSeek by default."""
api_key = os.getenv("DEEPSEEK_API_KEY") or os.getenv("ANTHROPIC_API_KEY")
if not api_key:
raise RuntimeError(
"Set DEEPSEEK_API_KEY or ANTHROPIC_API_KEY before running agent.py."
)
base_url = os.getenv("ANTHROPIC_BASE_URL", DEFAULT_BASE_URL)
return anthropic.Anthropic(base_url=base_url, api_key=api_key)
def assistant_content(blocks: Iterable[Any]) -> list[dict[str, Any]]:
"""Keep text and tool calls while omitting thinking blocks."""
content: list[dict[str, Any]] = []
for block in blocks:
if block.type == "text":
content.append({"type": "text", "text": block.text})
elif block.type == "tool_use":
content.append(
{
"type": "tool_use",
"id": block.id,
"name": block.name,
"input": block.input,
}
)
return content
def execute_tool_calls(blocks: Iterable[Any]) -> list[dict[str, Any]]:
"""Execute every tool request and pair each result with its call ID."""
results: list[dict[str, Any]] = []
for block in blocks:
if block.type != "tool_use":
continue
result = execute_tool(block.name, block.input)
results.append(
{
"type": "tool_result",
"tool_use_id": block.id,
"content": result,
}
)
return results
def run_agent_turn(
client: anthropic.Anthropic,
messages: list[dict[str, Any]],
*,
model: str = DEFAULT_MODEL,
output_fn: Callable[[str], None] = print,
) -> None:
"""Keep calling the model until it returns no more tool requests."""
while True:
response = client.messages.create(
model=model,
max_tokens=8192,
system=SYSTEM_PROMPT,
messages=messages,
tools=tool_definitions,
)
content = assistant_content(response.content)
for block in content:
if block["type"] == "text":
output_fn(block["text"])
messages.append({"role": "assistant", "content": content})
tool_results = execute_tool_calls(response.content)
if not tool_results:
return
messages.append({"role": "user", "content": tool_results})
def run_chat(
client: anthropic.Anthropic,
*,
model: str = DEFAULT_MODEL,
input_fn: Callable[[str], str] = input,
output_fn: Callable[[str], None] = print,
) -> None:
"""Run the outer user-input loop around the inner tool loop."""
messages: list[dict[str, Any]] = []
while True:
user_input = input_fn("> ")
if user_input.strip().lower() == "exit":
return
if not user_input.strip():
continue
messages.append({"role": "user", "content": user_input})
run_agent_turn(
client,
messages,
model=model,
output_fn=output_fn,
)
def main() -> None:
os.chdir(SCRIPT_DIR)
run_chat(create_client())
if __name__ == "__main__":
main()