-
Notifications
You must be signed in to change notification settings - Fork 8
chore: Convert JSON LDValue into the AiConfig Object #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
louis-launchdarkly
wants to merge
12
commits into
lc/SDK-1192/AI-config-feature-branch
Choose a base branch
from
lc/SDK-1192/setup-ai-config-parsing
base: lc/SDK-1192/AI-config-feature-branch
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0a8ad3f
chore: Convert JSON LDValue into the AiConfig Object
louis-launchdarkly 28947f1
chore: add github workflow for server ai
louis-launchdarkly f83ba94
chore: first step to make the datamodel types immutable
louis-launchdarkly 877d4e2
chore: temporary enable build
louis-launchdarkly 90f34b9
chore: using the valuesAs converter for messages
louis-launchdarkly ded3e63
chore: update meta and model to be immutable
devin-ai-integration[bot] ba1597a
chore: Address code review comments with type guards
louis-launchdarkly 5298fad
chore: update formatting
louis-launchdarkly 745db03
chore: meta should be null if key is not parsable
louis-launchdarkly 6eb0bc4
test: Split single test into multiple tests for AI data model classes
devin-ai-integration[bot] 0c2b3b5
chore: touch up the tests and have provider nullable
louis-launchdarkly 5244939
chore: implement builder pattern for datamodel classes and make const…
devin-ai-integration[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,23 @@ | ||
name: java-server-sdk-ai | ||
|
||
on: | ||
push: | ||
branches: [main, 'feat/**'] | ||
paths-ignore: | ||
- '**.md' #Do not need to run CI for markdown changes. | ||
pull_request: | ||
branches: [main, 'feat/**', 'lc/**'] | ||
paths-ignore: | ||
- '**.md' | ||
|
||
jobs: | ||
build-test-java-server-sdk-ai: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Shared CI Steps | ||
uses: ./.github/actions/ci | ||
with: | ||
workspace_path: 'lib/sdk/server-ai' | ||
java_version: 8 |
152 changes: 146 additions & 6 deletions
152
lib/sdk/server-ai/src/main/java/com/launchdarkly/sdk/server/ai/LDAiClient.java
This file contains hidden or 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 |
---|---|---|
@@ -1,28 +1,168 @@ | ||
package com.launchdarkly.sdk.server.ai; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import com.launchdarkly.logging.LDLogger; | ||
import com.launchdarkly.sdk.server.interfaces.LDClientInterface; | ||
import com.launchdarkly.sdk.LDValue; | ||
import com.launchdarkly.sdk.LDValueType; | ||
import com.launchdarkly.sdk.server.ai.datamodel.AiConfig; | ||
import com.launchdarkly.sdk.server.ai.datamodel.Message; | ||
import com.launchdarkly.sdk.server.ai.datamodel.Meta; | ||
import com.launchdarkly.sdk.server.ai.datamodel.Model; | ||
import com.launchdarkly.sdk.server.ai.datamodel.Provider; | ||
import com.launchdarkly.sdk.server.ai.interfaces.LDAiClientInterface; | ||
|
||
/** | ||
* The LaunchDarkly AI client. The client is capable of retrieving AI Configs from LaunchDarkly, | ||
* and generating events specific to usage of the AI Config when interacting with model providers. | ||
* The LaunchDarkly AI client. The client is capable of retrieving AI Configs | ||
* from LaunchDarkly, | ||
* and generating events specific to usage of the AI Config when interacting | ||
* with model providers. | ||
*/ | ||
public class LDAiClient implements LDAiClientInterface { | ||
public final class LDAiClient implements LDAiClientInterface { | ||
private LDClientInterface client; | ||
private LDLogger logger; | ||
|
||
/** | ||
* Creates a {@link LDAiClient} | ||
* | ||
* @param client LaunchDarkly Java Server SDK | ||
* @param client LaunchDarkly Java Server SDK | ||
*/ | ||
public LDAiClient(LDClientInterface client) { | ||
if(client == null) { | ||
//Error | ||
if (client == null) { | ||
// Error | ||
} else { | ||
this.client = client; | ||
this.logger = client.getLogger(); | ||
} | ||
} | ||
|
||
/** | ||
* Method to convert the JSON variable into the AiConfig object | ||
* | ||
* If the parsing failed, the code will log an error and | ||
* return a well formed but with nullable value nulled and disabled AIConfig | ||
* | ||
* Doing all the error checks, so if somehow LD backend return incorrect value | ||
* types, there is logging | ||
* This also opens up the possibility of allowing customer to build this using a | ||
* JSON string in the future | ||
* | ||
* @param value | ||
* @param key | ||
*/ | ||
protected AiConfig parseAiConfig(LDValue value, String key) { | ||
boolean enabled = false; | ||
|
||
// Verify the whole value is a JSON object | ||
if (!checkValueWithFailureLogging(value, LDValueType.OBJECT, logger, | ||
"Input to parseAiConfig must be a JSON object")) { | ||
return new AiConfig(enabled, null, null, null, null); | ||
} | ||
|
||
// Convert the _meta JSON object into Meta | ||
LDValue valueMeta = value.get("_ldMeta"); | ||
if (!checkValueWithFailureLogging(valueMeta, LDValueType.OBJECT, logger, "_ldMeta must be a JSON object")) { | ||
// Q: If we can't read _meta, enabled by spec would be defaulted to false. Does | ||
// it even matter the rest of the values? | ||
return new AiConfig(enabled, null, null, null, null); | ||
} | ||
|
||
// The booleanValue will get false if that value is something that we are not expecting | ||
enabled = valueMeta.get("enabled").booleanValue(); | ||
|
||
Meta meta = null; | ||
|
||
if (checkValueWithFailureLogging(valueMeta.get("variationKey"), LDValueType.STRING, logger, | ||
"variationKey should be a string")) { | ||
String variationKey = valueMeta.get("variationKey").stringValue(); | ||
|
||
meta = new Meta( | ||
variationKey, | ||
Optional.of(valueMeta.get("version").intValue())); | ||
} | ||
|
||
// Convert the optional model from an JSON object of with parameters and custom | ||
// into Model | ||
Model model = null; | ||
|
||
LDValue valueModel = value.get("model"); | ||
if (checkValueWithFailureLogging(valueModel, LDValueType.OBJECT, logger, | ||
"model if exists must be a JSON object")) { | ||
if (checkValueWithFailureLogging(valueModel.get("name"), LDValueType.STRING, logger, | ||
"model name must be a string and is required")) { | ||
String modelName = valueModel.get("name").stringValue(); | ||
|
||
// Prepare parameters and custom maps for Model | ||
HashMap<String, LDValue> parameters = null; | ||
HashMap<String, LDValue> custom = null; | ||
|
||
LDValue valueParameters = valueModel.get("parameters"); | ||
if (checkValueWithFailureLogging(valueParameters, LDValueType.OBJECT, logger, | ||
"non-null parameters must be a JSON object")) { | ||
parameters = new HashMap<>(); | ||
for (String k : valueParameters.keys()) { | ||
parameters.put(k, valueParameters.get(k)); | ||
} | ||
} | ||
|
||
LDValue valueCustom = valueModel.get("custom"); | ||
if (checkValueWithFailureLogging(valueCustom, LDValueType.OBJECT, logger, | ||
"non-null custom must be a JSON object")) { | ||
|
||
custom = new HashMap<>(); | ||
for (String k : valueCustom.keys()) { | ||
custom.put(k, valueCustom.get(k)); | ||
} | ||
} | ||
|
||
model = new Model(modelName, parameters, custom); | ||
} | ||
} | ||
|
||
// Convert the optional messages from an JSON array of JSON objects into Message | ||
// Q: Does it even make sense to have 0 messages? | ||
List<Message> messages = null; | ||
|
||
LDValue valueMessages = value.get("messages"); | ||
if (checkValueWithFailureLogging(valueMessages, LDValueType.ARRAY, logger, | ||
"messages if exists must be a JSON array")) { | ||
messages = new ArrayList<Message>(); | ||
valueMessages.valuesAs(new Message.MessageConverter()); | ||
for (Message message : valueMessages.valuesAs(new Message.MessageConverter())) { | ||
messages.add(message); | ||
} | ||
} | ||
|
||
// Convert the optional provider from an JSON object of with name into Provider | ||
LDValue valueProvider = value.get("provider"); | ||
Provider provider = null; | ||
|
||
if (checkValueWithFailureLogging(valueProvider, LDValueType.OBJECT, logger, | ||
"non-null provider must be a JSON object")) { | ||
if (checkValueWithFailureLogging(valueProvider.get("name"), LDValueType.STRING, logger, | ||
"provider name must be a String")) { | ||
String providerName = valueProvider.get("name").stringValue(); | ||
|
||
provider = new Provider(providerName); | ||
} | ||
} | ||
|
||
return new AiConfig(enabled, meta, model, messages, provider); | ||
} | ||
|
||
protected boolean checkValueWithFailureLogging(LDValue ldValue, LDValueType expectedType, LDLogger logger, | ||
String message) { | ||
if (ldValue.getType() != expectedType) { | ||
// TODO: In the next PR, make this required with some sort of default logger | ||
if (logger != null) { | ||
logger.error(message); | ||
} | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
lib/sdk/server-ai/src/main/java/com/launchdarkly/sdk/server/ai/datamodel/AiConfig.java
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add nullable and nonnull annotations on fields to help with consumption.
|
This file contains hidden or 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,43 @@ | ||
package com.launchdarkly.sdk.server.ai.datamodel; | ||
|
||
import java.util.List; | ||
|
||
public final class AiConfig { | ||
private final boolean enabled; | ||
|
||
private final Meta meta; | ||
|
||
private final Model model; | ||
|
||
private final List<Message> messages; | ||
|
||
private final Provider provider; | ||
|
||
public AiConfig(boolean enabled, Meta meta, Model model, List<Message> messages, Provider provider) { | ||
this.enabled = enabled; | ||
this.meta = meta; | ||
this.model = model; | ||
this.messages = messages; | ||
this.provider = provider; | ||
} | ||
|
||
public boolean isEnabled() { | ||
return enabled; | ||
} | ||
|
||
public List<Message> getMessages() { | ||
return messages; | ||
} | ||
|
||
public Meta getMeta() { | ||
return meta; | ||
} | ||
|
||
public Model getModel() { | ||
return model; | ||
} | ||
|
||
public Provider getProvider() { | ||
return provider; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
lib/sdk/server-ai/src/main/java/com/launchdarkly/sdk/server/ai/datamodel/Message.java
This file contains hidden or 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,37 @@ | ||
package com.launchdarkly.sdk.server.ai.datamodel; | ||
|
||
import com.launchdarkly.sdk.LDValue; | ||
|
||
public final class Message { | ||
public static class MessageConverter extends LDValue.Converter<Message> { | ||
@Override | ||
public LDValue fromType(Message message) { | ||
return LDValue.buildObject() | ||
.put("content", message.getContent()) | ||
.put("role", message.getRole().toString()) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public Message toType(LDValue ldValue) { | ||
return new Message(ldValue.get("content").stringValue(), Role.getRole(ldValue.get("role").stringValue())); | ||
} | ||
} | ||
|
||
private String content; | ||
|
||
private Role role; | ||
|
||
public Message(String content, Role role) { | ||
this.content = content; | ||
this.role = role; | ||
} | ||
|
||
public String getContent() { | ||
return content; | ||
} | ||
|
||
public Role getRole() { | ||
return role; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
lib/sdk/server-ai/src/main/java/com/launchdarkly/sdk/server/ai/datamodel/Meta.java
This file contains hidden or 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,37 @@ | ||
package com.launchdarkly.sdk.server.ai.datamodel; | ||
|
||
import java.util.Optional; | ||
|
||
public final class Meta { | ||
/** | ||
* The variation key. | ||
*/ | ||
private final String variationKey; | ||
|
||
/** | ||
* The variation version. | ||
*/ | ||
private final Optional<Integer> version; | ||
|
||
// The enable paramter is taken outside of the Meta to be a top level value of | ||
// AiConfig | ||
|
||
/** | ||
* Constructor for Meta with all required fields. | ||
* | ||
* @param variationKey the variation key | ||
* @param version the version | ||
*/ | ||
public Meta(String variationKey, Optional<Integer> version) { | ||
this.variationKey = variationKey; | ||
this.version = version != null ? version : Optional.empty(); | ||
} | ||
|
||
public String getVariationKey() { | ||
return variationKey; | ||
} | ||
|
||
public Optional<Integer> getVersion() { | ||
return version; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
lib/sdk/server-ai/src/main/java/com/launchdarkly/sdk/server/ai/datamodel/Model.java
This file contains hidden or 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,41 @@ | ||
package com.launchdarkly.sdk.server.ai.datamodel; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import com.launchdarkly.sdk.LDValue; | ||
|
||
public final class Model { | ||
private final String name; | ||
|
||
private final Map<String, LDValue> parameters; | ||
|
||
private final Map<String, LDValue> custom; | ||
|
||
/** | ||
* Constructor for Model with all required fields. | ||
* | ||
* @param name the model name | ||
* @param parameters the parameters map | ||
* @param custom the custom map | ||
*/ | ||
public Model(String name, Map<String, LDValue> parameters, Map<String, LDValue> custom) { | ||
this.name = name; | ||
this.parameters = parameters != null ? Collections.unmodifiableMap(new HashMap<>(parameters)) | ||
: Collections.emptyMap(); | ||
this.custom = custom != null ? Collections.unmodifiableMap(new HashMap<>(custom)) : Collections.emptyMap(); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public Map<String, LDValue> getParameters() { | ||
return parameters; | ||
} | ||
|
||
public Map<String, LDValue> getCustom() { | ||
return custom; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
lib/sdk/server-ai/src/main/java/com/launchdarkly/sdk/server/ai/datamodel/Provider.java
This file contains hidden or 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,13 @@ | ||
package com.launchdarkly.sdk.server.ai.datamodel; | ||
|
||
public final class Provider { | ||
private String name; | ||
|
||
public Provider(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.