ログ対応
このコミットが含まれているのは:
@@ -2,3 +2,4 @@
|
|||||||
/__pycache__
|
/__pycache__
|
||||||
/nizika_talking.wav
|
/nizika_talking.wav
|
||||||
/youtube.py
|
/youtube.py
|
||||||
|
/log.txt
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ from playsound import playsound
|
|||||||
from common_module import CommonModule
|
from common_module import CommonModule
|
||||||
from youtube import *
|
from youtube import *
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
class Main:
|
class Main:
|
||||||
@@ -30,17 +31,40 @@ class Main:
|
|||||||
italic = True)
|
italic = True)
|
||||||
nizika_font = pygame.font.SysFont ('07nikumarufont', 50)
|
nizika_font = pygame.font.SysFont ('07nikumarufont', 50)
|
||||||
|
|
||||||
|
messages: list[str] = []
|
||||||
|
|
||||||
while (True):
|
while (True):
|
||||||
screen.fill ((0, 255, 0))
|
screen.fill ((0, 255, 0))
|
||||||
|
|
||||||
if live_chat.is_alive ():
|
for i in range (4):
|
||||||
messages: list[str] = [c.message
|
screen.blit (
|
||||||
for c in live_chat.get ().items]
|
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 messages:
|
if live_chat.is_alive ():
|
||||||
message: str = random.choice (messages)
|
screen.blit (
|
||||||
|
system_font.render (
|
||||||
|
f'messages == {messages}',
|
||||||
|
True, (0, 0, 0)),
|
||||||
|
(i % 2, 64 + i // 2 * 2))
|
||||||
|
|
||||||
|
if live_chat.is_alive ():
|
||||||
|
chat_items: list = live_chat.get ().items
|
||||||
|
|
||||||
|
if chat_items:
|
||||||
|
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)
|
||||||
|
|
||||||
|
with open ('log.txt', 'a') as f:
|
||||||
|
f.write (f'{datetime.now ()}\t{json.dumps (chat_item.__dict__)}\t{answer}\n')
|
||||||
|
|
||||||
screen.blit (balloon, (0, 0))
|
screen.blit (balloon, (0, 0))
|
||||||
screen.blit (
|
screen.blit (
|
||||||
user_font.render (
|
user_font.render (
|
||||||
@@ -72,23 +96,6 @@ class Main:
|
|||||||
else:
|
else:
|
||||||
live_chat = pytchat.create (video_id = YOUTUBE_ID)
|
live_chat = pytchat.create (video_id = YOUTUBE_ID)
|
||||||
|
|
||||||
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))
|
|
||||||
|
|
||||||
pygame.display.update ()
|
pygame.display.update ()
|
||||||
|
|
||||||
for event in pygame.event.get ():
|
for event in pygame.event.get ():
|
||||||
|
|||||||
@@ -18,17 +18,23 @@ class Talk:
|
|||||||
openai.organization = OPENAI_ORGANISATION
|
openai.organization = OPENAI_ORGANISATION
|
||||||
openai.api_key = OPENAI_API_KEY
|
openai.api_key = OPENAI_API_KEY
|
||||||
|
|
||||||
return cls.__get_message (message).content
|
answer: chat.chat_completion_message.ChatCompletionMessage | None \
|
||||||
|
= cls.__get_message (message)
|
||||||
|
|
||||||
|
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) \
|
||||||
-> chat.chat_completion_message.ChatCompletionMessage:
|
-> chat.chat_completion_message.ChatCompletionMessage | None:
|
||||||
|
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': 'あなたの名前は伊地知ニジカ。誕生日は毎日。友達はゴートうひとりとラマ田リョウ。好きな人は喜多ちゃん。すべての語尾に「ぬ゛ん」か「だぬ゛ん」をつけて質問に短く答えろ。定期的に「ぬ゛ぅ゛ぅ゛ぅ゛ぅ゛ん゛」、「ボッチチャーン」、「あっ」のいずれかを発しろ'},
|
||||||
{'role': 'user',
|
{'role': 'user',
|
||||||
'content': message}]).choices[0].message
|
'content': message}]).choices[0].message
|
||||||
|
except openai.AuthenticationError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
新しい課題から参照
ユーザをブロックする