-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support GenAI on Tanzu Platform as a built-in CfEnvProcessor (#270)
Co-authored-by: Stuart Charlton <[email protected]>
- Loading branch information
Showing
4 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
java-cfenv-boot/src/main/java/io/pivotal/cfenv/spring/boot/GenAICfEnvProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.pivotal.cfenv.spring.boot; | ||
|
||
import java.util.Map; | ||
|
||
import io.pivotal.cfenv.core.CfCredentials; | ||
import io.pivotal.cfenv.core.CfService; | ||
|
||
/** | ||
* Retrieve GenAI on Tanzu Platform properties from {@link CfCredentials} and | ||
* set {@literal spring.ai} | ||
* Boot properties. | ||
* | ||
* @author Stuart Charlton | ||
**/ | ||
public class GenAICfEnvProcessor implements CfEnvProcessor { | ||
|
||
@Override | ||
public boolean accept(CfService service) { | ||
return service.existsByTagIgnoreCase("genai") || | ||
service.existsByLabelStartsWith("genai"); | ||
} | ||
|
||
@Override | ||
public void process(CfCredentials cfCredentials, Map<String, Object> properties) { | ||
properties.put("spring.ai.openai.base-url", cfCredentials.getString("api_base")); | ||
properties.put("spring.ai.openai.api-key", cfCredentials.getString("api_key")); | ||
} | ||
|
||
@Override | ||
public CfEnvProcessorProperties getProperties() { | ||
return CfEnvProcessorProperties.builder() | ||
.propertyPrefixes("spring.ai.openai") | ||
.serviceName("GenAI on Tanzu Platform") | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
java-cfenv-boot/src/test/java/io/pivotal/cfenv/spring/boot/GenAICfEnvProcessorTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.pivotal.cfenv.spring.boot; | ||
|
||
import mockit.Mock; | ||
import mockit.MockUp; | ||
import org.junit.Test; | ||
|
||
import io.pivotal.cfenv.test.AbstractCfEnvTests; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author Stuart Charlton | ||
*/ | ||
public class GenAICfEnvProcessorTests extends AbstractCfEnvTests { | ||
|
||
private static final String EXPECTED_TOKEN_FROM_JSON_FILE = "sk-KW5kiNOKDd_1dFxsAjpVa"; | ||
private static final String EXPECTED_URI_FROM_JSON_FILE = "https://genai-proxy.tpcf.io"; | ||
private static final String TEST_GENAI_INFO_JSON = "test-genai-info.json"; | ||
|
||
@Test | ||
public void genaiServiceCreation() { | ||
mockConnectorLibrary(false); | ||
mockVcapServices(getServicesPayload(readTestDataFile(TEST_GENAI_INFO_JSON))); | ||
assertThat(getEnvironment().getProperty("spring.ai.openai.base-url")).isEqualTo(EXPECTED_URI_FROM_JSON_FILE); | ||
assertThat(getEnvironment().getProperty("spring.ai.openai.api-key")).isEqualTo(EXPECTED_TOKEN_FROM_JSON_FILE); | ||
} | ||
|
||
private MockUp<?> mockConnectorLibrary(boolean isUsingConnectorLibrary) { | ||
return new MockUp<ConnectorLibraryDetector>() { | ||
@Mock | ||
public boolean isUsingConnectorLibrary() { | ||
return isUsingConnectorLibrary; | ||
} | ||
}; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
java-cfenv-boot/src/test/resources/io/pivotal/cfenv/spring/boot/test-genai-info.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"credentials": { | ||
"api_base": "https://genai-proxy.tpcf.io", | ||
"api_key": "sk-KW5kiNOKDd_1dFxsAjpVa" | ||
}, | ||
"instance_name": "genai", | ||
"label": "genai", | ||
"name": "genai", | ||
"plan": "shared", | ||
"provider": null, | ||
"syslog_drain_url": null, | ||
"tags": [ | ||
"genai", | ||
"llm" | ||
], | ||
"volume_mounts": [] | ||
} |