From a6395aeb19969ba5763c147ec664ca897065cd0b Mon Sep 17 00:00:00 2001 From: Rene Teigen Date: Mon, 6 Feb 2023 01:10:18 +0000 Subject: [PATCH] Add sources to a search based on which nodes it has fetched --- cogs/search_service_cog.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cogs/search_service_cog.py b/cogs/search_service_cog.py index 7237161..64ecfc2 100644 --- a/cogs/search_service_cog.py +++ b/cogs/search_service_cog.py @@ -1,6 +1,7 @@ import traceback import aiohttp +import re import discord from models.deepl_model import TranslationModel @@ -49,6 +50,10 @@ class SearchService(discord.Cog, name="SearchService"): response = await self.model.search(query, user_api_key, search_scope, nodes) + url_extract_pattern = "https?:\\/\\/(?:www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b(?:[-a-zA-Z0-9()@:%_\\+.~#?&\\/=]*)" + urls = re.findall(url_extract_pattern, str(response.get_formatted_sources(length=200)), flags=re.IGNORECASE) + urls = "\n".join(f"<{url}>" for url in urls) + await ctx.respond( - f"**Query:**\n\n{query.strip()}\n\n**Query response:**\n\n{response.response.strip()}" + f"**Query:**\n\n{query.strip()}\n\n**Query response:**\n\n{response.response.strip()}\n\n**Sources:**\n{urls}" )