Spaces:
Sleeping
Sleeping
File size: 3,606 Bytes
90cf215 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | """File IPC model adapter for an external LLM controller, including ChatGPT Work Mode.
The controller reads requests and returns normal model messages. It does not
edit evaluation results. All tool execution remains inside the LangGraph agent.
No benchmark answers or account credentials are included in this adapter.
"""
import json
import time
import uuid
from pathlib import Path
class ControllerModel:
def __init__(self, directory="controller_mailbox", timeout=1800):
self.directory = Path(directory)
self.directory.mkdir(exist_ok=True)
self.timeout = timeout
def __call__(self, messages, tools=None):
request_id = str(uuid.uuid4())
request_path = self.directory / "request.json"
response_path = self.directory / "response.json"
payload = {"request_id": request_id, "messages": messages,
"kind": "model",
"tools": tools or [], "response_format": {
"request_id": request_id,
"message": {"role": "assistant", "content": "final answer",
"tool_calls": []}}}
request_path.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
print("CONTROLLER_REQUEST", request_id, flush=True)
deadline = time.monotonic() + self.timeout
while time.monotonic() < deadline:
if response_path.exists():
try:
response = json.loads(response_path.read_text(encoding="utf-8"))
except (OSError, ValueError):
time.sleep(0.25)
continue
if response.get("request_id") == request_id:
message = response["message"]
if message.get("role") != "assistant":
raise ValueError("Controller must return an assistant message")
return message
time.sleep(0.25)
raise TimeoutError("External model controller did not respond")
class ControllerWebTools:
"""Bridge public web tools when direct network access is restricted.
The external controller executes its actual search/read tool and returns
the resulting observation, which is recorded by the agent normally.
"""
def __init__(self, directory="controller_mailbox/web", timeout=1800):
self.directory = Path(directory)
self.directory.mkdir(parents=True, exist_ok=True)
self.timeout = timeout
def request(self, operation, arguments):
request_id = str(uuid.uuid4())
payload = {"request_id": request_id, "kind": "web_tool", "operation": operation,
"arguments": arguments}
(self.directory / "request.json").write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
print("WEB_TOOL_REQUEST", request_id, operation, flush=True)
deadline = time.monotonic() + self.timeout
while time.monotonic() < deadline:
try:
response = json.loads((self.directory / "response.json").read_text(encoding="utf-8"))
if response.get("request_id") == request_id:
return response["observation"]
except (OSError, ValueError):
pass
time.sleep(0.25)
raise TimeoutError("External web tool did not respond")
def search_web(self, query):
return self.request("search_web", {"query": query})
def read_page(self, url, find=""):
return self.request("read_page", {"url": url, "find": find})
|