Skip to content
Merged
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,39 @@
/*
* Copyright 2021-2026 Aklivity Inc
*
* Licensed under the Aklivity Community License (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* https://www.aklivity.io/aklivity-community-license/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package io.aklivity.zilla.config.engine;

import java.util.Map;
import java.util.function.Function;

public final class StoredConfig extends NamedConfig
{
StoredConfig(
String name,
Map<String, Config> extensions)
{
super(name, extensions);
}

public static <T> StoredConfigBuilder<T> builder(
Function<StoredConfig, T> mapper)
{
return new StoredConfigBuilder<>(mapper);
}

public static StoredConfigBuilder<StoredConfig> builder()
{
return new StoredConfigBuilder<>(StoredConfig.class::cast);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2021-2026 Aklivity Inc
*
* Licensed under the Aklivity Community License (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* https://www.aklivity.io/aklivity-community-license/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package io.aklivity.zilla.config.engine;

import java.util.function.Function;

public final class StoredConfigBuilder<T> extends ConfigBuilder.Extensible<T, StoredConfigBuilder<T>>
{
private final Function<StoredConfig, T> mapper;

private String name;

StoredConfigBuilder(
Function<StoredConfig, T> mapper)
{
this.mapper = mapper;
}

@Override
@SuppressWarnings("unchecked")
protected Class<StoredConfigBuilder<T>> thisType()
{
return (Class<StoredConfigBuilder<T>>) getClass();
}

public StoredConfigBuilder<T> name(
String name)
{
this.name = name;
return this;
}

@Override
public T build()
{
return mapper.apply(new StoredConfig(name, extensions()));
}
}
4 changes: 4 additions & 0 deletions examples/tcp.echo.embedding/etc/zilla.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ name: example
embeddings:
moderator0:
type: glove
stores:
cache0:
type: memory
bindings:
north_tcp_server:
type: tcp
Expand All @@ -22,6 +25,7 @@ bindings:
- "You will never believe what happened next."
- "I have a massive secret but I absolutely cannot tell anyone here."
threshold: 0.94
store: cache0
telemetry:
exporters:
stdout_logs_exporter:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,28 @@
import io.aklivity.zilla.config.engine.EmbeddedConfig;
import io.aklivity.zilla.config.engine.ModelConfig;
import io.aklivity.zilla.config.engine.NamedConfig;
import io.aklivity.zilla.config.engine.StoredConfig;

public final class VectorModelConfig extends ModelConfig
{
public final EmbeddedConfig embedding;
public final List<String> reject;
public final double threshold;
public final StoredConfig store;

VectorModelConfig(
EmbeddedConfig embedding,
List<String> reject,
double threshold,
StoredConfig store,
Map<String, Config> extensions,
List<NamedConfig> refs)
{
super("vector", null, null, extensions, withEmbedding(embedding, refs));
super("vector", null, null, extensions, withRefs(embedding, store, refs));
this.embedding = embedding;
this.reject = reject;
this.threshold = threshold;
this.store = store;
}

public static <T> VectorModelConfigBuilder<T> builder(
Expand All @@ -54,15 +58,20 @@ public static VectorModelConfigBuilder<VectorModelConfig> builder()
return new VectorModelConfigBuilder<>(VectorModelConfig.class::cast);
}

private static List<NamedConfig> withEmbedding(
private static List<NamedConfig> withRefs(
EmbeddedConfig embedding,
StoredConfig store,
List<NamedConfig> refs)
{
List<NamedConfig> all = new ArrayList<>();
if (embedding != null)
{
all.add(embedding);
}
if (store != null)
{
all.add(store);
}
if (refs != null)
{
all.addAll(refs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import io.aklivity.zilla.config.engine.ConfigBuilder;
import io.aklivity.zilla.config.engine.EmbeddedConfig;
import io.aklivity.zilla.config.engine.StoredConfig;

public class VectorModelConfigBuilder<T> extends ConfigBuilder.Extensible<T, VectorModelConfigBuilder<T>>
{
Expand All @@ -28,6 +29,7 @@ public class VectorModelConfigBuilder<T> extends ConfigBuilder.Extensible<T, Vec
private EmbeddedConfig embedding;
private List<String> reject;
private double threshold;
private StoredConfig store;

VectorModelConfigBuilder(
Function<VectorModelConfig, T> mapper)
Expand Down Expand Up @@ -74,9 +76,16 @@ public VectorModelConfigBuilder<T> threshold(
return this;
}

public VectorModelConfigBuilder<T> store(
String store)
{
this.store = store != null ? StoredConfig.builder().name(store).build() : null;
return this;
}

@Override
public T build()
{
return mapper.apply(new VectorModelConfig(embedding, reject, threshold, extensions(), refs()));
return mapper.apply(new VectorModelConfig(embedding, reject, threshold, store, extensions(), refs()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public final class VectorModelConfigAdapter extends ConfigAdapter.Extensible<Mod
private static final String EMBEDDING_NAME = "embedding";
private static final String REJECT_NAME = "reject";
private static final String THRESHOLD_NAME = "threshold";
private static final String STORE_NAME = "store";

public VectorModelConfigAdapter(
List<ConfigExtAdapter<ModelConfig>> extensions)
Expand Down Expand Up @@ -66,6 +67,11 @@ public JsonValue adaptToJson(

builder.add(THRESHOLD_NAME, model.threshold);

if (model.store != null)
{
builder.add(STORE_NAME, model.store.name);
}

injectExtensions(model, builder);

return builder.build();
Expand All @@ -89,10 +95,15 @@ public ModelConfig adaptFromJson(
? object.getJsonNumber(THRESHOLD_NAME).doubleValue()
: 0.0;

String store = object.containsKey(STORE_NAME)
? object.getString(STORE_NAME)
: null;

VectorModelConfigBuilder<VectorModelConfig> builder = VectorModelConfig.builder()
.embedding(embedding)
.reject(reject)
.threshold(threshold);
.threshold(threshold)
.store(store);

injectExtensions(object, builder);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public void shouldReadVectorModel()
"reject phrase one",
"reject phrase two"
],
"threshold": 0.85
"threshold": 0.85,
"store": "cache0"
}""";

// WHEN
Expand All @@ -68,6 +69,30 @@ public void shouldReadVectorModel()
assertThat(config.embedding.name, equalTo("moderator0"));
assertThat(config.reject, contains("reject phrase one", "reject phrase two"));
assertThat(config.threshold, equalTo(0.85));
assertThat(config.store.name, equalTo("cache0"));
}

@Test
public void shouldDefaultStoreToNullWhenAbsent()
{
// GIVEN -- the store property is required by the vector model's own JSON schema, but the
// adapter itself stays defensive about a config built or parsed without going through it
String json = """
{
"model": "vector",
"embedding": "moderator0",
"reject":
[
"reject phrase one"
],
"threshold": 0.85
}""";

// WHEN
VectorModelConfig config = jsonb.fromJson(json, VectorModelConfig.class);

// THEN
assertThat(config.store, nullValue());
}

@Test
Expand All @@ -83,13 +108,15 @@ public void shouldWriteVectorModel()
"\"reject phrase one\"," +
"\"reject phrase two\"" +
"]," +
"\"threshold\":0.85" +
"\"threshold\":0.85," +
"\"store\":\"cache0\"" +
"}";
VectorModelConfig config = VectorModelConfig.builder()
.embedding("moderator0")
.reject("reject phrase one")
.reject("reject phrase two")
.threshold(0.85)
.store("cache0")
.build();

// WHEN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ bindings:
reject:
- "You will never believe what happened next."
threshold: 0.85
store: cache0
exit: test
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ name: test
embeddings:
embedding0:
type: test
stores:
cache0:
type: test
bindings:
net0:
type: test
Expand All @@ -29,4 +32,5 @@ bindings:
reject:
- "reject this message"
threshold: 0.99
store: cache0
exit: app0
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,19 @@
"type": "number",
"minimum": 0,
"maximum": 1
},
"store":
{
"title": "Store",
"type": "string"
}
},
"required":
[
"embedding",
"reject",
"threshold"
"threshold",
"store"
]
}
}
Expand Down
Loading
Loading