Repository navigation
Expand file tree
/
Copy path__init__.py
More file actions
87 lines (76 loc) · 2.93 KB
/
Copy path__init__.py
File metadata and controls
87 lines (76 loc) · 2.93 KB
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
from os import path
from time import sleep
# noinspection PyPackageRequirements
import whisper
from openai import (
OpenAI,
APIError,
OpenAIError,
ConflictError,
NotFoundError,
APIStatusError,
RateLimitError,
APITimeoutError,
BadRequestError,
APIConnectionError,
AuthenticationError,
InternalServerError,
PermissionDeniedError,
UnprocessableEntityError,
APIResponseValidationError,
)
import ai
from play.rules import RuleType
from settings.secrets import Secrets
from settings.settings import Settings
from utils.logging import log
secrets = Secrets(path.join(path.split(path.relpath(__file__))[0], 'secrets.yaml'))
settings = Settings(path.join(path.split(path.relpath(__file__))[0], 'settings.yaml'))
class Chat:
client = OpenAI(api_key=secrets.get('api_key'))
@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.completions.create(model=model,
messages=messages,
max_tokens=settings.get('chat.tokens'))
except RateLimitError as error:
log.error(f'{error}: Retrying in 60 seconds.')
sleep(60)
return Chat.send(prompt, session, character)
except (APIError, OpenAIError, ConflictError, NotFoundError, APIStatusError, APITimeoutError,
BadRequestError, APIConnectionError, AuthenticationError, InternalServerError, PermissionDeniedError,
UnprocessableEntityError, APIResponseValidationError) as error:
log.error(error)
return
try:
return completion.choices[0].message.content
except IndexError as error:
log.warning(error)
return
class Transcribe:
@staticmethod
def send(audio_path):
model = whisper.load_model(settings.get('transcribe.model'))
return model.transcribe(audio_path, fp16=False, word_timestamps=True)