Open Interpreter 自然語言電腦介面

使用 Open Interpreter 透過自然語言控制電腦,執行程式碼、管理檔案、瀏覽網頁

專案簡介

Open Interpreter 讓 LLM 在你的電腦上執行程式碼。透過類似 ChatGPT 的終端機介面,用自然語言控制電腦完成各種任務。

GitHub Stars: 62K+

主要功能

  • 程式執行 - Python、Shell、JavaScript
  • 檔案操作 - 讀寫、編輯、搜尋
  • 網頁瀏覽 - 擷取資訊
  • 系統控制 - 安裝軟體、管理程序
  • 多模型 - GPT-4、Claude、本地模型

安裝

1
pip install open-interpreter

快速開始

啟動

1
interpreter

使用 OpenAI

1
2
export OPENAI_API_KEY=sk-xxx
interpreter

使用本地模型

1
2
3
interpreter --local
# 或指定 Ollama 模型
interpreter --model ollama/llama3.3

基本使用

對話範例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
> Create a Python script that scans for open ports on localhost

I'll create a port scanning script for you.

import socket

def scan_ports(host, start_port, end_port):
    open_ports = []
    for port in range(start_port, end_port + 1):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(1)
        result = sock.connect_ex((host, port))
        if result == 0:
            open_ports.append(port)
        sock.close()
    return open_ports

# Scan common ports
ports = scan_ports('localhost', 1, 1024)
print(f"Open ports: {ports}")

Would you like me to run this code? (y/n)

檔案操作

1
2
3
4
5
> Find all Python files in the current directory that contain "password"

> Create a backup of all .md files in a new folder called "backup"

> Show me the structure of this project

系統任務

1
2
3
4
5
> Install pandas and matplotlib

> Check disk usage and clean up temp files

> List all running processes using more than 100MB memory

模式設定

安全模式

1
2
3
4
5
# 每次執行前確認
interpreter --safe_mode ask

# 自動執行
interpreter --safe_mode off

Python 中使用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from interpreter import interpreter

# 基本設定
interpreter.llm.model = "gpt-4o"
interpreter.auto_run = True  # 自動執行程式碼

# 開始對話
interpreter.chat("Create a simple web scraper")

# 或程式化呼叫
result = interpreter.chat("What's 2 + 2?", return_messages=True)

進階設定

完整設定

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
from interpreter import interpreter

# 模型設定
interpreter.llm.model = "gpt-4o"
interpreter.llm.temperature = 0.7
interpreter.llm.api_key = "your-key"

# 執行設定
interpreter.auto_run = False
interpreter.safe_mode = "ask"

# 系統訊息
interpreter.system_message = """
You are a security expert assistant.
Help with security analysis and vulnerability assessment.
"""

# 開始
interpreter.chat()

本地模型設定

1
2
3
interpreter.llm.model = "ollama/llama3.3"
interpreter.llm.api_base = "http://localhost:11434"
interpreter.offline = True

使用 Anthropic

1
2
interpreter.llm.model = "claude-3-5-sonnet-20241022"
interpreter.llm.api_key = "your-anthropic-key"

安全考量

限制執行

1
2
3
4
5
# 僅允許 Python
interpreter.computer.languages = ["python"]

# 禁用系統命令
interpreter.computer.languages = ["python"]  # 移除 shell

沙盒模式

1
2
3
4
5
# 使用 Docker
interpreter --docker

# 或 E2B 沙盒
interpreter --e2b

審核模式

1
interpreter.auto_run = False  # 每次執行前確認

使用案例

資料分析

1
2
3
4
5
> Load the CSV file 'logs.csv' and analyze security events

> Create a chart showing the distribution of event types

> Export the results to an Excel file

安全檢查

1
2
3
4
5
> Check if any ports are exposed on this machine

> Analyze the nginx access logs for suspicious requests

> Generate a security report of the system configuration

自動化任務

1
2
3
4
5
> Every hour, check the website https://example.com and alert if it's down

> Monitor the /var/log/auth.log file for failed login attempts

> Create a cron job to backup the database daily

程式化整合

API 伺服器

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from interpreter import interpreter
from fastapi import FastAPI

app = FastAPI()
interpreter.auto_run = True

@app.post("/chat")
async def chat(message: str):
    response = interpreter.chat(message, return_messages=True)
    return {"response": response[-1]["content"]}

批次處理

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
tasks = [
    "Check disk space",
    "List recent log errors",
    "Show memory usage"
]

for task in tasks:
    result = interpreter.chat(task, return_messages=True)
    print(f"Task: {task}")
    print(f"Result: {result[-1]['content']}\n")

電腦控制

滑鼠和鍵盤

1
2
3
4
5
6
# 需要額外安裝
pip install open-interpreter[os]

interpreter.computer.mouse.move(100, 200)
interpreter.computer.mouse.click()
interpreter.computer.keyboard.write("Hello")

螢幕擷取

1
screenshot = interpreter.computer.display.screenshot()

設定檔

建立設定

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# ~/.config/open-interpreter/config.yaml
llm:
  model: gpt-4o
  temperature: 0.7
  api_key: ${OPENAI_API_KEY}

auto_run: false
safe_mode: ask

system_message: |
  You are a helpful assistant focused on security tasks.  

使用設定

1
interpreter --config security

相關連結

延伸閱讀

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy