Spaces:
Sleeping
Sleeping
Commit ·
7b2be87
1
Parent(s): a6128af
final
Browse files- inference.py +32 -4
inference.py
CHANGED
|
@@ -22,6 +22,7 @@ import textwrap
|
|
| 22 |
from typing import Any, Dict, List, Optional
|
| 23 |
|
| 24 |
from openai import OpenAI
|
|
|
|
| 25 |
|
| 26 |
from csv_cleaner_env import CsvCleanerEnv
|
| 27 |
|
|
@@ -108,6 +109,22 @@ def parse_tool_call(text: str) -> Optional[Dict[str, Any]]:
|
|
| 108 |
return None
|
| 109 |
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
def get_model_response(
|
| 112 |
client: OpenAI,
|
| 113 |
task_desc: str,
|
|
@@ -186,12 +203,23 @@ async def run_task(client: OpenAI, env: CsvCleanerEnv, task_config: Dict) -> Non
|
|
| 186 |
if tool_call is None:
|
| 187 |
tool_call = {"tool": "get_dataset_info", "args": {}}
|
| 188 |
|
| 189 |
-
tool_name =
|
| 190 |
-
tool_args = tool_call.get("args", {})
|
| 191 |
|
| 192 |
try:
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
except Exception as e:
|
| 196 |
result_str = f"Error: {e}"
|
| 197 |
|
|
|
|
| 22 |
from typing import Any, Dict, List, Optional
|
| 23 |
|
| 24 |
from openai import OpenAI
|
| 25 |
+
from openenv.core.env_server.mcp_types import CallToolAction
|
| 26 |
|
| 27 |
from csv_cleaner_env import CsvCleanerEnv
|
| 28 |
|
|
|
|
| 109 |
return None
|
| 110 |
|
| 111 |
|
| 112 |
+
def normalize_tool_call(tool_call: Dict[str, Any]) -> tuple[str, Dict[str, Any]]:
|
| 113 |
+
"""Normalize model output into a safe tool name + args payload."""
|
| 114 |
+
tool_name = tool_call.get("tool", "get_dataset_info")
|
| 115 |
+
tool_args = tool_call.get("args", {})
|
| 116 |
+
|
| 117 |
+
if not isinstance(tool_args, dict):
|
| 118 |
+
tool_args = {}
|
| 119 |
+
|
| 120 |
+
# Model outputs sometimes include nulls for string fields; FastMCP rejects None for str args.
|
| 121 |
+
normalized_args: Dict[str, Any] = {}
|
| 122 |
+
for key, value in tool_args.items():
|
| 123 |
+
normalized_args[key] = "" if value is None else value
|
| 124 |
+
|
| 125 |
+
return tool_name, normalized_args
|
| 126 |
+
|
| 127 |
+
|
| 128 |
def get_model_response(
|
| 129 |
client: OpenAI,
|
| 130 |
task_desc: str,
|
|
|
|
| 203 |
if tool_call is None:
|
| 204 |
tool_call = {"tool": "get_dataset_info", "args": {}}
|
| 205 |
|
| 206 |
+
tool_name, tool_args = normalize_tool_call(tool_call)
|
|
|
|
| 207 |
|
| 208 |
try:
|
| 209 |
+
action = CallToolAction(tool_name=tool_name, arguments=tool_args)
|
| 210 |
+
result = await env.step(action)
|
| 211 |
+
|
| 212 |
+
obs = result.observation
|
| 213 |
+
obs_error = getattr(obs, "error", None)
|
| 214 |
+
if obs_error is not None:
|
| 215 |
+
result_str = f"Error: {getattr(obs_error, 'message', str(obs_error))}"
|
| 216 |
+
else:
|
| 217 |
+
obs_result = getattr(obs, "result", None)
|
| 218 |
+
if hasattr(obs_result, "data"):
|
| 219 |
+
obs_result = obs_result.data
|
| 220 |
+
elif isinstance(obs_result, dict) and "data" in obs_result:
|
| 221 |
+
obs_result = obs_result["data"]
|
| 222 |
+
result_str = str(obs_result) if obs_result is not None else ""
|
| 223 |
except Exception as e:
|
| 224 |
result_str = f"Error: {e}"
|
| 225 |
|