12
12
import org .elasticsearch .common .io .stream .StreamInput ;
13
13
import org .elasticsearch .common .io .stream .StreamOutput ;
14
14
import org .elasticsearch .index .mapper .vectors .DenseVectorFieldMapper ;
15
+ import org .elasticsearch .index .mapper .vectors .DenseVectorFieldMapperTestUtils ;
15
16
import org .elasticsearch .inference .Model ;
16
17
import org .elasticsearch .inference .ModelConfigurations ;
17
18
import org .elasticsearch .inference .ModelSecrets ;
25
26
import org .elasticsearch .xpack .inference .services .ServiceUtils ;
26
27
27
28
import java .io .IOException ;
29
+ import java .util .ArrayList ;
30
+ import java .util .Arrays ;
28
31
import java .util .HashMap ;
32
+ import java .util .List ;
29
33
import java .util .Map ;
30
34
35
+ import static org .elasticsearch .index .mapper .vectors .DenseVectorFieldMapper .BBQ_MIN_DIMS ;
31
36
import static org .elasticsearch .test .ESTestCase .randomAlphaOfLength ;
32
37
import static org .elasticsearch .test .ESTestCase .randomFrom ;
33
38
import static org .elasticsearch .test .ESTestCase .randomInt ;
@@ -39,9 +44,60 @@ public static TestModel createRandomInstance() {
39
44
}
40
45
41
46
public static TestModel createRandomInstance (TaskType taskType ) {
42
- var dimensions = taskType == TaskType .TEXT_EMBEDDING ? randomInt (64 ) : null ;
43
- var similarity = taskType == TaskType .TEXT_EMBEDDING ? randomFrom (SimilarityMeasure .values ()) : null ;
44
- var elementType = taskType == TaskType .TEXT_EMBEDDING ? randomFrom (DenseVectorFieldMapper .ElementType .values ()) : null ;
47
+ return createRandomInstance (taskType , null , null );
48
+ }
49
+
50
+ public static TestModel createRandomInstance (
51
+ TaskType taskType ,
52
+ List <DenseVectorFieldMapper .ElementType > excludedElementTypes ,
53
+ List <SimilarityMeasure > excludedSimilarities
54
+ ) {
55
+ // Use a max dimension count that has a reasonable probability of being compatible with BBQ
56
+ return createRandomInstance (taskType , excludedElementTypes , excludedSimilarities , BBQ_MIN_DIMS * 2 );
57
+ }
58
+
59
+ public static TestModel createRandomInstance (
60
+ TaskType taskType ,
61
+ List <DenseVectorFieldMapper .ElementType > excludedElementTypes ,
62
+ List <SimilarityMeasure > excludedSimilarities ,
63
+ int maxDimensions
64
+ ) {
65
+ List <DenseVectorFieldMapper .ElementType > supportedElementTypes = new ArrayList <>(
66
+ Arrays .asList (DenseVectorFieldMapper .ElementType .values ())
67
+ );
68
+ if (excludedElementTypes != null ) {
69
+ supportedElementTypes .removeAll (excludedElementTypes );
70
+ if (supportedElementTypes .isEmpty ()) {
71
+ throw new IllegalArgumentException ("No supported element types with excluded element types " + excludedElementTypes );
72
+ }
73
+ }
74
+
75
+ var elementType = taskType == TaskType .TEXT_EMBEDDING ? randomFrom (supportedElementTypes ) : null ;
76
+ var dimensions = taskType == TaskType .TEXT_EMBEDDING
77
+ ? DenseVectorFieldMapperTestUtils .randomCompatibleDimensions (elementType , maxDimensions )
78
+ : null ;
79
+
80
+ SimilarityMeasure similarity = null ;
81
+ if (taskType == TaskType .TEXT_EMBEDDING ) {
82
+ List <SimilarityMeasure > supportedSimilarities = new ArrayList <>(
83
+ DenseVectorFieldMapperTestUtils .getSupportedSimilarities (elementType )
84
+ );
85
+ if (excludedSimilarities != null ) {
86
+ supportedSimilarities .removeAll (excludedSimilarities );
87
+ }
88
+
89
+ if (supportedSimilarities .isEmpty ()) {
90
+ throw new IllegalArgumentException (
91
+ "No supported similarities for combination of element type ["
92
+ + elementType
93
+ + "] and excluded similarities "
94
+ + (excludedSimilarities == null ? List .of () : excludedSimilarities )
95
+ );
96
+ }
97
+
98
+ similarity = randomFrom (supportedSimilarities );
99
+ }
100
+
45
101
return new TestModel (
46
102
randomAlphaOfLength (4 ),
47
103
taskType ,
0 commit comments