diff --git a/plugins/botchat/plugin.py b/plugins/botchat/plugin.py index 4705bc5..0bc8509 100644 --- a/plugins/botchat/plugin.py +++ b/plugins/botchat/plugin.py @@ -115,6 +115,22 @@ async def search_searx(query): logger.info(f"Search summary {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') 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: prompt = prompt_file.read() 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) full_prompt = prompt.replace("", history_str) full_prompt = full_prompt.replace("", bot_name) @@ -145,9 +163,15 @@ async def process_search(ctx, query_str): :param ctx: Chat context object :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=query_str_trimmed.removeprefix("/search") 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) @@ -179,7 +203,6 @@ async def send_chat_responses(ctx, response_text): final_output_str = final_output_str.strip() if final_output_str.startswith("/search"): await ctx.channel.send(final_output_str) - await ctx.channel.send("*Search processing...*") await process_search(ctx, final_output_str) break if (final_output_str != ""): diff --git a/plugins/botchat/prompts/default.txt b/plugins/botchat/prompts/default.txt index d49c732..024f8bd 100644 --- a/plugins/botchat/prompts/default.txt +++ b/plugins/botchat/prompts/default.txt @@ -35,6 +35,8 @@ user42131: , who is running for president in 2024? Additional Context: Today's date is and the current time is