-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenai_conn.py
85 lines (70 loc) · 2.71 KB
/
openai_conn.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
import config as cfg
import requests
import io
import json
from logging import info, debug
OPENAI_TRANSCRIPTIONS_URL = 'https://api.openai.com/v1/audio/transcriptions'
OPENAI_CHAT_URL = 'https://api.openai.com/v1/chat/completions'
def audio2text(audio: io.BytesIO, voice_format: str) -> str:
debug('start')
headers = {'Authorization': f'Bearer {cfg.OPEN_AI_API_KEY}',
# 'Content-Type': 'multipart/form-data'
}
match voice_format:
case 'wav':
file_format = 'audio/wav'
case 'mp3':
file_format = 'audio/mpeg'
case _:
info(f'unknown audio format: {voice_format}')
assert False
files = [('file', (f'audio.{voice_format}', audio, file_format))]
data = {'model': 'whisper-1', 'language': 'ru'} # TODO get language from Telegram update language_code
response = requests.post(url=OPENAI_TRANSCRIPTIONS_URL,
headers=headers,
files=files,
data=data,
)
d = response.json()
if response.status_code == 200:
text = d.get('text')
if not text:
text = 'Не смог найти слова в аудио, сорян'
else:
text = f'Статус код ответа OpenAI: {response.status_code}'
if error_text := d.get('error', {}).get('message'):
text = f'{text}\nСообщение об ошибке от OpenAI: {error_text}'
debug('finish')
return text
# transcript = openai.Audio.transcribe("whisper-1", voice_wav, language='ru')
# return transcript.text
def chat(input_text: str, temperature: float = 1) -> str:
debug('start')
headers = {'Authorization': f'Bearer {cfg.OPEN_AI_API_KEY}',
'Content-Type': 'application/json'
}
data = json.dumps({
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": input_text,
}
],
"temperature": temperature
})
response = requests.post(url=OPENAI_CHAT_URL,
headers=headers,
data=data,
)
d = response.json()
if response.status_code == 200:
text = d.get('choices', [{}])[0].get('message', {}).get('content', '')
if not text:
text = 'Что-то не выходит нонче у Данилы каменный цветок ...'
else:
text = f'Статус код ответа OpenAI: {response.status_code}'
if error_text := d.get('error', {}).get('message'):
text = f'{text}\n\nСообщение об ошибке от OpenAI: {error_text}'
debug('finish')
return text