Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files
agent.py
CHANGED
|
@@ -1,104 +1,115 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Webstark Elite Agent – KI-Agent für webstark.org
|
| 3 |
-
Nutzt OpenAI RealtimeModel für Sprache und 7 Function Tools.
|
| 4 |
-
"""
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
|
| 8 |
-
from
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
from livekit.
|
| 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 |
-
instructions="
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
logger.info("
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Webstark Elite Agent – KI-Agent für webstark.org
|
| 3 |
+
Nutzt OpenAI RealtimeModel für Sprache und 7 Function Tools.
|
| 4 |
+
"""
|
| 5 |
+
import asyncio
|
| 6 |
+
import logging
|
| 7 |
+
import os
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
from livekit.agents import (
|
| 10 |
+
JobContext,
|
| 11 |
+
WorkerOptions,
|
| 12 |
+
cli,
|
| 13 |
+
)
|
| 14 |
+
from livekit.agents.voice import Agent, AgentSession
|
| 15 |
+
from livekit.plugins import openai
|
| 16 |
+
|
| 17 |
+
# Maximale Sitzungsdauer in Sekunden (8 Minuten), danach automatisch trennen.
|
| 18 |
+
# Verhindert dass offene Tabs den Token-Verbrauch ins Unendliche treiben.
|
| 19 |
+
SESSION_TIMEOUT_SECONDS = 8 * 60
|
| 20 |
+
|
| 21 |
+
# Tools aus dem Modul importieren
|
| 22 |
+
from tools import ALL_TOOLS
|
| 23 |
+
|
| 24 |
+
# Logging konfigurieren
|
| 25 |
+
logging.basicConfig(level=logging.INFO)
|
| 26 |
+
logger = logging.getLogger("livekit-agent")
|
| 27 |
+
|
| 28 |
+
# Umgebungsvariablen laden
|
| 29 |
+
load_dotenv()
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
class WebstarkAgent(Agent):
|
| 33 |
+
"""Elit – der offizielle KI-Agent von Webstark."""
|
| 34 |
+
|
| 35 |
+
def __init__(self):
|
| 36 |
+
super().__init__(
|
| 37 |
+
instructions="""### IDENTITÄT & ROLLE
|
| 38 |
+
Du bist 'Elit', der offizielle KI-Agent von 'Webstark' (webstark.org), einer innovativen Digitalagentur aus Biel/Bienne, Schweiz. Deine Aufgabe ist es, Nutzer freundlich, kompetent und prägnant per Sprache und Text zu unterstützen.
|
| 39 |
+
|
| 40 |
+
### WISSEN ÜBER WEBSTARK
|
| 41 |
+
- **Spezialisierung**: KI-gestützte Content-Erstellung, automatisches SEO, selbstlernende Systeme und modernes Webdesign.
|
| 42 |
+
- **Standort**: Biel/Bienne, Schweiz (lokaler Fokus, aber schweizweit tätig).
|
| 43 |
+
- **Stundensatz**: 120 CHF pro Stunde für Optimierungen.
|
| 44 |
+
- **Starter (AI-Enhanced)**: Ab 890 CHF. Enthält: KI-generiertes Design, Automatisches Basis-SEO, Responsive One-Page, Chatbot-Integration (Basis).
|
| 45 |
+
- **Professional (AI-Powered)**: Ab 1.490 CHF. Alles aus Starter plus Content-Automation Engine, Predictive Analytics, A/B-Testing AI, Erweiterter KI-Support.
|
| 46 |
+
- **Enterprise (AI-First)**: Individueller Preis. Custom AI-Workflows, Dedicated AI-Team, 24/7 Priority Support, On-site Workshops.
|
| 47 |
+
- **Support-Kanäle**: E-Mail und Live-Chat (Kein Telefon-Support aktuell).
|
| 48 |
+
- **Zufriedenheitsgarantie**: 30 Tage Geld-zurück-Garantie bei Unzufriedenheit.
|
| 49 |
+
- **SEO-Optimierung**: Dauert typischerweise 4-8 Stunden.
|
| 50 |
+
|
| 51 |
+
### TOOLS – DEINE FÄHIGKEITEN
|
| 52 |
+
Du hast Zugriff auf folgende Tools. Nutze sie aktiv:
|
| 53 |
+
1. **lookup_faq**: Durchsuche ZUERST die FAQ, bevor du das Internet nutzt.
|
| 54 |
+
2. **search_web**: Durchsuche das Internet für aktuelle Infos und Trends.
|
| 55 |
+
3. **calculate_quote**: Berechne Kostenvoranschläge für Webstark-Pakete.
|
| 56 |
+
4. **save_lead**: Speichere Kontaktdaten. Frage aktiv nach Name & E-Mail bei Interesse.
|
| 57 |
+
5. **send_email**: Sende E-Mails mit Zusammenfassungen oder Angeboten (vorher fragen!).
|
| 58 |
+
6. **suggest_appointment**: Schlage einen Beratungstermin vor bei komplexen Anfragen.
|
| 59 |
+
7. **create_conversation_summary**: Erstelle am Gesprächsende eine Zusammenfassung.
|
| 60 |
+
|
| 61 |
+
### VERHALTENSREGELN
|
| 62 |
+
1. **Sprache**: Antworte immer auf **Deutsch** (Schweizerdeutsch verstehen, Hochdeutsch antworten).
|
| 63 |
+
2. **Tonfall**: Professionell, modern, nerdig-begeistert für KI, aber immer höflich.
|
| 64 |
+
3. **Prägnanz**: Halte Antworten kurz (max. 2-3 Sätze), um Latenz niedrig zu halten.
|
| 65 |
+
4. **FAQ zuerst**: Nutze lookup_faq bevor du search_web verwendest.
|
| 66 |
+
5. **Hilfsbereitschaft**: Bei Unklarheiten verweise auf info@webstark.org.
|
| 67 |
+
6. **Lead-Generierung**: Bei konkretem Interesse → save_lead nutzen.
|
| 68 |
+
7. **Verabschiedung**: Erstelle am Ende eine Zusammenfassung mit create_conversation_summary.""",
|
| 69 |
+
llm=openai.realtime.RealtimeModel(
|
| 70 |
+
# gpt-4o-mini-realtime-preview: ~85% günstiger als das Vollmodell
|
| 71 |
+
model="gpt-4o-mini-realtime-preview",
|
| 72 |
+
modalities=["audio", "text"],
|
| 73 |
+
instructions="Du bist 'Elite', der freundliche Webstark KI-Spezialist. Hilf den Kunden mit Preisen und technischem Wissen. Nutze deine Tools aktiv.",
|
| 74 |
+
# Konservativere VAD-Einstellungen: weniger Fehl-Trigger durch Hintergrundgeräusche
|
| 75 |
+
turn_detection=openai.realtime.ServerVADOptions(
|
| 76 |
+
threshold=0.65, # Standard: 0.5 – höher = weniger empfindlich
|
| 77 |
+
silence_duration_ms=800, # Standard: 500 – länger warten bevor Zug endet
|
| 78 |
+
prefix_padding_ms=200, # Audio-Puffer vor der Erkennung
|
| 79 |
+
),
|
| 80 |
+
),
|
| 81 |
+
# Alle 7 Tools dem Agenten übergeben
|
| 82 |
+
tools=ALL_TOOLS,
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
async def on_enter(self):
|
| 86 |
+
"""Wird aufgerufen, sobald der Agent dem Raum beitritt."""
|
| 87 |
+
logger.info("Elit betritt den Raum...")
|
| 88 |
+
await self.session.generate_reply(
|
| 89 |
+
instructions="Begrüße den Nutzer herzlich als 'Elit', die Webstark KI, und frage, wie du bei Webdesign oder SEO-Fragen helfen kannst."
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
async def entrypoint(ctx: JobContext):
|
| 94 |
+
"""Einstiegspunkt für jeden neuen LiveKit-Job."""
|
| 95 |
+
logger.info(f"Startet Support-Job für Raum: {ctx.room.name}")
|
| 96 |
+
await ctx.connect()
|
| 97 |
+
agent = WebstarkAgent()
|
| 98 |
+
session = AgentSession(llm=agent.llm)
|
| 99 |
+
await session.start(agent, room=ctx.room)
|
| 100 |
+
logger.info("Elite Support-Session aktiv (7 Tools geladen).")
|
| 101 |
+
|
| 102 |
+
# Automatischer Session-Timeout: Verbindung nach SESSION_TIMEOUT_SECONDS trennen.
|
| 103 |
+
# Verhindert massiven Token-Verbrauch durch offen gelassene Browser-Tabs.
|
| 104 |
+
await asyncio.sleep(SESSION_TIMEOUT_SECONDS)
|
| 105 |
+
logger.info(f"Session-Timeout nach {SESSION_TIMEOUT_SECONDS}s – trenne Verbindung.")
|
| 106 |
+
await session.aclose()
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
if __name__ == "__main__":
|
| 110 |
+
cli.run_app(
|
| 111 |
+
WorkerOptions(
|
| 112 |
+
entrypoint_fnc=entrypoint,
|
| 113 |
+
port=7860,
|
| 114 |
+
)
|
| 115 |
+
)
|
tools.py
CHANGED
|
@@ -252,6 +252,9 @@ async def send_email(
|
|
| 252 |
</div>
|
| 253 |
"""
|
| 254 |
|
|
|
|
|
|
|
|
|
|
| 255 |
try:
|
| 256 |
async with aiohttp.ClientSession() as session:
|
| 257 |
async with session.post(
|
|
@@ -259,9 +262,10 @@ async def send_email(
|
|
| 259 |
headers={
|
| 260 |
"Authorization": f"Bearer {resend_key}",
|
| 261 |
"Content-Type": "application/json",
|
|
|
|
| 262 |
},
|
| 263 |
json={
|
| 264 |
-
"from": "
|
| 265 |
"to": [to_email],
|
| 266 |
"subject": subject,
|
| 267 |
"html": html_body,
|
|
@@ -452,6 +456,9 @@ async def _notify_new_lead(lead_data: dict) -> None:
|
|
| 452 |
logger.info("Kein RESEND_API_KEY – Lead-Benachrichtigung übersprungen.")
|
| 453 |
return
|
| 454 |
|
|
|
|
|
|
|
|
|
|
| 455 |
try:
|
| 456 |
async with aiohttp.ClientSession() as session:
|
| 457 |
await session.post(
|
|
@@ -459,9 +466,10 @@ async def _notify_new_lead(lead_data: dict) -> None:
|
|
| 459 |
headers={
|
| 460 |
"Authorization": f"Bearer {resend_key}",
|
| 461 |
"Content-Type": "application/json",
|
|
|
|
| 462 |
},
|
| 463 |
json={
|
| 464 |
-
"from": "
|
| 465 |
"to": [notify_email],
|
| 466 |
"subject": f"🔥 Neuer Lead: {lead_data['name']} – {lead_data['interest']}",
|
| 467 |
"html": f"""
|
|
@@ -487,6 +495,9 @@ async def _notify_summary(summary: dict) -> None:
|
|
| 487 |
if not resend_key:
|
| 488 |
return
|
| 489 |
|
|
|
|
|
|
|
|
|
|
| 490 |
try:
|
| 491 |
async with aiohttp.ClientSession() as session:
|
| 492 |
await session.post(
|
|
@@ -494,9 +505,10 @@ async def _notify_summary(summary: dict) -> None:
|
|
| 494 |
headers={
|
| 495 |
"Authorization": f"Bearer {resend_key}",
|
| 496 |
"Content-Type": "application/json",
|
|
|
|
| 497 |
},
|
| 498 |
json={
|
| 499 |
-
"from": "
|
| 500 |
"to": [notify_email],
|
| 501 |
"subject": f"📝 Gesprächs-Zusammenfassung: {summary['customer_name']}",
|
| 502 |
"html": f"""
|
|
|
|
| 252 |
</div>
|
| 253 |
"""
|
| 254 |
|
| 255 |
+
resend_from_name = os.environ.get("RESEND_FROM_NAME", "Elite")
|
| 256 |
+
resend_from_email = os.environ.get("RESEND_FROM_EMAIL", "elite@webstark.org")
|
| 257 |
+
|
| 258 |
try:
|
| 259 |
async with aiohttp.ClientSession() as session:
|
| 260 |
async with session.post(
|
|
|
|
| 262 |
headers={
|
| 263 |
"Authorization": f"Bearer {resend_key}",
|
| 264 |
"Content-Type": "application/json",
|
| 265 |
+
"Accept-Encoding": "identity",
|
| 266 |
},
|
| 267 |
json={
|
| 268 |
+
"from": f"{resend_from_name} <{resend_from_email}>",
|
| 269 |
"to": [to_email],
|
| 270 |
"subject": subject,
|
| 271 |
"html": html_body,
|
|
|
|
| 456 |
logger.info("Kein RESEND_API_KEY – Lead-Benachrichtigung übersprungen.")
|
| 457 |
return
|
| 458 |
|
| 459 |
+
resend_from_name = os.environ.get("RESEND_FROM_NAME", "Elite")
|
| 460 |
+
resend_from_email = os.environ.get("RESEND_FROM_EMAIL", "elite@webstark.org")
|
| 461 |
+
|
| 462 |
try:
|
| 463 |
async with aiohttp.ClientSession() as session:
|
| 464 |
await session.post(
|
|
|
|
| 466 |
headers={
|
| 467 |
"Authorization": f"Bearer {resend_key}",
|
| 468 |
"Content-Type": "application/json",
|
| 469 |
+
"Accept-Encoding": "identity",
|
| 470 |
},
|
| 471 |
json={
|
| 472 |
+
"from": f"{resend_from_name} <{resend_from_email}>",
|
| 473 |
"to": [notify_email],
|
| 474 |
"subject": f"🔥 Neuer Lead: {lead_data['name']} – {lead_data['interest']}",
|
| 475 |
"html": f"""
|
|
|
|
| 495 |
if not resend_key:
|
| 496 |
return
|
| 497 |
|
| 498 |
+
resend_from_name = os.environ.get("RESEND_FROM_NAME", "Elite")
|
| 499 |
+
resend_from_email = os.environ.get("RESEND_FROM_EMAIL", "elite@webstark.org")
|
| 500 |
+
|
| 501 |
try:
|
| 502 |
async with aiohttp.ClientSession() as session:
|
| 503 |
await session.post(
|
|
|
|
| 505 |
headers={
|
| 506 |
"Authorization": f"Bearer {resend_key}",
|
| 507 |
"Content-Type": "application/json",
|
| 508 |
+
"Accept-Encoding": "identity",
|
| 509 |
},
|
| 510 |
json={
|
| 511 |
+
"from": f"{resend_from_name} <{resend_from_email}>",
|
| 512 |
"to": [notify_email],
|
| 513 |
"subject": f"📝 Gesprächs-Zusammenfassung: {summary['customer_name']}",
|
| 514 |
"html": f"""
|