From 2e426e3edb1dde8e7fa31bee163f25fd545d9200 Mon Sep 17 00:00:00 2001 From: cameron Date: Sat, 11 May 2024 02:35:27 -0400 Subject: [PATCH] Updated search and chat logic --- plugins/botchat/plugin.py | 46 +++++++++++++++----- plugins/botchat/prompts/default.old.txt | 42 ++++++++++++++++++ plugins/botchat/prompts/default.txt | 13 +++++- plugins/botchat/prompts/summarize-search.txt | 11 +++++ 4 files changed, 100 insertions(+), 12 deletions(-) create mode 100644 plugins/botchat/prompts/default.old.txt create mode 100644 plugins/botchat/prompts/summarize-search.txt diff --git a/plugins/botchat/plugin.py b/plugins/botchat/plugin.py index 9254d8a..d2bcec4 100644 --- a/plugins/botchat/plugin.py +++ b/plugins/botchat/plugin.py @@ -26,6 +26,22 @@ def ci_replace(text, replace_str, new_str): result = compiled.sub(new_str, text) return result +async def summarize_search(results, query): + """ + Uses the LLM to summarize the given search results + + :param results: results to summarize + :param query: query to keep in context + :return: returns the summarized text + """ + logger.info("Prompting LLM for text summary") + summary_file = os.path.join(prompts_folder, "summarize-search.txt") + with open(summary_file, 'r') as summary_file: + summary_prompt = summary_file.read() + summary_prompt = summary_prompt.replace("", query) + summary_prompt = summary_prompt.replace("", results) + return await prompt_llm(summary_prompt, { "n_predict": 600 } ) + async def summarize(text): """ Uses the LLM to summarize the given text @@ -40,7 +56,7 @@ async def summarize(text): summary_prompt = summary_prompt.replace("", text) return await prompt_llm(summary_prompt) -async def prompt_llm(prompt): +async def prompt_llm(prompt, override_params={}): """ Prompts the upstream LLM for a completion of the given prompt @@ -48,10 +64,11 @@ async def prompt_llm(prompt): :return: returns a string consisting of completion text """ logger.info("Prompting LLM") - logger.info(f"PROMPT DATA\n{prompt}") + logger.debug(f"PROMPT DATA\n{prompt}") async with aiohttp.ClientSession(llm_config["api_base"]) as session: llm_params = { "prompt": prompt } llm_params.update(llm_config["llm_params"]) + llm_params.update(override_params) async with session.post("/completion", json=llm_params) as resp: logger.info(f"LLM response status {resp.status}") response_json=await resp.json() @@ -148,7 +165,7 @@ async def search_searx(query): if resp.status == 200: response=await resp.text() summary=await summarize(html2text.html2text(response)) - logger.info(f"Search summary {summary}") + logger.debug(f"Search summary {summary}") return summary else: logger.info(f"Search failed... Retrying") @@ -169,8 +186,8 @@ async def search_you(query): logger.info(f"Search response status {resp.status}") if resp.status == 200: response=await resp.text() - summary=await summarize(response) - logger.info(f"Search summary {summary}") + summary=await summarize_search(response, query) + logger.debug(f"Search summary {summary}") return summary else: logger.info(f"Search failed...") @@ -193,7 +210,7 @@ async def check_for_additional_context(chat_entries): return "" @commands.command(name='llm') -async def llm_response(ctx, additional_context=""): +async def llm_response(ctx, additional_context="", extra_prefix=""): """ Sends a response from the bot to the chat context in {ctx} @@ -212,7 +229,8 @@ async def llm_response(ctx, additional_context=""): full_prompt = full_prompt.replace("", str(datetime.date.today())) full_prompt = full_prompt.replace("