-
Notifications
You must be signed in to change notification settings - Fork 1
/
instance.py
111 lines (93 loc) · 3.86 KB
/
instance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import mimetypes
import os
from re import I
from typing import Any, Optional
import typing
import logger
import models
import chatModel
import config
import memory
import conversation
import chatModel
from langchain_core.messages import SystemMessage, HumanMessage
import google.generativeai as genai
import google.ai.generativelanguage as glm
import webFrontend.chatPlugins
class Chatbot:
def __init__(self, memory: memory.Memory, userName: str, additionalPlugins: list[typing.Any] = [], rtSession: bool = False) -> None:
pluginList = webFrontend.chatPlugins.defaultPluginList()
pluginList.extend(additionalPlugins)
if rtSession:
logger.Logger.log('Real time session detected, LLM initialization skipped.')
self.llm = None if rtSession else models.ChatModelProvider(memory.createCharPromptFromCharacter(userName), pluginList)
self.memory = memory
self.userName = userName
self.inChatting = False
self.conversation = conversation.ConversationMemory(
userName, self.memory)
def __enter__(self):
return None
def switchUser(self, name: str) -> None:
if self.inChatting:
logger.Logger.log('Unable to perform this action: Character is chatting!')
else:
self.userName = name
def begin(self, userInput: None | list[dict[str, str]]) -> str:
modelInput = self.convertMessageListToInput(userInput)
msg = self.llm.initiate(modelInput)
self.conversation.storeBotInput(chatModel.AIMessage(msg))
logger.Logger.log(msg)
return msg
def getAvailableStickers(self) -> list[str]:
return [i['name'] for i in self.memory.getAvailableStickers()]
def convertMessageToInput(self, message: dict[str, str]) -> str | glm.File:
logger.Logger.log(message)
if message['content_type'] == 'text':
return message['content']
elif message['content_type'] == 'image':
mime, binary = self.memory.dataProvider.getAttachment(
message['content'])
fp = self.memory.dataProvider.tempFilePathProvider(
mimetypes.guess_extension(mime))
with open(fp, 'wb+') as f:
f.write(binary)
r = genai.upload_file(fp, mime_type=mime)
logger.Logger.log('Removing temporary file:', fp)
os.remove(fp)
return r
elif message['content_type'] == 'audio':
mime, binary = self.memory.dataProvider.getAttachment(
message['content'])
fp = self.memory.dataProvider.tempFilePathProvider('m4a')
with open(fp, 'wb+') as f:
f.write(binary)
r = genai.upload_file(fp, mime_type=mime)
logger.Logger.log('Removing temporary file:', fp)
os.remove(fp)
return r
else:
raise ValueError(f'{__name__}: Unknown message type: {
message["type"]}')
def convertMessageListToInput(self, messages: list[dict[str, str]]) -> list[str | glm.File]:
return [self.convertMessageToInput(i) for i in messages]
def chat(self, userInput: list[dict[str, str]]) -> str:
modelInput = self.convertMessageListToInput(userInput)
msg = self.llm.chat(modelInput)
self.conversation.storeBotInput(chatModel.AIMessage(msg))
logger.Logger.log(msg)
return msg
def termination(self) -> None:
summary = self.llm.chat(f'EOF')
self.memory.storeMemory(self.userName, summary)
def terminateChat(self, force=False) -> None:
self.inChatting = False
if not force:
self.termination()
def __exit__(self, type, value, traceback) -> None:
self.inChatting = False
if value is None:
self.termination()
else:
# ignoring the process
pass