Updated search and chat logic
This commit is contained in:
1 parent
08a9543e6b
commit
2e426e3edb
4 files changed
+100
-12
No files matched your search
+36
-10
@@ -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>", query)
|
||||
summary_prompt = summary_prompt.replace("<WEBTEXT>", 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("<WEBTEXT>", 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("<DATE>", str(datetime.date.today()))
|
||||
full_prompt = full_prompt.replace("<TIME>", str(datetime.datetime.now().strftime("%-I:%M:%S %p")))
|
||||
full_prompt = full_prompt.replace("<ADD_CONTEXT>", f"{additional_context}")
|
||||
response = await prompt_llm(full_prompt)
|
||||
full_prompt = full_prompt + extra_prefix
|
||||
response = extra_prefix + await prompt_llm(full_prompt)
|
||||
await send_chat_responses(ctx, response)
|
||||
await log_history(ctx, history_str)
|
||||
|
||||
@@ -256,17 +274,25 @@ async def send_chat_responses(ctx, response_text):
|
||||
if line.startswith(f"{bot_name}:"):
|
||||
truncStr = line.replace(f"{bot_name}:","")
|
||||
output_strs.append(truncStr)
|
||||
elif line.find(":") > 0 and line.find(":") < 20:
|
||||
break
|
||||
else:
|
||||
output_strs.append(line.strip())
|
||||
break
|
||||
for outs in output_strs:
|
||||
final_output_str = await fixup_mentions(ctx, outs)
|
||||
final_output_str = final_output_str.strip()
|
||||
final_output_str = final_output_str.replace('`', '') #revoked backtick permissions
|
||||
if final_output_str.startswith("/search"):
|
||||
await ctx.channel.send(final_output_str)
|
||||
await process_search(ctx, final_output_str)
|
||||
break
|
||||
if "/search" in final_output_str:
|
||||
# the bot is using /search wrong again
|
||||
logger.info("Bot using tools improperly. Regenerating response.")
|
||||
await llm_response(ctx)
|
||||
return
|
||||
if final_output_str.startswith("/"):
|
||||
logger.info("Bot using non-existent tools. Regenerating response.")
|
||||
await llm_response(ctx, extra_prefix=" /search ")
|
||||
return
|
||||
if (final_output_str != ""):
|
||||
await ctx.channel.send(final_output_str)
|
||||
|
||||
|
||||
Reference in new issue
Block a user