Skip to content

Commit f85c603

Browse files
Stu CharltonStuart Charlton
andauthored
Support GenAI on Tanzu Platform as a built-in CfEnvProcessor (#270)
Co-authored-by: Stuart Charlton <[email protected]>
1 parent f30690c commit f85c603

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.pivotal.cfenv.spring.boot;
17+
18+
import java.util.Map;
19+
20+
import io.pivotal.cfenv.core.CfCredentials;
21+
import io.pivotal.cfenv.core.CfService;
22+
23+
/**
24+
* Retrieve GenAI on Tanzu Platform properties from {@link CfCredentials} and
25+
* set {@literal spring.ai}
26+
* Boot properties.
27+
*
28+
* @author Stuart Charlton
29+
**/
30+
public class GenAICfEnvProcessor implements CfEnvProcessor {
31+
32+
@Override
33+
public boolean accept(CfService service) {
34+
return service.existsByTagIgnoreCase("genai") ||
35+
service.existsByLabelStartsWith("genai");
36+
}
37+
38+
@Override
39+
public void process(CfCredentials cfCredentials, Map<String, Object> properties) {
40+
properties.put("spring.ai.openai.base-url", cfCredentials.getString("api_base"));
41+
properties.put("spring.ai.openai.api-key", cfCredentials.getString("api_key"));
42+
}
43+
44+
@Override
45+
public CfEnvProcessorProperties getProperties() {
46+
return CfEnvProcessorProperties.builder()
47+
.propertyPrefixes("spring.ai.openai")
48+
.serviceName("GenAI on Tanzu Platform")
49+
.build();
50+
}
51+
}

java-cfenv-boot/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ io.pivotal.cfenv.spring.boot.CfEnvProcessor=\
1313
io.pivotal.cfenv.spring.boot.AmqpCfEnvProcessor,\
1414
io.pivotal.cfenv.spring.boot.CredHubCfEnvProcessor,\
1515
io.pivotal.cfenv.spring.boot.CassandraCfEnvProcessor,\
16+
io.pivotal.cfenv.spring.boot.GenAICfEnvProcessor,\
1617
io.pivotal.cfenv.spring.boot.VaultCfEnvProcessor
1718

1819

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.pivotal.cfenv.spring.boot;
17+
18+
import mockit.Mock;
19+
import mockit.MockUp;
20+
import org.junit.Test;
21+
22+
import io.pivotal.cfenv.test.AbstractCfEnvTests;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
/**
27+
* @author Stuart Charlton
28+
*/
29+
public class GenAICfEnvProcessorTests extends AbstractCfEnvTests {
30+
31+
private static final String EXPECTED_TOKEN_FROM_JSON_FILE = "sk-KW5kiNOKDd_1dFxsAjpVa";
32+
private static final String EXPECTED_URI_FROM_JSON_FILE = "https://genai-proxy.tpcf.io";
33+
private static final String TEST_GENAI_INFO_JSON = "test-genai-info.json";
34+
35+
@Test
36+
public void genaiServiceCreation() {
37+
mockConnectorLibrary(false);
38+
mockVcapServices(getServicesPayload(readTestDataFile(TEST_GENAI_INFO_JSON)));
39+
assertThat(getEnvironment().getProperty("spring.ai.openai.base-url")).isEqualTo(EXPECTED_URI_FROM_JSON_FILE);
40+
assertThat(getEnvironment().getProperty("spring.ai.openai.api-key")).isEqualTo(EXPECTED_TOKEN_FROM_JSON_FILE);
41+
}
42+
43+
private MockUp<?> mockConnectorLibrary(boolean isUsingConnectorLibrary) {
44+
return new MockUp<ConnectorLibraryDetector>() {
45+
@Mock
46+
public boolean isUsingConnectorLibrary() {
47+
return isUsingConnectorLibrary;
48+
}
49+
};
50+
}
51+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"credentials": {
3+
"api_base": "https://genai-proxy.tpcf.io",
4+
"api_key": "sk-KW5kiNOKDd_1dFxsAjpVa"
5+
},
6+
"instance_name": "genai",
7+
"label": "genai",
8+
"name": "genai",
9+
"plan": "shared",
10+
"provider": null,
11+
"syslog_drain_url": null,
12+
"tags": [
13+
"genai",
14+
"llm"
15+
],
16+
"volume_mounts": []
17+
}

0 commit comments

Comments
 (0)