@@ -12,8 +12,8 @@ class CommonModule: | |||||
@classmethod | @classmethod | ||||
def index_by_f2c (cls, string: str, index: float) -> int: | def index_by_f2c (cls, string: str, index: float) -> int: | ||||
i = 0 | |||||
work = '' | |||||
i: int = 0 | |||||
work: str = '' | |||||
for c in string: | for c in string: | ||||
work += c | work += c | ||||
if cls.len_by_full (work) > index: | if cls.len_by_full (work) > index: | ||||
@@ -25,7 +25,7 @@ class CommonModule: | |||||
@classmethod | @classmethod | ||||
def mid_by_full (cls, string: str, start: float, length: float) -> str: | 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)] | return trimmed_left[:cls.index_by_f2c (trimmed_left, length)] | ||||
@@ -31,27 +31,15 @@ class Main: | |||||
italic = True) | italic = True) | ||||
nizika_font = pygame.font.SysFont ('07nikumarufont', 50) | nizika_font = pygame.font.SysFont ('07nikumarufont', 50) | ||||
messages: list[str] = [] | |||||
chat_items: list = [] | |||||
while (True): | while (True): | ||||
screen.fill ((0, 255, 0)) | screen.fill ((0, 255, 0)) | ||||
for i in range (4): | 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 ( | screen.blit ( | ||||
system_font.render (str (datetime.now ()), True, (0, 0, 0)), | 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 (): | if live_chat.is_alive (): | ||||
chat_items: list = live_chat.get ().items | chat_items: list = live_chat.get ().items | ||||
@@ -60,7 +48,7 @@ class Main: | |||||
chat_item = random.choice (chat_items) | chat_item = random.choice (chat_items) | ||||
chat_item.author = chat_item.author.__dict__ | chat_item.author = chat_item.author.__dict__ | ||||
message: str = chat_item.message | 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: | with open ('log.txt', 'a') as f: | ||||
f.write (f'{datetime.now ()}\t{json.dumps (chat_item.__dict__)}\t{answer}\n') | f.write (f'{datetime.now ()}\t{json.dumps (chat_item.__dict__)}\t{answer}\n') | ||||
@@ -73,19 +61,40 @@ class Main: | |||||
<= 21) | <= 21) | ||||
else (CommonModule.mid_by_full ( | else (CommonModule.mid_by_full ( | ||||
message, 0, 19.5) | message, 0, 19.5) | ||||
+ '...')), True, (0, 0, 0)), | |||||
+ '...')), | |||||
True, | |||||
(0, 0, 0)), | |||||
(120, 70)) | (120, 70)) | ||||
screen.blit ( | 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)) | (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 () | pygame.display.update () | ||||
noon.play () | 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: | if wav is not None: | ||||
with open ('./nizika_talking.wav', 'wb') as f: | with open ('./nizika_talking.wav', 'wb') as f: | ||||
f.write (wav) | f.write (wav) | ||||
@@ -1,6 +1,7 @@ | |||||
import sys | import sys | ||||
import openai | import openai | ||||
from openai.types import * | from openai.types import * | ||||
from datetime import datetime | |||||
from connection import * | from connection import * | ||||
@@ -14,23 +15,37 @@ class Talk: | |||||
top_p: int = 1 | top_p: int = 1 | ||||
@classmethod | @classmethod | ||||
def main (cls, message: str) -> str: | |||||
def main (cls, message: str, name: str | None) -> str: | |||||
openai.organization = OPENAI_ORGANISATION | openai.organization = OPENAI_ORGANISATION | ||||
openai.api_key = OPENAI_API_KEY | openai.api_key = OPENAI_API_KEY | ||||
answer: chat.chat_completion_message.ChatCompletionMessage | None \ | 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 | return cls.DUMMY_RESPONSE if answer is None else answer.content | ||||
@classmethod | @classmethod | ||||
def __get_message (cls, message: str) \ | |||||
def __get_message (cls, message: str, name: str | None) \ | |||||
-> chat.chat_completion_message.ChatCompletionMessage | None: | -> chat.chat_completion_message.ChatCompletionMessage | None: | ||||
try: | try: | ||||
return openai.chat.completions.create ( | return openai.chat.completions.create ( | ||||
model = 'gpt-3.5-turbo', | model = 'gpt-3.5-turbo', | ||||
messages = [{'role': 'system', | 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', | {'role': 'user', | ||||
'content': message}]).choices[0].message | 'content': message}]).choices[0].message | ||||
except openai.AuthenticationError: | except openai.AuthenticationError: | ||||