chore: import upstream snapshot with attribution
lint / build (3.10) (push) Failing after 1s
lint / build (3.11) (push) Failing after 1s

This commit is contained in:
wehub-resource-sync
2026-07-13 12:23:35 +08:00
commit c8c954c85d
127 changed files with 22519 additions and 0 deletions
View File
+485
View File
@@ -0,0 +1,485 @@
import os
import backoff
import numpy as np
from anthropic import Anthropic
from openai import (
AzureOpenAI,
APIConnectionError,
APIError,
AzureOpenAI,
OpenAI,
RateLimitError,
)
from google import genai
from google.genai import types
class LMMEngine:
pass
class OpenAIEmbeddingEngine(LMMEngine):
def __init__(
self,
embedding_model: str = "text-embedding-3-small",
api_key=None,
):
"""Init an OpenAI Embedding engine
Args:
embedding_model (str, optional): Model name. Defaults to "text-embedding-3-small".
api_key (_type_, optional): Auth key from OpenAI. Defaults to None.
"""
self.model = embedding_model
self.api_key = api_key
@backoff.on_exception(
backoff.expo,
(
APIError,
RateLimitError,
APIConnectionError,
),
)
def get_embeddings(self, text: str) -> np.ndarray:
api_key = self.api_key or os.getenv("OPENAI_API_KEY")
if api_key is None:
raise ValueError(
"An API Key needs to be provided in either the api_key parameter or as an environment variable named OPENAI_API_KEY"
)
client = OpenAI(api_key=api_key)
response = client.embeddings.create(model=self.model, input=text)
return np.array([data.embedding for data in response.data])
class GeminiEmbeddingEngine(LMMEngine):
def __init__(
self,
embedding_model: str = "text-embedding-004",
api_key=None,
):
"""Init an Gemini Embedding engine
Args:
embedding_model (str, optional): Model name. Defaults to "text-embedding-004".
api_key (_type_, optional): Auth key from Gemini. Defaults to None.
"""
self.model = embedding_model
self.api_key = api_key
@backoff.on_exception(
backoff.expo,
(
APIError,
RateLimitError,
APIConnectionError,
),
)
def get_embeddings(self, text: str) -> np.ndarray:
api_key = self.api_key or os.getenv("GEMINI_API_KEY")
if api_key is None:
raise ValueError(
"An API Key needs to be provided in either the api_key parameter or as an environment variable named GEMINI_API_KEY"
)
client = genai.Client(api_key=api_key)
result = client.models.embed_content(
model=self.model,
contents=text,
config=types.EmbedContentConfig(task_type="SEMANTIC_SIMILARITY"),
)
return np.array([i.values for i in result.embeddings])
class AzureOpenAIEmbeddingEngine(LMMEngine):
def __init__(
self,
embedding_model: str = "text-embedding-3-small",
api_key=None,
api_version=None,
endpoint_url=None,
):
"""Init an Azure OpenAI Embedding engine
Args:
embedding_model (str, optional): Model name. Defaults to "text-embedding-3-small".
api_key (_type_, optional): Auth key from Azure OpenAI. Defaults to None.
api_version (_type_, optional): API version. Defaults to None.
endpoint_url (_type_, optional): Endpoint URL. Defaults to None.
"""
self.model = embedding_model
self.api_key = api_key
self.api_version = api_version
self.endpoint_url = endpoint_url
@backoff.on_exception(
backoff.expo,
(
APIError,
RateLimitError,
APIConnectionError,
),
)
def get_embeddings(self, text: str) -> np.ndarray:
api_key = self.api_key or os.getenv("AZURE_OPENAI_API_KEY")
if api_key is None:
raise ValueError(
"An API Key needs to be provided in either the api_key parameter or as an environment variable named AZURE_OPENAI_API_KEY"
)
api_version = self.api_version or os.getenv("OPENAI_API_VERSION")
if api_version is None:
raise ValueError(
"An API Version needs to be provided in either the api_version parameter or as an environment variable named OPENAI_API_VERSION"
)
endpoint_url = self.endpoint_url or os.getenv("AZURE_OPENAI_ENDPOINT")
if endpoint_url is None:
raise ValueError(
"An Endpoint URL needs to be provided in either the endpoint_url parameter or as an environment variable named AZURE_OPENAI_ENDPOINT"
)
client = AzureOpenAI(
api_key=api_key,
api_version=api_version,
azure_endpoint=endpoint_url,
)
response = client.embeddings.create(input=text, model=self.model)
return np.array([data.embedding for data in response.data])
class LMMEngineOpenAI(LMMEngine):
def __init__(
self, base_url=None, api_key=None, model=None, rate_limit=-1, **kwargs
):
assert model is not None, "model must be provided"
self.model = model
self.base_url = base_url
self.api_key = api_key
self.request_interval = 0 if rate_limit == -1 else 60.0 / rate_limit
self.llm_client = None
@backoff.on_exception(
backoff.expo, (APIConnectionError, APIError, RateLimitError), max_time=60
)
def generate(self, messages, temperature=0.0, max_new_tokens=None, **kwargs):
api_key = self.api_key or os.getenv("OPENAI_API_KEY")
if api_key is None:
raise ValueError(
"An API Key needs to be provided in either the api_key parameter or as an environment variable named OPENAI_API_KEY"
)
if not self.llm_client:
if not self.base_url:
self.llm_client = OpenAI(api_key=api_key)
else:
self.llm_client = OpenAI(base_url=self.base_url, api_key=api_key)
return (
self.llm_client.chat.completions.create(
model=self.model,
messages=messages,
max_tokens=max_new_tokens if max_new_tokens else 4096,
temperature=temperature,
**kwargs,
)
.choices[0]
.message.content
)
class LMMEngineAnthropic(LMMEngine):
def __init__(
self, base_url=None, api_key=None, model=None, thinking=False, **kwargs
):
assert model is not None, "model must be provided"
self.model = model
self.thinking = thinking
self.api_key = api_key
self.llm_client = None
@backoff.on_exception(
backoff.expo, (APIConnectionError, APIError, RateLimitError), max_time=60
)
def generate(self, messages, temperature=0.0, max_new_tokens=None, **kwargs):
api_key = self.api_key or os.getenv("ANTHROPIC_API_KEY")
if api_key is None:
raise ValueError(
"An API Key needs to be provided in either the api_key parameter or as an environment variable named ANTHROPIC_API_KEY"
)
if not self.llm_client:
self.llm_client = Anthropic(api_key=api_key)
if self.thinking:
full_response = self.llm_client.messages.create(
system=messages[0]["content"][0]["text"],
model=self.model,
messages=messages[1:],
max_tokens=8192,
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
)
class LMMEngineGemini(LMMEngine):
def __init__(
self, base_url=None, api_key=None, model=None, rate_limit=-1, **kwargs
):
assert model is not None, "model must be provided"
self.model = model
self.base_url = base_url
self.api_key = api_key
self.request_interval = 0 if rate_limit == -1 else 60.0 / rate_limit
self.llm_client = None
@backoff.on_exception(
backoff.expo, (APIConnectionError, APIError, RateLimitError), max_time=60
)
def generate(self, messages, temperature=0.0, max_new_tokens=None, **kwargs):
api_key = self.api_key or os.getenv("GEMINI_API_KEY")
if api_key is None:
raise ValueError(
"An API Key needs to be provided in either the api_key parameter or as an environment variable named GEMINI_API_KEY"
)
base_url = self.base_url or os.getenv("GEMINI_ENDPOINT_URL")
if base_url is None:
raise ValueError(
"An endpoint URL needs to be provided in either the endpoint_url parameter or as an environment variable named GEMINI_ENDPOINT_URL"
)
if not self.llm_client:
self.llm_client = OpenAI(base_url=base_url, api_key=api_key)
return (
self.llm_client.chat.completions.create(
model=self.model,
messages=messages,
max_tokens=max_new_tokens if max_new_tokens else 4096,
temperature=temperature,
**kwargs,
)
.choices[0]
.message.content
)
class LMMEngineOpenRouter(LMMEngine):
def __init__(
self, base_url=None, api_key=None, model=None, rate_limit=-1, **kwargs
):
assert model is not None, "model must be provided"
self.model = model
self.base_url = base_url
self.api_key = api_key
self.request_interval = 0 if rate_limit == -1 else 60.0 / rate_limit
self.llm_client = None
@backoff.on_exception(
backoff.expo, (APIConnectionError, APIError, RateLimitError), max_time=60
)
def generate(self, messages, temperature=0.0, max_new_tokens=None, **kwargs):
api_key = self.api_key or os.getenv("OPENROUTER_API_KEY")
if api_key is None:
raise ValueError(
"An API Key needs to be provided in either the api_key parameter or as an environment variable named OPENROUTER_API_KEY"
)
base_url = self.base_url or os.getenv("OPEN_ROUTER_ENDPOINT_URL")
if base_url is None:
raise ValueError(
"An endpoint URL needs to be provided in either the endpoint_url parameter or as an environment variable named OPEN_ROUTER_ENDPOINT_URL"
)
if not self.llm_client:
self.llm_client = OpenAI(base_url=base_url, api_key=api_key)
return (
self.llm_client.chat.completions.create(
model=self.model,
messages=messages,
max_tokens=max_new_tokens if max_new_tokens else 4096,
temperature=temperature,
**kwargs,
)
.choices[0]
.message.content
)
class LMMEngineAzureOpenAI(LMMEngine):
def __init__(
self,
base_url=None,
api_key=None,
azure_endpoint=None,
model=None,
api_version=None,
rate_limit=-1,
**kwargs
):
assert model is not None, "model must be provided"
self.model = model
self.api_version = api_version
self.api_key = api_key
self.azure_endpoint = azure_endpoint
self.request_interval = 0 if rate_limit == -1 else 60.0 / rate_limit
self.llm_client = None
self.cost = 0.0
@backoff.on_exception(
backoff.expo, (APIConnectionError, APIError, RateLimitError), max_time=60
)
def generate(self, messages, temperature=0.0, max_new_tokens=None, **kwargs):
api_key = self.api_key or os.getenv("AZURE_OPENAI_API_KEY")
if api_key is None:
raise ValueError(
"An API Key needs to be provided in either the api_key parameter or as an environment variable named AZURE_OPENAI_API_KEY"
)
api_version = self.api_version or os.getenv("OPENAI_API_VERSION")
if api_version is None:
raise ValueError(
"api_version must be provided either as a parameter or as an environment variable named OPENAI_API_VERSION"
)
azure_endpoint = self.azure_endpoint or os.getenv("AZURE_OPENAI_ENDPOINT")
if azure_endpoint is None:
raise ValueError(
"An Azure API endpoint needs to be provided in either the azure_endpoint parameter or as an environment variable named AZURE_OPENAI_ENDPOINT"
)
if not self.llm_client:
self.llm_client = AzureOpenAI(
azure_endpoint=azure_endpoint,
api_key=api_key,
api_version=api_version,
)
completion = self.llm_client.chat.completions.create(
model=self.model,
messages=messages,
max_tokens=max_new_tokens if max_new_tokens else 4096,
temperature=temperature,
**kwargs,
)
total_tokens = completion.usage.total_tokens
self.cost += 0.02 * ((total_tokens + 500) / 1000)
return completion.choices[0].message.content
class LMMEnginevLLM(LMMEngine):
def __init__(
self, base_url=None, api_key=None, model=None, rate_limit=-1, **kwargs
):
assert model is not None, "model must be provided"
self.model = model
self.api_key = api_key
self.base_url = base_url
self.request_interval = 0 if rate_limit == -1 else 60.0 / rate_limit
self.llm_client = None
@backoff.on_exception(
backoff.expo, (APIConnectionError, APIError, RateLimitError), max_time=60
)
def generate(
self,
messages,
temperature=0.0,
top_p=0.8,
repetition_penalty=1.05,
max_new_tokens=512,
**kwargs
):
api_key = self.api_key or os.getenv("vLLM_API_KEY")
if api_key is None:
raise ValueError(
"A vLLM API key needs to be provided in either the api_key parameter or as an environment variable named vLLM_API_KEY"
)
base_url = self.base_url or os.getenv("vLLM_ENDPOINT_URL")
if base_url is None:
raise ValueError(
"An endpoint URL needs to be provided in either the endpoint_url parameter or as an environment variable named vLLM_ENDPOINT_URL"
)
if not self.llm_client:
self.llm_client = OpenAI(base_url=base_url, api_key=api_key)
completion = self.llm_client.chat.completions.create(
model=self.model,
messages=messages,
max_tokens=max_new_tokens if max_new_tokens else 4096,
temperature=temperature,
top_p=top_p,
extra_body={"repetition_penalty": repetition_penalty},
)
return completion.choices[0].message.content
class LMMEngineHuggingFace(LMMEngine):
def __init__(self, base_url=None, api_key=None, rate_limit=-1, **kwargs):
self.base_url = base_url
self.api_key = api_key
self.request_interval = 0 if rate_limit == -1 else 60.0 / rate_limit
self.llm_client = None
@backoff.on_exception(
backoff.expo, (APIConnectionError, APIError, RateLimitError), max_time=60
)
def generate(self, messages, temperature=0.0, max_new_tokens=None, **kwargs):
api_key = self.api_key or os.getenv("HF_TOKEN")
if api_key is None:
raise ValueError(
"A HuggingFace token needs to be provided in either the api_key parameter or as an environment variable named HF_TOKEN"
)
base_url = self.base_url
if base_url is None:
raise ValueError(
"HuggingFace endpoint must be provided as base_url parameter."
)
if not self.llm_client:
self.llm_client = OpenAI(base_url=base_url, api_key=api_key)
return (
self.llm_client.chat.completions.create(
model="tgi",
messages=messages,
max_tokens=max_new_tokens if max_new_tokens else 4096,
temperature=temperature,
**kwargs,
)
.choices[0]
.message.content
)
class LMMEngineParasail(LMMEngine):
def __init__(self, api_key=None, model=None, rate_limit=-1, **kwargs):
assert model is not None, "Parasail model id must be provided"
self.model = model
self.api_key = api_key
self.request_interval = 0 if rate_limit == -1 else 60.0 / rate_limit
self.llm_client = None
@backoff.on_exception(
backoff.expo, (APIConnectionError, APIError, RateLimitError), max_time=60
)
def generate(self, messages, temperature=0.0, max_new_tokens=None, **kwargs):
api_key = self.api_key or os.getenv("PARASAIL_API_KEY")
if api_key is None:
raise ValueError(
"A Parasail API key needs to be provided in either the api_key parameter or as an environment variable named PARASAIL_API_KEY"
)
if not self.llm_client:
self.llm_client = OpenAI(
base_url="https://api.parasail.io/v1", api_key=api_key
)
return (
self.llm_client.chat.completions.create(
model=self.model,
messages=messages,
max_tokens=max_new_tokens if max_new_tokens else 4096,
temperature=temperature,
**kwargs,
)
.choices[0]
.message.content
)
+420
View File
@@ -0,0 +1,420 @@
import json
import os
from typing import Dict, Tuple
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
from gui_agents.s2.core.module import BaseModule
from gui_agents.s2.memory.procedural_memory import PROCEDURAL_MEMORY
from gui_agents.s2.utils.common_utils import (
call_llm_safe,
load_embeddings,
load_knowledge_base,
save_embeddings,
)
from gui_agents.s2.utils.query_perplexica import query_to_perplexica
class KnowledgeBase(BaseModule):
def __init__(
self,
embedding_engine,
local_kb_path: str,
platform: str,
engine_params: Dict,
save_knowledge: bool = True,
):
super().__init__(engine_params, platform)
self.local_kb_path = local_kb_path
# initialize embedding engine
self.embedding_engine = embedding_engine
# Initialize paths for different memory types
self.episodic_memory_path = os.path.join(
self.local_kb_path, self.platform, "episodic_memory.json"
)
self.narrative_memory_path = os.path.join(
self.local_kb_path, self.platform, "narrative_memory.json"
)
self.embeddings_path = os.path.join(
self.local_kb_path, self.platform, "embeddings.pkl"
)
# Initialize trajectory tracking
self.task_trajectory = ""
self.current_subtask_trajectory = ""
self.current_search_query = ""
self.rag_module_system_prompt = PROCEDURAL_MEMORY.RAG_AGENT.replace(
"CURRENT_OS", self.platform
)
# All three agents share a generic RAG prompt that asks the agent to provide information for UI automation in CURRENT_OS
self.query_formulator = self._create_agent(self.rag_module_system_prompt)
self.llm_search_agent = self._create_agent(self.rag_module_system_prompt)
self.knowledge_fusion_agent = self._create_agent(self.rag_module_system_prompt)
self.narrative_summarization_agent = self._create_agent(
PROCEDURAL_MEMORY.TASK_SUMMARIZATION_PROMPT
)
self.episode_summarization_agent = self._create_agent(
PROCEDURAL_MEMORY.SUBTASK_SUMMARIZATION_PROMPT
)
self.save_knowledge = save_knowledge
def retrieve_knowledge(
self, instruction: str, search_query: str, search_engine: str = "llm"
) -> Tuple[str, str]:
"""Retrieve knowledge using search engine
Args:
instruction (str): task instruction
observation (Dict): current observation
search_engine (str): search engine to use"""
# Use search engine to retrieve knowledge based on the formulated query
search_results = self._search(instruction, search_query, search_engine)
return search_query, search_results
def formulate_query(self, instruction: str, observation: Dict) -> str:
"""Formulate search query based on instruction and current state"""
query_path = os.path.join(
self.local_kb_path, self.platform, "formulate_query.json"
)
try:
with open(query_path, "r") as f:
formulate_query = json.load(f)
except:
formulate_query = {}
if instruction in formulate_query:
return formulate_query[instruction]
self.query_formulator.reset()
self.query_formulator.add_message(
f"The task is: {instruction}\n"
"To use google search to get some useful information, first carefully analyze "
"the screenshot of the current desktop UI state, then given the task "
"instruction, formulate a question that can be used to search on the Internet "
"for information in helping with the task execution.\n"
"The question should not be too general or too specific. Please ONLY provide "
"the question.\nQuestion:",
image_content=(
observation["screenshot"] if "screenshot" in observation else None
),
role="user",
)
search_query = self.query_formulator.get_response().strip().replace('"', "")
print("search query: ", search_query)
formulate_query[instruction] = search_query
with open(query_path, "w") as f:
json.dump(formulate_query, f, indent=2)
return search_query
def _search(self, instruction: str, search_query: str, search_engine: str) -> str:
"""Execute search using specified engine"""
# Default to perplexica rag knowledge to see if the query exists
file = os.path.join(
self.local_kb_path, self.platform, f"{search_engine}_rag_knowledge.json"
)
try:
with open(file, "r") as f:
exist_search_results = json.load(f)
except:
exist_search_results = {}
if instruction in exist_search_results:
return exist_search_results[instruction]
if search_engine.lower() == "llm":
self.llm_search_agent.reset()
# Use LLM's internal knowledge like a search engine
self.llm_search_agent.add_message(search_query, role="user")
search_results = self.llm_search_agent.get_response()
elif search_engine.lower() == "perplexica":
# Use perplexica to search for the query
search_results = query_to_perplexica(search_query)
else:
raise ValueError(f"Unsupported search engine: {search_engine}")
exist_search_results[instruction] = search_results.strip()
with open(
os.path.join(
self.local_kb_path,
self.platform,
f"{search_engine}_rag_knowledge.json",
),
"w",
) as f:
json.dump(exist_search_results, f, indent=2)
return search_results
def retrieve_narrative_experience(self, instruction: str) -> Tuple[str, str]:
"""Retrieve narrative experience using embeddings"""
knowledge_base = load_knowledge_base(self.narrative_memory_path)
if not knowledge_base:
return "None", "None"
embeddings = load_embeddings(self.embeddings_path)
# Get or create instruction embedding
instruction_embedding = embeddings.get(instruction)
if instruction_embedding is None:
instruction_embedding = self.embedding_engine.get_embeddings(instruction)
embeddings[instruction] = instruction_embedding
# Get or create embeddings for knowledge base entries
candidate_embeddings = []
for key in knowledge_base:
candidate_embedding = embeddings.get(key)
if candidate_embedding is None:
candidate_embedding = self.embedding_engine.get_embeddings(key)
embeddings[key] = candidate_embedding
candidate_embeddings.append(candidate_embedding)
save_embeddings(self.embeddings_path, embeddings)
similarities = cosine_similarity(
instruction_embedding, np.vstack(candidate_embeddings)
)[0]
sorted_indices = np.argsort(similarities)[::-1]
keys = list(knowledge_base.keys())
idx = 1 if keys[sorted_indices[0]] == instruction else 0
return keys[sorted_indices[idx]], knowledge_base[keys[sorted_indices[idx]]]
def retrieve_episodic_experience(self, instruction: str) -> Tuple[str, str]:
"""Retrieve similar task experience using embeddings"""
knowledge_base = load_knowledge_base(self.episodic_memory_path)
if not knowledge_base:
return "None", "None"
embeddings = load_embeddings(self.embeddings_path)
# Get or create instruction embedding
instruction_embedding = embeddings.get(instruction)
if instruction_embedding is None:
instruction_embedding = self.embedding_engine.get_embeddings(instruction)
embeddings[instruction] = instruction_embedding
# Get or create embeddings for knowledge base entries
candidate_embeddings = []
for key in knowledge_base:
candidate_embedding = embeddings.get(key)
if candidate_embedding is None:
candidate_embedding = self.embedding_engine.get_embeddings(key)
embeddings[key] = candidate_embedding
candidate_embeddings.append(candidate_embedding)
save_embeddings(self.embeddings_path, embeddings)
similarities = cosine_similarity(
instruction_embedding, np.vstack(candidate_embeddings)
)[0]
sorted_indices = np.argsort(similarities)[::-1]
keys = list(knowledge_base.keys())
idx = 1 if keys[sorted_indices[0]] == instruction else 0
return keys[sorted_indices[idx]], knowledge_base[keys[sorted_indices[idx]]]
def knowledge_fusion(
self,
observation: Dict,
instruction: str,
web_knowledge: str,
similar_task: str,
experience: str,
) -> str:
"""Combine web knowledge with similar task experience"""
self.knowledge_fusion_agent.reset()
self.knowledge_fusion_agent.add_message(
f"Task: {instruction}\n"
f"**Web search result**:\n{web_knowledge}\n\n"
f"**Retrieved similar task experience**:\n"
f"Similar task:{similar_task}\n{experience}\n\n"
f"Based on the web search result and the retrieved similar task experience, "
f"if you think the similar task experience is indeed useful to the main task, "
f"integrate it with the web search result. Provide the final knowledge in a numbered list.",
image_content=(
observation["screenshot"] if "screenshot" in observation else None
),
role="user",
)
return self.knowledge_fusion_agent.get_response()
def save_episodic_memory(self, subtask_key: str, subtask_traj: str) -> None:
"""Save episodic memory (subtask level knowledge).
Args:
subtask_key (str): Key identifying the subtask
subtask_traj (str): Trajectory/experience of the subtask
"""
if not self.save_knowledge:
return
try:
kb = load_knowledge_base(self.episodic_memory_path)
except:
kb = {}
if subtask_key not in kb:
subtask_summarization = self.summarize_episode(subtask_traj)
kb[subtask_key] = subtask_summarization
os.makedirs(os.path.dirname(self.episodic_memory_path), exist_ok=True)
with open(self.episodic_memory_path, "w") as fout:
json.dump(kb, fout, indent=2)
return kb.get(subtask_key)
def save_narrative_memory(self, task_key: str, task_traj: str) -> None:
"""Save narrative memory (task level knowledge).
Args:
task_key (str): Key identifying the task
task_traj (str): Full trajectory/experience of the task
"""
if not self.save_knowledge:
return
try:
kb = load_knowledge_base(self.narrative_memory_path)
except:
kb = {}
if task_key not in kb:
task_summarization = self.summarize_narrative(task_traj)
kb[task_key] = task_summarization
os.makedirs(os.path.dirname(self.narrative_memory_path), exist_ok=True)
with open(self.narrative_memory_path, "w") as fout:
json.dump(kb, fout, indent=2)
return kb.get(task_key)
def initialize_task_trajectory(self, instruction: str) -> None:
"""Initialize a new task trajectory.
Args:
instruction (str): The task instruction
"""
self.task_trajectory = f"Task:\n{instruction}"
self.current_search_query = ""
self.current_subtask_trajectory = ""
def update_task_trajectory(self, meta_data: Dict) -> None:
"""Update the task trajectory with new metadata.
Args:
meta_data (Dict): Metadata from the agent's prediction
"""
if not self.current_search_query and "search_query" in meta_data:
self.current_search_query = meta_data["search_query"]
self.task_trajectory += (
"\n\nReflection:\n"
+ str(meta_data["reflection"])
+ "\n\n----------------------\n\nPlan:\n"
+ meta_data["executor_plan"]
)
def handle_subtask_trajectory(self, meta_data: Dict) -> None:
"""Handle subtask trajectory updates based on subtask status.
Args:
meta_data (Dict): Metadata containing subtask information
Returns:
bool: Whether the subtask was completed
"""
subtask_status = meta_data["subtask_status"]
subtask = meta_data["subtask"]
subtask_info = meta_data["subtask_info"]
if subtask_status in ["Start", "Done"]:
# If there's an existing subtask trajectory, finalize it
if self.current_subtask_trajectory:
self.current_subtask_trajectory += "\nSubtask Completed.\n"
subtask_key = self.current_subtask_trajectory.split(
"\n----------------------\n\nPlan:\n"
)[0]
self.save_episodic_memory(subtask_key, self.current_subtask_trajectory)
self.current_subtask_trajectory = ""
return True
# Start new subtask trajectory
self.current_subtask_trajectory = (
f"Task:\n{self.current_search_query}\n\n"
f"Subtask: {subtask}\n"
f"Subtask Instruction: {subtask_info}\n"
f"----------------------\n\n"
f'Plan:\n{meta_data["executor_plan"]}\n'
)
return False
elif subtask_status == "In":
# Continue current subtask trajectory
self.current_subtask_trajectory += (
f'\n----------------------\n\nPlan:\n{meta_data["executor_plan"]}\n'
)
return False
def finalize_task(self) -> None:
"""Finalize the task by saving any remaining trajectories."""
# Save any remaining subtask trajectory
if self.current_subtask_trajectory:
self.current_subtask_trajectory += "\nSubtask Completed.\n"
subtask_key = self.current_subtask_trajectory.split(
"\n----------------------\n\nPlan:\n"
)[0]
self.save_episodic_memory(subtask_key, self.current_subtask_trajectory)
# Save the complete task trajectory
if self.task_trajectory and self.current_search_query:
self.save_narrative_memory(self.current_search_query, self.task_trajectory)
# Reset trajectories
self.task_trajectory = ""
self.current_subtask_trajectory = ""
self.current_search_query = ""
def summarize_episode(self, trajectory):
"""Summarize the episode experience for lifelong learning reflection
Args:
trajectory: str: The episode experience to be summarized
"""
# Create Reflection on whole trajectories for next round trial, keep earlier messages as exemplars
self.episode_summarization_agent.add_message(trajectory)
subtask_summarization = call_llm_safe(self.episode_summarization_agent)
self.episode_summarization_agent.add_message(subtask_summarization)
return subtask_summarization
def summarize_narrative(self, trajectory):
"""Summarize the narrative experience for lifelong learning reflection
Args:
trajectory: str: The narrative experience to be summarized
"""
# Create Reflection on whole trajectories for next round trial
self.narrative_summarization_agent.add_message(trajectory)
task_summarization = call_llm_safe(self.narrative_summarization_agent)
return task_summarization
+295
View File
@@ -0,0 +1,295 @@
import base64
import numpy as np
from gui_agents.s2.core.engine import (
LMMEngineAnthropic,
LMMEngineAzureOpenAI,
LMMEngineHuggingFace,
LMMEngineOpenAI,
LMMEngineOpenRouter,
LMMEngineParasail,
LMMEnginevLLM,
LMMEngineGemini,
)
class LMMAgent:
def __init__(self, engine_params=None, system_prompt=None, engine=None):
if engine is None:
if engine_params is not None:
engine_type = engine_params.get("engine_type")
if engine_type == "openai":
self.engine = LMMEngineOpenAI(**engine_params)
elif engine_type == "anthropic":
self.engine = LMMEngineAnthropic(**engine_params)
elif engine_type == "azure":
self.engine = LMMEngineAzureOpenAI(**engine_params)
elif engine_type == "vllm":
self.engine = LMMEnginevLLM(**engine_params)
elif engine_type == "huggingface":
self.engine = LMMEngineHuggingFace(**engine_params)
elif engine_type == "gemini":
self.engine = LMMEngineGemini(**engine_params)
elif engine_type == "open_router":
self.engine = LMMEngineOpenRouter(**engine_params)
elif engine_type == "parasail":
self.engine = LMMEngineParasail(**engine_params)
else:
raise ValueError("engine_type is not supported")
else:
raise ValueError("engine_params must be provided")
else:
self.engine = engine
self.messages = [] # Empty messages
if system_prompt:
self.add_system_prompt(system_prompt)
else:
self.add_system_prompt("You are a helpful assistant.")
def encode_image(self, image_content):
# if image_content is a path to an image file, check type of the image_content to verify
if isinstance(image_content, str):
with open(image_content, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
else:
return base64.b64encode(image_content).decode("utf-8")
def reset(
self,
):
self.messages = [
{
"role": "system",
"content": [{"type": "text", "text": self.system_prompt}],
}
]
def add_system_prompt(self, system_prompt):
self.system_prompt = system_prompt
if len(self.messages) > 0:
self.messages[0] = {
"role": "system",
"content": [{"type": "text", "text": self.system_prompt}],
}
else:
self.messages.append(
{
"role": "system",
"content": [{"type": "text", "text": self.system_prompt}],
}
)
def remove_message_at(self, index):
"""Remove a message at a given index"""
if index < len(self.messages):
self.messages.pop(index)
def replace_message_at(
self, index, text_content, image_content=None, image_detail="high"
):
"""Replace a message at a given index"""
if index < len(self.messages):
self.messages[index] = {
"role": self.messages[index]["role"],
"content": [{"type": "text", "text": text_content}],
}
if image_content:
base64_image = self.encode_image(image_content)
self.messages[index]["content"].append(
{
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{base64_image}",
"detail": image_detail,
},
}
)
def add_message(
self,
text_content,
image_content=None,
role=None,
image_detail="high",
put_text_last=False,
):
"""Add a new message to the list of messages"""
# API-style inference from OpenAI and AzureOpenAI
if isinstance(
self.engine,
(
LMMEngineOpenAI,
LMMEngineAzureOpenAI,
LMMEngineHuggingFace,
LMMEngineGemini,
LMMEngineOpenRouter,
LMMEngineParasail,
),
):
# infer role from previous message
if role != "user":
if self.messages[-1]["role"] == "system":
role = "user"
elif self.messages[-1]["role"] == "user":
role = "assistant"
elif self.messages[-1]["role"] == "assistant":
role = "user"
message = {
"role": role,
"content": [{"type": "text", "text": text_content}],
}
if isinstance(image_content, np.ndarray) or image_content:
# Check if image_content is a list or a single image
if isinstance(image_content, list):
# If image_content is a list of images, loop through each image
for image in image_content:
base64_image = self.encode_image(image)
message["content"].append(
{
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{base64_image}",
"detail": image_detail,
},
}
)
else:
# If image_content is a single image, handle it directly
base64_image = self.encode_image(image_content)
message["content"].append(
{
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{base64_image}",
"detail": image_detail,
},
}
)
# Rotate text to be the last message if desired
if put_text_last:
text_content = message["content"].pop(0)
message["content"].append(text_content)
self.messages.append(message)
# For API-style inference from Anthropic
elif isinstance(self.engine, LMMEngineAnthropic):
# infer role from previous message
if role != "user":
if self.messages[-1]["role"] == "system":
role = "user"
elif self.messages[-1]["role"] == "user":
role = "assistant"
elif self.messages[-1]["role"] == "assistant":
role = "user"
message = {
"role": role,
"content": [{"type": "text", "text": text_content}],
}
if image_content:
# Check if image_content is a list or a single image
if isinstance(image_content, list):
# If image_content is a list of images, loop through each image
for image in image_content:
base64_image = self.encode_image(image)
message["content"].append(
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": base64_image,
},
}
)
else:
# If image_content is a single image, handle it directly
base64_image = self.encode_image(image_content)
message["content"].append(
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": base64_image,
},
}
)
self.messages.append(message)
# Locally hosted vLLM model inference
elif isinstance(self.engine, LMMEnginevLLM):
# infer role from previous message
if role != "user":
if self.messages[-1]["role"] == "system":
role = "user"
elif self.messages[-1]["role"] == "user":
role = "assistant"
elif self.messages[-1]["role"] == "assistant":
role = "user"
message = {
"role": role,
"content": [{"type": "text", "text": text_content}],
}
if image_content:
# Check if image_content is a list or a single image
if isinstance(image_content, list):
# If image_content is a list of images, loop through each image
for image in image_content:
base64_image = self.encode_image(image)
message["content"].append(
{
"type": "image_url",
"image_url": {
"url": f"data:image;base64,{base64_image}"
},
}
)
else:
# If image_content is a single image, handle it directly
base64_image = self.encode_image(image_content)
message["content"].append(
{
"type": "image_url",
"image_url": {"url": f"data:image;base64,{base64_image}"},
}
)
self.messages.append(message)
else:
raise ValueError("engine_type is not supported")
def get_response(
self,
user_message=None,
messages=None,
temperature=0.0,
max_new_tokens=None,
**kwargs,
):
"""Generate the next response based on previous messages"""
if messages is None:
messages = self.messages
if user_message:
messages.append(
{"role": "user", "content": [{"type": "text", "text": user_message}]}
)
return self.engine.generate(
messages,
temperature=temperature,
max_new_tokens=max_new_tokens,
**kwargs,
)
+17
View File
@@ -0,0 +1,17 @@
from typing import Dict, Optional
from gui_agents.s2.core.mllm import LMMAgent
class BaseModule:
def __init__(self, engine_params: Dict, platform: str):
self.engine_params = engine_params
self.platform = platform
def _create_agent(
self, system_prompt: str = None, engine_params: Optional[Dict] = None
) -> LMMAgent:
"""Create a new LMMAgent instance"""
agent = LMMAgent(engine_params or self.engine_params)
if system_prompt:
agent.add_system_prompt(system_prompt)
return agent