Skip to content

Commit 93040f1

Browse files
AICORE_SERVICE_KEY in .env file (#129)
* Allow File Path in AI Core Env Var * Sonarlint * Documentation * Update README.md Co-authored-by: Alexander Dümont <[email protected]> * Accept both env var and .env * Updated doc * Apply suggestions from code review Co-authored-by: Alexander Dümont <[email protected]> * Do not use System.getenv anymore * Fix links * ℹ️ The environment variable has priority over the `.env` file. --------- Co-authored-by: Alexander Dümont <[email protected]>
1 parent 1a804e0 commit 93040f1

File tree

6 files changed

+39
-9
lines changed

6 files changed

+39
-9
lines changed

README.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
- [Introduction](#introduction)
1313
- [General Requirements](#general-requirements)
1414
- [Connecting to SAP AI Core](#connecting-to-sap-ai-core)
15-
- [Option 1: Set Credentials as Environment Variable](#option-1-set-credentials-as-environment-variable)
15+
- [Option 1: Set Credentials as Environment Variable](#option-1-set-ai-core-credentials)
1616
- [Option 2: Regular Service Binding in SAP BTP Cloud Foundry](#option-2-regular-service-binding-in-sap-btp-cloud-foundry)
1717
- [Option 3: Define and Use a Destination](#option-3-define-and-use-a-destination)
1818
- [Getting Started](#getting-started)
@@ -64,7 +64,7 @@ There are multiple ways to provide these credentials:
6464

6565
| Option | Description |
6666
|--------|----------------------------------------------------------------------------------------------------------|
67-
| **1** | Set an environment variable explicitly: `AICORE_SERVICE_KEY` |
67+
| **1** | Create an `.env` file containing an `AICORE_SERVICE_KEY={...}` |
6868
| **2** | Regular service binding in SAP BTP Cloud Foundry (results in `VCAP_SERVICES` environment variable entry) |
6969
| **3** | Define and use a _Destination_ in the SAP BTP Destination Service |
7070

@@ -75,7 +75,7 @@ Additional methods (not recommended for production):
7575
- Leverage a "user-provided" service binding
7676
- Define and use a custom `ServiceBinding` or `ServiceBindingAccessor` in your application
7777

78-
### Option 1: Set Credentials as Environment Variable
78+
### Option 1: Set AI Core Credentials
7979

8080
<details>
8181
<summary>Click to view detailed steps</summary>
@@ -87,16 +87,33 @@ Additional methods (not recommended for production):
8787
- Navigate to **Instances and Subscriptions** -> **Instances** -> **AI Core**
8888
- Click **View Credentials** and copy the JSON content
8989

90-
**2. Set Environment Variable:**
90+
**2. Create `.env` file:**
91+
92+
- Create an `.env` file in the root directory of your application
93+
- Add an entry `AICORE_SERVICE_KEY='<content-of-service-key>'`
94+
95+
<details>
96+
<summary>Set an environment variable instead of .env</summary>
97+
98+
**2. Set an Environment Variable: (alternative)**
9199

92100
- In your IDE or terminal, set the environment variable `AICORE_SERVICE_KEY` with the copied JSON content
93101

94-
Example:
102+
ℹ️ The environment variable has priority over the `.env` file.
103+
104+
Example Linux/MacOS:
95105

96106
```shell
97107
export AICORE_SERVICE_KEY='{ "clientid": "...", "clientsecret": "...", "url": "...", "serviceurls": { "AI_API_URL": "..." } }'
98108
```
99109

110+
Example Windows:
111+
112+
```shell
113+
$env:AICORE_SERVICE_KEY='{ "clientid": "...", "clientsecret": "...", "url": "...", "serviceurls": { "AI_API_URL": "..." } }'
114+
```
115+
116+
</details>
100117
</details>
101118

102119
### Option 2: Regular Service Binding in SAP BTP Cloud Foundry
@@ -180,7 +197,7 @@ Before you begin, ensure you have:
180197
- Met the OpenAI Chat Completion module specific requirements
181198
- Refer to [Prerequisites for OpenAI Chat Completion](docs/guides/OPENAI_CHAT_COMPLETION.md#prerequisites)
182199
- Set up the AI Core credentials
183-
using [(1) Environment Variable](#option-1-set-credentials-as-environment-variable)
200+
using [(1) Environment variable or env-file](#option-1-set-ai-core-credentials)
184201
or [(2) Regular Service Binding](#option-2-regular-service-binding-in-sap-btp-cloud-foundry).
185202
- Deployed the OpenAI GPT-3.5 Turbo model in SAP AI Core.
186203
- Refer to [Deploying the OpenAI GPT-3.5 Turbo Model](docs/guides/OPENAI_CHAT_COMPLETION.md#usage)

core/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@
9898
<groupId>io.vavr</groupId>
9999
<artifactId>vavr</artifactId>
100100
</dependency>
101+
<dependency>
102+
<groupId>io.github.cdimascio</groupId>
103+
<artifactId>dotenv-java</artifactId>
104+
</dependency>
101105
<!-- scope "runtime" -->
102106
<dependency>
103107
<groupId>com.sap.cloud.sdk.cloudplatform</groupId>

core/src/main/java/com/sap/ai/sdk/core/AiCoreService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.sap.cloud.sdk.cloudplatform.connectivity.exception.DestinationAccessException;
1616
import com.sap.cloud.sdk.cloudplatform.connectivity.exception.DestinationNotFoundException;
1717
import com.sap.cloud.sdk.services.openapi.apiclient.ApiClient;
18+
import io.github.cdimascio.dotenv.Dotenv;
1819
import java.util.NoSuchElementException;
1920
import java.util.function.BiFunction;
2021
import java.util.function.Function;
@@ -104,7 +105,7 @@ protected void destinationSetHeaders(@Nonnull final DefaultHttpDestination.Build
104105
*/
105106
@Nonnull
106107
public AiCoreService withDestination(@Nonnull final Destination destination) {
107-
baseDestinationHandler = (service) -> destination;
108+
baseDestinationHandler = service -> destination;
108109
return this;
109110
}
110111

@@ -160,7 +161,7 @@ public AiCoreDeployment forDeploymentByScenario(@Nonnull final String scenarioId
160161
@Nonnull
161162
protected Destination getBaseDestination()
162163
throws DestinationAccessException, DestinationNotFoundException {
163-
val serviceKey = System.getenv("AICORE_SERVICE_KEY");
164+
val serviceKey = Dotenv.configure().ignoreIfMissing().load().get("AICORE_SERVICE_KEY");
164165
return DestinationResolver.getDestination(serviceKey);
165166
}
166167

core/src/main/java/com/sap/ai/sdk/core/DestinationResolver.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
import javax.annotation.Nullable;
2323
import lombok.AccessLevel;
2424
import lombok.Getter;
25+
import lombok.NoArgsConstructor;
2526
import lombok.extern.slf4j.Slf4j;
2627
import lombok.val;
2728

2829
/** Utility class to resolve the destination pointing to the AI Core service. */
2930
@Slf4j
31+
@NoArgsConstructor(access = AccessLevel.PRIVATE) // utility class should not be instantiated
3032
class DestinationResolver {
3133
static final String AI_CLIENT_TYPE_KEY = "URL.headers.AI-Client-Type";
3234
static final String AI_CLIENT_TYPE_VALUE = "AI SDK Java";

core/src/test/java/com/sap/ai/sdk/core/AiCoreServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.junit.jupiter.api.AfterEach;
2626
import org.junit.jupiter.api.Test;
2727

28-
public class AiCoreServiceTest {
28+
class AiCoreServiceTest {
2929

3030
// setup
3131
private static final Map<String, Object> URLS = Map.of("AI_API_URL", "https://srv");

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<system-stubs.version>2.1.3</system-stubs.version>
6565
<surefire.version>3.5.1</surefire.version>
6666
<springframework.version>6.1.14</springframework.version>
67+
<dotenv-java.version>3.0.0</dotenv-java.version>
6768
<mockito.version>5.14.2</mockito.version>
6869
<!-- Quality assurance -->
6970
<enforcer.skipBanLoggingFrameworks>false</enforcer.skipBanLoggingFrameworks>
@@ -92,6 +93,11 @@
9293
<artifactId>spring-web</artifactId>
9394
<version>${springframework.version}</version>
9495
</dependency>
96+
<dependency>
97+
<groupId>io.github.cdimascio</groupId>
98+
<artifactId>dotenv-java</artifactId>
99+
<version>${dotenv-java.version}</version>
100+
</dependency>
95101
<!-- scope "test" -->
96102
<dependency>
97103
<groupId>org.junit.jupiter</groupId>

0 commit comments

Comments
 (0)