Unverified Commit 1116590b authored by Chen Zhang's avatar Chen Zhang Committed by GitHub
Browse files

[gpt-oss] Validate gpt-oss python tool during initialization (#23856)


Signed-off-by: default avatarChen Zhang <zhangch99@outlook.com>
parent ccb97338
...@@ -1717,6 +1717,8 @@ async def init_app_state( ...@@ -1717,6 +1717,8 @@ async def init_app_state(
if args.tool_server == "demo": if args.tool_server == "demo":
tool_server: Optional[ToolServer] = DemoToolServer() tool_server: Optional[ToolServer] = DemoToolServer()
assert isinstance(tool_server, DemoToolServer)
await tool_server.init_and_validate()
elif args.tool_server: elif args.tool_server:
tool_server = MCPToolServer() tool_server = MCPToolServer()
await tool_server.add_tool_server(args.tool_server) await tool_server.add_tool_server(args.tool_server)
......
...@@ -4,6 +4,8 @@ import os ...@@ -4,6 +4,8 @@ import os
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Any from typing import TYPE_CHECKING, Any
from openai_harmony import Author, Message, Role, TextContent
from vllm.logger import init_logger from vllm.logger import init_logger
if TYPE_CHECKING: if TYPE_CHECKING:
...@@ -99,6 +101,28 @@ class HarmonyPythonTool(Tool): ...@@ -99,6 +101,28 @@ class HarmonyPythonTool(Tool):
return return
self.python_tool = PythonTool() self.python_tool = PythonTool()
async def validate(self):
if not self.enabled:
return
try:
message = Message(
author=Author(role=Role.ASSISTANT),
content=[TextContent(text="print('Hello, world!')")],
channel="analysis",
recipient="python",
content_type="code",
)
msgs = []
async for msg in self.python_tool.process(message):
msgs.append(msg)
assert msgs[0].content[0].text == "Hello, world!\n"
except Exception as e:
self.enabled = False
logger.warning_once(
"Code interpreter tool failed to initialize (%s), code "
"interpreter is disabled", e)
return
logger.info_once("Code interpreter tool initialized") logger.info_once("Code interpreter tool initialized")
async def get_result(self, context: "ConversationContext") -> Any: async def get_result(self, context: "ConversationContext") -> Any:
......
...@@ -162,10 +162,13 @@ class DemoToolServer(ToolServer): ...@@ -162,10 +162,13 @@ class DemoToolServer(ToolServer):
def __init__(self): def __init__(self):
self.tools: dict[str, Tool] = {} self.tools: dict[str, Tool] = {}
async def init_and_validate(self):
browser_tool = HarmonyBrowserTool() browser_tool = HarmonyBrowserTool()
python_tool = HarmonyPythonTool()
await python_tool.validate()
if browser_tool.enabled: if browser_tool.enabled:
self.tools["browser"] = browser_tool self.tools["browser"] = browser_tool
python_tool = HarmonyPythonTool()
if python_tool.enabled: if python_tool.enabled:
self.tools["python"] = python_tool self.tools["python"] = python_tool
logger.info("DemoToolServer initialized with tools: %s", logger.info("DemoToolServer initialized with tools: %s",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment