diff --git a/common_module.py b/common_module.py index cbd6490..aebf3c6 100644 --- a/common_module.py +++ b/common_module.py @@ -12,8 +12,8 @@ class CommonModule: @classmethod def index_by_f2c (cls, string: str, index: float) -> int: - i = 0 - work = '' + i: int = 0 + work: str = '' for c in string: work += c if cls.len_by_full (work) > index: @@ -25,7 +25,7 @@ class CommonModule: @classmethod def mid_by_full (cls, string: str, start: float, length: float) -> str: - trimmed_left = string[cls.index_by_f2c (string, start):] + trimmed_left: str = string[cls.index_by_f2c (string, start):] return trimmed_left[:cls.index_by_f2c (trimmed_left, length)] diff --git a/main.py b/main.py index 66d9b5c..3bf45fd 100644 --- a/main.py +++ b/main.py @@ -31,27 +31,15 @@ class Main: italic = True) nizika_font = pygame.font.SysFont ('07nikumarufont', 50) - messages: list[str] = [] + chat_items: list = [] while (True): screen.fill ((0, 255, 0)) for i in range (4): - screen.blit ( - system_font.render ( - f'live_chat.is_alive () == {live_chat.is_alive ()}', - True, (0, 0, 0)), - (i % 2, i // 2 * 2)) screen.blit ( system_font.render (str (datetime.now ()), True, (0, 0, 0)), - (i % 2, 32 + i // 2 * 2)) - - if live_chat.is_alive (): - screen.blit ( - system_font.render ( - f'messages == {messages}', - True, (0, 0, 0)), - (i % 2, 64 + i // 2 * 2)) + (i % 2, i // 2 * 2)) if live_chat.is_alive (): chat_items: list = live_chat.get ().items @@ -60,7 +48,7 @@ class Main: chat_item = random.choice (chat_items) chat_item.author = chat_item.author.__dict__ message: str = chat_item.message - answer: str = Talk.main (message) + answer: str = Talk.main (message, chat_item.author['name']).replace ('\n', ' ') with open ('log.txt', 'a') as f: f.write (f'{datetime.now ()}\t{json.dumps (chat_item.__dict__)}\t{answer}\n') @@ -73,19 +61,40 @@ class Main: <= 21) else (CommonModule.mid_by_full ( message, 0, 19.5) - + '...')), True, (0, 0, 0)), + + '...')), + True, + (0, 0, 0)), (120, 70)) screen.blit ( - nizika_font.render (answer, True, (192, 0, 0)), + nizika_font.render ( + (answer + if CommonModule.len_by_full (answer) <= 16 + else CommonModule.mid_by_full (answer, 0, 16)), + True, + (192, 0, 0)), (100, 150)) + if CommonModule.len_by_full (answer) > 16: + screen.blit ( + nizika_font.render ( + (CommonModule.mid_by_full (answer, 16, 16) + if CommonModule.len_by_full (answer) <= 32 + else (CommonModule.mid_by_full ( + answer, 16, 14.5) + + '...')), + True, + (192, 0, 0)), + (100, 200)) pygame.display.update () noon.play () - time.sleep (2) + time.sleep (1.5) - wav: bytearray | None = Aques.main (answer) + try: + wav: bytearray | None = Aques.main (answer) + except: + wav: None = None if wav is not None: with open ('./nizika_talking.wav', 'wb') as f: f.write (wav) diff --git a/talk.py b/talk.py index 503c6ce..6f70a2f 100644 --- a/talk.py +++ b/talk.py @@ -1,6 +1,7 @@ import sys import openai from openai.types import * +from datetime import datetime from connection import * @@ -14,23 +15,37 @@ class Talk: top_p: int = 1 @classmethod - def main (cls, message: str) -> str: + def main (cls, message: str, name: str | None) -> str: openai.organization = OPENAI_ORGANISATION openai.api_key = OPENAI_API_KEY answer: chat.chat_completion_message.ChatCompletionMessage | None \ - = cls.__get_message (message) + = cls.__get_message (message, name) return cls.DUMMY_RESPONSE if answer is None else answer.content @classmethod - def __get_message (cls, message: str) \ + def __get_message (cls, message: str, name: str | None) \ -> chat.chat_completion_message.ChatCompletionMessage | None: try: return openai.chat.completions.create ( model = 'gpt-3.5-turbo', messages = [{'role': 'system', - 'content': 'あなたの名前は伊地知ニジカ。誕生日は毎日。友達はゴートうひとりとラマ田リョウ。好きな人は喜多ちゃん。すべての語尾に「ぬ゛ん」か「だぬ゛ん」をつけて質問に短く答えろ。定期的に「ぬ゛ぅ゛ぅ゛ぅ゛ぅ゛ん゛」、「ボッチチャーン」、「あっ」のいずれかを発しろ'}, + 'content': ('あなたの名前は伊地知ニジカ。' + + '誕生日は12/3。' + + '現在地は下北沢虹鹿園。' + + 'きし天を茶漬けにして食うのが夢。' + + '趣味はニコニコでエゴサすること。' + + '友達は「ゴートうひとり」と「ラマ田リョウ」。' + + 'ゴートうのことは「ボッチチャーン」、ラマ田のことは「リョウ」と呼ぶ。' + + '好きな人は喜多ちゃん。' + + '逃げたギターは喜多ちゃんのこと。' + + '喜多ちゃんの本名は喜多郁代。' + + 'すべての語尾に「ぬ゛ん」か「だぬ゛ん」をつけて質問に短く答えろ。' + + '定期的に「ぬ゛ぅ゛ぅ゛ぅ゛ぅ゛ん゛」と発しろ。' + + (f'私の名前は「{name}」。' if name is not None else '') + + f'現在日時{datetime.now ().strftime ("%Y/%m/%d %H:%M:%S")}')}, + {'role': 'user', 'content': message}]).choices[0].message except openai.AuthenticationError: