Skip to content

An AI Agent that interacts with Windows OS at GUI level.

Python
18.7 KB
Raw Blame
import json
import logging
import os
import uuid
from collections.abc import AsyncIterator, Iterator
from typing import Any, overload

from ollama import AsyncClient, Client
from pydantic import BaseModel

from windows_use.messages import (
    AIMessage,
    BaseMessage,
    HumanMessage,
    ImageMessage,
    SystemMessage,
    ToolMessage,
)
from windows_use.providers.base import BaseChatLLM
from windows_use.providers.events import (
    LLMEvent,
    LLMEventType,
    LLMStreamEvent,
    LLMStreamEventType,
    Thinking,
    ToolCall,
)
from windows_use.providers.views import Metadata, TokenUsage
from windows_use.tools import Tool

logger = logging.getLogger(__name__)


class ChatOllama(BaseChatLLM):
    """
    Ollama LLM implementation following the BaseChatLLM protocol.
    """

    def __init__(
        self,
        model: str = "llama3.1",
        host: str | None = None,
        timeout: float = 600.0,
        temperature: float | None = None,
        think: bool | str | None = None,
        **kwargs,
    ):
        """
        Initialize the Ollama LLM.

        Args:
            model (str): The model name to use.
            host (str, optional): Ollama host URL. Defaults to OLLAMA_HOST environment variable or localhost.
            timeout (float): Request timeout.
            temperature (float, optional): Sampling temperature.
            think: Enable thinking traces. Pass True/False or an effort level
                ("low", "medium", "high") for models that support it.
            **kwargs: Additional arguments for chat.
        """
        self._model = model
        self.host = host or os.environ.get("OLLAMA_HOST", "http://localhost:11434")
        self.temperature = temperature
        self.think = think

        self.client = Client(host=self.host, timeout=timeout)
        self.aclient = AsyncClient(host=self.host, timeout=timeout)
        self.kwargs = kwargs

    @property
    def model_name(self) -> str:
        return self._model

    @property
    def provider(self) -> str:
        return "ollama"

    def _convert_messages(self, messages: list[BaseMessage]) -> list[dict]:
        """
        Convert BaseMessage objects to Ollama-compatible message dictionaries.
        """
        ollama_messages = []
        for msg in messages:
            if isinstance(msg, SystemMessage):
                ollama_messages.append({"role": "system", "content": msg.content})
            elif isinstance(msg, HumanMessage):
                ollama_messages.append({"role": "user", "content": msg.content})
            elif isinstance(msg, ImageMessage):
                b64_imgs = msg.convert_images(format="base64")
                ollama_messages.append(
                    {"role": "user", "content": msg.content or "", "images": b64_imgs}
                )
            elif isinstance(msg, AIMessage):
                msg_dict: dict = {"role": "assistant", "content": msg.content or ""}
                if getattr(msg, "thinking", None):
                    msg_dict["thinking"] = msg.thinking
                ollama_messages.append(msg_dict)
            elif isinstance(msg, ToolMessage):
                # Ollama expects assistant message with tool_calls followed by tool message
                # Reconstruct for history consistency
                asst_msg: dict = {
                    "role": "assistant",
                    "content": "",
                    "tool_calls": [{"function": {"name": msg.name, "arguments": msg.params}}],
                }
                if getattr(msg, "thinking", None):
                    asst_msg["thinking"] = msg.thinking
                ollama_messages.append(asst_msg)
                ollama_messages.append({"role": "tool", "content": msg.content or ""})
        return ollama_messages

    def _convert_tools(self, tools: list[Tool]) -> list[dict]:
        """
        Convert Tool objects to Ollama-compatible tool definitions.
        """
        return [{"type": "function", "function": tool.json_schema} for tool in tools]

    def _process_response(self, response: Any) -> LLMEvent:
        """Process Ollama API response into AIMessage or ToolMessage."""
        message = response.get("message", {})
        thinking = message.get("thinking")
        # Ollama doesn't expose thinking_tokens; estimate from content when available
        thinking_tokens = None
        if thinking:
            thinking_tokens = max(1, len(thinking) // 4)

        usage = TokenUsage(
            prompt_tokens=response.get("prompt_eval_count", 0),
            completion_tokens=response.get("eval_count", 0),
            total_tokens=response.get("prompt_eval_count", 0) + response.get("eval_count", 0),
            thinking_tokens=thinking_tokens,
        )

        tool_calls = message.get("tool_calls", [])
        if tool_calls:
            tool_call = tool_calls[0]
            func = tool_call.get("function", {})
            args = func.get("arguments", {})
            if isinstance(args, str):
                try:
                    args = json.loads(args) if args else {}
                except json.JSONDecodeError:
                    args = {}
            thinking_obj = Thinking(content=thinking, signature=None) if thinking else None
            return LLMEvent(
                type=LLMEventType.TOOL_CALL,
                tool_call=ToolCall(
                    id=f"call_{uuid.uuid4().hex[:8]}",  # Ollama doesn't return tool call ID consistently
                    name=func.get("name"),
                    params=args,
                ),
                thinking=thinking_obj,
                usage=usage,
            )
        thinking_obj = Thinking(content=thinking, signature=None) if thinking else None
        return LLMEvent(
            type=LLMEventType.TEXT,
            content=message.get("content", ""),
            thinking=thinking_obj,
            usage=usage,
        )

    @overload
    def invoke(
        self,
        messages: list[BaseMessage],
        tools: list[Tool] = [],
        structured_output: BaseModel | None = None,
        json_mode: bool = False,
    ) -> LLMEvent: ...

    def invoke(
        self,
        messages: list[BaseMessage],
        tools: list[Tool] = [],
        structured_output: BaseModel | None = None,
        json_mode: bool = False,
    ) -> LLMEvent:
        ollama_messages = self._convert_messages(messages)
        ollama_tools = self._convert_tools(tools) if tools else None

        params = {"model": self._model, "messages": ollama_messages, **self.kwargs}

        if ollama_tools:
            params["tools"] = ollama_tools
        if self.think is not None:
            params["think"] = self.think

        if self.temperature is not None:
            if "options" not in params:
                params["options"] = {}
            params["options"]["temperature"] = self.temperature

        if json_mode or structured_output:
            params["format"] = (
                "json" if not structured_output else structured_output.model_json_schema()
            )

        response = self.client.chat(**params)

        if structured_output:
            try:
                parsed = structured_output.model_validate_json(response["message"]["content"])
                content = parsed.model_dump() if hasattr(parsed, "model_dump") else str(parsed)
                usage = TokenUsage(
                    prompt_tokens=response.get("prompt_eval_count", 0),
                    completion_tokens=response.get("eval_count", 0),
                    total_tokens=response.get("prompt_eval_count", 0)
                    + response.get("eval_count", 0),
                    thinking_tokens=None,
                )
                return LLMEvent(
                    type=LLMEventType.TEXT,
                    content=json.dumps(content) if isinstance(content, dict) else content,
                    usage=usage,
                )
            except (json.JSONDecodeError, ValueError) as e:
                logger.error(f"Failed to parse structured output: {e}")
                # Fall through to normal response processing

        return self._process_response(response)

    @overload
    async def ainvoke(
        self,
        messages: list[BaseMessage],
        tools: list[Tool] = [],
        structured_output: BaseModel | None = None,
        json_mode: bool = False,
    ) -> LLMEvent: ...

    async def ainvoke(
        self,
        messages: list[BaseMessage],
        tools: list[Tool] = [],
        structured_output: BaseModel | None = None,
        json_mode: bool = False,
    ) -> LLMEvent:
        ollama_messages = self._convert_messages(messages)
        ollama_tools = self._convert_tools(tools) if tools else None

        params = {"model": self._model, "messages": ollama_messages, **self.kwargs}

        if ollama_tools:
            params["tools"] = ollama_tools
        if self.think is not None:
            params["think"] = self.think

        if self.temperature is not None:
            if "options" not in params:
                params["options"] = {}
            params["options"]["temperature"] = self.temperature

        if json_mode or structured_output:
            params["format"] = (
                "json" if not structured_output else structured_output.model_json_schema()
            )

        response = await self.aclient.chat(**params)

        if structured_output:
            try:
                parsed = structured_output.model_validate_json(response["message"]["content"])
                content = parsed.model_dump() if hasattr(parsed, "model_dump") else parsed
                usage = TokenUsage(
                    prompt_tokens=response.get("prompt_eval_count", 0),
                    completion_tokens=response.get("eval_count", 0),
                    total_tokens=response.get("prompt_eval_count", 0)
                    + response.get("eval_count", 0),
                    thinking_tokens=None,
                )
                return LLMEvent(
                    type=LLMEventType.TEXT,
                    content=json.dumps(content) if isinstance(content, dict) else content,
                    usage=usage,
                )
            except (json.JSONDecodeError, ValueError) as e:
                logger.error(f"Failed to parse structured output: {e}")

        return self._process_response(response)

    @overload
    def stream(
        self,
        messages: list[BaseMessage],
        tools: list[Tool] = [],
        structured_output: BaseModel | None = None,
        json_mode: bool = False,
    ) -> Iterator[LLMStreamEvent]: ...

    def stream(
        self,
        messages: list[BaseMessage],
        tools: list[Tool] = [],
        structured_output: BaseModel | None = None,
        json_mode: bool = False,
    ) -> Iterator[LLMStreamEvent]:
        ollama_messages = self._convert_messages(messages)
        ollama_tools = self._convert_tools(tools) if tools else None

        params = {"model": self._model, "messages": ollama_messages, "stream": True, **self.kwargs}

        if ollama_tools:
            params["tools"] = ollama_tools
        if self.think is not None:
            params["think"] = self.think

        if self.temperature is not None:
            if "options" not in params:
                params["options"] = {}
            params["options"]["temperature"] = self.temperature

        if json_mode:
            params["format"] = "json"

        response = self.client.chat(**params)

        text_started = False
        think_started = False
        accumulated_thinking: list[str] = []
        usage = None

        for chunk in response:
            message = chunk.get("message", {})
            # Ollama may send usage in the final chunk
            if "eval_count" in chunk or "prompt_eval_count" in chunk:
                thinking_tokens = (
                    max(1, sum(len(t) for t in accumulated_thinking) // 4)
                    if accumulated_thinking
                    else None
                )
                usage = TokenUsage(
                    prompt_tokens=chunk.get("prompt_eval_count", 0),
                    completion_tokens=chunk.get("eval_count", 0),
                    total_tokens=chunk.get("prompt_eval_count", 0) + chunk.get("eval_count", 0),
                    thinking_tokens=thinking_tokens,
                )
            if message.get("thinking"):
                accumulated_thinking.append(message["thinking"])
                if not think_started:
                    think_started = True
                    yield LLMStreamEvent(type=LLMStreamEventType.THINK_START)
                yield LLMStreamEvent(
                    type=LLMStreamEventType.THINK_DELTA, content=message["thinking"]
                )
            if "content" in message and message["content"]:
                if think_started:
                    yield LLMStreamEvent(type=LLMStreamEventType.THINK_END)
                    think_started = False
                if not text_started:
                    text_started = True
                    yield LLMStreamEvent(type=LLMStreamEventType.TEXT_START)
                yield LLMStreamEvent(type=LLMStreamEventType.TEXT_DELTA, content=message["content"])

            # Handle tool calls in stream
            tool_calls = message.get("tool_calls", [])
            if tool_calls:
                tc = tool_calls[0]
                func = tc.get("function", {})
                args = func.get("arguments", {})
                if isinstance(args, str):
                    try:
                        args = json.loads(args) if args else {}
                    except json.JSONDecodeError:
                        args = {}
                if think_started:
                    yield LLMStreamEvent(type=LLMStreamEventType.THINK_END)
                    think_started = False
                thinking_content = "".join(accumulated_thinking) or None
                thinking_obj = (
                    Thinking(content=thinking_content, signature=None) if thinking_content else None
                )
                yield LLMStreamEvent(
                    type=LLMStreamEventType.TOOL_CALL,
                    tool_call=ToolCall(
                        id=f"call_{uuid.uuid4().hex[:8]}", name=func.get("name"), params=args
                    ),
                    thinking=thinking_obj,
                    usage=usage,
                )

        if think_started:
            yield LLMStreamEvent(type=LLMStreamEventType.THINK_END)
        if text_started:
            yield LLMStreamEvent(type=LLMStreamEventType.TEXT_END, usage=usage)

    @overload
    async def astream(
        self,
        messages: list[BaseMessage],
        tools: list[Tool] = [],
        structured_output: BaseModel | None = None,
        json_mode: bool = False,
    ) -> AsyncIterator[LLMStreamEvent]: ...

    async def astream(
        self,
        messages: list[BaseMessage],
        tools: list[Tool] = [],
        structured_output: BaseModel | None = None,
        json_mode: bool = False,
    ) -> AsyncIterator[LLMStreamEvent]:
        ollama_messages = self._convert_messages(messages)
        ollama_tools = self._convert_tools(tools) if tools else None

        params = {"model": self._model, "messages": ollama_messages, "stream": True, **self.kwargs}

        if ollama_tools:
            params["tools"] = ollama_tools
        if self.think is not None:
            params["think"] = self.think

        if self.temperature is not None:
            if "options" not in params:
                params["options"] = {}
            params["options"]["temperature"] = self.temperature

        if json_mode:
            params["format"] = "json"

        response = await self.aclient.chat(**params)

        text_started = False
        think_started = False
        accumulated_thinking: list[str] = []
        usage = None

        async for chunk in response:
            message = chunk.get("message", {})
            # Ollama may send usage in the final chunk
            if "eval_count" in chunk or "prompt_eval_count" in chunk:
                thinking_tokens = (
                    max(1, sum(len(t) for t in accumulated_thinking) // 4)
                    if accumulated_thinking
                    else None
                )
                usage = TokenUsage(
                    prompt_tokens=chunk.get("prompt_eval_count", 0),
                    completion_tokens=chunk.get("eval_count", 0),
                    total_tokens=chunk.get("prompt_eval_count", 0) + chunk.get("eval_count", 0),
                    thinking_tokens=thinking_tokens,
                )
            if message.get("thinking"):
                accumulated_thinking.append(message["thinking"])
                if not think_started:
                    think_started = True
                    yield LLMStreamEvent(type=LLMStreamEventType.THINK_START)
                yield LLMStreamEvent(
                    type=LLMStreamEventType.THINK_DELTA, content=message["thinking"]
                )
            if "content" in message and message["content"]:
                if think_started:
                    yield LLMStreamEvent(type=LLMStreamEventType.THINK_END)
                    think_started = False
                if not text_started:
                    text_started = True
                    yield LLMStreamEvent(type=LLMStreamEventType.TEXT_START)
                yield LLMStreamEvent(type=LLMStreamEventType.TEXT_DELTA, content=message["content"])

            # Handle tool calls in stream
            tool_calls = message.get("tool_calls", [])
            if tool_calls:
                tc = tool_calls[0]
                func = tc.get("function", {})
                args = func.get("arguments", {})
                if isinstance(args, str):
                    try:
                        args = json.loads(args) if args else {}
                    except json.JSONDecodeError:
                        args = {}
                if think_started:
                    yield LLMStreamEvent(type=LLMStreamEventType.THINK_END)
                    think_started = False
                thinking_content = "".join(accumulated_thinking) or None
                thinking_obj = (
                    Thinking(content=thinking_content, signature=None) if thinking_content else None
                )
                yield LLMStreamEvent(
                    type=LLMStreamEventType.TOOL_CALL,
                    tool_call=ToolCall(
                        id=f"call_{uuid.uuid4().hex[:8]}", name=func.get("name"), params=args
                    ),
                    thinking=thinking_obj,
                    usage=usage,
                )

        if think_started:
            yield LLMStreamEvent(type=LLMStreamEventType.THINK_END)
        if text_started:
            yield LLMStreamEvent(type=LLMStreamEventType.TEXT_END, usage=usage)

    def get_metadata(self) -> Metadata:
        return Metadata(
            name=self._model,
            context_window=32768,  # Common default for llama3
            owned_by="ollama",
        )