Chuỗi bài: Humanoid
python
109 dòng
· Cập nhật 2026-03-12
planner.py
Humanoid/May_12/RoboOS/master/agents/planner.py
from typing import Any, Dict, Union
import yaml
from agents.prompts import MASTER_PLANNING_PLANNING
from flag_scale.flagscale.agent.collaboration import Collaborator
from openai import AzureOpenAI, OpenAI
class GlobalTaskPlanner:
"""A tool planner to plan task into sub-tasks."""
def __init__(
self,
config: Union[Dict, str] = None,
) -> None:
self.collaborator = Collaborator.from_config(config["collaborator"])
self.global_model: Any
self.model_name: str
self.global_model, self.model_name = self._get_model_info_from_config(
config["model"]
)
self.profiling = config["profiling"]
def _get_model_info_from_config(self, config: Dict) -> tuple:
"""Get the model info from config."""
candidate = config["model_dict"]
if candidate["cloud_model"] in config["model_select"]:
if candidate["cloud_type"] == "azure":
model_name = config["model_select"]
model_client = AzureOpenAI(
azure_endpoint=candidate["azure_endpoint"],
azure_deployment=candidate["azure_deployment"],
api_version=candidate["azure_api_version"],
api_key=candidate["azure_api_key"],
)
elif candidate["cloud_type"] == "default":
model_client = OpenAI(
base_url=candidate["cloud_server"],
api_key=candidate["cloud_api_key"],
)
model_name = config["model_select"]
else:
raise ValueError(f"Unsupported cloud type: {candidate['cloud_type']}")
return model_client, model_name
raise ValueError(f"Unsupported model: {config['model_select']}")
def _init_config(self, config_path="config.yaml"):
"""Initialize configuration"""
with open(config_path, "r", encoding="utf-8") as f:
config = yaml.safe_load(f)
return config
def display_profiling_info(self, description: str, message: any):
"""
Outputs profiling information if profiling is enabled.
:param message: The content to be printed. Can be of any type.
:param description: A brief title or description for the message.
"""
if self.profiling:
module_name = "master" # Name of the current module
print(f" [{module_name}] {description}:")
print(message)
def forward(self, task: str) -> str:
"""Get the sub-tasks from the task."""
all_robots_name = self.collaborator.read_all_agents_name()
all_robots_info = self.collaborator.read_all_agents_info()
all_environments_info = self.collaborator.read_environment(name=None)
content = MASTER_PLANNING_PLANNING.format(
robot_name_list=all_robots_name, robot_tools_info=all_robots_info, task=task, scene_info=all_environments_info
)
messages = [
{
"role": "user",
"content": [
{"type": "text", "text": content},
],
},
]
self.display_profiling_info("messages", messages)
from datetime import datetime
start_inference = datetime.now()
response = self.global_model.chat.completions.create(
model=self.model_name,
messages=messages,
temperature=0.2,
top_p=0.9,
max_tokens=2048,
seed=42,
)
end_inference = datetime.now()
self.display_profiling_info(
"inference time",
f"inference start:{start_inference} end:{end_inference} during:{end_inference-start_inference}",
)
self.display_profiling_info("response", response)
self.display_profiling_info("response.usage", response.usage)
return response.choices[0].message.content
Bài viết liên quan
Humanoid
python
Cập nhật 2026-03-12
run.py
run.py — python source code from the Humanoid learning materials (Humanoid/May_12/RoboOS/deploy/run.py).
Đọc bài viết →
Humanoid
python
Cập nhật 2026-03-12
utils.py
utils.py — python source code from the Humanoid learning materials (Humanoid/May_12/RoboOS/deploy/utils.py).
Đọc bài viết →
Humanoid
python
Cập nhật 2026-03-12
agent.py
agent.py — python source code from the Humanoid learning materials (Humanoid/May_12/RoboOS/master/agents/agent.py).
Đọc bài viết →
Humanoid
python
Cập nhật 2026-05-12
prompts.py
prompts.py — python source code from the Humanoid learning materials (Humanoid/May_12/RoboOS/master/agents/prompts.py).
Đọc bài viết →
Humanoid
python
Cập nhật 2026-05-12
run.py
run.py — python source code from the Humanoid learning materials (Humanoid/May_12/RoboOS/master/run.py).
Đọc bài viết →
Humanoid
python
Cập nhật 2026-05-12
__init__.py
__init__.py — python source code from the Humanoid learning materials (Humanoid/May_12/RoboOS/master/task_manager/__init__.py).
Đọc bài viết →