-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy_config.py
More file actions
71 lines (57 loc) · 2.15 KB
/
proxy_config.py
File metadata and controls
71 lines (57 loc) · 2.15 KB
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
"""
Proxy configuration for selective routing of LangSmith SDK requests
"""
import os
from typing import Optional, Dict
from dataclasses import dataclass
@dataclass(frozen=True)
class ProxyConfig:
"""Configuration for proxy settings"""
http_proxy: Optional[str] = None
https_proxy: Optional[str] = None
no_proxy: Optional[str] = None
enabled: bool = False
@classmethod
def from_env(cls) -> "ProxyConfig":
"""
Create proxy configuration from environment variables
Environment variables:
- LANGSMITH_PROXY_ENABLED: Set to 'true' to enable proxy
- LANGSMITH_HTTP_PROXY: HTTP proxy URL
- LANGSMITH_HTTPS_PROXY: HTTPS proxy URL
- LANGSMITH_NO_PROXY: Comma-separated list of hosts to bypass
Example:
export LANGSMITH_PROXY_ENABLED=true
export LANGSMITH_HTTP_PROXY=http://proxy.example.com:8080
export LANGSMITH_HTTPS_PROXY=https://proxy.example.com:8080
export LANGSMITH_NO_PROXY=localhost,127.0.0.1
"""
enabled = os.getenv("LANGSMITH_PROXY_ENABLED", "false").lower() == "true"
return cls(
http_proxy=os.getenv("LANGSMITH_HTTP_PROXY"),
https_proxy=os.getenv("LANGSMITH_HTTPS_PROXY"),
no_proxy=os.getenv("LANGSMITH_NO_PROXY"),
enabled=enabled
)
def to_dict(self) -> Dict[str, str]:
"""Convert to dictionary for requests.Session.proxies"""
if not self.enabled:
return {}
proxies = {}
if self.http_proxy:
proxies["http"] = self.http_proxy
if self.https_proxy:
proxies["https"] = self.https_proxy
return proxies
def __repr__(self) -> str:
if not self.enabled:
return "ProxyConfig(disabled)"
return (
f"ProxyConfig(enabled=True, "
f"http={self.http_proxy}, "
f"https={self.https_proxy}, "
f"no_proxy={self.no_proxy})"
)
def get_proxy_config() -> ProxyConfig:
"""Get the current proxy configuration"""
return ProxyConfig.from_env()