Spaces:
Runtime error
Runtime error
Download services/llm.py from Smartwaylabs/pipecat-livekit: direct link, hf CLI and curl.
- Browser
- Download file 6.22 kB
-
https://huggingface.co/spaces/Smartwaylabs/pipecat-livekit/resolve/main/services/llm.py
- Command line
-
hf download hf://spaces/Smartwaylabs/pipecat-livekit/services/llm.py
-
curl -L -o llm.py https://huggingface.co/spaces/Smartwaylabs/pipecat-livekit/resolve/main/services/llm.py
6.22 kB
| """ | |
| LLM service factory. | |
| Creates Large Language Model services with provider selection support. | |
| Supported providers: OpenAI (default), Anthropic, Google, Groq. | |
| """ | |
| import os | |
| from typing import Optional, Union | |
| from loguru import logger | |
| from pipecat.processors.metrics.sentry import SentryMetrics | |
| from pipecat.services.openai.llm import OpenAILLMService | |
| # Try to import optional LLM providers | |
| try: | |
| from pipecat.services.anthropic.llm import AnthropicLLMService | |
| ANTHROPIC_AVAILABLE = True | |
| except ImportError: | |
| AnthropicLLMService = None | |
| ANTHROPIC_AVAILABLE = False | |
| try: | |
| from pipecat.services.google.llm import GoogleLLMService | |
| GOOGLE_LLM_AVAILABLE = True | |
| except ImportError: | |
| GoogleLLMService = None | |
| GOOGLE_LLM_AVAILABLE = False | |
| try: | |
| from pipecat.services.groq.llm import GroqLLMService | |
| GROQ_AVAILABLE = True | |
| except ImportError: | |
| GroqLLMService = None | |
| GROQ_AVAILABLE = False | |
| # Default models per provider | |
| DEFAULT_OPENAI_MODEL = "gpt-4.1-mini" | |
| DEFAULT_ANTHROPIC_MODEL = "claude-3-5-sonnet-20241022" | |
| DEFAULT_GOOGLE_MODEL = "gemini-2.0-flash-exp" | |
| DEFAULT_GROQ_MODEL = "llama-3.3-70b-versatile" | |
| LLMService = Union[ | |
| OpenAILLMService, | |
| "AnthropicLLMService", | |
| "GoogleLLMService", | |
| "GroqLLMService", | |
| ] | |
| def create_llm_service( | |
| provider: str = "openai", | |
| model: Optional[str] = None, | |
| temperature: float = 0.2, | |
| api_key: Optional[str] = None, | |
| ) -> LLMService: | |
| """ | |
| Create an LLM service instance based on provider. | |
| Args: | |
| provider: LLM provider name (openai, anthropic, google, groq) | |
| model: Model name (uses provider default if not specified) | |
| temperature: Sampling temperature (0.0-2.0) | |
| api_key: API key (defaults to provider-specific env var) | |
| Returns: | |
| Configured LLM service instance | |
| Raises: | |
| ValueError: If provider is not supported or API key is missing | |
| """ | |
| provider_lower = provider.lower() | |
| if provider_lower == "openai": | |
| return _create_openai_llm(model, temperature, api_key) | |
| elif provider_lower == "anthropic": | |
| return _create_anthropic_llm(model, temperature, api_key) | |
| elif provider_lower == "google": | |
| return _create_google_llm(model, temperature, api_key) | |
| elif provider_lower == "groq": | |
| return _create_groq_llm(model, temperature, api_key) | |
| else: | |
| raise ValueError( | |
| f"Unsupported LLM provider: {provider}. " | |
| f"Supported: openai, anthropic, google, groq" | |
| ) | |
| def _create_openai_llm( | |
| model: Optional[str] = None, | |
| temperature: float = 0.2, | |
| api_key: Optional[str] = None, | |
| ) -> OpenAILLMService: | |
| """Create OpenAI LLM service.""" | |
| key = api_key or os.environ.get("OPENAI_API_KEY") | |
| if not key: | |
| raise ValueError("OPENAI_API_KEY environment variable is required") | |
| model_name = model or DEFAULT_OPENAI_MODEL | |
| llm = OpenAILLMService( | |
| api_key=key, | |
| model=model_name, | |
| metrics=SentryMetrics(), | |
| params=OpenAILLMService.InputParams(temperature=temperature), | |
| ) | |
| logger.info(f"LLM service created [provider=OpenAI, model={model_name}, temperature={temperature}]") | |
| return llm | |
| def _create_anthropic_llm( | |
| model: Optional[str] = None, | |
| temperature: float = 0.7, | |
| api_key: Optional[str] = None, | |
| ) -> "AnthropicLLMService": | |
| """Create Anthropic Claude LLM service.""" | |
| if not ANTHROPIC_AVAILABLE: | |
| raise ValueError( | |
| "Anthropic LLM not available. Install with: pip install pipecat-ai[anthropic]" | |
| ) | |
| key = api_key or os.environ.get("ANTHROPIC_API_KEY") | |
| if not key: | |
| raise ValueError("ANTHROPIC_API_KEY environment variable is required") | |
| model_name = model or DEFAULT_ANTHROPIC_MODEL | |
| llm = AnthropicLLMService( | |
| api_key=key, | |
| model=model_name, | |
| params=AnthropicLLMService.InputParams( | |
| temperature=temperature, | |
| max_tokens=1024, | |
| ), | |
| ) | |
| logger.info(f"LLM service created [provider=Anthropic, model={model_name}, temperature={temperature}]") | |
| return llm | |
| def _create_google_llm( | |
| model: Optional[str] = None, | |
| temperature: float = 0.8, | |
| api_key: Optional[str] = None, | |
| ) -> "GoogleLLMService": | |
| """Create Google Gemini LLM service.""" | |
| if not GOOGLE_LLM_AVAILABLE: | |
| raise ValueError( | |
| "Google LLM not available. Install with: pip install pipecat-ai[google]" | |
| ) | |
| key = api_key or os.environ.get("GOOGLE_API_KEY") | |
| if not key: | |
| raise ValueError("GOOGLE_API_KEY environment variable is required") | |
| model_name = model or DEFAULT_GOOGLE_MODEL | |
| llm = GoogleLLMService( | |
| api_key=key, | |
| model=model_name, | |
| params=GoogleLLMService.InputParams( | |
| temperature=temperature, | |
| max_output_tokens=1000, | |
| ), | |
| ) | |
| logger.info(f"LLM service created [provider=Google, model={model_name}, temperature={temperature}]") | |
| return llm | |
| def _create_groq_llm( | |
| model: Optional[str] = None, | |
| temperature: float = 0.2, | |
| api_key: Optional[str] = None, | |
| ) -> "GroqLLMService": | |
| """Create Groq LLM service (ultra-fast inference).""" | |
| if not GROQ_AVAILABLE: | |
| raise ValueError( | |
| "Groq LLM not available. Install with: pip install pipecat-ai[groq]" | |
| ) | |
| key = api_key or os.environ.get("GROQ_API_KEY") | |
| if not key: | |
| raise ValueError("GROQ_API_KEY environment variable is required") | |
| model_name = model or DEFAULT_GROQ_MODEL | |
| llm = GroqLLMService( | |
| api_key=key, | |
| model=model_name, | |
| ) | |
| logger.info(f"LLM service created [provider=Groq, model={model_name}]") | |
| return llm | |
| def get_available_llm_providers() -> list[str]: | |
| """Get list of available LLM providers based on installed packages and env vars.""" | |
| providers = [] | |
| if os.environ.get("OPENAI_API_KEY"): | |
| providers.append("openai") | |
| if ANTHROPIC_AVAILABLE and os.environ.get("ANTHROPIC_API_KEY"): | |
| providers.append("anthropic") | |
| if GOOGLE_LLM_AVAILABLE and os.environ.get("GOOGLE_API_KEY"): | |
| providers.append("google") | |
| if GROQ_AVAILABLE and os.environ.get("GROQ_API_KEY"): | |
| providers.append("groq") | |
| return providers | |