|
| 1 | +from dataclasses import dataclass, field |
| 2 | +from typing import Optional, Dict, Any |
| 3 | + |
| 4 | + |
| 5 | +@dataclass |
| 6 | +class CacheConfig: |
| 7 | + ttl: int = 60 * 60 * 24 * 30 # 30 days |
| 8 | + maxsize: int = 1000 |
| 9 | + |
| 10 | + |
| 11 | +@dataclass |
| 12 | +class HeliconeSingleton: |
| 13 | + api_key: Optional[str] = None |
| 14 | + base_url: Optional[str] = "https://oai.helicone.ai" |
| 15 | + cache_config: Optional[CacheConfig] = None |
| 16 | + _instance: Optional['HeliconeSingleton'] = None |
| 17 | + |
| 18 | + # New fields for configurable headers |
| 19 | + target_url: Optional[str] = None |
| 20 | + openai_api_base: Optional[str] = None |
| 21 | + request_id: Optional[str] = None |
| 22 | + model_override: Optional[str] = None |
| 23 | + prompt_id: Optional[str] = None |
| 24 | + user_id: Optional[str] = None |
| 25 | + fallbacks: Optional[str] = None |
| 26 | + rate_limit_policy: Optional[str] = None |
| 27 | + session_id: Optional[str] = None |
| 28 | + session_path: Optional[str] = None |
| 29 | + session_name: Optional[str] = None |
| 30 | + posthog_key: Optional[str] = None |
| 31 | + posthog_host: Optional[str] = None |
| 32 | + omit_response: Optional[bool] = None |
| 33 | + omit_request: Optional[bool] = None |
| 34 | + cache_enabled: Optional[bool] = None |
| 35 | + retry_enabled: Optional[bool] = None |
| 36 | + moderations_enabled: Optional[bool] = None |
| 37 | + llm_security_enabled: Optional[bool] = None |
| 38 | + stream_force_format: Optional[bool] = None |
| 39 | + custom_properties: Dict[str, str] = field(default_factory=dict) |
| 40 | + |
| 41 | + def __new__(cls): |
| 42 | + if cls._instance is None: |
| 43 | + cls._instance = super().__new__(cls) |
| 44 | + return cls._instance |
| 45 | + |
| 46 | + def default_headers(self) -> Dict[str, Any]: |
| 47 | + headers = {"Helicone-Auth": f"Bearer {self.api_key}"} |
| 48 | + |
| 49 | + if self.target_url: |
| 50 | + headers["Helicone-Target-URL"] = self.target_url |
| 51 | + if self.openai_api_base: |
| 52 | + headers["Helicone-OpenAI-Api-Base"] = self.openai_api_base |
| 53 | + if self.request_id: |
| 54 | + headers["Helicone-Request-Id"] = self.request_id |
| 55 | + if self.model_override: |
| 56 | + headers["Helicone-Model-Override"] = self.model_override |
| 57 | + if self.prompt_id: |
| 58 | + headers["Helicone-Prompt-Id"] = self.prompt_id |
| 59 | + if self.user_id: |
| 60 | + headers["Helicone-User-Id"] = self.user_id |
| 61 | + if self.fallbacks: |
| 62 | + headers["Helicone-Fallbacks"] = self.fallbacks |
| 63 | + if self.rate_limit_policy: |
| 64 | + headers["Helicone-RateLimit-Policy"] = self.rate_limit_policy |
| 65 | + if self.session_id: |
| 66 | + headers["Helicone-Session-Id"] = self.session_id |
| 67 | + if self.session_path: |
| 68 | + headers["Helicone-Session-Path"] = self.session_path |
| 69 | + if self.session_name: |
| 70 | + headers["Helicone-Session-Name"] = self.session_name |
| 71 | + if self.posthog_key: |
| 72 | + headers["Helicone-Posthog-Key"] = self.posthog_key |
| 73 | + if self.posthog_host: |
| 74 | + headers["Helicone-Posthog-Host"] = self.posthog_host |
| 75 | + |
| 76 | + # Boolean headers |
| 77 | + for header, value in { |
| 78 | + "Helicone-Omit-Response": self.omit_response, |
| 79 | + "Helicone-Omit-Request": self.omit_request, |
| 80 | + "Helicone-Cache-Enabled": (self.cache_enabled and "true") or (self.cache_config.maxsize or self.cache_config.ttl and "true"), |
| 81 | + "Helicone-Retry-Enabled": self.retry_enabled, |
| 82 | + "Helicone-Moderations-Enabled": self.moderations_enabled, |
| 83 | + "Helicone-LLM-Security-Enabled": self.llm_security_enabled, |
| 84 | + "Helicone-Stream-Force-Format": self.stream_force_format |
| 85 | + }.items(): |
| 86 | + if value is not None: |
| 87 | + headers[header] = str(value).lower() |
| 88 | + |
| 89 | + # Custom properties |
| 90 | + for key, value in self.custom_properties.items(): |
| 91 | + headers[f"Helicone-Property-{key}"] = value |
| 92 | + |
| 93 | + return headers |
| 94 | + |
| 95 | + |
| 96 | +helicone_config = HeliconeSingleton() |
0 commit comments