-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathconfig.py
More file actions
81 lines (66 loc) · 3.45 KB
/
config.py
File metadata and controls
81 lines (66 loc) · 3.45 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
72
73
74
75
76
77
78
79
80
81
import os
import sys
# Configuration
class Config:
def __init__(self):
self.openai_api_key = os.environ.get("OPENAI_API_KEY")
if not self.openai_api_key:
raise ValueError("OPENAI_API_KEY not found in environment variables")
# Add Anthropic API key for client validation
self.anthropic_api_key = os.environ.get("ANTHROPIC_API_KEY")
if not self.anthropic_api_key:
print("Warning: ANTHROPIC_API_KEY not set. Client API key validation will be disabled.")
self.openai_base_url = os.environ.get("OPENAI_BASE_URL", "https://api.openai.com/v1")
self.azure_api_version = os.environ.get("AZURE_API_VERSION") # For Azure OpenAI
self.host = os.environ.get("HOST", "0.0.0.0")
self.port = int(os.environ.get("PORT", "8082"))
self.log_level = os.environ.get("LOG_LEVEL", "INFO")
self.max_tokens_limit = int(os.environ.get("MAX_TOKENS_LIMIT", "4096"))
self.min_tokens_limit = int(os.environ.get("MIN_TOKENS_LIMIT", "100"))
# Connection settings
self.request_timeout = int(os.environ.get("REQUEST_TIMEOUT", "90"))
self.max_retries = int(os.environ.get("MAX_RETRIES", "2"))
# Model settings - BIG and SMALL models
self.big_model = os.environ.get("BIG_MODEL", "gpt-4o")
self.middle_model = os.environ.get("MIDDLE_MODEL", self.big_model)
self.small_model = os.environ.get("SMALL_MODEL", "gpt-4o-mini")
# Tooling API settings
self.tooling_api = os.environ.get("TOOLING_API", "openai").lower() # "openai" or "kosong"
self.max_tools_limit = int(os.environ.get("MAX_TOOLS_LIMIT", "100")) # Maximum tools to send to API
def validate_api_key(self):
"""Basic API key validation"""
if not self.openai_api_key:
return False
# Basic format check for OpenAI API keys
if not self.openai_api_key.startswith('sk-'):
return False
return True
def validate_client_api_key(self, client_api_key):
"""Validate client's Anthropic API key"""
# If no ANTHROPIC_API_KEY is set in environment, skip validation
if not self.anthropic_api_key:
return True
# Check if the client's API key matches the expected value
return client_api_key == self.anthropic_api_key
def get_custom_headers(self):
"""Get custom headers from environment variables"""
custom_headers = {}
# Get all environment variables
env_vars = dict(os.environ)
# Find CUSTOM_HEADER_* environment variables
for env_key, env_value in env_vars.items():
if env_key.startswith('CUSTOM_HEADER_'):
# Convert CUSTOM_HEADER_KEY to Header-Key
# Remove 'CUSTOM_HEADER_' prefix and convert to header format
header_name = env_key[14:] # Remove 'CUSTOM_HEADER_' prefix
if header_name: # Make sure it's not empty
# Convert underscores to hyphens for HTTP header format
header_name = header_name.replace('_', '-')
custom_headers[header_name] = env_value
return custom_headers
try:
config = Config()
print(f" Configuration loaded: API_KEY={'*' * 20}..., BASE_URL='{config.openai_base_url}'")
except Exception as e:
print(f"=4 Configuration Error: {e}")
sys.exit(1)