|
| 1 | +--- |
| 2 | +title: "Computer use tool" |
| 3 | +--- |
| 4 | + |
| 5 | +Anthropic computer use is fully supported in Portkey. |
| 6 | +For more information on the computer use tool, please refer to the [Anthropic documentation](https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/computer-use-tool). |
| 7 | + |
| 8 | + |
| 9 | +### Usage |
| 10 | +<CodeGroup> |
| 11 | + ```py Python |
| 12 | + from portkey_ai import Portkey |
| 13 | + |
| 14 | + # Initialize the Portkey client |
| 15 | + portkey = Portkey( |
| 16 | + api_key="PORTKEY_API_KEY", # Replace with your Portkey API key |
| 17 | + virtual_key="VIRTUAL_KEY", # Add your provider's virtual key |
| 18 | + ) |
| 19 | + |
| 20 | + # Create the request |
| 21 | + response = portkey.chat.completions.create( |
| 22 | + model="claude-4-opus-20250514", |
| 23 | + max_tokens=3000, |
| 24 | + thinking={ |
| 25 | + "type": "enabled", |
| 26 | + "budget_tokens": 2030 |
| 27 | + }, |
| 28 | + stream=False, |
| 29 | + tools=[ |
| 30 | + { |
| 31 | + type: "computer", |
| 32 | + computer: { |
| 33 | + name: "computer_20250124", # This is the version of the tool |
| 34 | + display_width_px: 1024, |
| 35 | + display_height_px: 768, |
| 36 | + display_number: 1 |
| 37 | + } |
| 38 | + }, |
| 39 | + { |
| 40 | + type: "text_editor_20250429", |
| 41 | + name: "str_replace_based_edit_tool" |
| 42 | + }, |
| 43 | + { |
| 44 | + type: "bash_20250124", |
| 45 | + name: "bash" |
| 46 | + } |
| 47 | + ], |
| 48 | + messages=[ |
| 49 | + { |
| 50 | + role: "user", |
| 51 | + content: "Save a picture of a cat to my desktop." |
| 52 | + } |
| 53 | + ] |
| 54 | + ) |
| 55 | + print(response) |
| 56 | + |
| 57 | + ``` |
| 58 | + ```ts NodeJS |
| 59 | + import Portkey from 'portkey-ai'; |
| 60 | + |
| 61 | + // Initialize the Portkey client |
| 62 | + const portkey = new Portkey({ |
| 63 | + apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key |
| 64 | + virtualKey: "VIRTUAL_KEY", // Add your anthropic's virtual key |
| 65 | + strictOpenAiCompliance: false |
| 66 | + }); |
| 67 | + |
| 68 | + // Generate a chat completion |
| 69 | + async function getChatCompletionFunctions() { |
| 70 | + const response = await portkey.chat.completions.create({ |
| 71 | + model: "claude-4-opus-20250514", |
| 72 | + max_tokens: 3000, |
| 73 | + thinking: { |
| 74 | + type: "enabled", |
| 75 | + budget_tokens: 2030 |
| 76 | + }, |
| 77 | + stream: false, |
| 78 | + tools: [ |
| 79 | + { |
| 80 | + type: "computer", |
| 81 | + computer: { |
| 82 | + name: "computer_20250124", // This is the version of the tool |
| 83 | + display_width_px: 1024, |
| 84 | + display_height_px: 768, |
| 85 | + display_number: 1 |
| 86 | + } |
| 87 | + }, |
| 88 | + { |
| 89 | + type: "text_editor_20250429", |
| 90 | + name: "str_replace_based_edit_tool" |
| 91 | + }, |
| 92 | + { |
| 93 | + type: "bash_20250124", |
| 94 | + name: "bash" |
| 95 | + } |
| 96 | + ], |
| 97 | + "messages": [ |
| 98 | + { |
| 99 | + role: "user", |
| 100 | + content: "Save a picture of a cat to my desktop." |
| 101 | + } |
| 102 | + ] |
| 103 | + }); |
| 104 | + console.log(response); |
| 105 | + } |
| 106 | + // Call the function |
| 107 | + getChatCompletionFunctions(); |
| 108 | + ``` |
| 109 | + ```js OpenAI NodeJS |
| 110 | + import OpenAI from 'openai'; |
| 111 | + import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai' |
| 112 | + |
| 113 | + const openai = new OpenAI({ |
| 114 | + apiKey: 'ANTHROPIC_API_KEY', // defaults to process.env["OPENAI_API_KEY"], |
| 115 | + baseURL: PORTKEY_GATEWAY_URL, |
| 116 | + defaultHeaders: createHeaders({ |
| 117 | + provider: "anthropic", |
| 118 | + apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"] |
| 119 | + strictOpenAiCompliance: false |
| 120 | + }) |
| 121 | + }); |
| 122 | + |
| 123 | + // Generate a chat completion with streaming |
| 124 | + async function getChatCompletionFunctions(){ |
| 125 | + const response = await openai.chat.completions.create({ |
| 126 | + model: "claude-4-opus-20250514", |
| 127 | + max_tokens: 3000, |
| 128 | + thinking: { |
| 129 | + type: "enabled", |
| 130 | + budget_tokens: 2030 |
| 131 | + }, |
| 132 | + stream: false, |
| 133 | + tools: [ |
| 134 | + { |
| 135 | + type: "computer", |
| 136 | + computer: { |
| 137 | + name: "computer_20250124", // This is the version of the tool |
| 138 | + display_width_px: 1024, |
| 139 | + display_height_px: 768, |
| 140 | + display_number: 1 |
| 141 | + } |
| 142 | + }, |
| 143 | + { |
| 144 | + type: "text_editor_20250429", |
| 145 | + name: "str_replace_based_edit_tool" |
| 146 | + }, |
| 147 | + { |
| 148 | + type: "bash_20250124", |
| 149 | + name: "bash" |
| 150 | + } |
| 151 | + ], |
| 152 | + messages: [ |
| 153 | + { |
| 154 | + role: "user", |
| 155 | + content: "Save a picture of a cat to my desktop." |
| 156 | + } |
| 157 | + ] |
| 158 | + }); |
| 159 | + |
| 160 | + console.log(response) |
| 161 | + } |
| 162 | + await getChatCompletionFunctions(); |
| 163 | + ``` |
| 164 | + ```py OpenAI Python |
| 165 | + from openai import OpenAI |
| 166 | + from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders |
| 167 | + |
| 168 | + openai = OpenAI( |
| 169 | + api_key='Anthropic_API_KEY', |
| 170 | + base_url=PORTKEY_GATEWAY_URL, |
| 171 | + default_headers=createHeaders( |
| 172 | + provider="anthropic", |
| 173 | + api_key="PORTKEY_API_KEY", |
| 174 | + strict_open_ai_compliance=False |
| 175 | + ) |
| 176 | + ) |
| 177 | + |
| 178 | + |
| 179 | + response = openai.chat.completions.create( |
| 180 | + model="claude-4-opus-20250514", |
| 181 | + max_tokens=3000, |
| 182 | + thinking={ |
| 183 | + "type": "enabled", |
| 184 | + "budget_tokens": 2030 |
| 185 | + }, |
| 186 | + stream=False, |
| 187 | + tools=[ |
| 188 | + { |
| 189 | + type: "computer", |
| 190 | + computer: { |
| 191 | + name: "computer_20250124", # This is the version of the tool |
| 192 | + display_width_px: 1024, |
| 193 | + display_height_px: 768, |
| 194 | + display_number: 1 |
| 195 | + } |
| 196 | + }, |
| 197 | + { |
| 198 | + type: "text_editor_20250429", |
| 199 | + name: "str_replace_based_edit_tool" |
| 200 | + }, |
| 201 | + { |
| 202 | + type: "bash_20250124", |
| 203 | + name: "bash" |
| 204 | + } |
| 205 | + ], |
| 206 | + messages=[ |
| 207 | + { |
| 208 | + role: "user", |
| 209 | + content: "Save a picture of a cat to my desktop." |
| 210 | + } |
| 211 | + ] |
| 212 | + ) |
| 213 | + |
| 214 | + print(response) |
| 215 | + ``` |
| 216 | + ```sh cURL |
| 217 | + curl "https://api.portkey.ai/v1/chat/completions" \ |
| 218 | + -H "Content-Type: application/json" \ |
| 219 | + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ |
| 220 | + -H "x-portkey-provider: anthropic" \ |
| 221 | + -H "x-api-key: $ANTHROPIC_API_KEY" \ |
| 222 | + -H "x-portkey-strict-open-ai-compliance: false" \ |
| 223 | + -d '{ |
| 224 | + "model": "claude-4-opus-20250514", |
| 225 | + "max_tokens": 3000, |
| 226 | + "thinking": { |
| 227 | + "type": "enabled", |
| 228 | + "budget_tokens": 2030 |
| 229 | + }, |
| 230 | + "stream": false, |
| 231 | + "tools": [ |
| 232 | + { |
| 233 | + "type": "computer", |
| 234 | + "computer": { |
| 235 | + "name": "computer_20250124", |
| 236 | + "display_width_px": 1024, |
| 237 | + "display_height_px": 768, |
| 238 | + "display_number": 1 |
| 239 | + } |
| 240 | + }, |
| 241 | + { |
| 242 | + "type": "text_editor_20250429", |
| 243 | + "name": "str_replace_based_edit_tool" |
| 244 | + }, |
| 245 | + { |
| 246 | + "type": "bash_20250124", |
| 247 | + "name": "bash" |
| 248 | + } |
| 249 | + ], |
| 250 | + "messages": [ |
| 251 | + { |
| 252 | + "role": "user", |
| 253 | + "content": "Save a picture of a cat to my desktop." |
| 254 | + } |
| 255 | + ] |
| 256 | + }' |
| 257 | + ``` |
| 258 | +</CodeGroup> |
0 commit comments