bug6095: abstracting "Deployment" with id and model name

This commit is contained in:
Axel Uhl
2025-02-20 01:14:00 +01:00
parent b287aa367d
commit e296c70524
8 changed files with 217 additions and 2 deletions
@@ -6,6 +6,7 @@ import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.util.Optional;
import org.apache.http.client.ClientProtocolException;
import org.json.simple.JSONObject;
@@ -14,11 +15,13 @@ import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import com.sap.sse.aicore.AICore;
import com.sap.sse.aicore.Credentials;
import com.sap.sse.aicore.CredentialsParser;
import com.sap.sse.aicore.Deployment;
import com.sap.sse.common.Util;
@Ignore("Requires system property sap.aicore.credentials to be set and contain a JSON credentials string")
//@Ignore("Requires system property sap.aicore.credentials to be set and contain a JSON credentials string")
public class TestWithSecretCredentials {
private Credentials credentials;
@@ -48,4 +51,10 @@ public class TestWithSecretCredentials {
final JSONObject deploymentsJson = credentials.getJSONResponse("/v2/lm/deployments");
assertNotNull(deploymentsJson);
}
@Test
public void testFindingGpt4oMiniModel() throws UnsupportedOperationException, ClientProtocolException, URISyntaxException, IOException, ParseException {
final Optional<Deployment> gpt4oMini = AICore.create(credentials).getDeploymentByModelName("gpt-4o-mini");
assertTrue(gpt4oMini.isPresent());
}
}
+2 -1
View File
@@ -8,7 +8,8 @@ Bundle-Activator: com.sap.sse.aicore.Activator
Bundle-Vendor: SAP
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Automatic-Module-Name: com.sap.sse.aicore
Import-Package: com.sap.sse.util,
Import-Package: com.sap.sse.common,
com.sap.sse.util,
org.osgi.framework;version="1.3.0"
Require-Bundle: org.json.simple,
org.apache.httpcomponents.httpclient,
@@ -0,0 +1,23 @@
package com.sap.sse.aicore;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Optional;
import org.apache.http.client.ClientProtocolException;
import org.json.simple.parser.ParseException;
import com.sap.sse.aicore.impl.AICoreImpl;
import com.sap.sse.common.Util;
public interface AICore {
static AICore create(final Credentials credentials) {
return new AICoreImpl(credentials);
}
Iterable<Deployment> getDeployments() throws UnsupportedOperationException, ClientProtocolException, URISyntaxException, IOException, ParseException;
default Optional<Deployment> getDeploymentByModelName(String modelName) throws UnsupportedOperationException, ClientProtocolException, URISyntaxException, IOException, ParseException {
return Util.stream(getDeployments()).filter(d->d.getModelName().equals(modelName)).findAny();
}
}
@@ -0,0 +1,13 @@
package com.sap.sse.aicore;
import java.io.IOException;
import java.net.URISyntaxException;
import org.apache.http.client.ClientProtocolException;
import org.json.simple.parser.ParseException;
public interface ChatSession {
void addSystemPrompt(String prompt);
void addPrompt(String prompt);
String submit() throws UnsupportedOperationException, ClientProtocolException, URISyntaxException, IOException, ParseException;
}
@@ -0,0 +1,10 @@
package com.sap.sse.aicore;
import com.sap.sse.common.WithID;
public interface Deployment extends WithID {
@Override
String getId();
String getModelName();
}
@@ -0,0 +1,72 @@
package com.sap.sse.aicore.impl;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.client.ClientProtocolException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
import com.sap.sse.aicore.AICore;
import com.sap.sse.aicore.Credentials;
import com.sap.sse.aicore.Deployment;
public class AICoreImpl implements AICore {
private final static String DEPLOYMENTS_PATH = "/v2/lm/deployments";
private static final String DEPLOYMENT_ID = "id";
private static final String DEPLOYMENT_DETAILS = "details";
private static final String DEPLOYMENT_DETAILS_RESOURCES = "resources";
private static final String DEPLOYMENT_DETAILS_RESOURCES_BACKEND_DETAILS = "backendDetails";
private static final String DEPLOYMENT_DETAILS_RESOURCES_BACKEND_DETAILS_MODEL = "model";
private static final String DEPLOYMENT_DETAILS_RESOURCES_BACKEND_DETAILS_MODEL_NAME = "name";
private final Credentials credentials;
public AICoreImpl(Credentials credentials) {
super();
this.credentials = credentials;
}
@Override
public Iterable<Deployment> getDeployments() throws UnsupportedOperationException, ClientProtocolException, URISyntaxException, IOException, ParseException {
final List<Deployment> result = new ArrayList<>();
final JSONObject deploymentsJson = credentials.getJSONResponse(DEPLOYMENTS_PATH);
for (final Object deploymentJson : (JSONArray) deploymentsJson.get("resources")) {
final JSONObject deploymentJsonObject = (JSONObject) deploymentJson;
final String id = (String) deploymentJsonObject.get(DEPLOYMENT_ID);
final String modelName;
if (deploymentJsonObject.containsKey(DEPLOYMENT_DETAILS)) {
final JSONObject deploymentDetails = (JSONObject) deploymentJsonObject.get(DEPLOYMENT_DETAILS);
if (deploymentDetails.containsKey(DEPLOYMENT_DETAILS_RESOURCES)) {
final JSONObject deploymentDetailsResources = (JSONObject) deploymentDetails.get(DEPLOYMENT_DETAILS_RESOURCES);
if (deploymentDetailsResources.containsKey(DEPLOYMENT_DETAILS_RESOURCES_BACKEND_DETAILS)) {
final JSONObject backendDetails = (JSONObject) deploymentDetailsResources.get(DEPLOYMENT_DETAILS_RESOURCES_BACKEND_DETAILS);
if (backendDetails.containsKey(DEPLOYMENT_DETAILS_RESOURCES_BACKEND_DETAILS_MODEL)) {
final JSONObject model = (JSONObject) backendDetails.get(DEPLOYMENT_DETAILS_RESOURCES_BACKEND_DETAILS_MODEL);
if (model.containsKey(DEPLOYMENT_DETAILS_RESOURCES_BACKEND_DETAILS_MODEL_NAME)) {
modelName = (String) model.get(DEPLOYMENT_DETAILS_RESOURCES_BACKEND_DETAILS_MODEL_NAME);
} else {
modelName = null;
}
} else {
modelName = null;
}
} else {
modelName = null;
}
} else {
modelName = null;
}
} else {
modelName = null;
}
if (modelName != null) {
result.add(new DeploymentImpl(id, modelName));
}
}
return result;
}
}
@@ -0,0 +1,63 @@
package com.sap.sse.aicore.impl;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
import com.sap.sse.aicore.AICore;
import com.sap.sse.aicore.ChatSession;
import com.sap.sse.aicore.Credentials;
import com.sap.sse.aicore.Deployment;
public class ChatSessionImpl implements ChatSession {
private final static String CHAT_PATH_TEMPLATE = "/v2/inference/deployments/%s/chat/completions?api-version=2024-06-01";
private final List<String> systemPrompts;
private final List<String> userPrompts;
private final Credentials credentials;
private final AICore aiCore;
private Deployment deployment;
public ChatSessionImpl(final Credentials credentials, final String modelName) throws UnsupportedOperationException,
ClientProtocolException, URISyntaxException, IOException, ParseException {
this.credentials = credentials;
this.systemPrompts = new ArrayList<>();
this.userPrompts = new ArrayList<>();
this.aiCore = new AICoreImpl(credentials);
this.deployment = aiCore.getDeploymentByModelName(modelName).get();
}
@Override
public void addSystemPrompt(String prompt) {
systemPrompts.add(prompt);
}
@Override
public void addPrompt(String prompt) {
userPrompts.add(prompt);
}
private String getChatPath() {
return String.format(CHAT_PATH_TEMPLATE, deployment.getId());
}
@Override
public String submit() throws UnsupportedOperationException, ClientProtocolException, URISyntaxException, IOException, ParseException {
final JSONObject chatResponse = credentials.getJSONResponse(getChatPath());
// TODO place prompts in something like
// "messages": [
// {
// "role": "user",
// "content": "Hello!"
// }
// ]}
// read results from something like choices[].message.content
return null;
}
}
@@ -0,0 +1,24 @@
package com.sap.sse.aicore.impl;
import com.sap.sse.aicore.Deployment;
public class DeploymentImpl implements Deployment {
private final String id;
private final String modelName;
public DeploymentImpl(String id, String modelName) {
super();
this.id = id;
this.modelName = modelName;
}
@Override
public String getId() {
return id;
}
@Override
public String getModelName() {
return modelName;
}
}