Skip to content

Commit

Permalink
fix closed IO error
Browse files Browse the repository at this point in the history
  • Loading branch information
Kav-K committed Apr 26, 2023
1 parent 5ccb360 commit f09aea3
Showing 1 changed file with 48 additions and 40 deletions.
88 changes: 48 additions & 40 deletions cogs/search_service_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@

from contextlib import redirect_stdout


from langchain.agents.conversational_chat.output_parser import ConvoOutputParser

original_parse = ConvoOutputParser.parse
Expand Down Expand Up @@ -104,12 +103,21 @@ def my_parse(self, text):
ConvoOutputParser.parse = my_parse


class CaptureStdout:
def __enter__(self):
self.buffer = io.StringIO()
self.original_stdout = sys.stdout
sys.stdout = self.buffer
return self.buffer

def __exit__(self, exc_type, exc_val, exc_tb):
sys.stdout = self.original_stdout


async def capture_stdout(func, *args, **kwargs):
buffer = io.StringIO()
with redirect_stdout(buffer):
with CaptureStdout() as buffer:
result = await func(*args, **kwargs)
captured_output = buffer.getvalue()
buffer.close()
return result, captured_output


Expand Down Expand Up @@ -215,12 +223,12 @@ class SearchService(discord.Cog, name="SearchService"):
"""Cog containing translation commands and retrieval of translation services"""

def __init__(
self,
bot,
gpt_model,
usage_service,
deletion_service,
converser_cog,
self,
bot,
gpt_model,
usage_service,
deletion_service,
converser_cog,
):
super().__init__()
self.bot = bot
Expand All @@ -234,12 +242,12 @@ def __init__(
# Make a mapping of all the country codes and their full country names:

async def paginate_embed(
self, response_text, user: discord.Member, original_link=None
self, response_text, user: discord.Member, original_link=None
):
"""Given a response text make embed pages and return a list of the pages."""

response_text = [
response_text[i : i + self.EMBED_CUTOFF]
response_text[i: i + self.EMBED_CUTOFF]
for i in range(0, len(response_text), self.EMBED_CUTOFF)
]
pages = []
Expand Down Expand Up @@ -277,7 +285,7 @@ async def paginate_chat_embed(self, response_text):
"""Given a response text make embed pages and return a list of the pages."""

response_text = [
response_text[i : i + 3500] for i in range(0, len(response_text), 7000)
response_text[i: i + 3500] for i in range(0, len(response_text), 7000)
]
pages = []
first = False
Expand Down Expand Up @@ -408,7 +416,7 @@ async def on_message(self, message):
self.thread_awaiting_responses.remove(message.channel.id)

async def search_chat_command(
self, ctx: discord.ApplicationContext, search_scope=2, use_gpt4: bool = False
self, ctx: discord.ApplicationContext, search_scope=2, use_gpt4: bool = False
):
embed_title = f"{ctx.user.name}'s internet-connected conversation with GPT"
message_embed = discord.Embed(
Expand Down Expand Up @@ -493,18 +501,18 @@ async def search_chat_command(
self.chat_agents[thread.id] = agent_chain

async def search_command(
self,
ctx: discord.ApplicationContext,
query,
search_scope,
nodes,
deep,
response_mode,
model="gpt-3.5-turbo",
multistep=False,
redo=None,
from_followup=None,
followup_user=None,
self,
ctx: discord.ApplicationContext,
query,
search_scope,
nodes,
deep,
response_mode,
model="gpt-3.5-turbo",
multistep=False,
redo=None,
from_followup=None,
followup_user=None,
):
"""Command handler for the search command"""
await ctx.defer() if not redo else None
Expand All @@ -523,8 +531,8 @@ async def search_command(
return

if (
not EnvService.get_google_search_api_key()
or not EnvService.get_google_search_engine_id()
not EnvService.get_google_search_api_key()
or not EnvService.get_google_search_engine_id()
):
await ctx.respond(
embed=EmbedStatics.get_search_failure_embed(
Expand Down Expand Up @@ -611,10 +619,10 @@ async def search_command(

class SearchView(discord.ui.View):
def __init__(
self,
ctx,
search_cog,
response_text,
self,
ctx,
search_cog,
response_text,
):
super().__init__(timeout=None) # No timeout
self.search_cog = search_cog
Expand Down Expand Up @@ -701,14 +709,14 @@ async def callback(self, interaction: discord.Interaction):

# Build the context
context_text = (
"Original question: "
+ query
+ "\n"
+ "Answer to original: "
+ self.response_text
+ "\n"
+ "Follow-up question: "
+ self.children[0].value
"Original question: "
+ query
+ "\n"
+ "Answer to original: "
+ self.response_text
+ "\n"
+ "Follow-up question: "
+ self.children[0].value
)

# Get the link of the message that the user interacted on
Expand Down

0 comments on commit f09aea3

Please sign in to comment.