-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
78 lines (59 loc) · 2.08 KB
/
Copy pathagent.py
File metadata and controls
78 lines (59 loc) · 2.08 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
"""Chapter 1 reference: a minimal multi-turn LLM client."""
from collections.abc import Callable, Iterable
import os
from typing import Any
import anthropic
DEFAULT_BASE_URL = "https://api.deepseek.com/anthropic"
DEFAULT_MODEL = "claude-sonnet-4.6"
SYSTEM_PROMPT = "You are a helpful assistant."
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 text_content(blocks: Iterable[Any]) -> list[dict[str, str]]:
"""Convert text blocks into the message format sent back to the API."""
return [
{"type": "text", "text": block.text}
for block in blocks
if block.type == "text"
]
def run_chat(
client: anthropic.Anthropic,
*,
model: str = DEFAULT_MODEL,
input_fn: Callable[[str], str] = input,
output_fn: Callable[[str], None] = print,
) -> None:
"""Run a synchronous terminal chat until the user enters ``exit``."""
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})
response = client.messages.create(
model=model,
max_tokens=8192,
system=SYSTEM_PROMPT,
messages=messages,
)
assistant_message = text_content(response.content)
for block in assistant_message:
output_fn(block["text"])
messages.append(
{
"role": "assistant",
"content": assistant_message,
}
)
def main() -> None:
run_chat(create_client())
if __name__ == "__main__":
main()