This Flask application demonstrates how to integrate the Anthropic API with the "Computer Use" feature using Claude 3.5 Sonnet. The application allows users to input prompts, which Claude processes and responds to by executing specified tools.
- Features
- Prerequisites
- Setup Instructions
- Usage
- Troubleshooting
- Security Considerations
- Notes
- License
- Real Tool Execution: Executes bash commands and text editing tasks as instructed by Claude.
- Enhanced User Interface: A user-friendly chat interface with conversation history.
- Security Measures: Restricts dangerous commands and runs commands in a controlled environment.
- Troubleshooting Steps: Guidance on resolving common issues, including API authentication and billing errors.
- Python 3.7 or higher
- Anaconda installed on your system
- Anthropic API Key: With access to the "Computer Use" beta feature and sufficient credits
git clone https://github.com/yourusername/claude-computer-use-demo.git
cd claude-computer-use-demo
Note: Replace yourusername
with your GitHub username if you have forked the repository.
Create a new environment named claude_env
with Python 3.9:
conda create -n claude_env python=3.9
conda activate claude_env
Install the required Python packages:
pip install -r requirements.txt
set ANTHROPIC_API_KEY=your_anthropic_api_key
export ANTHROPIC_API_KEY='your_anthropic_api_key'
Important:
- Replace
your_anthropic_api_key
with your actual Anthropic API key. - Ensure you set the environment variable in the same terminal session where you will run the application.
- Do not include quotes around the API key on Windows.
python app.py
You should see output indicating the server is running:
* Serving Flask app 'app'
* Debug mode: on
...
* Running on http://127.0.0.1:5000
-
Open your web browser and navigate to
http://127.0.0.1:5000/
. -
You will see a chat interface where you can interact with Claude.
-
Enter Prompts: Type your prompt into the input box and press "Send".
Example Prompts:
"Run the command 'echo Hello World' in bash."
"Replace 'world' with 'Earth' in the text 'Hello world!'"
-
View Responses: Claude will process your request and display the output.
-
Cause: The environment variable is not set or not accessible.
-
Solution:
- Ensure you set the
ANTHROPIC_API_KEY
environment variable in the same terminal session. - Do not include extra spaces or quotes when setting the variable on Windows.
- Restart the Flask app after setting the variable.
- Ensure you set the
-
Cause: The API key is missing or not being sent in the request headers.
-
Solution:
- Verify that
ANTHROPIC_API_KEY
is set and notNone
. - Ensure the headers in
app.py
are correctly set with proper capitalization.
- Verify that
-
Cause: The API key is incorrect or lacks necessary permissions.
-
Solution:
- Double-check your API key for typos.
- Ensure your API key has access to the "Computer Use" beta feature.
-
Cause: Required packages are not installed in the environment.
-
Solution:
- Activate the correct Conda environment.
- Install dependencies using
pip install -r requirements.txt
.
If you receive an error message like:
{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "Your credit balance is too low to access the Anthropic API. Please go to Plans & Billing to upgrade or purchase credits."
}
}
-
Check Your Anthropic Account Balance:
- Log in to your Anthropic account.
- Navigate to the Plans & Billing section.
- Verify your current credit balance.
-
Purchase Credits or Upgrade Your Plan:
- If your balance is low, purchase additional credits.
- Consider upgrading your plan if necessary.
-
Ensure Beta Feature Access:
- Confirm you have access to the "Computer Use" beta feature.
- Request access from Anthropic if needed.
-
Retry the Application:
- After resolving billing issues, restart the Flask app.
- Test the application to ensure it's working correctly.
-
API Key Confidentiality:
- Do not expose your Anthropic API key in code repositories, logs, or console outputs.
- Remove or comment out any
print
statements that display the API key inapp.py
.
-
Command Execution Safety:
- The application restricts dangerous commands to prevent harmful operations.
- Commands are executed with limited privileges and timeouts.
-
Sandboxed Environment:
- For added security, consider running the application inside a Docker container or virtual machine.
-
Environment Activation:
- Always activate your Conda environment before running the application.
-
Dependencies:
- Ensure all dependencies in
requirements.txt
are installed.
- Ensure all dependencies in
-
Anthropic API Access:
- Confirm that your API key is valid and has the necessary permissions.
-
Error Handling:
- The application includes basic error handling. For production use, enhance exception handling and logging as needed.
This project is licensed under the MIT License.
- Anthropic API Documentation: Anthropic API Docs
- Flask Documentation: Flask Docs
- Anaconda Documentation: Anaconda Docs
This project is based on integrating Anthropic's Claude 3.5 Sonnet with a Flask application to demonstrate the "Computer Use" feature.
Disclaimer: Always exercise caution when executing code or commands received from any external source, including AI models. Ensure that appropriate security measures are in place to protect your system.
Contact: For any questions or issues regarding this project, please open an issue on GitHub or reach out.