2020# Environment Variables
2121# --------------------------------------------------
2222MODEL_TYPE = os .getenv ("MODEL_TYPE" , "OpenAI" )
23- OPENAI_MODEL_ID = os .getenv ("OPENAI_MODEL_ID" , "gemini-2.5-flash" )
24- OPENAI_BASE_URL = os .getenv ("LLM_BASE_URL" ) or os .getenv ("OPENAI_BASE_URL" )
23+ OPENAI_MODEL_ID = os .getenv ("LLM_MODEL" ) or os .getenv ("OPENAI_MODEL_ID" , "gateway-managed" )
2524LLM_GATEWAY_URL = os .getenv ("LLM_GATEWAY_URL" , "https://portfolio-llm-gateway.onrender.com/v1" ).strip ()
2625LLM_GATEWAY_TIMEOUT = float (os .getenv ("LLM_GATEWAY_TIMEOUT" , "180" ))
2726# Demo/portfolio gateways commonly rate-limit bursts. Avoid immediate SDK retries
2827# that amplify a 429; make the value configurable for production.
29- LLM_GATEWAY_MAX_RETRIES = max (0 , int (os .getenv ("LLM_GATEWAY_MAX_RETRIES" , "2 " )))
28+ LLM_GATEWAY_MAX_RETRIES = max (0 , int (os .getenv ("LLM_GATEWAY_MAX_RETRIES" , "0 " )))
3029VLLM_BASE_URL = os .getenv ("VLLM_BASE_URL" )
3130VLLM_CHAT_MODEL_ID = os .getenv ("VLLM_CHAT_MODEL_ID" )
3231VLLM_API_KEY = os .getenv ("VLLM_API_KEY" ) or "local"
33- DIRECT_PROVIDER = os .getenv ("LLM_DIRECT_PROVIDER" , "google" ).strip ().lower ()
34- GOOGLE_OPENAI_BASE_URL = os .getenv ("GOOGLE_OPENAI_BASE_URL" , "https://generativelanguage.googleapis.com/v1beta/openai/" ).strip ()
35- GOOGLE_API_KEY = os .getenv ("GOOGLE_API_KEY" , "" ).strip ()
3632# --------------------------------------------------
3733# Message Normalization (vLLM compatibility)
3834# --------------------------------------------------
@@ -92,80 +88,47 @@ def _gateway_config(self):
9288
9389 def get_client (self ):
9490 token , gateway_url = self ._gateway_config ()
95- if token :
96- if not gateway_url :
97- raise RuntimeError ("LLM gateway token present but LLM_GATEWAY_URL is not configured" )
98- logger .info (
99- "Using request-scoped Portfolio LLM Gateway for model=%s base_url=%s" ,
100- self .id ,
101- gateway_url ,
91+ if not token :
92+ raise RuntimeError (
93+ "LLM gateway session token is required. Pass X-LLM-Gateway-Token from the portfolio session."
10294 )
103- return OpenAI (
104- api_key = token ,
105- base_url = gateway_url ,
106- timeout = max (float (self .timeout or 0 ), LLM_GATEWAY_TIMEOUT ),
107- max_retries = LLM_GATEWAY_MAX_RETRIES if self .max_retries in (None , 0 ) else int (self .max_retries ),
108- default_headers = self .default_headers ,
109- default_query = self .default_query ,
110- )
111- # Standalone LegacyLens deployments do not have a portfolio session token.
112- # Use a remote OpenAI-compatible free-tier provider instead of silently
113- # falling through to an unauthenticated OpenRouter endpoint.
114- if DIRECT_PROVIDER == "google" :
115- if not GOOGLE_API_KEY :
116- raise RuntimeError (
117- "No LLM gateway session token and GOOGLE_API_KEY is not configured for direct Gemini fallback"
118- )
119- logger .info (
120- "No gateway token; using direct Google Gemini OpenAI-compatible endpoint for model=%s" ,
121- self .id ,
122- )
123- return OpenAI (
124- api_key = GOOGLE_API_KEY ,
125- base_url = GOOGLE_OPENAI_BASE_URL ,
126- timeout = max (float (self .timeout or 0 ), LLM_GATEWAY_TIMEOUT ),
127- max_retries = LLM_GATEWAY_MAX_RETRIES if self .max_retries in (None , 0 ) else int (self .max_retries ),
128- default_headers = self .default_headers ,
129- default_query = self .default_query ,
130- )
131- return super ().get_client ()
95+ if not gateway_url :
96+ raise RuntimeError ("LLM gateway token present but LLM_GATEWAY_URL is not configured" )
97+ logger .info (
98+ "Using request-scoped Portfolio LLM Gateway for model=%s base_url=%s" ,
99+ self .id ,
100+ gateway_url ,
101+ )
102+ return OpenAI (
103+ api_key = token ,
104+ base_url = gateway_url ,
105+ timeout = max (float (self .timeout or 0 ), LLM_GATEWAY_TIMEOUT ),
106+ max_retries = LLM_GATEWAY_MAX_RETRIES if self .max_retries in (None , 0 ) else int (self .max_retries ),
107+ default_headers = self .default_headers ,
108+ default_query = self .default_query ,
109+ )
132110
133111 def get_async_client (self ):
134112 token , gateway_url = self ._gateway_config ()
135- if token :
136- if not gateway_url :
137- raise RuntimeError ("LLM gateway token present but LLM_GATEWAY_URL is not configured" )
138- logger .info (
139- "Using request-scoped Portfolio LLM Gateway (async) for model=%s base_url=%s" ,
140- self .id ,
141- gateway_url ,
113+ if not token :
114+ raise RuntimeError (
115+ "LLM gateway session token is required. Pass X-LLM-Gateway-Token from the portfolio session."
142116 )
143- return AsyncOpenAI (
144- api_key = token ,
145- base_url = gateway_url ,
146- timeout = max (float (self .timeout or 0 ), LLM_GATEWAY_TIMEOUT ),
147- max_retries = LLM_GATEWAY_MAX_RETRIES if self .max_retries in (None , 0 ) else int (self .max_retries ),
148- default_headers = self .default_headers ,
149- default_query = self .default_query ,
150- )
151- if DIRECT_PROVIDER == "google" :
152- if not GOOGLE_API_KEY :
153- raise RuntimeError (
154- "No LLM gateway session token and GOOGLE_API_KEY is not configured for direct Gemini fallback"
155- )
156- logger .info (
157- "No gateway token; using direct Google Gemini OpenAI-compatible endpoint (async) for model=%s" ,
158- self .id ,
159- )
160- return AsyncOpenAI (
161- api_key = GOOGLE_API_KEY ,
162- base_url = GOOGLE_OPENAI_BASE_URL ,
163- timeout = max (float (self .timeout or 0 ), LLM_GATEWAY_TIMEOUT ),
164- max_retries = LLM_GATEWAY_MAX_RETRIES if self .max_retries in (None , 0 ) else int (self .max_retries ),
165- default_headers = self .default_headers ,
166- default_query = self .default_query ,
167- )
168- return super ().get_async_client ()
117+ if not gateway_url :
118+ raise RuntimeError ("LLM gateway token present but LLM_GATEWAY_URL is not configured" )
119+ logger .info (
120+ "Using request-scoped Portfolio LLM Gateway (async) for model=%s base_url=%s" ,
121+ self .id ,
122+ gateway_url ,
123+ )
124+ return AsyncOpenAI (
125+ api_key = token ,
126+ base_url = gateway_url ,
127+ timeout = max (float (self .timeout or 0 ), LLM_GATEWAY_TIMEOUT ),
128+ max_retries = LLM_GATEWAY_MAX_RETRIES if self .max_retries in (None , 0 ) else int (self .max_retries ),
129+ default_headers = self .default_headers ,
130+ default_query = self .default_query ,
131+ )
169132
170133
171134
@@ -177,8 +140,8 @@ def create_model():
177140 if MODEL_TYPE == "OpenAI" :
178141 model = GatewayAwareOpenAIChat (
179142 id = OPENAI_MODEL_ID ,
180- api_key = os . getenv ( "OPENAI_API_KEY" ) ,
181- base_url = OPENAI_BASE_URL ,
143+ api_key = "gateway-session" ,
144+ base_url = LLM_GATEWAY_URL ,
182145 temperature = 0.1 ,
183146 )
184147 logger .info ("Using OpenAI-compatible model with request-scoped gateway support" )
0 commit comments