-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
58 lines (49 loc) · 1.8 KB
/
__init__.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
from os import path
from time import sleep
import ollama
from ollama import RequestError, ResponseError
import ai
from play.rules import RuleType
from settings.settings import Settings
from utils.logging import log
settings = Settings(path.join(path.split(path.relpath(__file__))[0], 'settings.yaml'))
class Chat:
client = ollama.Client(host=settings.get("host"), timeout=settings.get("timeout"))
@staticmethod
def send(prompt, session, character=None):
messages = []
if character:
messages.append(
{"role": ai.Role.SYSTEM.value, "content": character.personality.get('description')}
)
if character.task:
messages.append(
{"role": ai.Role.USER.value,
"content": f'{character.task.description} {character.serialize_rules(RuleType.PERMANENT)}'}
)
if session.history.summary:
messages.append(session.history.summary.serialize())
if session.history.moments:
messages.extend(
[entry.serialize(character.name) for entry in session.history.get()]
)
messages.append(
{
"role": ai.Role.USER.value,
"content": f'{character.serialize_rules(RuleType.TEMPORARY)} {prompt}'
}
)
model = character.personality.get("model", settings.get("chat.model"))
try:
completion = Chat.client.chat(
model=model,
messages=messages,
)
except (RequestError, ResponseError) as error:
log.error(error)
return
try:
return completion.get("message", {}).get("content", "")
except IndexError as error:
log.warning(error)
return