make search and index embeds look nicer

Kaveen Kumarasinghe 2 years ago
parent 3c54cb97b6
commit dba1f934a5

@ -6,6 +6,7 @@ import discord
from discord.ext import pages
from models.deepl_model import TranslationModel
from models.embed_statics_model import EmbedStatics
from models.search_model import Search
from services.environment_service import EnvService
from services.moderations_service import Moderation
@ -114,7 +115,7 @@ class SearchService(discord.Cog, name="SearchService"):
not EnvService.get_google_search_api_key()
or not EnvService.get_google_search_engine_id()
):
await ctx.respond("The search service is not enabled.")
await ctx.respond(embed=EmbedStatics.get_search_failure_embed(str("The search service is not enabled on this server.")),)
return
try:
@ -123,13 +124,13 @@ class SearchService(discord.Cog, name="SearchService"):
)
except ValueError as e:
await ctx.respond(
str(e),
embed=EmbedStatics.get_search_failure_embed(str(e)),
ephemeral=True,
)
return
except Exception:
except Exception as e:
await ctx.respond(
"An error occurred. Check the console for more details.", ephemeral=True
embed=EmbedStatics.get_search_failure_embed(str(e)), ephemeral=True
)
traceback.print_exc()
return
@ -189,7 +190,7 @@ class SearchView(discord.ui.View):
search_cog,
response_text,
):
super().__init__(timeout=3600) # 1 hour interval to redo.
super().__init__(timeout=None) # No timeout
self.search_cog = search_cog
self.ctx = ctx
self.response_text = response_text
@ -226,7 +227,7 @@ class RedoButton(discord.ui.Button["SearchView"]):
async def callback(self, interaction: discord.Interaction):
"""Redo the search"""
await interaction.response.send_message(
"Redoing search...", ephemeral=True, delete_after=15
embed=EmbedStatics.get_search_redo_progress_embed(), ephemeral=True, delete_after=15
)
await self.search_cog.search_command(
self.search_cog.redo_users[self.ctx.user.id].ctx,

@ -209,3 +209,38 @@ class EmbedStatics:
url="https://i.imgur.com/VLJ32x7.png"
)
return embed
@staticmethod
def get_edit_command_output_embed(response_text):
embed = discord.Embed(
title="GPT3 Edits",
description=f"{response_text}",
color=discord.Color.light_grey(),
)
return embed
@staticmethod
def get_search_failure_embed(message):
embed = discord.Embed(
title="AI-Assisted Search",
description=f"An error occured while performing search: {message}",
color=discord.Color.red(),
)
# thumbnail of https://i.imgur.com/VLJ32x7.png
embed.set_thumbnail(
url="https://i.imgur.com/VLJ32x7.png"
)
return embed
@staticmethod
def get_search_redo_progress_embed():
embed = discord.Embed(
title="AI-Assisted Search",
description=f"Your original search request is being redone. This may take a while.",
color=discord.Color.blurple(),
)
# thumbnail of https://i.imgur.com/VLJ32x7.png
embed.set_thumbnail(
url="https://i.imgur.com/txHhNzL.png"
)
return embed

@ -15,6 +15,7 @@ from typing import List, Optional
from pathlib import Path
from datetime import date
from discord import InteractionResponse, Interaction
from discord.ext import pages
from langchain import OpenAI
@ -924,13 +925,12 @@ class ComposeModal(discord.ui.View):
if len(indexes) < 1:
await interaction.response.send_message(
"You must select at least 1 index", ephemeral=True
embed=EmbedStatics.get_index_compose_failure_embed("You must select at least 1 index"), ephemeral=True
)
else:
composing_message = await interaction.response.send_message(
"Composing indexes, this may take a long time, you will be DMed when it's ready!",
embed=EmbedStatics.get_index_compose_progress_embed(),
ephemeral=True,
delete_after=120,
)
# Compose the indexes
try:
@ -969,8 +969,10 @@ class ComposeModal(discord.ui.View):
pass
try:
await composing_message.delete()
composing_message : Interaction
await composing_message.delete_original_response()
except:
pass
traceback.print_exc()
else:
await interaction.response.defer(ephemeral=True)

