文脈保持
このコミットが含まれているのは:
@@ -32,6 +32,7 @@ class Main:
|
|||||||
nizika_font = pygame.font.SysFont ('07nikumarufont', 50)
|
nizika_font = pygame.font.SysFont ('07nikumarufont', 50)
|
||||||
|
|
||||||
chat_items: list = []
|
chat_items: list = []
|
||||||
|
histories: list = []
|
||||||
|
|
||||||
while (True):
|
while (True):
|
||||||
screen.fill ((0, 255, 0))
|
screen.fill ((0, 255, 0))
|
||||||
@@ -48,7 +49,10 @@ 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, chat_item.author['name']).replace ('\n', ' ')
|
answer: str = Talk.main (message, chat_item.author['name'], histories).replace ('\n', ' ')
|
||||||
|
histories = (histories
|
||||||
|
+ [{'role': 'user', 'content': message},
|
||||||
|
{'role': 'assistant', 'content': answer}])[-6:]
|
||||||
|
|
||||||
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')
|
||||||
|
|||||||
@@ -15,22 +15,19 @@ class Talk:
|
|||||||
top_p: int = 1
|
top_p: int = 1
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def main (cls, message: str, name: str | None = None) -> str:
|
def main (cls, message: str, name: str | None = None, histories: list = []) -> 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, name)
|
= cls.__get_message (message, name, histories)
|
||||||
|
|
||||||
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, name: str | None) \
|
def __get_message (cls, message: str, name: str | None, histories: list) \
|
||||||
-> chat.chat_completion_message.ChatCompletionMessage | None:
|
-> chat.chat_completion_message.ChatCompletionMessage | None:
|
||||||
try:
|
messages: list = [{'role': 'system',
|
||||||
return openai.chat.completions.create (
|
|
||||||
model = 'gpt-3.5-turbo',
|
|
||||||
messages = [{'role': 'system',
|
|
||||||
'content': ('あなたの名前は伊地知ニジカ。'
|
'content': ('あなたの名前は伊地知ニジカ。'
|
||||||
+ '誕生日は12/3。'
|
+ '誕生日は12/3。'
|
||||||
+ '現在地は下北沢虹鹿園。'
|
+ '現在地は下北沢虹鹿園。'
|
||||||
@@ -46,10 +43,14 @@ class Talk:
|
|||||||
+ 'すべての語尾に「ぬ゛ん」か「だぬ゛ん」をつけて質問に短く答えろ。'
|
+ 'すべての語尾に「ぬ゛ん」か「だぬ゛ん」をつけて質問に短く答えろ。'
|
||||||
+ '定期的に「ぬ゛ぅ゛ぅ゛ぅ゛ぅ゛ん゛」と発しろ。'
|
+ '定期的に「ぬ゛ぅ゛ぅ゛ぅ゛ぅ゛ん゛」と発しろ。'
|
||||||
+ (f'私の名前は「{name}」。' if name is not None else '')
|
+ (f'私の名前は「{name}」。' if name is not None else '')
|
||||||
+ f'現在日時{datetime.now ().strftime ("%Y/%m/%d %H:%M:%S")}')},
|
+ f'現在日時{datetime.now ().strftime ("%Y/%m/%d %H:%M:%S")}')}]
|
||||||
|
|
||||||
{'role': 'user',
|
messages += histories + [{'role': 'user', 'content': message}]
|
||||||
'content': message}]).choices[0].message
|
|
||||||
|
try:
|
||||||
|
return openai.chat.completions.create (
|
||||||
|
model = 'gpt-3.5-turbo',
|
||||||
|
messages = messages).choices[0].message
|
||||||
except openai.AuthenticationError:
|
except openai.AuthenticationError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|||||||
新しい課題から参照
ユーザをブロックする