Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions gui_agents/s1/mllm/MultimodalEngine.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
import openai
import requests
from anthropic import Anthropic
from gui_agents.utils import (
anthropic_supports_temperature,
extract_anthropic_text,
)
from openai import APIConnectionError, APIError, AzureOpenAI, OpenAI, RateLimitError
from PIL import Image

Expand Down Expand Up @@ -115,18 +119,16 @@ def __init__(self, api_key=None, model=None, **kwargs):
)
def generate(self, messages, temperature=0.0, max_new_tokens=None, **kwargs):
"""Generate the next message based on previous messages"""
return (
self.llm_client.messages.create(
system=messages[0]["content"][0]["text"],
model=self.model,
messages=messages[1:],
max_tokens=max_new_tokens if max_new_tokens else 4096,
temperature=temperature,
**kwargs,
)
.content[0]
.text
)
request_kwargs = {
"system": messages[0]["content"][0]["text"],
"model": self.model,
"messages": messages[1:],
"max_tokens": max_new_tokens if max_new_tokens else 4096,
**kwargs,
}
if anthropic_supports_temperature(self.model):
request_kwargs["temperature"] = temperature
return extract_anthropic_text(self.llm_client.messages.create(**request_kwargs))


class OpenAIEmbeddingEngine(LMMEngine):
Expand Down
33 changes: 18 additions & 15 deletions gui_agents/s2/core/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import backoff
import numpy as np
from anthropic import Anthropic
from gui_agents.utils import (
anthropic_supports_temperature,
extract_anthropic_text,
extract_anthropic_thinking,
)
from openai import (
AzureOpenAI,
APIConnectionError,
Expand Down Expand Up @@ -215,21 +220,19 @@ def generate(self, messages, temperature=0.0, max_new_tokens=None, **kwargs):
thinking={"type": "enabled", "budget_tokens": 4096},
**kwargs,
)
thoughts = full_response.content[0].thinking
print("CLAUDE 3.7 THOUGHTS:", thoughts)
return full_response.content[1].text
return (
self.llm_client.messages.create(
system=messages[0]["content"][0]["text"],
model=self.model,
messages=messages[1:],
max_tokens=max_new_tokens if max_new_tokens else 4096,
temperature=temperature,
**kwargs,
)
.content[0]
.text
)
thoughts = extract_anthropic_thinking(full_response)
print("CLAUDE THOUGHTS:", thoughts)
return extract_anthropic_text(full_response)
request_kwargs = {
"system": messages[0]["content"][0]["text"],
"model": self.model,
"messages": messages[1:],
"max_tokens": max_new_tokens if max_new_tokens else 4096,
**kwargs,
}
if anthropic_supports_temperature(self.model):
request_kwargs["temperature"] = temperature
return extract_anthropic_text(self.llm_client.messages.create(**request_kwargs))


class LMMEngineGemini(LMMEngine):
Expand Down
34 changes: 18 additions & 16 deletions gui_agents/s2_5/core/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import backoff
from anthropic import Anthropic
from gui_agents.utils import (
anthropic_supports_temperature,
extract_anthropic_text,
extract_anthropic_thinking,
)
from openai import (
AzureOpenAI,
APIConnectionError,
Expand Down Expand Up @@ -107,20 +112,17 @@ def generate(self, messages, temperature=0.0, max_new_tokens=None, **kwargs):
thinking={"type": "enabled", "budget_tokens": 4096},
**kwargs,
)
thoughts = full_response.content[0].thinking
return full_response.content[1].text
return (
self.llm_client.messages.create(
system=messages[0]["content"][0]["text"],
model=self.model,
messages=messages[1:],
max_tokens=max_new_tokens if max_new_tokens else 4096,
temperature=temp,
**kwargs,
)
.content[0]
.text
)
return extract_anthropic_text(full_response)
request_kwargs = {
"system": messages[0]["content"][0]["text"],
"model": self.model,
"messages": messages[1:],
"max_tokens": max_new_tokens if max_new_tokens else 4096,
**kwargs,
}
if anthropic_supports_temperature(self.model):
request_kwargs["temperature"] = temp
return extract_anthropic_text(self.llm_client.messages.create(**request_kwargs))

