Skip to content

Commit 9ed7eed

Browse files
committed
Add env variable for llm gateway
1 parent 30a9827 commit 9ed7eed

4 files changed

Lines changed: 35 additions & 6 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,18 @@ Instead of using `datacustomcode configure`, you can also set credentials via en
229229
| `SFDC_REFRESH_TOKEN` | OAuth refresh token |
230230
| `SFDC_ACCESS_TOKEN` | (Optional) OAuth core/access token |
231231

232+
**Einstein Platform API Environment (Optional):**
233+
| Variable | Description |
234+
|----------|-------------|
235+
| `SFDC_EINSTEIN_API_ENV` | Einstein Platform API environment: `dev`, `test`, `stage`, or `prod`. If not set, automatically inferred from login URL. Set this explicitly if auto-detection fails. |
236+
232237
Example usage:
233238
```bash
234239
export SFDC_LOGIN_URL="https://login.salesforce.com"
235240
export SFDC_CLIENT_ID="your_client_id"
236241
export SFDC_CLIENT_SECRET="your_client_secret"
237242
export SFDC_REFRESH_TOKEN="your_refresh_token"
243+
export SFDC_EINSTEIN_API_ENV="test" # optional
238244

239245
datacustomcode run ./payload/entrypoint.py
240246
```

src/datacustomcode/einstein_platform_client.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
import os
1617
from typing import (
1718
Any,
1819
Dict,
@@ -30,10 +31,6 @@
3031

3132

3233
class EinsteinPlatformClient:
33-
EINSTEIN_PLATFORM_MODELS_URL = (
34-
"https://api.salesforce.com/einstein/platform/v1/models"
35-
)
36-
3734
def __init__(
3835
self,
3936
credentials_profile: Optional[str] = None,
@@ -48,8 +45,34 @@ def __init__(
4845
self._token_provider = CredentialsTokenProvider(profile)
4946
logger.debug(f"Using credentials token provider with profile: {profile}")
5047
self.token_response = None
48+
self._einstein_url_cache: Optional[str] = None
5149
super().__init__(**kwargs)
5250

51+
def _get_einstein_platform_url(self) -> str:
52+
if self._einstein_url_cache is not None:
53+
return self._einstein_url_cache
54+
55+
env = os.environ.get("SFDC_EINSTEIN_API_ENV", "prod").lower()
56+
if env not in ("dev", "test", "stage", "prod"):
57+
logger.warning(
58+
f"Unknown SFDC_EINSTEIN_API_ENV value '{env}', defaulting to prod"
59+
)
60+
env = "prod"
61+
62+
base_url = self._get_base_url_for_env(env)
63+
logger.info(f"Using Einstein Platform API endpoint: {base_url} (env={env})")
64+
self._einstein_url_cache = f"{base_url}/einstein/platform/v1/models"
65+
return self._einstein_url_cache
66+
67+
def _get_base_url_for_env(self, env: str) -> str:
68+
env_map = {
69+
"dev": "https://dev.api.salesforce.com",
70+
"test": "https://test.api.salesforce.com",
71+
"stage": "https://stage.api.salesforce.com",
72+
"prod": "https://api.salesforce.com",
73+
}
74+
return env_map.get(env, "https://api.salesforce.com")
75+
5376
def _get_headers(self):
5477
if self.token_response is None:
5578
self.token_response = self._token_provider.get_token()

src/datacustomcode/einstein_predictions/impl/default.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def predict(self, request: PredictionRequest) -> PredictionResponse:
4848
)
4949

5050
api_url = (
51-
f"{self.EINSTEIN_PLATFORM_MODELS_URL}/{request.model_api_name}/{endpoint}"
51+
f"{self._get_einstein_platform_url()}/{request.model_api_name}/{endpoint}"
5252
)
5353

5454
prediction_columns: List[Dict[str, Any]] = []

src/datacustomcode/llm_gateway/default.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class DefaultLLMGateway(EinsteinPlatformClient, LLMGateway):
2929

3030
def generate_text(self, request: GenerateTextRequest) -> GenerateTextResponse:
3131
api_url = (
32-
f"{self.EINSTEIN_PLATFORM_MODELS_URL}/{request.model_name}/generations"
32+
f"{self._get_einstein_platform_url()}/{request.model_name}/generations"
3333
)
3434

3535
payload: Dict[str, Any] = {"prompt": request.prompt}

0 commit comments

Comments
 (0)