mirror of
https://github.com/langgenius/dify.git
synced 2024-11-16 11:42:29 +08:00
Update json_in_md_parser.py (#8983)
Some checks are pending
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/amd64, build-api-amd64) (push) Waiting to run
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/arm64, build-api-arm64) (push) Waiting to run
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/amd64, build-web-amd64) (push) Waiting to run
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/arm64, build-web-arm64) (push) Waiting to run
Build and Push API & Web / create-manifest (api, DIFY_API_IMAGE_NAME, merge-api-images) (push) Blocked by required conditions
Build and Push API & Web / create-manifest (web, DIFY_WEB_IMAGE_NAME, merge-web-images) (push) Blocked by required conditions
Some checks are pending
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/amd64, build-api-amd64) (push) Waiting to run
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/arm64, build-api-arm64) (push) Waiting to run
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/amd64, build-web-amd64) (push) Waiting to run
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/arm64, build-web-arm64) (push) Waiting to run
Build and Push API & Web / create-manifest (api, DIFY_API_IMAGE_NAME, merge-api-images) (push) Blocked by required conditions
Build and Push API & Web / create-manifest (web, DIFY_WEB_IMAGE_NAME, merge-web-images) (push) Blocked by required conditions
Co-authored-by: crazywoola <427733928@qq.com>
This commit is contained in:
parent
415d27c8bf
commit
4373777871
|
@ -7,7 +7,8 @@ from core.tools.tool.builtin_tool import BuiltinTool
|
||||||
|
|
||||||
|
|
||||||
class DiscordWebhookTool(BuiltinTool):
|
class DiscordWebhookTool(BuiltinTool):
|
||||||
def _invoke(self, user_id: str, tool_parameters: dict[str, Any]
|
def _invoke(
|
||||||
|
self, user_id: str, tool_parameters: dict[str, Any]
|
||||||
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
||||||
"""
|
"""
|
||||||
Incoming Webhooks
|
Incoming Webhooks
|
||||||
|
@ -15,19 +16,20 @@ class DiscordWebhookTool(BuiltinTool):
|
||||||
https://discord.com/developers/docs/resources/webhook#execute-webhook
|
https://discord.com/developers/docs/resources/webhook#execute-webhook
|
||||||
"""
|
"""
|
||||||
|
|
||||||
content = tool_parameters.get('content', '')
|
content = tool_parameters.get("content", "")
|
||||||
if not content:
|
if not content:
|
||||||
return self.create_text_message('Invalid parameter content')
|
return self.create_text_message("Invalid parameter content")
|
||||||
|
|
||||||
webhook_url = tool_parameters.get('webhook_url', '')
|
webhook_url = tool_parameters.get("webhook_url", "")
|
||||||
|
|
||||||
if not webhook_url.startswith('https://discord.com/api/webhooks/'):
|
if not webhook_url.startswith("https://discord.com/api/webhooks/"):
|
||||||
return self.create_text_message(
|
return self.create_text_message(
|
||||||
f'Invalid parameter webhook_url ${webhook_url}, \
|
f"Invalid parameter webhook_url ${webhook_url}, \
|
||||||
not a valid Discord webhook URL')
|
not a valid Discord webhook URL"
|
||||||
|
)
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
'Content-Type': 'application/json',
|
"Content-Type": "application/json",
|
||||||
}
|
}
|
||||||
params = {}
|
params = {}
|
||||||
payload = {
|
payload = {
|
||||||
|
@ -35,15 +37,13 @@ class DiscordWebhookTool(BuiltinTool):
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
res = httpx.post(webhook_url, headers=headers,
|
res = httpx.post(webhook_url, headers=headers, params=params, json=payload)
|
||||||
params=params, json=payload)
|
|
||||||
if res.is_success:
|
if res.is_success:
|
||||||
return self.create_text_message(
|
return self.create_text_message("Text message was sent successfully")
|
||||||
"Text message was sent successfully")
|
|
||||||
else:
|
else:
|
||||||
return self.create_text_message(
|
return self.create_text_message(
|
||||||
f"Failed to send the text message, \
|
f"Failed to send the text message, \
|
||||||
status code: {res.status_code}, response: {res.text}")
|
status code: {res.status_code}, response: {res.text}"
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return self.create_text_message(
|
return self.create_text_message("Failed to send message through webhook. {}".format(e))
|
||||||
"Failed to send message through webhook. {}".format(e))
|
|
||||||
|
|
|
@ -4,25 +4,28 @@ from core.llm_generator.output_parser.errors import OutputParserError
|
||||||
|
|
||||||
|
|
||||||
def parse_json_markdown(json_string: str) -> dict:
|
def parse_json_markdown(json_string: str) -> dict:
|
||||||
# Remove the triple backticks if present
|
# Get json from the backticks/braces
|
||||||
json_string = json_string.strip()
|
json_string = json_string.strip()
|
||||||
start_index = json_string.find("```json")
|
starts = ["```json", "```", "``", "`", "{"]
|
||||||
end_index = json_string.find("```", start_index + len("```json"))
|
ends = ["```", "``", "`", "}"]
|
||||||
|
end_index = -1
|
||||||
if start_index != -1 and end_index != -1:
|
for s in starts:
|
||||||
extracted_content = json_string[start_index + len("```json") : end_index].strip()
|
start_index = json_string.find(s)
|
||||||
|
if start_index != -1:
|
||||||
# Parse the JSON string into a Python dictionary
|
if json_string[start_index] != "{":
|
||||||
|
start_index += len(s)
|
||||||
|
break
|
||||||
|
if start_index != -1:
|
||||||
|
for e in ends:
|
||||||
|
end_index = json_string.rfind(e, start_index)
|
||||||
|
if end_index != -1:
|
||||||
|
if json_string[end_index] == "}":
|
||||||
|
end_index += 1
|
||||||
|
break
|
||||||
|
if start_index != -1 and end_index != -1 and start_index < end_index:
|
||||||
|
extracted_content = json_string[start_index:end_index].strip()
|
||||||
|
print("content:", extracted_content, start_index, end_index)
|
||||||
parsed = json.loads(extracted_content)
|
parsed = json.loads(extracted_content)
|
||||||
elif start_index != -1 and end_index == -1 and json_string.endswith("``"):
|
|
||||||
end_index = json_string.find("``", start_index + len("```json"))
|
|
||||||
extracted_content = json_string[start_index + len("```json") : end_index].strip()
|
|
||||||
|
|
||||||
# Parse the JSON string into a Python dictionary
|
|
||||||
parsed = json.loads(extracted_content)
|
|
||||||
elif json_string.startswith("{"):
|
|
||||||
# Parse the JSON string into a Python dictionary
|
|
||||||
parsed = json.loads(json_string)
|
|
||||||
else:
|
else:
|
||||||
raise Exception("Could not find JSON block in the output.")
|
raise Exception("Could not find JSON block in the output.")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user