From 18ac706c10df979229edc7d99e0c1fe9f7b599c9 Mon Sep 17 00:00:00 2001 From: Producer Matt <58014742+ProducerMatt@users.noreply.github.com> Date: Mon, 19 Jun 2023 00:13:56 -0500 Subject: [PATCH] Adding the "prompt" alternative --- modules/AlignmentNewsletterSearch.py | 2 +- modules/chatgpt.py | 2 +- modules/duckduckgo.py | 6 +++--- modules/gpt3module.py | 2 +- modules/module.py | 9 +++++++++ modules/testModule.py | 2 +- modules/videosearch.py | 2 +- modules/why.py | 4 ++-- modules/wolfram.py | 4 ++-- runstampy | 2 +- servicemodules/discord.py | 28 +++++++++++++++++++++------- 11 files changed, 43 insertions(+), 20 deletions(-) diff --git a/modules/AlignmentNewsletterSearch.py b/modules/AlignmentNewsletterSearch.py index 25ddaeae..1934f7dc 100644 --- a/modules/AlignmentNewsletterSearch.py +++ b/modules/AlignmentNewsletterSearch.py @@ -147,7 +147,7 @@ def process_message(self, message: ServiceMessage) -> Response: query = match.group("query") return Response( - confidence=9, callback=self.process_search_request, args=[query] + confidence=9, callback=self.process_search_request, prompt=query ) async def process_search_request(self, query) -> Response: diff --git a/modules/chatgpt.py b/modules/chatgpt.py index 353d4e36..efa08df4 100644 --- a/modules/chatgpt.py +++ b/modules/chatgpt.py @@ -67,7 +67,7 @@ def process_message(self, message: ServiceMessage) -> Response: return Response() return Response( - confidence=3, callback=self.chatgpt_chat, args=[message], kwargs={} + confidence=3, callback=self.chatgpt_chat, prompt=message, ) def process_message_from_stampy(self, message) -> None: diff --git a/modules/duckduckgo.py b/modules/duckduckgo.py index 0eb3d9e1..9354d17d 100644 --- a/modules/duckduckgo.py +++ b/modules/duckduckgo.py @@ -23,7 +23,7 @@ def process_message(self, message: ServiceMessage) -> Response: return Response( confidence=10, callback=self.ask, - args=[text[m.end(0):]], + prompt=text[m.end(0):], why="This is definitely a web search", ) print(f"Text didn't match: {text}") @@ -31,13 +31,13 @@ def process_message(self, message: ServiceMessage) -> Response: return Response( confidence=6, callback=self.ask, - args=[text], + prompt=text, why="It's a question, we might be able to answer it", ) return Response( confidence=2, callback=self.ask, - args=[text], + prompt=text, why="It's not a question but we might be able to look it up", ) return Response() diff --git a/modules/gpt3module.py b/modules/gpt3module.py index 9d4f6e55..8b68e8e8 100644 --- a/modules/gpt3module.py +++ b/modules/gpt3module.py @@ -75,7 +75,7 @@ def process_message(self, message: ServiceMessage) -> Response: return Response() return Response( - confidence=2, callback=self.gpt3_chat, args=[message], kwargs={} + confidence=2, callback=self.gpt3_chat, prompt=message, ) def process_message_from_stampy(self, message: ServiceMessage) -> None: diff --git a/modules/module.py b/modules/module.py index f4dbdbf3..fc4eb25a 100644 --- a/modules/module.py +++ b/modules/module.py @@ -90,6 +90,7 @@ class Response: confidence: float = 0.0 text: Union[str, Iterable[str]] = "" callback: Optional[Callable] = None + prompt: Optional[Union[ServiceMessage, str]] = None args: list = field(default_factory=list) kwargs: dict = field(default_factory=dict) @@ -109,6 +110,13 @@ def __repr__(self) -> str: kwargs = self.kwargs module = str(self.module) why = self.why + if prompt: + if isinstance(self.prompt, ServiceMessage): + prompt = self.prompt.id + else: + prompt = str(self.prompt)[:20] + "..." if len(self.prompt) > 20 else self.prompt # limit length + else: + prompt = None return ( "Response(" + (f"{embed=} " if embed else "") @@ -119,6 +127,7 @@ def __repr__(self) -> str: + (f"{kwargs=} " if kwargs else "") + (f"{module=} " if module else "") + (f"{why=}" if why else "") + + (f"{prompt=}" if prompt else "") + ")" ) diff --git a/modules/testModule.py b/modules/testModule.py index 3f5f7290..17876671 100644 --- a/modules/testModule.py +++ b/modules/testModule.py @@ -122,7 +122,7 @@ def process_message(self, message: ServiceMessage): return Response( confidence=10, callback=self.run_integration_tests, - args=[message], + prompt=message, kwargs={"modules_dict": modules_dict}, ) diff --git a/modules/videosearch.py b/modules/videosearch.py index a7913f32..2cb102eb 100644 --- a/modules/videosearch.py +++ b/modules/videosearch.py @@ -135,7 +135,7 @@ def process_message(self, message): m = re.match(self.re_search, text) if m: query = m.group("query") - return Response(confidence=9, callback=self.process_search_request, args=[query]) + return Response(confidence=9, callback=self.process_search_request, prompt=query) # This is either not at me, or not something we can handle return Response() diff --git a/modules/why.py b/modules/why.py index 67592010..8fe0e448 100644 --- a/modules/why.py +++ b/modules/why.py @@ -24,14 +24,14 @@ def process_message(self, message: ServiceMessage) -> Response: return Response( confidence=10, callback=self.specific, - args=[message], + prompt=message, why="A stamp owner wants to know why I said something.", ) else: return Response( confidence=10, callback=self.general, - args=[message], + prompt=message, why="A stamp owner wants to know why I said something.", ) else: diff --git a/modules/wolfram.py b/modules/wolfram.py index 5600308d..dbf7d705 100644 --- a/modules/wolfram.py +++ b/modules/wolfram.py @@ -35,14 +35,14 @@ def process_message(self, message): return Response( confidence=5, callback=self.ask, - args=[text], + prompt=text, why="It's a question, we might be able to answer it", ) else: return Response( confidence=1, callback=self.ask, - args=[text], + prompt=text, why="It's not a question but we might be able to look it up", ) diff --git a/runstampy b/runstampy index 1cdcab27..532db30a 100755 --- a/runstampy +++ b/runstampy @@ -24,7 +24,7 @@ if [ "${STAMPY_RUN_TESTS:-nil}" != "nil" ]; then pylint stam; else while true; do - python stam.py + python -Werror stam.py EXIT_CODE=$? # echo "Return code: ${EXIT_CODE}" # DEBUG if [ "${STOP_ON_ERROR:-nil}" != "nil" ] && [ $EXIT_CODE != "42" ]; then diff --git a/servicemodules/discord.py b/servicemodules/discord.py index 1e36e7d4..2d752d38 100644 --- a/servicemodules/discord.py +++ b/servicemodules/discord.py @@ -180,7 +180,7 @@ async def on_message( for response in responses: args_string = "" - if response.callback: + if response.callback and response.args: args_string = ", ".join([a.__repr__() for a in response.args]) if response.kwargs: args_string += ", " + ", ".join( @@ -189,6 +189,7 @@ async def on_message( for k, v in response.kwargs.items() ] ) + log.info( self.class_name, response_module=str(response.module), @@ -197,6 +198,9 @@ async def on_message( response_callback=( response.callback.__name__ if response.callback else None ), + response_prompt=( + response.prompt if response.prompt else None + ), response_args=args_string, response_text=( response.text @@ -222,13 +226,23 @@ async def on_message( # Note that sometimes a callback will run but not send a message, in which case he'll seem to be typing but not say anything. I think this will be rare though. async with message.channel._channel.typing(): if inspect.iscoroutinefunction(top_response.callback): - new_response = await top_response.callback( - *top_response.args, **top_response.kwargs - ) + if top_response.args: + new_response = await top_response.callback( + *top_response.args, **top_response.kwargs + ) + else: + new_response = await top_response.callback( + top-response.prompt + ) else: - new_response = top_response.callback( - *top_response.args, **top_response.kwargs - ) + if top_response.args or top_response.kwargs: + new_response = top_response.callback( + *top_response.args, **top_response.kwargs + ) + else: + new_response = top_response.callback( + top-response.prompt + ) new_response.module = top_response.module new_response.text = limit_text_and_notify(