Reduce tool redundancy for [Judge0 CE] (#3837)
Co-authored-by: crazywoola <427733928@qq.com>
This commit is contained in:
parent
86e7330fa2
commit
bf9fc8fef4
6 changed files with 61 additions and 111 deletions
|
|
@ -1,14 +1,14 @@
|
|||
from typing import Any
|
||||
|
||||
from core.tools.errors import ToolProviderCredentialValidationError
|
||||
from core.tools.provider.builtin.judge0ce.tools.submitCodeExecutionTask import SubmitCodeExecutionTaskTool
|
||||
from core.tools.provider.builtin.judge0ce.tools.executeCode import ExecuteCodeTool
|
||||
from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController
|
||||
|
||||
|
||||
class Judge0CEProvider(BuiltinToolProviderController):
|
||||
def _validate_credentials(self, credentials: dict[str, Any]) -> None:
|
||||
try:
|
||||
SubmitCodeExecutionTaskTool().fork_tool_runtime(
|
||||
ExecuteCodeTool().fork_tool_runtime(
|
||||
meta={
|
||||
"credentials": credentials,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,59 @@
|
|||
import json
|
||||
from typing import Any, Union
|
||||
|
||||
import requests
|
||||
from httpx import post
|
||||
|
||||
from core.tools.entities.tool_entities import ToolInvokeMessage
|
||||
from core.tools.tool.builtin_tool import BuiltinTool
|
||||
|
||||
|
||||
class ExecuteCodeTool(BuiltinTool):
|
||||
def _invoke(self, user_id: str, tool_parameters: dict[str, Any]) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
||||
"""
|
||||
invoke tools
|
||||
"""
|
||||
api_key = self.runtime.credentials['X-RapidAPI-Key']
|
||||
|
||||
url = "https://judge0-ce.p.rapidapi.com/submissions"
|
||||
|
||||
querystring = {"base64_encoded": "false", "fields": "*"}
|
||||
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"X-RapidAPI-Key": api_key,
|
||||
"X-RapidAPI-Host": "judge0-ce.p.rapidapi.com"
|
||||
}
|
||||
|
||||
payload = {
|
||||
"language_id": tool_parameters['language_id'],
|
||||
"source_code": tool_parameters['source_code'],
|
||||
"stdin": tool_parameters.get('stdin', ''),
|
||||
"expected_output": tool_parameters.get('expected_output', ''),
|
||||
"additional_files": tool_parameters.get('additional_files', ''),
|
||||
}
|
||||
|
||||
response = post(url, data=json.dumps(payload), headers=headers, params=querystring)
|
||||
|
||||
if response.status_code != 201:
|
||||
raise Exception(response.text)
|
||||
|
||||
token = response.json()['token']
|
||||
|
||||
url = f"https://judge0-ce.p.rapidapi.com/submissions/{token}"
|
||||
headers = {
|
||||
"X-RapidAPI-Key": api_key
|
||||
}
|
||||
|
||||
response = requests.get(url, headers=headers)
|
||||
if response.status_code == 200:
|
||||
result = response.json()
|
||||
return self.create_text_message(text=f"stdout: {result.get('stdout', '')}\n"
|
||||
f"stderr: {result.get('stderr', '')}\n"
|
||||
f"compile_output: {result.get('compile_output', '')}\n"
|
||||
f"message: {result.get('message', '')}\n"
|
||||
f"status: {result['status']['description']}\n"
|
||||
f"time: {result.get('time', '')} seconds\n"
|
||||
f"memory: {result.get('memory', '')} bytes")
|
||||
else:
|
||||
return self.create_text_message(text=f"Error retrieving submission details: {response.text}")
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
identity:
|
||||
name: submitCodeExecutionTask
|
||||
author: Richards Tu
|
||||
label:
|
||||
en_US: Submit Code Execution Task
|
||||
zh_Hans: 提交代码执行任务
|
||||
description:
|
||||
human:
|
||||
en_US: A tool for submitting code execution task to Judge0 CE.
|
||||
zh_Hans: 一个用于向 Judge0 CE 提交代码执行任务的工具。
|
||||
llm: A tool for submitting a new code execution task to Judge0 CE. It takes in the source code, language ID, standard input (optional), expected output (optional), and additional files (optional) as parameters; and returns a unique token representing the submission.
|
||||
parameters:
|
||||
- name: source_code
|
||||
type: string
|
||||
required: true
|
||||
label:
|
||||
en_US: Source Code
|
||||
zh_Hans: 源代码
|
||||
human_description:
|
||||
en_US: The source code to be executed.
|
||||
zh_Hans: 要执行的源代码。
|
||||
llm_description: The source code to be executed.
|
||||
form: llm
|
||||
- name: language_id
|
||||
type: number
|
||||
required: true
|
||||
label:
|
||||
en_US: Language ID
|
||||
zh_Hans: 语言 ID
|
||||
human_description:
|
||||
en_US: The ID of the language in which the source code is written.
|
||||
zh_Hans: 源代码所使用的语言的 ID。
|
||||
llm_description: The ID of the language in which the source code is written. For example, 50 for C++, 71 for Python, etc.
|
||||
form: llm
|
||||
- name: stdin
|
||||
type: string
|
||||
required: false
|
||||
label:
|
||||
en_US: Standard Input
|
||||
zh_Hans: 标准输入
|
||||
human_description:
|
||||
en_US: The standard input to be provided to the program.
|
||||
zh_Hans: 提供给程序的标准输入。
|
||||
llm_description: The standard input to be provided to the program. Optional.
|
||||
form: llm
|
||||
- name: expected_output
|
||||
type: string
|
||||
required: false
|
||||
label:
|
||||
en_US: Expected Output
|
||||
zh_Hans: 期望输出
|
||||
human_description:
|
||||
en_US: The expected output of the program. Used for comparison in some scenarios.
|
||||
zh_Hans: 程序的期望输出。在某些场景下用于比较。
|
||||
llm_description: The expected output of the program. Used for comparison in some scenarios. Optional.
|
||||
form: llm
|
||||
- name: additional_files
|
||||
type: string
|
||||
required: false
|
||||
label:
|
||||
en_US: Additional Files
|
||||
zh_Hans: 附加文件
|
||||
human_description:
|
||||
en_US: Base64 encoded additional files for the submission.
|
||||
zh_Hans: 提交的 Base64 编码的附加文件。
|
||||
llm_description: Base64 encoded additional files for the submission. Optional.
|
||||
form: llm
|
||||
|
|
@ -1,37 +0,0 @@
|
|||
from typing import Any, Union
|
||||
|
||||
import requests
|
||||
|
||||
from core.tools.entities.tool_entities import ToolInvokeMessage
|
||||
from core.tools.tool.builtin_tool import BuiltinTool
|
||||
|
||||
|
||||
class GetExecutionResultTool(BuiltinTool):
|
||||
def _invoke(self,
|
||||
user_id: str,
|
||||
tool_parameters: dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
||||
"""
|
||||
invoke tools
|
||||
"""
|
||||
api_key = self.runtime.credentials['X-RapidAPI-Key']
|
||||
|
||||
url = f"https://judge0-ce.p.rapidapi.com/submissions/{tool_parameters['token']}"
|
||||
headers = {
|
||||
"X-RapidAPI-Key": api_key
|
||||
}
|
||||
|
||||
response = requests.get(url, headers=headers)
|
||||
|
||||
if response.status_code == 200:
|
||||
result = response.json()
|
||||
return self.create_text_message(text=f"Submission details:\n"
|
||||
f"stdout: {result.get('stdout', '')}\n"
|
||||
f"stderr: {result.get('stderr', '')}\n"
|
||||
f"compile_output: {result.get('compile_output', '')}\n"
|
||||
f"message: {result.get('message', '')}\n"
|
||||
f"status: {result['status']['description']}\n"
|
||||
f"time: {result.get('time', '')} seconds\n"
|
||||
f"memory: {result.get('memory', '')} bytes")
|
||||
else:
|
||||
return self.create_text_message(text=f"Error retrieving submission details: {response.text}")
|
||||
|
|
@ -1,23 +0,0 @@
|
|||
identity:
|
||||
name: getExecutionResult
|
||||
author: Richards Tu
|
||||
label:
|
||||
en_US: Get Execution Result
|
||||
zh_Hans: 获取执行结果
|
||||
description:
|
||||
human:
|
||||
en_US: A tool for retrieving the details of a code submission by a specific token from submitCodeExecutionTask.
|
||||
zh_Hans: 一个用于通过 submitCodeExecutionTask 工具提供的特定令牌来检索代码提交详细信息的工具。
|
||||
llm: A tool for retrieving the details of a code submission by a specific token from submitCodeExecutionTask.
|
||||
parameters:
|
||||
- name: token
|
||||
type: string
|
||||
required: true
|
||||
label:
|
||||
en_US: Token
|
||||
zh_Hans: 令牌
|
||||
human_description:
|
||||
en_US: The submission's unique token.
|
||||
zh_Hans: 提交的唯一令牌。
|
||||
llm_description: The submission's unique token. MUST get from submitCodeExecution.
|
||||
form: llm
|
||||
|
|
@ -1,49 +0,0 @@
|
|||
import json
|
||||
from typing import Any, Union
|
||||
|
||||
from httpx import post
|
||||
|
||||
from core.tools.entities.tool_entities import ToolInvokeMessage
|
||||
from core.tools.tool.builtin_tool import BuiltinTool
|
||||
|
||||
|
||||
class SubmitCodeExecutionTaskTool(BuiltinTool):
|
||||
def _invoke(self, user_id: str, tool_parameters: dict[str, Any]) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
||||
"""
|
||||
invoke tools
|
||||
"""
|
||||
api_key = self.runtime.credentials['X-RapidAPI-Key']
|
||||
|
||||
source_code = tool_parameters['source_code']
|
||||
language_id = tool_parameters['language_id']
|
||||
stdin = tool_parameters.get('stdin', '')
|
||||
expected_output = tool_parameters.get('expected_output', '')
|
||||
additional_files = tool_parameters.get('additional_files', '')
|
||||
|
||||
url = "https://judge0-ce.p.rapidapi.com/submissions"
|
||||
|
||||
querystring = {"base64_encoded": "false", "fields": "*"}
|
||||
|
||||
payload = {
|
||||
"language_id": language_id,
|
||||
"source_code": source_code,
|
||||
"stdin": stdin,
|
||||
"expected_output": expected_output,
|
||||
"additional_files": additional_files,
|
||||
}
|
||||
|
||||
headers = {
|
||||
"content-type": "application/json",
|
||||
"Content-Type": "application/json",
|
||||
"X-RapidAPI-Key": api_key,
|
||||
"X-RapidAPI-Host": "judge0-ce.p.rapidapi.com"
|
||||
}
|
||||
|
||||
response = post(url, data=json.dumps(payload), headers=headers, params=querystring)
|
||||
|
||||
if response.status_code != 201:
|
||||
raise Exception(response.text)
|
||||
|
||||
token = response.json()['token']
|
||||
|
||||
return self.create_text_message(text=token)
|
||||
Loading…
Reference in a new issue