-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
agent meta classes in common (#1757)
* agent meta classes Signed-off-by: Jing Zhang <[email protected]> * address comments Signed-off-by: Jing Zhang <[email protected]> * add one more UT Signed-off-by: Jing Zhang <[email protected]> * add more UT Signed-off-by: Jing Zhang <[email protected]> * more UT Signed-off-by: Jing Zhang <[email protected]> --------- Signed-off-by: Jing Zhang <[email protected]>
- Loading branch information
Showing
9 changed files
with
1,111 additions
and
0 deletions.
There are no files selected for viewing
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
102 changes: 102 additions & 0 deletions
102
common/src/main/java/org/opensearch/ml/common/agent/LLMSpec.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,102 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common.agent; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
import org.opensearch.core.xcontent.ToXContentObject; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.core.xcontent.XContentParser; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken; | ||
import static org.opensearch.ml.common.utils.StringUtils.getParameterMap; | ||
|
||
|
||
@Getter | ||
public class LLMSpec implements ToXContentObject { | ||
public static final String MODEL_ID_FIELD = "model_id"; | ||
public static final String PARAMETERS_FIELD = "parameters"; | ||
|
||
private String modelId; | ||
private Map<String, String> parameters; | ||
|
||
|
||
@Builder(toBuilder = true) | ||
public LLMSpec(String modelId, Map<String, String> parameters) { | ||
if (modelId == null) { | ||
throw new IllegalArgumentException("model id is null"); | ||
} | ||
this.modelId = modelId; | ||
this.parameters = parameters; | ||
} | ||
|
||
public LLMSpec(StreamInput input) throws IOException{ | ||
modelId = input.readString(); | ||
if (input.readBoolean()) { | ||
parameters = input.readMap(StreamInput::readString, StreamInput::readOptionalString); | ||
} | ||
} | ||
|
||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeString(modelId); | ||
if (parameters != null && parameters.size() > 0) { | ||
out.writeBoolean(true); | ||
out.writeMap(parameters, StreamOutput::writeString, StreamOutput::writeOptionalString); | ||
} else { | ||
out.writeBoolean(false); | ||
} | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
if (modelId != null) { | ||
builder.field(MODEL_ID_FIELD, modelId); | ||
} | ||
if (parameters != null && parameters.size() > 0) { | ||
builder.field(PARAMETERS_FIELD, parameters); | ||
} | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
public static LLMSpec parse(XContentParser parser) throws IOException { | ||
String modelId = null; | ||
Map<String, String> parameters = null; | ||
|
||
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser); | ||
while (parser.nextToken() != XContentParser.Token.END_OBJECT) { | ||
String fieldName = parser.currentName(); | ||
parser.nextToken(); | ||
|
||
switch (fieldName) { | ||
case MODEL_ID_FIELD: | ||
modelId = parser.text(); | ||
break; | ||
case PARAMETERS_FIELD: | ||
parameters = getParameterMap(parser.map()); | ||
break; | ||
default: | ||
parser.skipChildren(); | ||
break; | ||
} | ||
} | ||
return LLMSpec.builder() | ||
.modelId(modelId) | ||
.parameters(parameters) | ||
.build(); | ||
} | ||
|
||
public static LLMSpec fromStream(StreamInput in) throws IOException { | ||
LLMSpec toolSpec = new LLMSpec(in); | ||
return toolSpec; | ||
} | ||
} |
Oops, something went wrong.