Kaveen Kumarasinghe 2 years ago
commit 91a5a98370

@ -810,7 +810,14 @@ class Commands(discord.Cog, name="Commands"):
guild_ids=ALLOWED_GUILDS, guild_ids=ALLOWED_GUILDS,
) )
@discord.option(name="query", description="The query to search", required=True) @discord.option(name="query", description="The query to search", required=True)
@discord.option(name="scope", description="How many top links to use for context", required=False, input_type=discord.SlashCommandOptionType.integer, max_value=8, min_value=1) @discord.option(
name="scope",
description="How many top links to use for context",
required=False,
input_type=discord.SlashCommandOptionType.integer,
max_value=8,
min_value=1,
)
@discord.guild_only() @discord.guild_only()
async def search(self, ctx: discord.ApplicationContext, query: str, scope: int): async def search(self, ctx: discord.ApplicationContext, query: str, scope: int):
await self.search_cog.search_command(ctx, query, scope) await self.search_cog.search_command(ctx, query, scope)

@ -12,6 +12,7 @@ ALLOWED_GUILDS = EnvService.get_allowed_guilds()
USER_INPUT_API_KEYS = EnvService.get_user_input_api_keys() USER_INPUT_API_KEYS = EnvService.get_user_input_api_keys()
USER_KEY_DB = EnvService.get_api_db() USER_KEY_DB = EnvService.get_api_db()
class SearchService(discord.Cog, name="SearchService"): class SearchService(discord.Cog, name="SearchService"):
"""Cog containing translation commands and retrieval of translation services""" """Cog containing translation commands and retrieval of translation services"""
@ -35,7 +36,10 @@ class SearchService(discord.Cog, name="SearchService"):
if not user_api_key: if not user_api_key:
return return
if not EnvService.get_google_search_api_key() or not EnvService.get_google_search_engine_id(): if (
not EnvService.get_google_search_api_key()
or not EnvService.get_google_search_engine_id()
):
await ctx.send("The search service is not enabled.") await ctx.send("The search service is not enabled.")
return return

@ -6,7 +6,12 @@ from functools import partial
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import aiohttp import aiohttp
from gpt_index import QuestionAnswerPrompt, GPTSimpleVectorIndex, BeautifulSoupWebReader, Document from gpt_index import (
QuestionAnswerPrompt,
GPTSimpleVectorIndex,
BeautifulSoupWebReader,
Document,
)
from gpt_index.readers.web import DEFAULT_WEBSITE_EXTRACTOR from gpt_index.readers.web import DEFAULT_WEBSITE_EXTRACTOR
from services.environment_service import EnvService from services.environment_service import EnvService
@ -29,6 +34,7 @@ class Search:
"answer the question, say that you were unable to answer the question if there is not sufficient context to formulate a decisive answer. The search query was: {query_str}\n" "answer the question, say that you were unable to answer the question if there is not sufficient context to formulate a decisive answer. The search query was: {query_str}\n"
) )
self.openai_key = os.getenv("OPENAI_TOKEN") self.openai_key = os.getenv("OPENAI_TOKEN")
def index_webpage(self, url) -> list[Document]: def index_webpage(self, url) -> list[Document]:
documents = BeautifulSoupWebReader( documents = BeautifulSoupWebReader(
website_extractor=DEFAULT_WEBSITE_EXTRACTOR website_extractor=DEFAULT_WEBSITE_EXTRACTOR
@ -61,7 +67,9 @@ class Search:
# Concatenate all the text for a given website into one string and save it into an array: # Concatenate all the text for a given website into one string and save it into an array:
documents = [] documents = []
for link in links: for link in links:
document = await self.loop.run_in_executor(None, partial(self.index_webpage, link)) document = await self.loop.run_in_executor(
None, partial(self.index_webpage, link)
)
[documents.append(doc) for doc in document] [documents.append(doc) for doc in document]
index = GPTSimpleVectorIndex(documents) index = GPTSimpleVectorIndex(documents)
@ -70,4 +78,3 @@ class Search:
response = index.query(query, text_qa_template=self.qaprompt) response = index.query(query, text_qa_template=self.qaprompt)
return response return response

Loading…
Cancel
Save