fix messages response object to Claude
This commit is contained in:
@@ -15,7 +15,7 @@ HEADERS = {
|
||||
}
|
||||
|
||||
|
||||
def send_prompt(api_key, user_prompt):
|
||||
def send_prompt(api_key, messages):
|
||||
headers = HEADERS.copy()
|
||||
headers["x-api-key"] = api_key
|
||||
|
||||
@@ -39,12 +39,7 @@ def send_prompt(api_key, user_prompt):
|
||||
"name": "str_replace_editor"
|
||||
}
|
||||
],
|
||||
"messages": [
|
||||
{
|
||||
"role": "user",
|
||||
"content": user_prompt
|
||||
}
|
||||
],
|
||||
"messages": messages,
|
||||
"thinking": {
|
||||
"type": "enabled",
|
||||
"budget_tokens": 1024
|
||||
@@ -75,16 +70,28 @@ def extract_tool_use(response):
|
||||
|
||||
|
||||
def run():
|
||||
messages = []
|
||||
|
||||
if len(sys.argv) < 3:
|
||||
print("Usage: python claude_computer_use_cli.py <api_key> '<your_prompt>'")
|
||||
return
|
||||
|
||||
api_key = sys.argv[1]
|
||||
prompt = sys.argv[2]
|
||||
messages.append({
|
||||
"role": "user",
|
||||
"content": prompt
|
||||
})
|
||||
|
||||
print(f"Sending prompt: {prompt}")
|
||||
first_response = send_prompt(api_key, prompt)
|
||||
print(first_response)
|
||||
first_response = send_prompt(api_key, messages)
|
||||
|
||||
# We can't just append the whole first response, otherwise we get 400 bad req:
|
||||
# "Extra inputs are not permitted"
|
||||
messages.append({
|
||||
'role': 'assistant',
|
||||
'content': first_response.get('content', []),
|
||||
})
|
||||
|
||||
tool_use = extract_tool_use(first_response)
|
||||
if not tool_use:
|
||||
@@ -92,12 +99,31 @@ def run():
|
||||
print(first_response["content"])
|
||||
return
|
||||
|
||||
print(f"Tool requested: {tool_use['name']}")
|
||||
print(f"Input: {tool_use['input']}")
|
||||
print(tool_use)
|
||||
# print first response from Claude
|
||||
for block in first_response.get("content", []):
|
||||
print(f'{block['type']}:')
|
||||
if block["type"] == "text":
|
||||
print(f'\t{block["text"]}')
|
||||
elif block["type"] == "thinking":
|
||||
print(f'\t{block["thinking"]}')
|
||||
elif block["type"] == "tool_use":
|
||||
print(f'\tTool use requested: {block["name"]}')
|
||||
print(f'\tInput: {block["input"]}')
|
||||
|
||||
fake_result = {"result": f"(Stub) Executed {tool_use['name']} successfully."}
|
||||
# this is a stub for the tool execution
|
||||
fake_result_message = {
|
||||
"role": "user",
|
||||
"content": [
|
||||
{
|
||||
"type": "tool_result",
|
||||
"tool_use_id": tool_use["id"],
|
||||
"content": "(Stub) Executed tool successfully"
|
||||
}
|
||||
]
|
||||
}
|
||||
messages.append(fake_result_message)
|
||||
|
||||
# send the fake result back to Claude
|
||||
headers = HEADERS.copy()
|
||||
headers["x-api-key"] = api_key
|
||||
|
||||
@@ -105,54 +131,7 @@ def run():
|
||||
"model": MODEL,
|
||||
"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": "(Stub) Executed tool successfully"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"messages": messages,
|
||||
"thinking": {
|
||||
"type": "enabled",
|
||||
"budget_tokens": 1024
|
||||
|
||||
Reference in New Issue
Block a user