Fix image handling and do not react to bots or unchanged messages.
This commit is contained in:
@@ -10,7 +10,7 @@ from pathlib import Path
|
||||
from io import BytesIO
|
||||
from pprint import pformat
|
||||
from functools import lru_cache, wraps
|
||||
from typing import Optional, List, Dict, Any, Tuple
|
||||
from typing import Optional, List, Dict, Any, Tuple, Union
|
||||
|
||||
|
||||
def pp(*args, **kw):
|
||||
@@ -107,19 +107,21 @@ def same_channel(item1: Dict[str, Any], item2: Dict[str, Any]) -> bool:
|
||||
|
||||
class AIMessageBase(object):
|
||||
def __init__(self) -> None:
|
||||
pass
|
||||
self.vars: List[str] = []
|
||||
|
||||
def __str__(self) -> str:
|
||||
return json.dumps(vars(self))
|
||||
return json.dumps({k: v for k, v in vars(self).items() if k in self.vars})
|
||||
|
||||
|
||||
class AIMessage(AIMessageBase):
|
||||
def __init__(self, user: str, message: str, channel: str = "chat", direct: bool = False, historise_question: bool = True) -> None:
|
||||
self.user = user
|
||||
self.message = message
|
||||
self.urls: Optional[List[str]] = None
|
||||
self.channel = channel
|
||||
self.direct = direct
|
||||
self.historise_question = historise_question
|
||||
self.vars = ['user', 'message', 'channel', 'direct']
|
||||
|
||||
|
||||
class AIResponse(AIMessageBase):
|
||||
@@ -137,6 +139,7 @@ class AIResponse(AIMessageBase):
|
||||
self.staff = staff
|
||||
self.picture = picture
|
||||
self.hack = hack
|
||||
self.vars = ['answer', 'answer_needed', 'channel', 'staff', 'picture', 'hack']
|
||||
|
||||
|
||||
class AIResponderBase(object):
|
||||
@@ -182,7 +185,13 @@ class AIResponder(AIResponderBase):
|
||||
self.shrink_history_by_one()
|
||||
for msg in self.history:
|
||||
messages.append(msg)
|
||||
messages.append({"role": "user", "content": str(message)})
|
||||
if not message.urls:
|
||||
messages.append({"role": "user", "content": str(message)})
|
||||
else:
|
||||
content: List[Dict[str, Union[str, Dict[str, str]]]] = [{"type": "text", "text": str(message)}]
|
||||
for url in message.urls:
|
||||
content.append({"type": "image_url", "image_url": {"url": url}})
|
||||
messages.append({"role": "user", "content": content})
|
||||
return messages
|
||||
|
||||
async def draw(self, description: str) -> BytesIO:
|
||||
@@ -270,6 +279,8 @@ class AIResponder(AIResponderBase):
|
||||
answer: Dict[str, Any],
|
||||
limit: int,
|
||||
historise_question: bool = True) -> None:
|
||||
if type(question['content']) != str:
|
||||
question['content'] = question['content'][0]['text']
|
||||
if historise_question:
|
||||
self.history.append(question)
|
||||
self.history.append(answer)
|
||||
|
||||
Reference in New Issue
Block a user