Skip to content

Commit 5d8bf35

Browse files
danmichaeljonesDan Jones
andauthored
Update response model with sortProperty (#35)
* Add new QuerySort object to search responses * Update unit tests to include sorts * Add test for ask mode --------- Co-authored-by: Dan Jones <[email protected]>
1 parent f5c2649 commit 5d8bf35

File tree

4 files changed

+201
-2
lines changed

4 files changed

+201
-2
lines changed

src/query/agent.test.ts

Lines changed: 176 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import { WeaviateClient } from "weaviate-client";
22
import { QueryAgent } from "./agent.js";
33
import { ApiQueryAgentResponse } from "./response/api-response.js";
4-
import { QueryAgentResponse, ComparisonOperator } from "./response/response.js";
5-
import { ApiSearchModeResponse } from "./response/api-response.js";
4+
import {
5+
QueryAgentResponse,
6+
ComparisonOperator,
7+
AskModeResponse,
8+
} from "./response/response.js";
9+
import {
10+
ApiSearchModeResponse,
11+
ApiAskModeResponse,
12+
} from "./response/api-response.js";
613
import { QueryAgentError } from "./response/error.js";
714

815
it("runs the query agent", async () => {
@@ -96,6 +103,134 @@ it("runs the query agent", async () => {
96103
});
97104
});
98105

106+
it("runs the query agent ask", async () => {
107+
const mockClient = {
108+
getConnectionDetails: jest.fn().mockResolvedValue({
109+
host: "test-cluster",
110+
bearerToken: "test-token",
111+
headers: { "X-Provider": "test-key" },
112+
}),
113+
} as unknown as WeaviateClient;
114+
115+
const apiSuccess: ApiAskModeResponse = {
116+
searches: [
117+
{
118+
query: "search query",
119+
filters: {
120+
filter_type: "integer",
121+
property_name: "test_property",
122+
operator: ComparisonOperator.GreaterThan,
123+
value: 0,
124+
},
125+
collection: "test_collection",
126+
sort_property: undefined,
127+
},
128+
{
129+
query: undefined,
130+
filters: {
131+
filter_type: "integer",
132+
property_name: "test_property",
133+
operator: ComparisonOperator.GreaterThan,
134+
value: 0,
135+
},
136+
collection: "test_collection",
137+
sort_property: {
138+
property_name: "test_property",
139+
order: "ascending",
140+
tie_break: {
141+
property_name: "test_property_2",
142+
order: "descending",
143+
tie_break: undefined,
144+
},
145+
},
146+
},
147+
],
148+
aggregations: [],
149+
usage: {
150+
model_units: 1,
151+
usage_in_plan: true,
152+
remaining_plan_requests: 2,
153+
},
154+
total_time: 1.5,
155+
is_partial_answer: false,
156+
missing_information: [],
157+
final_answer: "Test answer",
158+
sources: [
159+
{
160+
object_id: "123",
161+
collection: "test-collection",
162+
},
163+
],
164+
};
165+
166+
global.fetch = jest.fn(() =>
167+
Promise.resolve({
168+
ok: true,
169+
json: () => Promise.resolve(apiSuccess),
170+
} as Response),
171+
) as jest.Mock;
172+
173+
const agent = new QueryAgent(mockClient, {
174+
systemPrompt: "test system prompt",
175+
});
176+
177+
const response = await agent.ask("What is the capital of France?", {
178+
collections: ["test-collection"],
179+
});
180+
181+
expect(response).toEqual<AskModeResponse>({
182+
outputType: "finalState",
183+
searches: [
184+
{
185+
collection: "test_collection",
186+
query: "search query",
187+
filters: {
188+
filterType: "integer",
189+
propertyName: "test_property",
190+
operator: ComparisonOperator.GreaterThan,
191+
value: 0,
192+
},
193+
sortProperty: undefined,
194+
},
195+
{
196+
collection: "test_collection",
197+
query: undefined,
198+
filters: {
199+
filterType: "integer",
200+
propertyName: "test_property",
201+
operator: ComparisonOperator.GreaterThan,
202+
value: 0,
203+
},
204+
sortProperty: {
205+
propertyName: "test_property",
206+
order: "ascending",
207+
tieBreak: {
208+
propertyName: "test_property_2",
209+
order: "descending",
210+
},
211+
},
212+
},
213+
],
214+
aggregations: [],
215+
usage: {
216+
modelUnits: 1,
217+
usageInPlan: true,
218+
remainingPlanRequests: 2,
219+
},
220+
totalTime: 1.5,
221+
isPartialAnswer: false,
222+
missingInformation: [],
223+
finalAnswer: "Test answer",
224+
sources: [
225+
{
226+
objectId: "123",
227+
collection: "test-collection",
228+
},
229+
],
230+
display: expect.any(Function),
231+
});
232+
});
233+
99234
it("search-only mode success: caches searches and sends on subsequent request", async () => {
100235
const mockClient = {
101236
getConnectionDetails: jest.fn().mockResolvedValue({
@@ -118,6 +253,26 @@ it("search-only mode success: caches searches and sends on subsequent request",
118253
value: 0,
119254
},
120255
collection: "test_collection",
256+
sort_property: undefined,
257+
},
258+
{
259+
query: undefined,
260+
filters: {
261+
filter_type: "integer",
262+
property_name: "test_property",
263+
operator: ComparisonOperator.GreaterThan,
264+
value: 0,
265+
},
266+
collection: "test_collection",
267+
sort_property: {
268+
property_name: "test_property",
269+
order: "ascending",
270+
tie_break: {
271+
property_name: "test_property_2",
272+
order: "descending",
273+
tie_break: undefined,
274+
},
275+
},
121276
},
122277
],
123278
usage: {
@@ -202,6 +357,25 @@ it("search-only mode success: caches searches and sends on subsequent request",
202357
operator: ComparisonOperator.GreaterThan,
203358
value: 0,
204359
},
360+
sortProperty: undefined,
361+
},
362+
{
363+
collection: "test_collection",
364+
query: undefined,
365+
filters: {
366+
filterType: "integer",
367+
propertyName: "test_property",
368+
operator: ComparisonOperator.GreaterThan,
369+
value: 0,
370+
},
371+
sortProperty: {
372+
propertyName: "test_property",
373+
order: "ascending",
374+
tieBreak: {
375+
propertyName: "test_property_2",
376+
order: "descending",
377+
},
378+
},
205379
},
206380
],
207381
usage: {

src/query/response/api-response.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,17 @@ export type ApiAskModeResponse = {
1919
sources?: ApiSource[];
2020
};
2121

22+
export type ApiQuerySort = {
23+
property_name: string;
24+
order: "ascending" | "descending";
25+
tie_break?: ApiQuerySort;
26+
};
27+
2228
export type ApiSearch = {
2329
query?: string;
2430
filters?: ApiPropertyFilter | ApiFilterAndOr;
2531
collection: string;
32+
sort_property?: ApiQuerySort;
2633
};
2734

2835
export type ApiAggregation = {

src/query/response/response-mapping.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
AskModeResponse,
1717
Search,
1818
ModelUnitUsage,
19+
QuerySort,
1920
} from "./response.js";
2021

2122
import {
@@ -32,6 +33,7 @@ import {
3233
ApiAskModeResponse,
3334
ApiSearch,
3435
ApiModelUnitUsage,
36+
ApiQuerySort,
3537
} from "./api-response.js";
3638

3739
import { ServerSentEvent } from "./server-sent-events.js";
@@ -67,8 +69,17 @@ const mapSearches = (searches: ApiSearch[]): Search[] =>
6769
query: search.query,
6870
filters: search.filters ? mapFilter(search.filters) : undefined,
6971
collection: search.collection,
72+
sortProperty: search.sort_property
73+
? mapQuerySort(search.sort_property)
74+
: undefined,
7075
}));
7176

77+
const mapQuerySort = (sort: ApiQuerySort): QuerySort => ({
78+
propertyName: sort.property_name,
79+
order: sort.order,
80+
tieBreak: sort.tie_break ? mapQuerySort(sort.tie_break) : undefined,
81+
});
82+
7283
const mapUsage = (usage: ApiModelUnitUsage): ModelUnitUsage => ({
7384
modelUnits: usage.model_units,
7485
usageInPlan: usage.usage_in_plan,

src/query/response/response.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export type Search = {
1717
query?: string;
1818
filters?: PropertyFilter | FilterAndOr;
1919
collection: string;
20+
sortProperty?: QuerySort;
2021
};
2122

2223
export type Aggregation = {
@@ -37,6 +38,12 @@ export type ModelUnitUsage = {
3738
remainingPlanRequests: number;
3839
};
3940

41+
export type QuerySort = {
42+
propertyName: string;
43+
order: "ascending" | "descending";
44+
tieBreak?: QuerySort;
45+
};
46+
4047
export type QueryAgentResponse = {
4148
outputType: "finalState";
4249
originalQuery: string;

0 commit comments

Comments
 (0)