跳至主要內容

電腦使用

電腦使用可讓模型透過擷取螢幕截圖並執行點擊、輸入與捲動等動作,與電腦介面互動。這使 AI 模型能夠自主操作桌面環境。

支援的提供者:

  • Anthropic API (anthropic/)
  • Bedrock (Anthropic) (bedrock/)
  • Vertex AI (Anthropic) (vertex_ai/)

支援的工具類型:

  • computer - 具顯示參數的電腦互動工具
  • bash - Bash shell 工具
  • text_editor - 文字編輯器工具
  • web_search - 網頁搜尋工具

LiteLLM 會將所有支援提供者的電腦使用工具標準化。

快速開始

import os 
from litellm import completion

os.environ["ANTHROPIC_API_KEY"] = "your-api-key"

# Computer use tool
tools = [
{
"type": "computer_20241022",
"name": "computer",
"display_height_px": 768,
"display_width_px": 1024,
"display_number": 0,
}
]

messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Take a screenshot and tell me what you see"
},
{
"type": "image_url",
"image_url": {
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=="
}
}
]
}
]

response = completion(
model="anthropic/claude-3-5-sonnet-latest",
messages=messages,
tools=tools,
)

print(response)

檢查模型是否支援 computer use

使用 litellm.supports_computer_use(model="") -> 若模型支援電腦使用則回傳 True,若不支援則回傳 False

import litellm

assert litellm.supports_computer_use(model="anthropic/claude-3-5-sonnet-latest") == True
assert litellm.supports_computer_use(model="anthropic/claude-3-7-sonnet-20250219") == True
assert litellm.supports_computer_use(model="bedrock/anthropic.claude-haiku-4-5-20251001:0") == True
assert litellm.supports_computer_use(model="vertex_ai/claude-3-5-sonnet") == True
assert litellm.supports_computer_use(model="openai/gpt-4") == False

不同的工具類型

電腦使用支援數種不同的工具類型,以滿足各種互動模式:

computer_20241022 工具提供直接的螢幕互動能力。

import os 
from litellm import completion

os.environ["ANTHROPIC_API_KEY"] = "your-api-key"

tools = [
{
"type": "computer_20241022",
"name": "computer",
"display_height_px": 768,
"display_width_px": 1024,
"display_number": 0,
}
]

messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Click on the search button in the screenshot"
},
{
"type": "image_url",
"image_url": {
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=="
}
}
]
}
]

response = completion(
model="anthropic/claude-3-5-sonnet-latest",
messages=messages,
tools=tools,
)

print(response)

具備多種工具的進階用法

您可以在單一請求中結合不同的電腦使用工具:

import os 
from litellm import completion

os.environ["ANTHROPIC_API_KEY"] = "your-api-key"

tools = [
{
"type": "computer_20241022",
"name": "computer",
"display_height_px": 768,
"display_width_px": 1024,
"display_number": 0,
},
{
"type": "bash_20241022",
"name": "bash"
},
{
"type": "text_editor_20250124",
"name": "str_replace_editor"
}
]

messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Take a screenshot, then create a file describing what you see, and finally use bash to show the file contents"
},
{
"type": "image_url",
"image_url": {
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=="
}
}
]
}
]

response = completion(
model="anthropic/claude-3-5-sonnet-latest",
messages=messages,
tools=tools,
)

print(response)

規格

電腦工具 (computer_20241022)

{
"type": "computer_20241022",
"name": "computer",
"display_height_px": 768, // Required: Screen height in pixels
"display_width_px": 1024, // Required: Screen width in pixels
"display_number": 0 // Optional: Display number (default: 0)
}

Bash 工具 (bash_20241022)

{
"type": "bash_20241022",
"name": "bash" // Required: Tool name
}

文字編輯器工具 (text_editor_20250124)

{
"type": "text_editor_20250124",
"name": "str_replace_editor" // Required: Tool name
}

網頁搜尋工具 (web_search_20250305)

{
"type": "web_search_20250305",
"name": "web_search" // Required: Tool name
}