-
Notifications
You must be signed in to change notification settings - Fork 156
Prompt Management Create API #3779
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
rcho93
wants to merge
18
commits into
opensearch-project:feature/prompt
Choose a base branch
from
rcho93:feature/prompt-management
base: feature/prompt
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.
+1,987
−2
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
2be0f2e
initial commit for MCP server in OpenSearch (#3781)
zane-neo a985d84
support MCP session management (#3803)
zane-neo a86d9b3
Create prompt API & System Index for prompt
rcho93 03acb08
spotless is applied & disabled wildcard import
rcho93 441bc9e
javadoc added & header added & minor code errors fixed
rcho93 772e053
Apply spotless
rcho93 a871a34
apply spotless
rcho93 b5441bd
addressed comments
rcho93 9ff4c68
apply spotless
rcho93 79a8bcc
addresses comments
rcho93 dc8cc73
apply spotless
rcho93 bcd81e4
addressed comments
rcho93 b2a8dea
solve gradle build issue
rcho93 4afc166
add test cases for create-api
rcho93 c8534ef
fix javadoc test case failure
rcho93 61444c5
fix javadoc error
rcho93 b2050ff
fix guava noclass issue
rcho93 cf6017c
fix jacocoTestCoverageVerification fail
rcho93 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
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
251 changes: 251 additions & 0 deletions
251
common/src/main/java/org/opensearch/ml/common/MLPrompt.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,251 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common; | ||
|
||
import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken; | ||
import static org.opensearch.ml.common.CommonValue.TENANT_ID_FIELD; | ||
|
||
import java.io.IOException; | ||
import java.time.Instant; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
import org.opensearch.core.common.io.stream.Writeable; | ||
import org.opensearch.core.xcontent.ToXContentObject; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.core.xcontent.XContentParser; | ||
|
||
import lombok.Builder; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
|
||
/** | ||
* MLPrompt is the class to store prompt information. | ||
*/ | ||
@Getter | ||
@EqualsAndHashCode | ||
public class MLPrompt implements ToXContentObject, Writeable { | ||
|
||
public static final String PROMPT_ID_FIELD = "prompt_id"; | ||
public static final String NAME_FIELD = "name"; | ||
public static final String DESCRIPTION_FIELD = "description"; | ||
public static final String VERSION_FIELD = "version"; | ||
public static final String PROMPT_FIELD = "prompt"; | ||
public static final String TAGS_FIELD = "tags"; | ||
public static final String CREATE_TIME_FIELD = "create_time"; | ||
public static final String LAST_UPDATE_TIME_FIELD = "last_update_time"; | ||
|
||
private String promptId; | ||
private String name; | ||
private String description; | ||
private String version; | ||
private Map<String, String> prompt; | ||
private List<String> tags; | ||
private String tenantId; | ||
private Instant createTime; | ||
private Instant lastUpdateTime; | ||
|
||
/** | ||
* Constructor to pass values to the MLPrompt constructor | ||
* | ||
* @param promptId The prompt id of the MLPrompt | ||
* @param name The name of the MLPrompt | ||
* @param description The description of the MLPrompt | ||
* @param version The version of the MLPrompt | ||
* @param prompt The prompt of the MLPrompt -> contains system and user prompts | ||
* @param tags The tags of the MLPrompt | ||
* @param tenantId The tenant id of the MLPrompt | ||
* @param createTime The create time of the MLPrompt | ||
* @param lastUpdateTime The last update time of the MLPrompt | ||
*/ | ||
@Builder(toBuilder = true) | ||
public MLPrompt( | ||
String promptId, | ||
String name, | ||
String description, | ||
String version, | ||
Map<String, String> prompt, | ||
List<String> tags, | ||
String tenantId, | ||
Instant createTime, | ||
Instant lastUpdateTime | ||
) { | ||
this.promptId = promptId; | ||
this.name = name; | ||
this.description = description; | ||
this.version = version; | ||
this.prompt = prompt; | ||
this.tags = tags; | ||
this.tenantId = tenantId; | ||
this.createTime = createTime; | ||
this.lastUpdateTime = lastUpdateTime; | ||
} | ||
|
||
/** | ||
* Deserialize the Stream Input and constructs MLPrompt | ||
* | ||
* @param input Abstract class that describes Stream Input | ||
* @throws IOException if an I/O exception occurred while reading from input stream | ||
*/ | ||
public MLPrompt(StreamInput input) throws IOException { | ||
this.promptId = input.readOptionalString(); | ||
this.name = input.readOptionalString(); | ||
this.description = input.readOptionalString(); | ||
this.version = input.readOptionalString(); | ||
this.prompt = input.readMap(s -> s.readString(), s -> s.readString()); | ||
this.tags = input.readList(StreamInput::readString); | ||
this.tenantId = input.readOptionalString(); | ||
this.createTime = input.readOptionalInstant(); | ||
this.lastUpdateTime = input.readOptionalInstant(); | ||
} | ||
|
||
/** | ||
* Serialize and Writes the MLPrompt object to the output stream. | ||
* | ||
* @param out Abstract class that describes Stream Output | ||
* @throws IOException if an I/O exception occurred while writing to the output stream | ||
*/ | ||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeOptionalString(promptId); | ||
out.writeOptionalString(name); | ||
out.writeOptionalString(description); | ||
out.writeOptionalString(version); | ||
out.writeMap(prompt, StreamOutput::writeString, StreamOutput::writeString); | ||
out.writeCollection(tags, StreamOutput::writeString); | ||
out.writeOptionalString(tenantId); | ||
out.writeOptionalInstant(createTime); | ||
out.writeOptionalInstant(lastUpdateTime); | ||
} | ||
|
||
/** | ||
* Serialize and Writes the MLPrompt object to the XContentBuilder | ||
* | ||
* @param xContentBuilder XContentBuilder | ||
* @param params Parameters that need to be written to xContentBuilder | ||
* @return XContentBuilder | ||
* @throws IOException if an I/O exception occurred while writing field values to xContent | ||
*/ | ||
@Override | ||
public XContentBuilder toXContent(XContentBuilder xContentBuilder, Params params) throws IOException { | ||
XContentBuilder builder = xContentBuilder.startObject(); | ||
if (promptId != null) { | ||
builder.field(PROMPT_ID_FIELD, promptId); | ||
} | ||
if (name != null) { | ||
builder.field(NAME_FIELD, name); | ||
} | ||
if (description != null) { | ||
builder.field(DESCRIPTION_FIELD, description); | ||
} | ||
if (version != null) { | ||
builder.field(VERSION_FIELD, version); | ||
} | ||
if (prompt != null) { | ||
builder.field(PROMPT_FIELD, prompt); | ||
} | ||
if (tags != null) { | ||
builder.field(TAGS_FIELD, tags); | ||
} | ||
if (tenantId != null) { | ||
builder.field(TENANT_ID_FIELD, tenantId); | ||
} | ||
if (createTime != null) { | ||
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. tenantid is missing here. |
||
builder.field(CREATE_TIME_FIELD, createTime.toEpochMilli()); | ||
} | ||
if (lastUpdateTime != null) { | ||
builder.field(LAST_UPDATE_TIME_FIELD, lastUpdateTime.toEpochMilli()); | ||
} | ||
return builder.endObject(); | ||
} | ||
|
||
/** | ||
* Creates MLPrompt from stream input | ||
* | ||
* @param in Input Stream | ||
* @return MLPrompt | ||
* @throws IOException if an I/O exception occurred while reading from input stream | ||
*/ | ||
public static MLPrompt fromStream(StreamInput in) throws IOException { | ||
return new MLPrompt(in); | ||
} | ||
|
||
/** | ||
* Creates MLPrompt from XContentParser | ||
* | ||
* @param parser XContentParser | ||
* @return MLPrompt | ||
* @throws IOException if an I/O exception occurred while parsing the XContentParser into MLPrompt fields | ||
*/ | ||
public static MLPrompt parse(XContentParser parser) throws IOException { | ||
String promptId = null; | ||
String name = null; | ||
String description = null; | ||
String version = null; | ||
Map<String, String> prompt = null; | ||
List<String> tags = null; | ||
String tenantId = null; | ||
Instant createTime = null; | ||
Instant lastUpdateTime = 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 PROMPT_ID_FIELD: | ||
promptId = parser.text(); | ||
break; | ||
case NAME_FIELD: | ||
name = parser.text(); | ||
break; | ||
case DESCRIPTION_FIELD: | ||
description = parser.text(); | ||
break; | ||
case VERSION_FIELD: | ||
version = parser.text(); | ||
break; | ||
case PROMPT_FIELD: | ||
prompt = parser.mapStrings(); | ||
break; | ||
case TAGS_FIELD: | ||
tags = new ArrayList<>(); | ||
ensureExpectedToken(XContentParser.Token.START_ARRAY, parser.currentToken(), parser); | ||
while (parser.nextToken() != XContentParser.Token.END_ARRAY) { | ||
tags.add(parser.text()); | ||
} | ||
break; | ||
case TENANT_ID_FIELD: | ||
tenantId = parser.text(); | ||
break; | ||
case CREATE_TIME_FIELD: | ||
createTime = Instant.ofEpochMilli(parser.longValue()); | ||
break; | ||
case LAST_UPDATE_TIME_FIELD: | ||
lastUpdateTime = Instant.ofEpochMilli(parser.longValue()); | ||
break; | ||
default: | ||
parser.skipChildren(); | ||
break; | ||
} | ||
} | ||
return MLPrompt | ||
.builder() | ||
.promptId(promptId) | ||
.name(name) | ||
.description(description) | ||
.version(version) | ||
.prompt(prompt) | ||
.tags(tags) | ||
.tenantId(tenantId) | ||
.createTime(createTime) | ||
.lastUpdateTime(lastUpdateTime) | ||
.build(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
common/src/main/java/org/opensearch/ml/common/transport/prompt/MLCreatePromptAction.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,17 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common.transport.prompt; | ||
|
||
import org.opensearch.action.ActionType; | ||
|
||
public class MLCreatePromptAction extends ActionType<MLCreatePromptResponse> { | ||
public static MLCreatePromptAction INSTANCE = new MLCreatePromptAction(); | ||
public static final String NAME = "cluster:admin/opensearch/ml/create_prompt"; | ||
|
||
private MLCreatePromptAction() { | ||
super(NAME, MLCreatePromptResponse::new); | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add java doc for every class and main method so that it's readable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is a new file, a header is needed. Check the other classes and copy the header in all new files.
/*
*/