Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

Rewrote notebook to be easier to follow

Dillon DuPont5928c41dc044
diff --git a/notebooks/ollama_nb.ipynb b/notebooks/ollama_nb.ipynb
index a6b6334b3d..52303fa87a 100644
--- a/notebooks/ollama_nb.ipynb
+++ b/notebooks/ollama_nb.ipynb
@@ -2,48 +2,258 @@
"cells": [
{
"cell_type": "markdown",
- "id": "8e017e51",
+ "id": "title-intro",
"metadata": {},
"source": [
- "# Running Ollama ComputerAgents\n",
+ "# Using Ollama with Cua (Docker Edition)\n",
"\n",
- "This notebook guides you through examples on how to use your local Ollama to run computer-using models"
+ "This notebook demonstrates multiple ways to use Ollama with the Cua ComputerAgent, mirroring the structure of `notebooks/sota_hackathon.ipynb` while running the computer inside Docker.\n",
+ "\n",
+ "We'll cover three patterns:\n",
+ "\n",
+ "1. Use an all-in-one CUA model served by Ollama (e.g. `model=\"ollama/blaifa/InternVL3_5\"`).\n",
+ "2. Use a strong CUA grounding model composed with an Ollama VLM (e.g. `model=\"openai/computer-use-preview+ollama/gemma3:4b\"`).\n",
+ "3. Conceptual: different ways to customize/extend your agent (link + outline only).\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "prereq",
+ "metadata": {},
+ "source": [
+ "## πŸ’» Prerequisites\n",
+ "\n",
+ "- Install [Ollama](https://ollama.com/) (optional, but recommended for this notebook)\n",
+ "- Install [Docker Desktop](https://www.docker.com/products/docker-desktop/).\n",
+ "- Install Python dependencies for this repo (we recommend [pdm](https://pdm-project.org/en/latest/#recommended-installation-method)).\n",
+ "- Ensure you can run Jupyter notebooks in this repository's `.venv`.\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "env-setup",
+ "metadata": {},
+ "source": [
+ "## πŸ”‘ Environment Setup (.env)\n",
+ "\n",
+ "Create a `.env` file with your API keys. You can use any provider keys that you plan to compose. For example, if composing with OpenAI or Anthropic, add those keys too.\n",
+ "\n",
+ "Add these entries as needed (empty values are fine if not used):\n",
+ "\n",
+ "- `OPENAI_API_KEY` (if composing with OpenAI)\n",
+ "- `ANTHROPIC_API_KEY` (if composing with Anthropic)\n",
+ "- `OLLAMA_HOST` (defaults to `http://localhost:11434`)\n",
+ "\n",
+ "Note: For Cua Cloud computers, you would also set `CUA_API_KEY` and `CUA_CONTAINER_NAME`, but this notebook uses Docker for the computer.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "1a0f9a75",
+ "id": "create-env",
"metadata": {},
"outputs": [],
"source": [
- "from agent import ComputerAgent\n",
- "from computer import Computer, VMProviderType\n",
- "import logging\n",
+ "# Create a .env template if it doesn't exist\n",
+ "ENV_TEMPLATE = \"\"\"# Optional environment variables for composition:\n",
+ "OPENAI_API_KEY=\n",
+ "ANTHROPIC_API_KEY=\n",
+ "\n",
+ "# Ollama endpoint (default shown)\n",
+ "OLLAMA_HOST=http://localhost:11434\n",
+ "\"\"\"\n",
+ "\n",
+ "from pathlib import Path\n",
+ "if not Path('.env').exists():\n",
+ " Path('.env').write_text(ENV_TEMPLATE)\n",
+ " print('A .env file was created! Fill in the empty values you need.')\n",
+ "else:\n",
+ " print('.env already exists')\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "load-env",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Load .env into environment\n",
"import os\n",
+ "from dotenv import load_dotenv\n",
+ "load_dotenv(dotenv_path='.env', override=True)\n",
+ "print('OPENAI_API_KEY set:', bool(os.getenv('OPENAI_API_KEY')))\n",
+ "print('ANTHROPIC_API_KEY set:', bool(os.getenv('ANTHROPIC_API_KEY')))\n",
+ "print('OLLAMA_HOST:', os.getenv('OLLAMA_HOST', 'http://localhost:11434'))\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "ollama-docker",
+ "metadata": {},
+ "source": [
+ "## 🐳 Run Ollama via Docker (recommended)\n",
+ "\n",
+ "If you don't already have Ollama running locally, you can run it with Docker. \n",
+ "Run the following command in your terminal (outside the notebook):\n",
+ "\n",
+ "```bash\n",
+ "docker run -d --name ollama -p 11434:11434 -v ollama:/root/.ollama \\\n",
+ " ollama/ollama:latest\n",
+ "```\n",
+ "\n",
+ "Then pull any models you need, for example (terminal):\n",
+ "\n",
+ "```bash\n",
+ "docker exec -it ollama ollama pull gemma3:4b\n",
+ "docker exec -it ollama ollama pull blaifa/InternVL3_5\n",
+ "```\n",
+ "\n",
+ "Make sure your `OLLAMA_HOST` points to `http://localhost:11434`.\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "computer-docker",
+ "metadata": {},
+ "source": [
+ "## πŸ–₯️ Launch a Docker Computer\n",
+ "\n",
+ "We'll run the computer using the Cua Docker provider.\n",
+ "You can watch the live VNC stream at `http://localhost:8006/`.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "launch-computer",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import logging\n",
+ "from computer import Computer, VMProviderType\n",
+ "import webbrowser\n",
"\n",
- "# First we need to connect to a computer\n",
"computer = Computer(\n",
- " provider_type=VMProviderType.CLOUD,\n",
" os_type=\"linux\",\n",
- " name=os.environ[\"CUA_CONTAINER_NAME\"],\n",
- " api_key=os.environ[\"CUA_API_KEY\"]\n",
+ " provider_type=VMProviderType.DOCKER,\n",
+ " verbosity=logging.INFO\n",
")\n",
"await computer.run()\n",
"\n",
- "# Then we need to pick a model to use\n",
- "agent = ComputerAgent(\n",
+ "# Optional: open the VNC page in your browser\n",
+ "webbrowser.open('http://localhost:8006/', new=0, autoraise=True)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "section-1",
+ "metadata": {},
+ "source": [
+ "## 1) All-in-one CUA model via Ollama\n",
+ "\n",
+ "Some community models on Ollama are trained for computer use end-to-end.\n",
+ "Point the agent's model to an Ollama-served model using the `ollama/` prefix.\n",
+ "\n",
+ "Example: `model=\"ollama/blaifa/InternVL3_5\"`.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "run-allinone",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import logging\n",
+ "from pathlib import Path\n",
+ "from agent import ComputerAgent\n",
+ "\n",
+ "agent_all_in_one = ComputerAgent(\n",
+ " model=\"ollama/blaifa/InternVL3_5\",\n",
+ " tools=[computer],\n",
+ " trajectory_dir=str(Path('trajectories')),\n",
+ " only_n_most_recent_images=3,\n",
+ " verbosity=logging.INFO,\n",
+ " # instructions=\"You are a helpful assistant.\" # Editable instructions for prompt engineering\n",
+ ")\n",
+ "\n",
+ "print('Running all-in-one Ollama CUA model...')\n",
+ "async for _ in agent_all_in_one.run(\"Open the web browser and go to example.com\"):\n",
+ " pass\n",
+ "print('βœ… Done')\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "section-2",
+ "metadata": {},
+ "source": [
+ "## 2) Compose a strong CUA grounding model with an Ollama VLM\n",
+ "\n",
+ "You can compose a grounding (reasoning + tool-use) model with a local Ollama VLM for vision.\n",
+ "Use a `+ollama/<model>` suffix to compose.\n",
+ "\n",
+ "Examples:\n",
+ "- `openai/computer-use-preview+ollama/gemma3:4b`\n",
+ "- `anthropic/claude-3-5-sonnet-20241022+ollama/gemma3:4b`\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "run-composed",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from agent import ComputerAgent\n",
+ "import logging\n",
+ "\n",
+ "agent_composed = ComputerAgent(\n",
" model=\"openai/computer-use-preview+ollama/gemma3:4b\",\n",
" tools=[computer],\n",
- " trajectory_dir=\"trajectories\",\n",
- " verbosity=logging.DEBUG,\n",
- " instructions=(\n",
- " \"You are a computer-use agent. \"\n",
- " \"Control the computer by using the 'computer' tool and specifying the action type in the 'type' argument.\"\n",
- " )\n",
+ " trajectory_dir='trajectories',\n",
+ " only_n_most_recent_images=3,\n",
+ " verbosity=logging.INFO,\n",
")\n",
- "async for _ in agent.run(\"Hello! Open the web browser\"):\n",
- " pass"
+ "\n",
+ "print('Running composed agent (OpenAI grounding + Ollama VLM)...')\n",
+ "async for _ in agent_composed.run(\"Open a text editor and type: Hello from composed model!\"):\n",
+ " pass\n",
+ "print('βœ… Done')\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "section-3-conceptual",
+ "metadata": {},
+ "source": [
+ "## 3) Customize your agent (ideas only)\n",
+ "\n",
+ "Rather than providing runnable code here, see the Cua docs for a full menu of customization options: https://docs.trycua.com/docs/agent-sdk/customizing-computeragent\n",
+ "\n",
+ "Levels of customization you can explore:\n",
+ "\n",
+ "1) Simple β€” Prompt engineering\n",
+ "2) Easy β€” Tools\n",
+ "3) Intermediate β€” Callbacks\n",
+ "4) Expert β€” Custom agent via `register_agent` (see `libs/python/agent/agent/decorators.py` β†’ `register_agent`)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "wrapup",
+ "metadata": {},
+ "source": [
+ "## βœ… Summary\n",
+ "\n",
+ "- You ran the computer in Docker via the Cua Docker provider and viewed it at `http://localhost:8006/`.\n",
+ "- You tried two runnable ways to leverage Ollama and reviewed a conceptual path to go further:\n",
+ " - All-in-one computer-use model served by Ollama.\n",
+ " - A composed agent using a strong grounding model + an Ollama VLM.\n",
+ " - A link + outline for further customization paths (prompting, tools, callbacks, custom agent via `register_agent`).\n",
+ "\n",
+ "Explore more configurations and models in the Cua docs.\n"
]
}
],
@@ -54,16 +264,7 @@
"name": "python3"
},
"language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.12.11"
+ "name": "python"
}
},
"nbformat": 4,