mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-12 16:49:00 +08:00
Reduce tool redundancy for [Judge0 CE] (#3837)
Co-authored-by: crazywoola <427733928@qq.com>
This commit is contained in:
parent
86e7330fa2
commit
bf9fc8fef4
@ -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}")
|
@ -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…
x
Reference in New Issue
Block a user