Updated search functionality

This commit is contained in:
cameron 2024-05-10 20:34:55 -04:00
parent 8a1316aeef
commit 73a37f79c0
2 changed files with 26 additions and 1 deletions

View File

@ -115,6 +115,22 @@ async def search_searx(query):
logger.info(f"Search summary {summary}") logger.info(f"Search summary {summary}")
return summary return summary
async def check_for_additional_context(chat_entries):
#Check chat_entries
chat_entries_rev = chat_entries.copy()
chat_entries_rev.reverse()
for entry in chat_entries_rev:
found = re.search(r"Search \[\[#(\d.+)\]\]",entry)
if found:
cache_id = found.group(1)
search_filename = os.path.join(plugin_folder, 'search_cache', f"{cache_id}.txt")
if (os.path.exists(search_filename)):
logger.info(f"Retrieving cached additional context id #{cache_id}...")
with open(search_filename, 'r') as search_file:
return str(search_file.read())
break
return ""
@commands.command(name='llm') @commands.command(name='llm')
async def llm_response(ctx, additional_context=""): async def llm_response(ctx, additional_context=""):
""" """
@ -127,6 +143,8 @@ async def llm_response(ctx, additional_context=""):
with open(prompt_file, 'r') as prompt_file: with open(prompt_file, 'r') as prompt_file:
prompt = prompt_file.read() prompt = prompt_file.read()
history_arr = await get_chat_history(ctx) history_arr = await get_chat_history(ctx)
if additional_context == "":
additional_context = await check_for_additional_context(history_arr) #Check for recent searches
history_str = '\n'.join(history_arr) history_str = '\n'.join(history_arr)
full_prompt = prompt.replace("<CONVHISTORY>", history_str) full_prompt = prompt.replace("<CONVHISTORY>", history_str)
full_prompt = full_prompt.replace("<BOTNAME>", bot_name) full_prompt = full_prompt.replace("<BOTNAME>", bot_name)
@ -145,9 +163,15 @@ async def process_search(ctx, query_str):
:param ctx: Chat context object :param ctx: Chat context object
:param query: Query string (beginning with /search) :param query: Query string (beginning with /search)
""" """
search_id = random.randint(1000,99999)
await ctx.channel.send(f"*Search [[#{search_id}]] processing...*")
query_str_trimmed=query_str.strip() query_str_trimmed=query_str.strip()
query=query_str_trimmed.removeprefix("/search") query=query_str_trimmed.removeprefix("/search")
search_results = await search_searx(query) search_results = await search_searx(query)
os.makedirs(os.path.join(plugin_folder, 'search_cache'))
search_filename = os.path.join(plugin_folder, 'search_cache', f"{search_id}.txt")
with open(search_filename, 'w') as search_file:
search_file.write(search_results)
await llm_response(ctx, search_results) await llm_response(ctx, search_results)
@ -179,7 +203,6 @@ async def send_chat_responses(ctx, response_text):
final_output_str = final_output_str.strip() final_output_str = final_output_str.strip()
if final_output_str.startswith("/search"): if final_output_str.startswith("/search"):
await ctx.channel.send(final_output_str) await ctx.channel.send(final_output_str)
await ctx.channel.send("*Search processing...*")
await process_search(ctx, final_output_str) await process_search(ctx, final_output_str)
break break
if (final_output_str != ""): if (final_output_str != ""):

View File

@ -35,6 +35,8 @@ user42131: <BOTNAME>, who is running for president in 2024?
Additional Context: Additional Context:
Today's date is <DATE> and the current time is <TIME>. Today's date is <DATE> and the current time is <TIME>.
<ADD_CONTEXT> <ADD_CONTEXT>
### Response: ### Response:
<CONVHISTORY> <CONVHISTORY>
<BOTNAME>: <BOTNAME>: