|
1 |
| -/* |
2 |
| - * Copyright OpenSearch Contributors |
3 |
| - * SPDX-License-Identifier: Apache-2.0 |
4 |
| - */ |
5 |
| - |
6 | 1 | package org.opensearch.ml.common.model;
|
7 | 2 |
|
8 |
| -import lombok.Builder; |
9 |
| -import lombok.EqualsAndHashCode; |
10 |
| -import lombok.Getter; |
11 |
| -import org.opensearch.core.common.io.stream.StreamInput; |
| 3 | +import org.opensearch.client.Client; |
12 | 4 | import org.opensearch.core.common.io.stream.StreamOutput;
|
| 5 | +import org.opensearch.core.xcontent.NamedXContentRegistry; |
13 | 6 | import org.opensearch.core.xcontent.ToXContentObject;
|
14 |
| -import org.opensearch.core.xcontent.XContentBuilder; |
15 |
| -import org.opensearch.core.xcontent.XContentParser; |
16 | 7 |
|
17 | 8 | import java.io.IOException;
|
18 |
| -import java.util.ArrayList; |
19 |
| -import java.util.List; |
20 |
| - |
21 |
| -import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken; |
22 |
| - |
23 |
| -@EqualsAndHashCode |
24 |
| -@Getter |
25 |
| -public class Guardrail implements ToXContentObject { |
26 |
| - public static final String STOP_WORDS_FIELD = "stop_words"; |
27 |
| - public static final String REGEX_FIELD = "regex"; |
28 |
| - |
29 |
| - private List<StopWords> stopWords; |
30 |
| - private String[] regex; |
31 |
| - |
32 |
| - @Builder(toBuilder = true) |
33 |
| - public Guardrail(List<StopWords> stopWords, String[] regex) { |
34 |
| - this.stopWords = stopWords; |
35 |
| - this.regex = regex; |
36 |
| - } |
37 |
| - |
38 |
| - public Guardrail(StreamInput input) throws IOException { |
39 |
| - if (input.readBoolean()) { |
40 |
| - stopWords = new ArrayList<>(); |
41 |
| - int size = input.readInt(); |
42 |
| - for (int i=0; i<size; i++) { |
43 |
| - stopWords.add(new StopWords(input)); |
44 |
| - } |
45 |
| - } |
46 |
| - regex = input.readStringArray(); |
47 |
| - } |
48 |
| - |
49 |
| - public void writeTo(StreamOutput out) throws IOException { |
50 |
| - if (stopWords != null && stopWords.size() > 0) { |
51 |
| - out.writeBoolean(true); |
52 |
| - out.writeInt(stopWords.size()); |
53 |
| - for (StopWords e : stopWords) { |
54 |
| - e.writeTo(out); |
55 |
| - } |
56 |
| - } else { |
57 |
| - out.writeBoolean(false); |
58 |
| - } |
59 |
| - out.writeStringArray(regex); |
60 |
| - } |
61 | 9 |
|
62 |
| - @Override |
63 |
| - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
64 |
| - builder.startObject(); |
65 |
| - if (stopWords != null && stopWords.size() > 0) { |
66 |
| - builder.field(STOP_WORDS_FIELD, stopWords); |
67 |
| - } |
68 |
| - if (regex != null) { |
69 |
| - builder.field(REGEX_FIELD, regex); |
70 |
| - } |
71 |
| - builder.endObject(); |
72 |
| - return builder; |
73 |
| - } |
| 10 | +public abstract class Guardrail implements ToXContentObject { |
74 | 11 |
|
75 |
| - public static Guardrail parse(XContentParser parser) throws IOException { |
76 |
| - List<StopWords> stopWords = null; |
77 |
| - String[] regex = null; |
| 12 | + public abstract void writeTo(StreamOutput out) throws IOException; |
78 | 13 |
|
79 |
| - ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser); |
80 |
| - while (parser.nextToken() != XContentParser.Token.END_OBJECT) { |
81 |
| - String fieldName = parser.currentName(); |
82 |
| - parser.nextToken(); |
| 14 | + public abstract Boolean validate(String input); |
83 | 15 |
|
84 |
| - switch (fieldName) { |
85 |
| - case STOP_WORDS_FIELD: |
86 |
| - stopWords = new ArrayList<>(); |
87 |
| - ensureExpectedToken(XContentParser.Token.START_ARRAY, parser.currentToken(), parser); |
88 |
| - while (parser.nextToken() != XContentParser.Token.END_ARRAY) { |
89 |
| - stopWords.add(StopWords.parse(parser)); |
90 |
| - } |
91 |
| - break; |
92 |
| - case REGEX_FIELD: |
93 |
| - regex = parser.list().toArray(new String[0]); |
94 |
| - break; |
95 |
| - default: |
96 |
| - parser.skipChildren(); |
97 |
| - break; |
98 |
| - } |
99 |
| - } |
100 |
| - return Guardrail.builder() |
101 |
| - .stopWords(stopWords) |
102 |
| - .regex(regex) |
103 |
| - .build(); |
104 |
| - } |
| 16 | + public abstract void init(NamedXContentRegistry xContentRegistry, Client client); |
105 | 17 | }
|
0 commit comments