-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprachassistent.py
126 lines (106 loc) · 4.13 KB
/
sprachassistent.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import dotenv
from openai import OpenAI
import dotenv
from gtts import gTTS
import speech_recognition as sr
import os
import json
from tools import tools, get_current_date, get_weather, get_ip, location, crawl4ai, search_duckduckgo, search_wikipedia
# Load environment variables
dotenv.load_dotenv()
api_key = os.getenv('api_key')
api_endpoint = os.getenv('api_endpoint')
def init_openai_client():
return OpenAI(base_url=api_endpoint, api_key=api_key)
def record_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Bitte sprechen Sie jetzt...")
return r.listen(source)
def get_ai_response(client, user_input):
response = client.chat.completions.create(
model="mixtral-8x7b-32768",
messages=[
{
"role": "system",
"content": "Du bist ein hilfsbereiter Assistent. Antworte stets auf Deutsch. "
"Halte dich bitte immer so kurz und präzise wie möglich. Bitte nutze keine Emojis. Gebe immer genaue Informationen, es geht um die Kariere des Nutzers."
"Nutze die verfügbaren Funktionen für Datum/Uhrzeit, Wetterabfragen, IP-Adressen und Standortinformationen."
},
{"role": "user", "content": user_input}
],
tools=tools,
tool_choice="auto"
)
message = response.choices[0].message
# Check if the model wants to call functions
if message.tool_calls:
# Create a list to store function responses
function_messages = []
for tool_call in message.tool_calls:
function_name = tool_call.function.name
try:
function_args = json.loads(tool_call.function.arguments)
except:
function_args = {}
# Execute the function
if function_name == "get_current_date":
result = get_current_date()
elif function_name == "get_weather":
result = get_weather(**function_args)
elif function_name == "get_ip":
result = get_ip()
elif function_name == "location":
result = location()
elif function_name == "crawl4ai":
result = crawl4ai()
elif function_name == "search_duckduckgo":
result = search_duckduckgo(**function_args)
elif function_name == "search_wikipedia":
result = search_wikipedia(**function_args)
else:
continue
function_messages.append({
"tool_call_id": tool_call.id,
"role": "tool",
"name": function_name,
"content": json.dumps(result)
})
# Get the final response
second_response = client.chat.completions.create(
model="mixtral-8x7b-32768",
messages=[
{
"role": "system",
"content": "Du bist ein hilfsbereiter Assistent. Antworte stets auf Deutsch. "
"Halte dich bitte immer so kurz und präzise wie möglich. Bitte nutze keine Emojis."
},
{"role": "user", "content": user_input},
message,
*function_messages
]
)
return second_response
return response
def text_to_speech(text):
tts = gTTS(text=text, lang='de')
tts.save("response.mp3")
os.system("mpg321 response.mp3")
def main():
client = init_openai_client()
while True:
print("Ich höre zu...")
try:
audio = record_audio()
r = sr.Recognizer()
user_input = r.recognize_google(audio, language='de-DE')
print("Sie sagten: " + user_input)
response = get_ai_response(client, user_input)
response_text = response.choices[0].message.content
print(response_text)
text_to_speech(response_text)
except sr.UnknownValueError:
print("Entschuldigung, ich konnte Sie nicht verstehen")
print("Bereit für die nächste Eingabe...")
if __name__ == "__main__":
main()