Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -80,7 +80,7 @@ class ComputerUseDemo:
|
|
| 80 |
)
|
| 81 |
elif tool_name == "str_replace_editor":
|
| 82 |
return ToolResult(
|
| 83 |
-
output=f"
|
| 84 |
)
|
| 85 |
elif tool_name == "bash":
|
| 86 |
return ToolResult(
|
|
@@ -111,12 +111,14 @@ class ComputerUseDemo:
|
|
| 111 |
# Create system prompt for computer use
|
| 112 |
base_system_prompt = """You have access to a computing environment through specialized tools.
|
| 113 |
Use the computer tool to interact with the GUI, the text editor for file operations,
|
| 114 |
-
and bash for command-line tasks.
|
| 115 |
-
|
| 116 |
-
of any issues."""
|
| 117 |
|
| 118 |
full_system_prompt = f"{base_system_prompt}\n{system_prompt}" if system_prompt else base_system_prompt
|
| 119 |
|
|
|
|
|
|
|
|
|
|
| 120 |
while True:
|
| 121 |
# Call Claude API with computer use enabled
|
| 122 |
response = anthropic.beta.messages.create(
|
|
@@ -128,17 +130,30 @@ class ComputerUseDemo:
|
|
| 128 |
betas=["computer-use-2024-10-22"]
|
| 129 |
)
|
| 130 |
|
|
|
|
|
|
|
|
|
|
| 131 |
# Check for tool calls in the response
|
| 132 |
tool_calls = []
|
| 133 |
for content in response.content:
|
| 134 |
if hasattr(content, 'tool_calls') and content.tool_calls:
|
| 135 |
tool_calls.extend(content.tool_calls)
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
# Handle tool calls if present
|
| 138 |
if tool_calls:
|
| 139 |
tool_call = tool_calls[0] # Handle first tool call
|
| 140 |
tool_result = self.execute_tool(tool_call.name, tool_call.parameters)
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
# Add tool interactions to message history
|
| 143 |
messages.append({
|
| 144 |
"role": "assistant",
|
|
@@ -149,12 +164,9 @@ class ComputerUseDemo:
|
|
| 149 |
"content": [tool_result.to_dict()]
|
| 150 |
})
|
| 151 |
else:
|
| 152 |
-
# Final response received
|
| 153 |
-
assistant_response = response.content[0].text
|
| 154 |
-
chat_history.append((message, assistant_response))
|
| 155 |
break
|
| 156 |
|
| 157 |
-
return
|
| 158 |
|
| 159 |
except Exception as e:
|
| 160 |
return chat_history, f"Error processing message: {str(e)}"
|
|
|
|
| 80 |
)
|
| 81 |
elif tool_name == "str_replace_editor":
|
| 82 |
return ToolResult(
|
| 83 |
+
output=f"Created/modified file with content: {tool_input}"
|
| 84 |
)
|
| 85 |
elif tool_name == "bash":
|
| 86 |
return ToolResult(
|
|
|
|
| 111 |
# Create system prompt for computer use
|
| 112 |
base_system_prompt = """You have access to a computing environment through specialized tools.
|
| 113 |
Use the computer tool to interact with the GUI, the text editor for file operations,
|
| 114 |
+
and bash for command-line tasks. After each action, explicitly describe what you did and what happened.
|
| 115 |
+
Always show the results of your actions."""
|
|
|
|
| 116 |
|
| 117 |
full_system_prompt = f"{base_system_prompt}\n{system_prompt}" if system_prompt else base_system_prompt
|
| 118 |
|
| 119 |
+
conversation_log = []
|
| 120 |
+
conversation_log.append((message, "")) # Add user message
|
| 121 |
+
|
| 122 |
while True:
|
| 123 |
# Call Claude API with computer use enabled
|
| 124 |
response = anthropic.beta.messages.create(
|
|
|
|
| 130 |
betas=["computer-use-2024-10-22"]
|
| 131 |
)
|
| 132 |
|
| 133 |
+
# Get the assistant's response text
|
| 134 |
+
assistant_message = response.content[0].text if response.content else ""
|
| 135 |
+
|
| 136 |
# Check for tool calls in the response
|
| 137 |
tool_calls = []
|
| 138 |
for content in response.content:
|
| 139 |
if hasattr(content, 'tool_calls') and content.tool_calls:
|
| 140 |
tool_calls.extend(content.tool_calls)
|
| 141 |
|
| 142 |
+
# Add assistant's initial response to conversation
|
| 143 |
+
if assistant_message:
|
| 144 |
+
conversation_log.append(("", assistant_message))
|
| 145 |
+
|
| 146 |
# Handle tool calls if present
|
| 147 |
if tool_calls:
|
| 148 |
tool_call = tool_calls[0] # Handle first tool call
|
| 149 |
tool_result = self.execute_tool(tool_call.name, tool_call.parameters)
|
| 150 |
|
| 151 |
+
# Add tool execution to conversation
|
| 152 |
+
if tool_result.error:
|
| 153 |
+
conversation_log.append(("", f"Error: {tool_result.error}"))
|
| 154 |
+
else:
|
| 155 |
+
conversation_log.append(("", f"Tool execution: {tool_result.output}"))
|
| 156 |
+
|
| 157 |
# Add tool interactions to message history
|
| 158 |
messages.append({
|
| 159 |
"role": "assistant",
|
|
|
|
| 164 |
"content": [tool_result.to_dict()]
|
| 165 |
})
|
| 166 |
else:
|
|
|
|
|
|
|
|
|
|
| 167 |
break
|
| 168 |
|
| 169 |
+
return conversation_log, None
|
| 170 |
|
| 171 |
except Exception as e:
|
| 172 |
return chat_history, f"Error processing message: {str(e)}"
|