-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcomparison-filter-parity.test.js
More file actions
219 lines (199 loc) · 5.95 KB
/
comparison-filter-parity.test.js
File metadata and controls
219 lines (199 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
"use strict";
// This file keeps the ComparisonFilter contract honest.
// It checks that vector-store search forwards `in` and `nin` filters unchanged, and that the user-facing picker/help/example surfaces advertise that contract clearly.
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const test = require("node:test");
function withMockedOpenAI(FakeOpenAI, callback) {
const openaiModule = require("openai");
const originalDescriptor = Object.getOwnPropertyDescriptor(openaiModule, "OpenAI");
Object.defineProperty(openaiModule, "OpenAI", {
value: FakeOpenAI,
configurable: true,
enumerable: true,
writable: true,
});
const run = async () => {
try {
return await callback();
} finally {
if (originalDescriptor) {
Object.defineProperty(openaiModule, "OpenAI", originalDescriptor);
}
}
};
return run();
}
const locale = JSON.parse(
fs.readFileSync(path.join(__dirname, "..", "locales", "en-US", "node.json"), "utf8")
);
const vectorStoresHelp = fs.readFileSync(
path.join(__dirname, "..", "src", "vector-stores", "help.html"),
"utf8"
);
const vectorStoreSearchExample = JSON.parse(
fs.readFileSync(path.join(__dirname, "..", "examples", "vector-store-search.json"), "utf8")
);
test("searchVectorStore forwards ComparisonFilter in/nin payloads unchanged to the OpenAI SDK", async () => {
const calls = [];
const requestPayload = {
vector_store_id: "vs_123",
query: "release alignment notes",
filters: {
type: "and",
filters: [
{
type: "in",
key: "team",
value: ["platform", "integrations"],
},
{
type: "nin",
key: "status",
value: ["archived", "draft"],
},
],
},
max_num_results: 5,
rewrite_query: true,
};
class FakeOpenAI {
constructor(clientParams) {
calls.push({ method: "ctor", clientParams });
this.vectorStores = {
search: async (vectorStoreId, body) => {
calls.push({ method: "vectorStores.search", vectorStoreId, body });
return {
data: [
{
file_id: "file_123",
filename: "release-notes.md",
attributes: {
team: "platform",
status: "published",
},
},
],
};
},
};
}
}
await withMockedOpenAI(FakeOpenAI, async () => {
const modulePath = require.resolve("../src/vector-stores/methods.js");
delete require.cache[modulePath];
const vectorStoreMethods = require("../src/vector-stores/methods.js");
const clientContext = {
clientParams: {
apiKey: "sk-test",
baseURL: "https://api.example.com/v1",
},
};
const response = await vectorStoreMethods.searchVectorStore.call(clientContext, {
payload: requestPayload,
});
assert.deepEqual(response, [
{
file_id: "file_123",
filename: "release-notes.md",
attributes: {
team: "platform",
status: "published",
},
},
]);
delete require.cache[modulePath];
});
const searchCalls = calls.filter((entry) => entry.method === "vectorStores.search");
assert.deepEqual(searchCalls, [
{
method: "vectorStores.search",
vectorStoreId: "vs_123",
body: {
query: "release alignment notes",
filters: {
type: "and",
filters: [
{
type: "in",
key: "team",
value: ["platform", "integrations"],
},
{
type: "nin",
key: "status",
value: ["archived", "draft"],
},
],
},
max_num_results: 5,
rewrite_query: true,
},
},
]);
});
test("vector-store picker and help document the search contract and ComparisonFilter operators", () => {
assert.equal(
locale.OpenaiApi.parameters.searchVectorStore,
"search vector store"
);
assert.match(vectorStoresHelp, /Search Vector Store/);
assert.match(vectorStoresHelp, /vector_store_id/);
assert.match(vectorStoresHelp, /query/);
assert.match(vectorStoresHelp, /ComparisonFilter/);
assert.match(vectorStoresHelp, /CompoundFilter/);
assert.match(vectorStoresHelp, /<code>in<\/code>/);
assert.match(vectorStoresHelp, /<code>nin<\/code>/);
assert.match(vectorStoresHelp, /max_num_results/);
assert.match(vectorStoresHelp, /rewrite_query/);
});
test("vector-store search example demonstrates in/nin ComparisonFilter usage", () => {
assert.ok(Array.isArray(vectorStoreSearchExample));
const openaiNode = vectorStoreSearchExample.find(
(entry) => entry.type === "OpenAI API"
);
const injectNode = vectorStoreSearchExample.find(
(entry) => entry.type === "inject" && entry.name === "Search Vector Store"
);
const commentNode = vectorStoreSearchExample.find(
(entry) => entry.type === "comment"
);
assert.ok(openaiNode);
assert.ok(injectNode);
assert.ok(commentNode);
assert.equal(openaiNode.method, "searchVectorStore");
assert.match(commentNode.info, /ComparisonFilter/);
assert.match(commentNode.info, /`in`/);
assert.match(commentNode.info, /`nin`/);
assert.equal(
injectNode.props.find((prop) => prop.p === "ai.vector_store_id").v,
"vs_replace_me"
);
assert.equal(
injectNode.props.find((prop) => prop.p === "ai.query").v,
"release alignment notes"
);
assert.equal(
injectNode.props.find((prop) => prop.p === "ai.max_num_results").v,
"5"
);
assert.deepEqual(
JSON.parse(injectNode.props.find((prop) => prop.p === "ai.filters").v),
{
type: "and",
filters: [
{
type: "in",
key: "team",
value: ["platform", "integrations"],
},
{
type: "nin",
key: "status",
value: ["archived", "draft"],
},
],
}
);
});