-
Notifications
You must be signed in to change notification settings - Fork 1
/
assistant.py
64 lines (52 loc) · 2.75 KB
/
assistant.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
import openai
from elevenlabs import play
from elevenlabs.client import ElevenLabs
openai.api_key = 'PUT_YOUR_API_KEY_HERE'
elevenlabs = ElevenLabs(
api_key="PUT_YOUR_API_KEY_HERE" # Defaults to ELEVEN_API_KEY
)
def interact_with_assistant():
message_history = [{
"role": "system",
"content": "You are an AI assistant tasked with having a friendly conversation with a user. Your goal is to engage the user, ask relevant questions, and keep the conversation flowing in a natural way while being helpful and answering the user's questions to the best of your knowledge."
}]
while True:
user_input = input("Ваше сообщение: ")
if user_input.lower() == "quit":
break
elif user_input.lower() == "clear":
# Очищаем историю, оставляем только системное сообщение
message_history = [{
"role": "system",
"content": "You are an AI assistant tasked with having a friendly conversation with a user. Your goal is to engage the user, ask relevant questions, and keep the conversation flowing in a natural way while being helpful and answering the user's questions to the best of your knowledge."
}]
print("\033[92m" + "История сообщений очищена." + "\033[0m")
continue
# Добавляем сообщение пользователя в историю
message_history.append({
"role": "user",
"content": user_input
})
# Создаём запрос к модели GPT-4o
response = openai.ChatCompletion.create(
model="gpt-4o",
messages=message_history
)
# Получаем ответ ассистента и добавляем его в историю
assistant_reply = response['choices'][0]['message']['content']
print("\033[93m" + "Ассистент: " + assistant_reply + "\033[0m")
message_history.append({
"role": "assistant",
"content": assistant_reply
})
# Если сообщений стало больше 20, удаляем самые старые
if len(message_history) > 20:
message_history = message_history[-20:]
# Генерация аудио из текста и воспроизведение
audio_stream = elevenlabs.generate(text=assistant_reply,
voice="Rachel",
model="eleven_multilingual_v2",
stream=True)
play(audio_stream)
# Запускаем диалог с ассистентом
interact_with_assistant()