This commit is contained in:
leopengpong
2025-06-03 17:56:23 -07:00
parent 4b1267796f
commit 0ae1edf185
+67 -5
View File
@@ -21,7 +21,7 @@ def send_prompt(api_key, user_prompt):
body = {
"model": MODEL,
"max_tokens": 1024,
"max_tokens": 2048,
"tools": [
{
"type": f"computer_{TOOL_VERSION}",
@@ -47,14 +47,24 @@ def send_prompt(api_key, user_prompt):
],
"thinking": {
"type": "enabled",
"budget_tokens": 512
"budget_tokens": 1024
}
}
with httpx.Client(timeout=60.0) as client:
try:
response = client.post(ANTHROPIC_API_URL, headers=headers, json=body)
response.raise_for_status()
return response.json()
except httpx.HTTPStatusError as e:
print(f"[ERROR] HTTP {e.response.status_code} - {e.response.reason_phrase}")
try:
print("[DETAIL] Response JSON:")
print(json.dumps(e.response.json(), indent=2))
except Exception:
print("[DETAIL] Raw Response Text:")
print(e.response.text)
sys.exit(1)
def extract_tool_use(response):
@@ -74,6 +84,7 @@ def run():
print(f"Sending prompt: {prompt}")
first_response = send_prompt(api_key, prompt)
print(first_response)
tool_use = extract_tool_use(first_response)
if not tool_use:
@@ -83,6 +94,7 @@ def run():
print(f"Tool requested: {tool_use['name']}")
print(f"Input: {tool_use['input']}")
print(tool_use)
fake_result = {"result": f"(Stub) Executed {tool_use['name']} successfully."}
@@ -91,26 +103,76 @@ def run():
tool_result_msg = {
"model": MODEL,
"max_tokens": 512,
"max_tokens": 2048,
"tools": [],
"messages": [
{
"role": "user",
"content": prompt
},
{
"role": "assistant",
"content": [
{
'type': 'thinking',
'thinking': (
"The user is asking me to take a screenshot of their desktop. I can do this "
"using the \"computer\" function with the \"screenshot\" action.\n\n"
"I need to call the following:\n"
"- Function: computer\n"
"- Action: screenshot"
),
'signature': (
"ErUBCkYIBBgCIkDPXNmcKH0varjvxjdtV6bzHp8OfBbJ3Jg+rJZvBGpXIE1L4RrroreDvBLcoC4uO76tNHDikWlnwv7UAgUtHEFMEgy6tLHT/"
"oFQ4tczh00aDMND7yTs4mXU4utOjiIw08TVLfADRoZLfX5ookvmFuDfN0m2l7KPcgAfQ1Af7AH+T2YHqi+DRtnuGxt35vAeKh1sTRvojVlHU"
"4en+7+TxdQ+yEC3x70IkuQgF5DHQhgC"
)
},
{
'type': 'text',
'text': "I'll take a screenshot of your desktop right away."
},
{
'type': 'tool_use',
'id': tool_use["id"],
'name': 'computer',
'input': {
'action': 'screenshot'
}
}
]
},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": tool_use["id"],
"content": fake_result
"content": "(Stub) Executed tool successfully"
}
]
}
]
],
"thinking": {
"type": "enabled",
"budget_tokens": 1024
}
}
with httpx.Client(timeout=60.0) as client:
try:
response = client.post(ANTHROPIC_API_URL, headers=headers, json=tool_result_msg)
response.raise_for_status()
final_response = response.json()
except httpx.HTTPStatusError as e:
print(f"[ERROR] HTTP {e.response.status_code} - {e.response.reason_phrase}")
try:
print("[DETAIL] Response JSON:")
print(json.dumps(e.response.json(), indent=2))
except Exception:
print("[DETAIL] Raw Response Text:")
print(e.response.text)
sys.exit(1)
print("Final Claude response:")
for block in final_response.get("content", []):