|
| 1 | +/* |
| 2 | +Copyright 2026 The Spice.ai OSS Authors |
| 3 | +
|
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +of this software and associated documentation files (the "Software"), to deal |
| 6 | +in the Software without restriction, including without limitation the rights |
| 7 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +copies of the Software, and to permit persons to whom the Software is |
| 9 | +furnished to do so, subject to the following conditions: |
| 10 | +
|
| 11 | +The above copyright notice and this permission notice shall be included in all |
| 12 | +copies or substantial portions of the Software. |
| 13 | +
|
| 14 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +SOFTWARE. |
| 21 | +*/ |
| 22 | + |
| 23 | +package ai.spice; |
| 24 | + |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +import com.google.gson.annotations.SerializedName; |
| 30 | + |
| 31 | +/** |
| 32 | + * A natural-language query against the runtime's {@code /v1/nsql} endpoint. |
| 33 | + * |
| 34 | + * <p> |
| 35 | + * Only {@code query} is required. The runtime needs an LLM model configured |
| 36 | + * in the Spicepod to translate it; when exactly one is configured, {@code |
| 37 | + * model} may be left unset and the runtime selects it. |
| 38 | + */ |
| 39 | +public class NsqlRequest { |
| 40 | + @SerializedName("query") |
| 41 | + private final String query; |
| 42 | + |
| 43 | + @SerializedName("model") |
| 44 | + private String model; |
| 45 | + |
| 46 | + @SerializedName("datasets") |
| 47 | + private List<String> datasets; |
| 48 | + |
| 49 | + // Boxed and left null when unset, rather than a primitive boolean, so Gson |
| 50 | + // omits this field entirely instead of always sending "sample_data_enabled": |
| 51 | + // false — the runtime already defaults it to false, and gospice's equivalent |
| 52 | + // field is "omitempty". |
| 53 | + @SerializedName("sample_data_enabled") |
| 54 | + private Boolean sampleDataEnabled; |
| 55 | + |
| 56 | + @SerializedName("prompt_cache_key") |
| 57 | + private String promptCacheKey; |
| 58 | + |
| 59 | + /** |
| 60 | + * Creates a request for the given natural-language query. |
| 61 | + * |
| 62 | + * @param query the question to answer, in natural language |
| 63 | + */ |
| 64 | + public NsqlRequest(String query) { |
| 65 | + this.query = query; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Names the LLM used to generate SQL. When unset, the runtime uses the |
| 70 | + * only compatible model configured in the Spicepod, and reports an error |
| 71 | + * if there is not exactly one. |
| 72 | + * |
| 73 | + * @param model the model name |
| 74 | + * @return this request |
| 75 | + */ |
| 76 | + public NsqlRequest withModel(String model) { |
| 77 | + this.model = model; |
| 78 | + return this; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Hints which datasets to sample when building model context. This is a |
| 83 | + * sampling hint only — it does not restrict which tables the generated |
| 84 | + * query may reference. When unset, all datasets are used. |
| 85 | + * |
| 86 | + * @param datasets the dataset names to sample |
| 87 | + * @return this request |
| 88 | + */ |
| 89 | + public NsqlRequest withDatasets(List<String> datasets) { |
| 90 | + this.datasets = datasets == null ? null : new ArrayList<>(datasets); |
| 91 | + return this; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Includes sample rows in the context given to the model. It improves |
| 96 | + * generation on ambiguous schemas at the cost of sending data values to |
| 97 | + * the model. |
| 98 | + * |
| 99 | + * @param sampleDataEnabled whether to include sample data |
| 100 | + * @return this request |
| 101 | + */ |
| 102 | + public NsqlRequest withSampleDataEnabled(boolean sampleDataEnabled) { |
| 103 | + this.sampleDataEnabled = sampleDataEnabled; |
| 104 | + return this; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * A stable key forwarded to the model provider for prompt caching. Reuse |
| 109 | + * it across related requests to benefit from it. |
| 110 | + * |
| 111 | + * @param promptCacheKey the cache key |
| 112 | + * @return this request |
| 113 | + */ |
| 114 | + public NsqlRequest withPromptCacheKey(String promptCacheKey) { |
| 115 | + this.promptCacheKey = promptCacheKey; |
| 116 | + return this; |
| 117 | + } |
| 118 | + |
| 119 | + public String getQuery() { |
| 120 | + return this.query; |
| 121 | + } |
| 122 | + |
| 123 | + public String getModel() { |
| 124 | + return this.model; |
| 125 | + } |
| 126 | + |
| 127 | + public List<String> getDatasets() { |
| 128 | + return this.datasets == null ? null : Collections.unmodifiableList(this.datasets); |
| 129 | + } |
| 130 | + |
| 131 | + public boolean isSampleDataEnabled() { |
| 132 | + return Boolean.TRUE.equals(this.sampleDataEnabled); |
| 133 | + } |
| 134 | + |
| 135 | + public String getPromptCacheKey() { |
| 136 | + return this.promptCacheKey; |
| 137 | + } |
| 138 | +} |
0 commit comments