Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.searchrelevance.action.llmprompttemplate;

import org.opensearch.action.ActionType;

/**
* Action for deleting LLM prompt templates
*/
public class DeleteLlmPromptTemplateAction extends ActionType<DeleteLlmPromptTemplateResponse> {

public static final DeleteLlmPromptTemplateAction INSTANCE = new DeleteLlmPromptTemplateAction();
public static final String NAME = "cluster:admin/opensearch/search_relevance/llm_prompt_template/delete";

private DeleteLlmPromptTemplateAction() {
super(NAME, DeleteLlmPromptTemplateResponse::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.searchrelevance.action.llmprompttemplate;

import static org.opensearch.action.ValidateActions.addValidationError;

import java.io.IOException;

import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;

/**
* Request for deleting an LLM prompt template
*/
public class DeleteLlmPromptTemplateRequest extends ActionRequest {

private String templateId;

public DeleteLlmPromptTemplateRequest() {}

public DeleteLlmPromptTemplateRequest(String templateId) {
this.templateId = templateId;
}

public DeleteLlmPromptTemplateRequest(StreamInput in) throws IOException {
super(in);
this.templateId = in.readString();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeString(templateId);
}

@Override
public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = null;

if (templateId == null || templateId.trim().isEmpty()) {
validationException = addValidationError("template_id is required", validationException);
}

return validationException;
}

public String getTemplateId() {
return templateId;
}

public void setTemplateId(String templateId) {
this.templateId = templateId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.searchrelevance.action.llmprompttemplate;

import java.io.IOException;

import org.opensearch.core.action.ActionResponse;
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;

/**
* Response for deleting an LLM prompt template
*/
public class DeleteLlmPromptTemplateResponse extends ActionResponse implements ToXContentObject {

private final String templateId;
private final String result;
private final boolean found;

public DeleteLlmPromptTemplateResponse(String templateId, String result, boolean found) {
this.templateId = templateId;
this.result = result;
this.found = found;
}

public DeleteLlmPromptTemplateResponse(StreamInput in) throws IOException {
super(in);
this.templateId = in.readString();
this.result = in.readString();
this.found = in.readBoolean();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(templateId);
out.writeString(result);
out.writeBoolean(found);
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field("template_id", templateId);
builder.field("result", result);
builder.field("found", found);
builder.endObject();
return builder;
}

public String getTemplateId() {
return templateId;
}

public String getResult() {
return result;
}

public boolean isFound() {
return found;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.searchrelevance.action.llmprompttemplate;

import java.util.Locale;

import org.opensearch.action.support.ActionFilters;
import org.opensearch.action.support.HandledTransportAction;
import org.opensearch.common.inject.Inject;
import org.opensearch.core.action.ActionListener;
import org.opensearch.searchrelevance.dao.LlmPromptTemplateDao;
import org.opensearch.searchrelevance.indices.SearchRelevanceIndicesManager;
import org.opensearch.tasks.Task;
import org.opensearch.transport.TransportService;

/**
* Transport action for deleting LLM prompt templates
*/
public class DeleteLlmPromptTemplateTransportAction extends HandledTransportAction<
DeleteLlmPromptTemplateRequest,
DeleteLlmPromptTemplateResponse> {

private final LlmPromptTemplateDao llmPromptTemplateDao;

@Inject
public DeleteLlmPromptTemplateTransportAction(
TransportService transportService,
ActionFilters actionFilters,
SearchRelevanceIndicesManager indicesManager
) {
super(DeleteLlmPromptTemplateAction.NAME, transportService, actionFilters, DeleteLlmPromptTemplateRequest::new);
this.llmPromptTemplateDao = new LlmPromptTemplateDao(indicesManager);
}

@Override
protected void doExecute(Task task, DeleteLlmPromptTemplateRequest request, ActionListener<DeleteLlmPromptTemplateResponse> listener) {
llmPromptTemplateDao.deleteLlmPromptTemplate(request.getTemplateId(), ActionListener.wrap(deleteResponse -> {
DeleteLlmPromptTemplateResponse response = new DeleteLlmPromptTemplateResponse(
deleteResponse.getId(),
deleteResponse.getResult().toString().toLowerCase(Locale.ROOT),
deleteResponse.getResult().toString().equals("DELETED")
);
listener.onResponse(response);
}, listener::onFailure));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.searchrelevance.action.llmprompttemplate;

import org.opensearch.action.ActionType;

/**
* Action for getting LLM prompt templates
*/
public class GetLlmPromptTemplateAction extends ActionType<GetLlmPromptTemplateResponse> {

public static final GetLlmPromptTemplateAction INSTANCE = new GetLlmPromptTemplateAction();
public static final String NAME = "cluster:admin/opensearch/search_relevance/llm_prompt_template/get";

private GetLlmPromptTemplateAction() {
super(NAME, GetLlmPromptTemplateResponse::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.searchrelevance.action.llmprompttemplate;

import java.io.IOException;

import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;

/**
* Request for getting an LLM prompt template
*/
public class GetLlmPromptTemplateRequest extends ActionRequest {

private String templateId;

public GetLlmPromptTemplateRequest() {}

public GetLlmPromptTemplateRequest(String templateId) {
this.templateId = templateId;
}

public GetLlmPromptTemplateRequest(StreamInput in) throws IOException {
super(in);
this.templateId = in.readOptionalString();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeOptionalString(templateId);
}

@Override
public ActionRequestValidationException validate() {
// template_id is optional - if null, it means search all templates
return null;
}

public String getTemplateId() {
return templateId;
}

public void setTemplateId(String templateId) {
this.templateId = templateId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.searchrelevance.action.llmprompttemplate;

import java.io.IOException;

import org.opensearch.action.search.SearchResponse;
import org.opensearch.core.action.ActionResponse;
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.searchrelevance.model.LlmPromptTemplate;

/**
* Response for getting an LLM prompt template
*/
public class GetLlmPromptTemplateResponse extends ActionResponse implements ToXContentObject {

private final LlmPromptTemplate template;
private final SearchResponse searchResponse;
private final boolean found;

public GetLlmPromptTemplateResponse(LlmPromptTemplate template, boolean found) {
this.template = template;
this.searchResponse = null;
this.found = found;
}

public GetLlmPromptTemplateResponse(SearchResponse searchResponse, boolean found) {
this.template = null;
this.searchResponse = searchResponse;
this.found = found;
}

public GetLlmPromptTemplateResponse(StreamInput in) throws IOException {
super(in);
this.found = in.readBoolean();
boolean hasTemplate = in.readBoolean();
if (hasTemplate) {
this.template = new LlmPromptTemplate(in);
this.searchResponse = null;
} else {
this.template = null;
this.searchResponse = found ? new SearchResponse(in) : null;
}
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeBoolean(found);
out.writeBoolean(template != null);
if (template != null) {
template.writeTo(out);
} else if (searchResponse != null) {
searchResponse.writeTo(out);
}
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
if (template != null) {
// Single template response
builder.startObject();
builder.field("found", found);
if (found && template != null) {
builder.field("template", template);
}
builder.endObject();
} else if (searchResponse != null) {
// Search response - return the search response directly
return searchResponse.toXContent(builder, params);
} else {
builder.startObject();
builder.field("found", false);
builder.endObject();
}
return builder;
}

public LlmPromptTemplate getTemplate() {
return template;
}

public SearchResponse getSearchResponse() {
return searchResponse;
}

public boolean isFound() {
return found;
}
}
Loading