Internet search and document summarization

This commit is contained in:
cameron committed 2024-05-10 20:03:39 -04:00
1 parent 3e4529dcc0
commit 8a1316aeef
3 files changed
+95 -3

No files matched your search

+61 -1
View File
@@ -7,7 +7,9 @@ import yaml
import random
import os
import logging
import html2text
import re
import datetime
logger=logging.getLogger("plugin.botchat")
plugin_folder=os.path.dirname(os.path.realpath(__file__))
@@ -23,6 +25,20 @@ def ci_replace(text, replace_str, new_str):
result = compiled.sub(new_str, text)
return result
async def summarize(text):
"""
Uses the LLM to summarize the given text
:param text: text to summarize
:return: returns the summarized text
"""
logger.info("Prompting LLM for text summary")
summary_file = os.path.join(prompts_folder, "summarize.txt")
with open(summary_file, 'r') as summary_file:
summary_prompt = summary_file.read()
summary_prompt = summary_prompt.replace("<WEBTEXT>", text)
return await prompt_llm(summary_prompt)
async def prompt_llm(prompt):
"""
Prompts the upstream LLM for a completion of the given prompt
@@ -83,8 +99,24 @@ async def log_history(ctx, history):
# history_file.write(history)
pass
async def search_searx(query):
"""
Searches the given query on SearX and returns an LLM summary
:param query: search query
"""
search_url="https://metasearx.com/"
async with aiohttp.ClientSession(search_url) as session:
search_params = { "q": query }
async with session.get("/", data=search_params) as resp:
logger.info(f"Search response status {resp.status}")
response=await resp.text()
summary=await summarize(html2text.html2text(response))
logger.info(f"Search summary {summary}")
return summary
@commands.command(name='llm')
async def llm_response(ctx):
async def llm_response(ctx, additional_context=""):
"""
Sends a response from the bot to the chat context in {ctx}
@@ -98,16 +130,35 @@ async def llm_response(ctx):
history_str = '\n'.join(history_arr)
full_prompt = prompt.replace("<CONVHISTORY>", history_str)
full_prompt = full_prompt.replace("<BOTNAME>", bot_name)
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)
await send_chat_responses(ctx, response)
await log_history(ctx, history_str)
async def process_search(ctx, query_str):
"""
Fires off when the search tool is used, processes the given query,
and continues generating text for chat
:param ctx: Chat context object
:param query: Query string (beginning with /search)
"""
query_str_trimmed=query_str.strip()
query=query_str_trimmed.removeprefix("/search")
search_results = await search_searx(query)
await llm_response(ctx, search_results)
async def send_chat_responses(ctx, response_text):
"""
Helper function for sending out the text in {response_text} to the discord server
context in {ctx}, handling breaking it into multiple parts and not sending
text that the LLM should not have generated, such as other users
Also handles tool usage
:param ctx: Message context that we're replying to
:param response_text: String containing message we want to send
"""
@@ -125,6 +176,12 @@ async def send_chat_responses(ctx, response_text):
output_strs.append(line.strip())
for outs in output_strs:
final_output_str = await fixup_mentions(ctx, outs)
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 != ""):
await ctx.channel.send(final_output_str)
@@ -168,6 +225,9 @@ async def handle_message(ctx):
"""
bot_id = llm_config['bot'].user.id
logger.info(f"Dank-bot <@{bot_id}> received message")
if (ctx.content.startswith("!")):
logger.info("Dank-bot command, not running LLM")
return
# First case, bot DMed
if (isinstance(ctx.channel,discord.DMChannel) and ctx.author.id != bot_id):