@@ -35,7 +35,7 @@ public function searchWithResults(): void
35
35
{
36
36
$ searchTerm = 'find similar documents ' ;
37
37
$ vector = new Vector ([0.1 , 0.2 , 0.3 ]);
38
-
38
+
39
39
$ document1 = new VectorDocument (
40
40
Uuid::v4 (),
41
41
$ vector ,
@@ -46,112 +46,112 @@ public function searchWithResults(): void
46
46
$ vector ,
47
47
new Metadata (['title ' => 'Document 2 ' , 'content ' => 'Second document content ' ]),
48
48
);
49
-
49
+
50
50
$ rawResult = $ this ->createMock (RawResultInterface::class);
51
51
$ vectorResult = new VectorResult ($ vector );
52
52
$ resultPromise = new ResultPromise (
53
- fn () => $ vectorResult ,
53
+ fn () => $ vectorResult ,
54
54
$ rawResult
55
55
);
56
-
56
+
57
57
$ platform = $ this ->createMock (PlatformInterface::class);
58
58
$ platform ->expects ($ this ->once ())
59
59
->method ('invoke ' )
60
60
->with ($ this ->isInstanceOf (Model::class), $ searchTerm )
61
61
->willReturn ($ resultPromise );
62
-
62
+
63
63
$ vectorStore = $ this ->createMock (VectorStoreInterface::class);
64
64
$ vectorStore ->expects ($ this ->once ())
65
65
->method ('query ' )
66
66
->with ($ vector )
67
67
->willReturn ([$ document1 , $ document2 ]);
68
-
68
+
69
69
$ model = new Model ('test-model ' );
70
70
$ similaritySearch = new SimilaritySearch ($ platform , $ model , $ vectorStore );
71
-
71
+
72
72
$ result = $ similaritySearch ($ searchTerm );
73
-
73
+
74
74
$ expected = 'Found documents with following information: ' .PHP_EOL ;
75
75
$ expected .= '{"title":"Document 1","content":"First document content"} ' ;
76
76
$ expected .= '{"title":"Document 2","content":"Second document content"} ' ;
77
-
77
+
78
78
$ this ->assertSame ($ expected , $ result );
79
79
$ this ->assertSame ([$ document1 , $ document2 ], $ similaritySearch ->usedDocuments );
80
80
}
81
-
81
+
82
82
#[Test]
83
83
public function searchWithoutResults (): void
84
84
{
85
85
$ searchTerm = 'find nothing ' ;
86
86
$ vector = new Vector ([0.1 , 0.2 , 0.3 ]);
87
-
87
+
88
88
$ rawResult = $ this ->createMock (RawResultInterface::class);
89
89
$ vectorResult = new VectorResult ($ vector );
90
90
$ resultPromise = new ResultPromise (
91
- fn () => $ vectorResult ,
91
+ fn () => $ vectorResult ,
92
92
$ rawResult
93
93
);
94
-
94
+
95
95
$ platform = $ this ->createMock (PlatformInterface::class);
96
96
$ platform ->expects ($ this ->once ())
97
97
->method ('invoke ' )
98
98
->with ($ this ->isInstanceOf (Model::class), $ searchTerm )
99
99
->willReturn ($ resultPromise );
100
-
100
+
101
101
$ vectorStore = $ this ->createMock (VectorStoreInterface::class);
102
102
$ vectorStore ->expects ($ this ->once ())
103
103
->method ('query ' )
104
104
->with ($ vector )
105
105
->willReturn ([]);
106
-
106
+
107
107
$ model = new Model ('test-model ' );
108
108
$ similaritySearch = new SimilaritySearch ($ platform , $ model , $ vectorStore );
109
-
109
+
110
110
$ result = $ similaritySearch ($ searchTerm );
111
-
111
+
112
112
$ this ->assertSame ('No results found ' , $ result );
113
113
$ this ->assertSame ([], $ similaritySearch ->usedDocuments );
114
114
}
115
-
115
+
116
116
#[Test]
117
117
public function searchWithSingleResult (): void
118
118
{
119
119
$ searchTerm = 'specific query ' ;
120
120
$ vector = new Vector ([0.5 , 0.6 , 0.7 ]);
121
-
121
+
122
122
$ document = new VectorDocument (
123
123
Uuid::v4 (),
124
124
$ vector ,
125
125
new Metadata (['title ' => 'Single Document ' , 'description ' => 'Only one match ' ]),
126
126
);
127
-
127
+
128
128
$ rawResult = $ this ->createMock (RawResultInterface::class);
129
129
$ vectorResult = new VectorResult ($ vector );
130
130
$ resultPromise = new ResultPromise (
131
- fn () => $ vectorResult ,
131
+ fn () => $ vectorResult ,
132
132
$ rawResult
133
133
);
134
-
134
+
135
135
$ platform = $ this ->createMock (PlatformInterface::class);
136
136
$ platform ->expects ($ this ->once ())
137
137
->method ('invoke ' )
138
138
->with ($ this ->isInstanceOf (Model::class), $ searchTerm )
139
139
->willReturn ($ resultPromise );
140
-
140
+
141
141
$ vectorStore = $ this ->createMock (VectorStoreInterface::class);
142
142
$ vectorStore ->expects ($ this ->once ())
143
143
->method ('query ' )
144
144
->with ($ vector )
145
145
->willReturn ([$ document ]);
146
-
146
+
147
147
$ model = new Model ('test-model ' );
148
148
$ similaritySearch = new SimilaritySearch ($ platform , $ model , $ vectorStore );
149
-
149
+
150
150
$ result = $ similaritySearch ($ searchTerm );
151
-
151
+
152
152
$ expected = 'Found documents with following information: ' .PHP_EOL ;
153
153
$ expected .= '{"title":"Single Document","description":"Only one match"} ' ;
154
-
154
+
155
155
$ this ->assertSame ($ expected , $ result );
156
156
$ this ->assertSame ([$ document ], $ similaritySearch ->usedDocuments );
157
157
}
0 commit comments