Spaces:
Sleeping
Sleeping
| """Realtime asistan icin tool implementasyonlari. | |
| WhatsApp BF-WAB'da kanitlanmis smart_warehouse_with_price.py'yi kullanir. | |
| """ | |
| from __future__ import annotations | |
| import logging | |
| import re | |
| from smart_warehouse_with_price import get_warehouse_stock_smart_with_price | |
| logger = logging.getLogger(__name__) | |
| # ---------- Telaffuz duzeltmeleri ---------- | |
| def apply_pronunciation_fixes(text: str) -> str: | |
| if not isinstance(text, str): | |
| return text | |
| return text.replace("Caddebostan", "Cadde Bostan") | |
| # ---------- URL'leri tool sonucundan strip ---------- | |
| def strip_urls(text: str) -> str: | |
| """Modelin URL okumamasi icin tool result'undan link satirlari kaldirilir.""" | |
| if not text: | |
| return text | |
| text = re.sub(r"(?im)^\s*(?:Link|URL|Url|๐.*?):.*$", "", text) | |
| text = re.sub(r"https?://\S+", "", text) | |
| text = re.sub(r"\n{3,}", "\n\n", text).strip() | |
| return text | |
| # ---------- OpenAI realtime tool definition ---------- | |
| TOOLS = [ | |
| { | |
| "type": "function", | |
| "name": "get_warehouse_stock", | |
| "description": ( | |
| "Trek bisiklet, aksesuar veya yedek parca icin magaza stok durumu, " | |
| "fiyat ve urun linkini getirir. Musteri stok, fiyat veya urun " | |
| "varligini sordugunda kullan." | |
| ), | |
| "parameters": { | |
| "type": "object", | |
| "properties": { | |
| "user_message": { | |
| "type": "string", | |
| "description": ( | |
| "Musterinin urun/stok sorusu " | |
| "(orn. 'Madone SLR 9 var mi', 'Marlin 5 fiyat')" | |
| ), | |
| } | |
| }, | |
| "required": ["user_message"], | |
| }, | |
| } | |
| ] | |
| def handle_tool_call_sync(name: str, arguments: dict) -> str: | |
| """Tool dispatcher. Sync โ realtime relay'de to_thread ile sarilacak.""" | |
| try: | |
| if name == "get_warehouse_stock": | |
| msg = arguments.get("user_message", "") | |
| logger.info(f"[tool] get_warehouse_stock query: {msg!r}") | |
| result = get_warehouse_stock_smart_with_price(msg) | |
| if result is None: | |
| return "Stok bilgisi bulunamadi." | |
| return apply_pronunciation_fixes(str(result)) | |
| return f"Bilinmeyen fonksiyon: {name}" | |
| except Exception as e: | |
| logger.exception(f"Tool call hatasi ({name})") | |
| return f"Hata: {e}" | |