Skip to content

Commit aee94ba

Browse files
committed
feat: add methods to cast Generative to specific classes
1 parent 30744ec commit aee94ba

3 files changed

Lines changed: 228 additions & 7 deletions

File tree

src/main/java/io/weaviate/client6/v1/api/collections/Generative.java

Lines changed: 200 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
import io.weaviate.client6.v1.api.collections.generative.OpenAiGenerative;
2929
import io.weaviate.client6.v1.api.collections.generative.XaiGenerative;
3030
import io.weaviate.client6.v1.internal.ObjectBuilder;
31+
import io.weaviate.client6.v1.internal.TaggedUnion;
3132
import io.weaviate.client6.v1.internal.json.JsonEnum;
3233

33-
public interface Generative {
34+
public interface Generative extends TaggedUnion<Generative.Kind, Object> {
3435
public enum Kind implements JsonEnum<Kind> {
3536
ANYSCALE("generative-anyscale"),
3637
AWS("generative-aws"),
@@ -64,10 +65,6 @@ public static Kind valueOfJson(String jsonValue) {
6465
}
6566
}
6667

67-
Kind _kind();
68-
69-
Object _self();
70-
7168
/** Configure a default {@code generative-anthropic} module. */
7269
public static Generative anthropic() {
7370
return AnthropicGenerative.of();
@@ -277,6 +274,201 @@ public static Generative xai(Function<XaiGenerative.Builder, ObjectBuilder<XaiGe
277274
return XaiGenerative.of(fn);
278275
}
279276

277+
/** Is this a {@code generative-anyscale} provider? */
278+
default boolean isAnyscale() {
279+
return _is(Generative.Kind.ANYSCALE);
280+
}
281+
282+
/**
283+
* Get as {@link AnyscaleGenerative} instance.
284+
*
285+
* @throws IllegalStateException if the current kind is not
286+
* {@code generative-anyscale}.
287+
*/
288+
default AnyscaleGenerative asAnyscale() {
289+
return _as(Generative.Kind.ANYSCALE);
290+
}
291+
292+
/** Is this a {@code generative-aws} provider? */
293+
default boolean isAws() {
294+
return _is(Generative.Kind.AWS);
295+
}
296+
297+
/**
298+
* Get as {@link AwsGenerative} instance.
299+
*
300+
* @throws IllegalStateException if the current kind is not
301+
* {@code generative-aws}.
302+
*/
303+
default AwsGenerative asAws() {
304+
return _as(Generative.Kind.AWS);
305+
}
306+
307+
/** Is this a {@code generative-anthropic} provider? */
308+
default boolean isAnthropic() {
309+
return _is(Generative.Kind.ANTHROPIC);
310+
}
311+
312+
/**
313+
* Get as {@link AnthropicGenerative} instance.
314+
*
315+
* @throws IllegalStateException if the current kind is not
316+
* {@code generative-anthropic}.
317+
*/
318+
default AnthropicGenerative asAnthropic() {
319+
return _as(Generative.Kind.ANTHROPIC);
320+
}
321+
322+
/** Is this a {@code generative-cohere} provider? */
323+
default boolean isCohere() {
324+
return _is(Generative.Kind.COHERE);
325+
}
326+
327+
/**
328+
* Get as {@link CohereGenerative} instance.
329+
*
330+
* @throws IllegalStateException if the current kind is not
331+
* {@code generative-cohere}.
332+
*/
333+
default CohereGenerative asCohere() {
334+
return _as(Generative.Kind.COHERE);
335+
}
336+
337+
/** Is this a {@code generative-databricks} provider? */
338+
default boolean isDatabricks() {
339+
return _is(Generative.Kind.DATABRICKS);
340+
}
341+
342+
/**
343+
* Get as {@link DatabricksGenerative} instance.
344+
*
345+
* @throws IllegalStateException if the current kind is not
346+
* {@code generative-databricks}.
347+
*/
348+
default DatabricksGenerative asDatabricks() {
349+
return _as(Generative.Kind.DATABRICKS);
350+
}
351+
352+
/** Is this a {@code generative-friendliai} provider? */
353+
default boolean isFriendliai() {
354+
return _is(Generative.Kind.FRIENDLIAI);
355+
}
356+
357+
/**
358+
* Get as {@link FriendliaiGenerative} instance.
359+
*
360+
* @throws IllegalStateException if the current kind is not
361+
* {@code generative-friendliai}.
362+
*/
363+
default FriendliaiGenerative asFriendliai() {
364+
return _as(Generative.Kind.FRIENDLIAI);
365+
}
366+
367+
/** Is this a {@code generative-palm} provider? */
368+
default boolean isGoogle() {
369+
return _is(Generative.Kind.GOOGLE);
370+
}
371+
372+
/**
373+
* Get as {@link GoogleGenerative} instance.
374+
*
375+
* @throws IllegalStateException if the current kind is not
376+
* {@code generative-palm}.
377+
*/
378+
default GoogleGenerative asGoogle() {
379+
return _as(Generative.Kind.GOOGLE);
380+
}
381+
382+
/** Is this a {@code generative-mistral} provider? */
383+
default boolean isMistral() {
384+
return _is(Generative.Kind.MISTRAL);
385+
}
386+
387+
/**
388+
* Get as {@link MistralGenerative} instance.
389+
*
390+
* @throws IllegalStateException if the current kind is not
391+
* {@code generative-mistral}.
392+
*/
393+
default MistralGenerative asMistral() {
394+
return _as(Generative.Kind.MISTRAL);
395+
}
396+
397+
/** Is this a {@code generative-nvidia} provider? */
398+
default boolean isNvidia() {
399+
return _is(Generative.Kind.NVIDIA);
400+
}
401+
402+
/**
403+
* Get as {@link NvidiaGenerative} instance.
404+
*
405+
* @throws IllegalStateException if the current kind is not
406+
* {@code generative-nvidia}.
407+
*/
408+
default NvidiaGenerative asNvidia() {
409+
return _as(Generative.Kind.NVIDIA);
410+
}
411+
412+
/** Is this a {@code generative-ollama} provider? */
413+
default boolean isOllama() {
414+
return _is(Generative.Kind.OLLAMA);
415+
}
416+
417+
/**
418+
* Get as {@link OllamaGenerative} instance.
419+
*
420+
* @throws IllegalStateException if the current kind is not
421+
* {@code generative-ollama}.
422+
*/
423+
default OllamaGenerative asOllama() {
424+
return _as(Generative.Kind.OLLAMA);
425+
}
426+
427+
/** Is this a {@code generative-openai} provider? */
428+
default boolean isOpenAi() {
429+
return _is(Generative.Kind.OPENAI);
430+
}
431+
432+
/**
433+
* Get as {@link OpenAiGenerative} instance.
434+
*
435+
* @throws IllegalStateException if the current kind is not
436+
* {@code generative-openai}.
437+
*/
438+
default OpenAiGenerative asOpenAi() {
439+
return _as(Generative.Kind.OPENAI);
440+
}
441+
442+
/** Is this an Azure-specific {@code generative-openai} provider? */
443+
default boolean isAzure() {
444+
return _is(Generative.Kind.AZURE_OPENAI);
445+
}
446+
447+
/**
448+
* Get as {@link AzureOpenAiGenerative} instance.
449+
*
450+
* @throws IllegalStateException if the current kind is not
451+
* {@code generative-openai}.
452+
*/
453+
default AzureOpenAiGenerative asAzure() {
454+
return _as(Generative.Kind.AZURE_OPENAI);
455+
}
456+
457+
/** Is this a {@code generative-xai} provider? */
458+
default boolean isXai() {
459+
return _is(Generative.Kind.XAI);
460+
}
461+
462+
/**
463+
* Get as {@link XaiGenerative} instance.
464+
*
465+
* @throws IllegalStateException if the current kind is not
466+
* {@code generative-xai}.
467+
*/
468+
default XaiGenerative asXai() {
469+
return _as(Generative.Kind.XAI);
470+
}
471+
280472
public static enum CustomTypeAdapterFactory implements TypeAdapterFactory {
281473
INSTANCE;
282474

@@ -316,14 +508,15 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
316508
init(gson);
317509
}
318510

319-
final TypeAdapter<T> writeAdapter = (TypeAdapter<T>) gson.getDelegateAdapter(this, TypeToken.get(rawType));
511+
final TypeAdapter<Generative> writeAdapter = (TypeAdapter<Generative>) gson.getDelegateAdapter(this,
512+
TypeToken.get(rawType));
320513
return (TypeAdapter<T>) new TypeAdapter<Generative>() {
321514

322515
@Override
323516
public void write(JsonWriter out, Generative value) throws IOException {
324517
out.beginObject();
325518
out.name(value._kind().jsonValue());
326-
writeAdapter.write(out, (T) value._self());
519+
writeAdapter.write(out, value._self());
327520
out.endObject();
328521
}
329522

src/main/java/io/weaviate/client6/v1/api/collections/generative/AnthropicGenerative.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import io.weaviate.client6.v1.api.collections.Generative;
1111
import io.weaviate.client6.v1.internal.ObjectBuilder;
12+
import io.weaviate.client6.v1.internal.TaggedUnion;
1213

1314
public record AnthropicGenerative(
1415
@SerializedName("model") String model,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.weaviate.client6.v1.internal;
2+
3+
public interface TaggedUnion<KindT extends Enum<KindT>, SelfT> {
4+
KindT _kind();
5+
6+
SelfT _self();
7+
8+
/** Does the current instance have the kind? */
9+
default boolean _is(KindT kind) {
10+
return _kind() == kind;
11+
}
12+
13+
/** Convert tagged union instance to one of its variants. */
14+
default <Value extends TaggedUnion<KindT, SelfT>> Value _as(KindT kind) {
15+
return TaggedUnion.as(this, kind);
16+
}
17+
18+
/** Convert tagged union instance to one of its variants. */
19+
public static <Union extends TaggedUnion<Tag, ?>, Tag extends Enum<Tag>, Value> Value as(Union union, Tag kind) {
20+
if (union._is(kind)) {
21+
@SuppressWarnings("unchecked")
22+
Value value = (Value) union._self();
23+
return value;
24+
}
25+
throw new IllegalStateException("Cannot convert '%s' variant to '%s'".formatted(union._kind(), kind));
26+
}
27+
}

0 commit comments

Comments
 (0)