@ -22,9 +22,10 @@ from gpt_index import (
SimpleDirectoryReader,
GPTTreeIndex,
MockLLMPredictor,
MockEmbedding,
MockEmbedding
)
from gpt_index.indices.knowledge_graph import GPTKnowledgeGraphIndex
from gpt_index.prompts.prompt_type import PromptType
from gpt_index.readers.web import DEFAULT_WEBSITE_EXTRACTOR
from langchain import OpenAI
@ -69,54 +70,68 @@ class Search:
embed = discord.Embed(
title="Searching the web...",
description="Refining google search query...",
color=0x00FF00,
color=discord.Color.blurple(),
)
embed.set_thumbnail(url="https://i.imgur.com/txHhNzL.png")
return embed
def build_search_refined_embed(self, refined_query):
embed = discord.Embed(
title="Searching the web...",
description="Refined query: "
+ refined_query
+ "\n\nRetrieving links from google...",
color=0x00FF00,
description="Refined query:\n"
+ f"`{refined_query}`"
+ "\nRetrieving links from google...",
color=discord.Color.blurple(),
)
embed.set_thumbnail(url="https://i.imgur.com/txHhNzL.png")
return embed
def build_search_links_retrieved_embed(self, refined_query):
embed = discord.Embed(
title="Searching the web...",
description="Refined query: "
+ refined_query
+ "\n\nRetrieved links from Google\n\n"
"Retrieving webpages...",
color=0x00FF00,
description="Refined query:\n"
+ f"`{refined_query}`"
"\nRetrieving webpages...",
color=discord.Color.blurple(),
)
embed.set_thumbnail(url="https://i.imgur.com/txHhNzL.png")
return embed
def build_search_webpages_retrieved_embed(self, refined_query):
embed = discord.Embed(
title="Searching the web...",
description="Refined query: "
+ refined_query
+ "\n\nRetrieved links from Google\n\n"
"Retrieved webpages\n\n"
"Indexing...",
color=0x00FF00,
description="Refined query:\n"
+ f"`{refined_query}`"
"\nIndexing...",
color=discord.Color.blurple(),
)
embed.set_thumbnail(url="https://i.imgur.com/txHhNzL.png")
return embed
def build_search_indexed_embed(self, refined_query):
embed = discord.Embed(
title="Searching the web...",
description="Refined query: "
+ refined_query
+ "\n\nRetrieved links from Google\n\n"
"Retrieved webpages\n\n"
"Indexed\n\n"
"Thinking about your question...",
color=0x00FF00,
description="Refined query:\n"
+ f"`{refined_query}`"
"\nThinking about your question...",
color=discord.Color.blurple(),
)
embed.set_thumbnail(url="https://i.imgur.com/txHhNzL.png")
return embed
def build_search_final_embed(self, refined_query,price):
embed = discord.Embed(
title="Searching the web...",
description="Refined query:\n"
+ f"`{refined_query}`"
"\nDone!\n||The total price was $"+price+"||",
color=discord.Color.blurple(),
)
embed.set_thumbnail(url="https://i.imgur.com/txHhNzL.png")
return embed
def index_webpage(self, url) -> list[Document]:
@ -192,6 +207,9 @@ class Search:
else:
os.environ["OPENAI_API_KEY"] = user_api_key
# Initialize the search cost
price = 0
if ctx:
in_progress_message = (
await ctx.respond(embed=self.build_search_started_embed())
@ -199,22 +217,24 @@ class Search:
else await ctx.channel.send(embed=self.build_search_started_embed())
)
llm_predictor = LLMPredictor(llm=OpenAI(model_name="text-davinci-003"))
try:
llm_predictor_presearch = OpenAI(
max_tokens=50,
temperature=0.25,
presence_penalty=0.65,
model_name="text-davinci-003",
model_name="text-davinci-003"
)
# Refine a query to send to google custom search API
query_refined = llm_predictor_presearch.generate(
prompts=[
f"You are to be given a search query for google. Change the query such that putting it into the Google Custom Search API will return the most relevant websites to assist in answering the original query. If the original query is inferring knowledge about the current day, insert the current day into the refined prompt. If the original query is inferring knowledge about the current month, insert the current month and year into the refined prompt. If the original query is inferring knowledge about the current year, insert the current year into the refined prompt. Generally, if the original query is inferring knowledge about something that happened recently, insert the current month into the refined query. Avoid inserting a day, month, or year for queries that purely ask about facts and about things that don't have much time-relevance. The current date is {str(datetime.now().date())}. Do not insert the current date if not neccessary. Respond with only the refined query for the original query. Dont use punctuation or quotation marks.\n\nExamples:\n---\nOriginal Query: Who is Harald Baldr?\nRefined Query: Harald Baldr biography\n---\nOriginal Query: What happened today with the Ohio train derailment?\nRefined Query: Ohio train derailment details {str(datetime.now().date())}\n---\nOriginal Query: Is copper in drinking water bad for you?\nRefined Query: copper in drinking water adverse effects\n---\nOriginal Query: What's the current time in Mississauga?\nRefined Query: current time Mississauga\nNow, refine the user input query.\nOriginal Query: {query}\nRefined Query:"
]
prompt = f"You are to be given a search query for google. Change the query such that putting it into the Google Custom Search API will return the most relevant websites to assist in answering the original query. If the original query is inferring knowledge about the current day, insert the current day into the refined prompt. If the original query is inferring knowledge about the current month, insert the current month and year into the refined prompt. If the original query is inferring knowledge about the current year, insert the current year into the refined prompt. Generally, if the original query is inferring knowledge about something that happened recently, insert the current month into the refined query. Avoid inserting a day, month, or year for queries that purely ask about facts and about things that don't have much time-relevance. The current date is {str(datetime.now().date())}. Do not insert the current date if not neccessary. Respond with only the refined query for the original query. Dont use punctuation or quotation marks.\n\nExamples:\n---\nOriginal Query: Who is Harald Baldr?\nRefined Query: Harald Baldr biography\n---\nOriginal Query: What happened today with the Ohio train derailment?\nRefined Query: Ohio train derailment details {str(datetime.now().date())}\n---\nOriginal Query: Is copper in drinking water bad for you?\nRefined Query: copper in drinking water adverse effects\n---\nOriginal Query: What's the current time in Mississauga?\nRefined Query: current time Mississauga\nNow, refine the user input query.\nOriginal Query: {query}\nRefined Query:"
query_refined = await llm_predictor_presearch.agenerate(
prompts=[prompt],
)
query_refined_text = query_refined.generations[0][0].text
price += await self.usage_service.get_price(query_refined.llm_output.get("token_usage").get("total_tokens"))
except Exception as e:
traceback.print_exc()
query_refined_text = query
@ -338,6 +358,7 @@ class Search:
await self.usage_service.update_usage(
embedding_model.last_token_usage, embeddings=True
)
price += total_usage_price
else:
llm_predictor_deep = LLMPredictor(llm=OpenAI(model_name="text-davinci-003"))
# Try a mock call first
@ -397,6 +418,7 @@ class Search:
await self.usage_service.update_usage(
llm_predictor_deep.last_token_usage, embeddings=False
)
price += total_usage_price
if ctx:
await self.try_edit(
@ -448,8 +470,11 @@ class Search:
await self.usage_service.update_usage(
embedding_model.last_token_usage, embeddings=True
)
price += await self.usage_service.get_price(llm_predictor.last_token_usage) + await self.usage_service.get_price(
embedding_model.last_token_usage, True
)
if ctx:
await self.try_delete(in_progress_message)
await self.try_edit(in_progress_message, self.build_search_final_embed(query_refined_text, str(price)))
return response, query_refined_text

@ -303,10 +303,10 @@ class TextService:
elif from_edit_command:
if codex:
response_text = response_text.strip()
response_text = f"***Prompt: {prompt}***\n***Instruction: {instruction}***\n\n```\n{response_text}\n```"
response_text = f"***Prompt:\n `{prompt}`***\n***Instruction:\n `{instruction}`***\n\n```\n{response_text}\n```"
else:
response_text = response_text.strip()
response_text = f"***Prompt: {prompt}***\n***Instruction: {instruction}***\n\n{response_text}\n"
response_text = f"***Prompt:\n `{prompt}`***\n***Instruction:\n `{instruction}`***\n\n{response_text}\n"
# If gpt3 tries writing a user mention try to replace it with their name
response_text = await converser_cog.mention_to_username(ctx, response_text)
@ -411,7 +411,7 @@ class TextService:
)
elif from_edit_command:
response_message = await ctx.respond(
response_text,
embed=EmbedStatics.get_edit_command_output_embed(response_text),
view=ConversationView(
ctx,
converser_cog,
@ -469,7 +469,12 @@ class TextService:
"Over 2000 characters", delete_after=5
)
else:
await response_message.edit(content=response_text)
if not from_edit_command:
await response_message.edit(content=response_text)
else:
await response_message.edit(
embed=EmbedStatics.get_edit_command_output_embed(response_text)
)
await converser_cog.send_debug_message(
converser_cog.generate_debug_message(prompt, response),

Loading…
Cancel
Save