@backoff.on_exception(
backoff.expo, (APIConnectionError, APIError, RateLimitError), max_time=60
Expand All @@ -140,8 +142,8 @@ def generate_with_thinking(
**kwargs,
)

thoughts = full_response.content[0].thinking
answer = full_response.content[1].text
thoughts = extract_anthropic_thinking(full_response)
answer = extract_anthropic_text(full_response)
full_response = (
f"<thoughts>\n{thoughts}\n</thoughts>\n\n<answer>\n{answer}\n</answer>\n"
)
Expand Down
2 changes: 1 addition & 1 deletion gui_agents/s3/agents/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(
"""
super().__init__(worker_engine_params, platform)

self.temperature = worker_engine_params.get("temperature", 0.0)
self.temperature = worker_engine_params.get("temperature") or 0.0
self.use_thinking = worker_engine_params.get("model", "") in [
"claude-opus-4-20250514",
"claude-sonnet-4-20250514",
Expand Down
3 changes: 2 additions & 1 deletion gui_agents/s3/cli_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,9 @@ def main():
"model": args.model,
"base_url": args.model_url,
"api_key": args.model_api_key,
"temperature": getattr(args, "model_temperature", None),
}
if args.model_temperature is not None:
engine_params["temperature"] = args.model_temperature

# Load the grounding engine from a custom endpoint
engine_params_for_grounding = {
Expand Down
43 changes: 25 additions & 18 deletions gui_agents/s3/core/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import backoff
from anthropic import Anthropic
from gui_agents.utils import (
anthropic_supports_temperature,
extract_anthropic_text,
extract_anthropic_thinking,
)
from openai import (
AzureOpenAI,
APIConnectionError,
Expand Down Expand Up @@ -95,8 +100,13 @@ def generate(self, messages, temperature=0.0, max_new_tokens=None, **kwargs):
"An API Key needs to be provided in either the api_key parameter or as an environment variable named ANTHROPIC_API_KEY"
)
self.llm_client = Anthropic(api_key=api_key)
# Use the instance temperature if not specified in the call
temp = self.temperature if temperature is None else temperature
# Prefer instance temperature when set; otherwise use call arg or default to 0.0
if self.temperature is not None:
temp = self.temperature
elif temperature is not None:
temp = temperature
else:
temp = 0.0
if self.thinking:
full_response = self.llm_client.messages.create(
system=messages[0]["content"][0]["text"],
Expand All @@ -106,20 +116,17 @@ def generate(self, messages, temperature=0.0, max_new_tokens=None, **kwargs):
thinking={"type": "enabled", "budget_tokens": 4096},
**kwargs,
)
thoughts = full_response.content[0].thinking
return full_response.content[1].text
return (
self.llm_client.messages.create(
system=messages[0]["content"][0]["text"],
model=self.model,
messages=messages[1:],
max_tokens=max_new_tokens if max_new_tokens else 4096,
temperature=temp,
**kwargs,
)
.content[0]
.text
)
return extract_anthropic_text(full_response)
request_kwargs = {
"system": messages[0]["content"][0]["text"],
"model": self.model,
"messages": messages[1:],
"max_tokens": max_new_tokens if max_new_tokens else 4096,
**kwargs,
}
if anthropic_supports_temperature(self.model):
request_kwargs["temperature"] = temp
return extract_anthropic_text(self.llm_client.messages.create(**request_kwargs))

@backoff.on_exception(
backoff.expo, (APIConnectionError, APIError, RateLimitError), max_time=60
Expand All @@ -144,8 +151,8 @@ def generate_with_thinking(
**kwargs,
)

thoughts = full_response.content[0].thinking
answer = full_response.content[1].text
thoughts = extract_anthropic_thinking(full_response)
answer = extract_anthropic_text(full_response)
full_response = (
f"<thoughts>\n{thoughts}\n</thoughts>\n\n<answer>\n{answer}\n</answer>\n"
)
Expand Down
41 changes: 41 additions & 0 deletions gui_agents/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,52 @@
"""General utility."""

import platform
import re
import requests
import zipfile
import io
import os

# Anthropic models that reject non-default sampling params (temperature, top_p, top_k).
_ANTHROPIC_NO_TEMPERATURE_PATTERNS = (
re.compile(r"sonnet-5"),
re.compile(r"opus-4-7"),
re.compile(r"opus-4-8"),
)


def anthropic_supports_temperature(model: str) -> bool:
"""Return False for Anthropic models that reject the temperature parameter."""
model_lower = model.lower()
return not any(
pattern.search(model_lower) for pattern in _ANTHROPIC_NO_TEMPERATURE_PATTERNS
)


def extract_anthropic_text(response) -> str:
"""Concatenate text from an Anthropic Messages response.

Newer models (e.g. Sonnet 5) enable adaptive thinking by default, so the
response content can contain ThinkingBlocks that have no `text` attribute.
This skips non-text blocks and returns only the assistant's text output.
"""
texts = [
block.text
for block in response.content
if getattr(block, "type", None) == "text"
]
return "".join(texts)


def extract_anthropic_thinking(response) -> str:
"""Concatenate thinking/reasoning text from an Anthropic Messages response."""
thoughts = [
getattr(block, "thinking", "")
for block in response.content
if getattr(block, "type", None) == "thinking"
]
return "".join(thoughts)


def download_kb_data(
version="s2",
Expand Down
Loading
Loading