-
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 1 commit
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
Next
Next commit
chore: Convert JSON LDValue into the AiConfig Object
- Loading branch information
commit 0a8ad3fa4798651187cce0439712f6c67d85da7f
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 |
---|---|---|
@@ -1,12 +1,27 @@ | ||
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.datamodel.Role; | ||
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 { | ||
private LDClientInterface client; | ||
|
@@ -15,14 +30,151 @@ public class LDAiClient implements LDAiClientInterface { | |
/** | ||
* Creates a {@link LDAiClient} | ||
* | ||
* @param client LaunchDarkly Java Server SDK | ||
* @param client LaunchDarkly Java Server SDK | ||
*/ | ||
public LDAiClient(LDClientInterface client) { | ||
louis-launchdarkly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
* | ||
* Currently, I am doing this in a mutable way, just to verify the logic convert | ||
* LDValue into AiConfig. | ||
* It is possible we need a builder to create immutable version of this for ease | ||
louis-launchdarkly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* of use in a later PR. | ||
* | ||
* @param value | ||
* @param key | ||
*/ | ||
protected AiConfig parseAiConfig(LDValue value, String key) { | ||
AiConfig result = new AiConfig(); | ||
|
||
try { | ||
// Verify the whole value is a JSON object | ||
if (value == null || value.getType() != LDValueType.OBJECT) { | ||
throw new AiConfigParseException("Input to parseAiConfig must be a JSON object"); | ||
} | ||
|
||
// Convert the _meta JSON object into Meta | ||
LDValue valueMeta = value.get("_ldMeta"); | ||
if (valueMeta == LDValue.ofNull() || valueMeta.getType() != LDValueType.OBJECT) { | ||
louis-launchdarkly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw new AiConfigParseException("_ldMeta must be a JSON object"); | ||
} | ||
|
||
Meta meta = new Meta(); | ||
// TODO: Do we expect customer calling this to build default value? | ||
// If we do, then some of the values would be null | ||
meta.setEnabled(ldValueNullCheck(valueMeta.get("enabled")).booleanValue()); | ||
meta.setVariationKey(ldValueNullCheck(valueMeta.get("variationKey")).stringValue()); | ||
Optional<Integer> version = Optional.of(valueMeta.get("version").intValue()); | ||
meta.setVersion(version); | ||
result.setMeta(meta); | ||
|
||
// Convert the optional messages from an JSON array of JSON objects into Message | ||
// Q: Does it even make sense to have 0 messages? | ||
LDValue valueMessages = value.get("messages"); | ||
if (valueMeta == LDValue.ofNull() || valueMessages.getType() != LDValueType.ARRAY) { | ||
throw new AiConfigParseException("messages must be a JSON array"); | ||
} | ||
|
||
List<Message> messages = new ArrayList<Message>(); | ||
for (LDValue valueMessage : valueMessages.values()) { | ||
louis-launchdarkly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (valueMessage == LDValue.ofNull() || valueMessage.getType() != LDValueType.OBJECT) { | ||
throw new AiConfigParseException("individual message must be a JSON object"); | ||
} | ||
|
||
Message message = new Message(); | ||
message.setContent(ldValueNullCheck(valueMessage.get("content")).stringValue()); | ||
// TODO: For absolute safety, we need to check this is one out of the three | ||
// possible enum | ||
message.setRole(Role.valueOf(valueMessage.get("role").stringValue().toUpperCase())); | ||
messages.add(message); | ||
} | ||
result.setMessages(messages); | ||
|
||
// Convert the optional model from an JSON object of with parameters and custom | ||
// into Model | ||
LDValue valueModel = value.get("model"); | ||
if (valueModel == LDValue.ofNull() || valueModel.getType() != LDValueType.OBJECT) { | ||
throw new AiConfigParseException("model must be a JSON object"); | ||
} | ||
|
||
Model model = new Model(); | ||
model.setName(ldValueNullCheck(valueModel.get("name")).stringValue()); | ||
|
||
LDValue valueParameters = valueModel.get("parameters"); | ||
if (valueParameters.getType() != LDValueType.NULL) { | ||
if (valueParameters.getType() != LDValueType.OBJECT) { | ||
throw new AiConfigParseException("non-null parameters must be a JSON object"); | ||
} | ||
|
||
HashMap<String, LDValue> parameters = new HashMap<>(); | ||
for (String k : valueParameters.keys()) { | ||
parameters.put(k, valueParameters.get(k)); | ||
} | ||
model.setParameters(parameters); | ||
} else { | ||
// Parameters is optional - so we can just set null and proceed | ||
|
||
// TODO: Mustash validation somewhere | ||
model.setParameters(null); | ||
} | ||
|
||
LDValue valueCustom = valueModel.get("custom"); | ||
if (valueCustom.getType() != LDValueType.NULL) { | ||
if (valueCustom.getType() != LDValueType.OBJECT) { | ||
throw new AiConfigParseException("non-null custom must be a JSON object"); | ||
} | ||
|
||
HashMap<String, LDValue> custom = new HashMap<>(); | ||
for (String k : valueCustom.keys()) { | ||
custom.put(k, valueCustom.get(k)); | ||
} | ||
model.setCustom(custom); | ||
} else { | ||
// Custom is optional - we can just set null and proceed | ||
model.setCustom(null); | ||
} | ||
result.setModel(model); | ||
|
||
// Convert the optional provider from an JSON object of with name into Provider | ||
LDValue valueProvider = value.get("provider"); | ||
if (valueProvider.getType() != LDValueType.NULL) { | ||
if (valueProvider.getType() != LDValueType.OBJECT) { | ||
throw new AiConfigParseException("non-null provider must be a JSON object"); | ||
} | ||
|
||
Provider provider = new Provider(); | ||
provider.setName(ldValueNullCheck(valueProvider.get("name")).stringValue()); | ||
result.setProvider(provider); | ||
} else { | ||
// Provider is optional - we can just set null and proceed | ||
result.setProvider(null); | ||
} | ||
} catch (AiConfigParseException e) { | ||
// logger.error(e.getMessage()); | ||
return null; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
protected <T> T ldValueNullCheck(T ldValue) throws AiConfigParseException { | ||
if (ldValue == LDValue.ofNull()) { | ||
throw new AiConfigParseException("Unexpected Null value for non-optional field"); | ||
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. Probably helpful to add field name. |
||
} | ||
return ldValue; | ||
} | ||
|
||
class AiConfigParseException extends Exception { | ||
AiConfigParseException(String exceptionMessage) { | ||
super(exceptionMessage); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
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,45 @@ | ||
package com.launchdarkly.sdk.server.ai.datamodel; | ||
|
||
import java.util.List; | ||
|
||
public class AiConfig { | ||
louis-launchdarkly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private List<Message> messages; | ||
|
||
private Meta meta; | ||
|
||
private Model model; | ||
|
||
private Provider provider; | ||
|
||
public List<Message> getMessages() { | ||
return messages; | ||
} | ||
|
||
public void setMessages(List<Message> messages) { | ||
this.messages = messages; | ||
} | ||
|
||
public Meta getMeta() { | ||
return meta; | ||
} | ||
|
||
public void setMeta(Meta meta) { | ||
this.meta = meta; | ||
} | ||
|
||
public Model getModel() { | ||
return model; | ||
} | ||
|
||
public void setModel(Model model) { | ||
this.model = model; | ||
} | ||
|
||
public Provider getProvider() { | ||
return provider; | ||
} | ||
|
||
public void setProvider(Provider provider) { | ||
this.provider = provider; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
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,23 @@ | ||
package com.launchdarkly.sdk.server.ai.datamodel; | ||
|
||
public class Message { | ||
private String content; | ||
|
||
private Role role; | ||
|
||
public String getContent() { | ||
return content; | ||
} | ||
|
||
public void setContent(String content) { | ||
this.content = content; | ||
} | ||
|
||
public Role getRole() { | ||
return role; | ||
} | ||
|
||
public void setRole(Role role) { | ||
this.role = role; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
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,46 @@ | ||
package com.launchdarkly.sdk.server.ai.datamodel; | ||
|
||
import java.util.Optional; | ||
|
||
public class Meta { | ||
/** | ||
* The variation key. | ||
*/ | ||
private String variationKey; | ||
|
||
/** | ||
* The variation version. | ||
*/ | ||
private Optional<Integer> version; | ||
|
||
/** | ||
* If the config is enabled. | ||
*/ | ||
private boolean enabled; | ||
|
||
// Getters and Setters | ||
|
||
public String getVariationKey() { | ||
return variationKey; | ||
} | ||
|
||
public void setVariationKey(String variationKey) { | ||
this.variationKey = variationKey; | ||
} | ||
|
||
public Optional<Integer> getVersion() { | ||
return version; | ||
} | ||
|
||
public void setVersion(Optional<Integer> version) { | ||
this.version = version; | ||
} | ||
|
||
public boolean isEnabled() { | ||
return enabled; | ||
} | ||
|
||
public void setEnabled(boolean enabled) { | ||
this.enabled = enabled; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
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,39 @@ | ||
package com.launchdarkly.sdk.server.ai.datamodel; | ||
|
||
import java.util.HashMap; | ||
|
||
import com.launchdarkly.sdk.LDValue; | ||
|
||
public class Model { | ||
private String name; | ||
|
||
private HashMap<String, LDValue> parameters; | ||
|
||
private HashMap<String, LDValue> custom; | ||
|
||
// Getters and Setters | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public HashMap<String, LDValue> getParameters() { | ||
return parameters; | ||
} | ||
|
||
public void setParameters(HashMap<String, LDValue> parameters) { | ||
this.parameters = parameters; | ||
} | ||
|
||
public HashMap<String, LDValue> getCustom() { | ||
return custom; | ||
} | ||
|
||
public void setCustom(HashMap<String, LDValue> custom) { | ||
this.custom = 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 class Provider { | ||
private String name; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = 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.