Skip to content

Commit 7ffbbcd

Browse files
committed
swith ask mode to v2 API
1 parent 53aa3d0 commit 7ffbbcd

File tree

4 files changed

+221
-98
lines changed

4 files changed

+221
-98
lines changed

src/query/agent.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import {
33
QueryAgentResponse,
44
ProgressMessage,
55
StreamedTokens,
6+
AskModeResponse,
67
} from "./response/response.js";
78
import {
89
mapResponse,
910
mapProgressMessageFromSSE,
1011
mapStreamedTokensFromSSE,
1112
mapResponseFromSSE,
13+
mapAskModeResponse,
1214
} from "./response/response-mapping.js";
1315
import { mapApiResponse } from "./response/api-response-mapping.js";
1416
import { fetchServerSentEvents } from "./response/server-sent-events.js";
@@ -109,11 +111,11 @@ export class QueryAgent {
109111
async ask(
110112
query: QueryAgentQuery,
111113
{ collections }: QueryAgentAskOptions = {},
112-
): Promise<QueryAgentResponse> {
114+
): Promise<AskModeResponse> {
113115
const targetCollections = this.validateCollections(collections);
114116
const { requestHeaders, connectionHeaders } = await getHeaders(this.client);
115117

116-
const response = await fetch(`${this.agentsHost}/agent/query`, {
118+
const response = await fetch(`${this.agentsHost}/query/ask`, {
117119
method: "POST",
118120
headers: requestHeaders,
119121
body: JSON.stringify({
@@ -128,7 +130,7 @@ export class QueryAgent {
128130
await handleError(await response.text());
129131
}
130132

131-
return mapResponse(await response.json());
133+
return mapAskModeResponse(await response.json());
132134
}
133135

134136
/**

src/query/response/api-response.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,41 @@ import {
88
ComparisonOperator,
99
} from "./response.js";
1010

11+
export type ApiAskModeResponse = {
12+
searches: ApiSearch[];
13+
aggregations: ApiAggregation[];
14+
usage: ApiModelUnitUsage;
15+
total_time: number;
16+
is_partial_answer?: boolean;
17+
missing_information?: string[];
18+
final_answer: string;
19+
sources?: ApiSource[];
20+
};
21+
22+
export type ApiSearch = {
23+
query?: string;
24+
filters?: ApiPropertyFilter | ApiFilterAndOr;
25+
collection: string;
26+
};
27+
28+
export type ApiAggregation = {
29+
groupby_property?: string;
30+
aggregation: ApiPropertyAggregation;
31+
filters?: ApiPropertyFilter | ApiFilterAndOr;
32+
collection: string;
33+
};
34+
35+
export type ApiFilterAndOr = {
36+
combine: "AND" | "OR";
37+
filters: (ApiPropertyFilter | ApiFilterAndOr)[];
38+
};
39+
40+
export type ApiModelUnitUsage = {
41+
model_units: number;
42+
usage_in_plan: boolean;
43+
remaining_plan_requests: number;
44+
};
45+
1146
export type ApiQueryAgentResponse = {
1247
original_query: string;
1348
collection_names: string[];

src/query/response/response-mapping.ts

Lines changed: 144 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {
1414
WeaviateObjectWithCollection,
1515
WeaviateReturnWithCollection,
1616
SearchModeResponse,
17+
FilterAndOr,
18+
AskModeResponse,
1719
} from "./response.js";
1820

1921
import {
@@ -28,10 +30,46 @@ import {
2830
ApiSearchModeResponse,
2931
ApiWeaviateObject,
3032
ApiWeaviateReturn,
33+
ApiFilterAndOr,
34+
ApiAskModeResponse,
3135
} from "./api-response.js";
3236

3337
import { ServerSentEvent } from "./server-sent-events.js";
3438

39+
export const mapAskModeResponse = (
40+
response: ApiAskModeResponse,
41+
): AskModeResponse => {
42+
const properties: AskModeResponseProperties = {
43+
outputType: "finalState",
44+
searches: response.searches.map((search) => ({
45+
query: search.query,
46+
filters: search.filters ? mapFilter(search.filters) : undefined,
47+
collection: search.collection,
48+
})),
49+
aggregations: response.aggregations.map((aggregation) => ({
50+
groupbyProperty: aggregation.groupby_property,
51+
aggregation: mapPropertyAggregation(aggregation.aggregation),
52+
filters: aggregation.filters ? mapFilter(aggregation.filters) : undefined,
53+
collection: aggregation.collection,
54+
})),
55+
usage: {
56+
modelUnits: response.usage.model_units,
57+
usageInPlan: response.usage.usage_in_plan,
58+
remainingPlanRequests: response.usage.remaining_plan_requests,
59+
},
60+
totalTime: response.total_time,
61+
isPartialAnswer: response.is_partial_answer,
62+
missingInformation: response.missing_information,
63+
finalAnswer: response.final_answer,
64+
sources: response.sources ? mapSources(response.sources) : undefined,
65+
};
66+
67+
return {
68+
...properties,
69+
display: () => display(properties),
70+
};
71+
};
72+
3573
export const mapResponse = (
3674
response: ApiQueryAgentResponse,
3775
): QueryAgentResponse => {
@@ -59,7 +97,7 @@ const mapInnerSearches = (searches: ApiSearchResult[]): SearchResult[] =>
5997
searches.map((result) => ({
6098
collection: result.collection,
6199
queries: result.queries,
62-
filters: result.filters.map(mapPropertyFilters),
100+
filters: result.filters.map((filter) => filter.map(mapPropertyFilter)),
63101
filterOperators: result.filter_operators,
64102
}));
65103

@@ -100,102 +138,112 @@ const mapDatePropertyFilter = (
100138
return undefined;
101139
};
102140

103-
const mapPropertyFilters = (filters: ApiPropertyFilter[]): PropertyFilter[] =>
104-
filters.map((filter) => {
105-
switch (filter.filter_type) {
106-
case "integer":
107-
return {
108-
filterType: "integer",
109-
propertyName: filter.property_name,
110-
operator: filter.operator,
111-
value: filter.value,
112-
};
113-
114-
case "integer_array":
115-
return {
116-
filterType: "integerArray",
117-
propertyName: filter.property_name,
118-
operator: filter.operator,
119-
value: filter.value,
120-
};
121-
122-
case "text":
123-
return {
124-
filterType: "text",
125-
propertyName: filter.property_name,
126-
operator: filter.operator,
127-
value: filter.value,
128-
};
129-
130-
case "text_array":
131-
return {
132-
filterType: "textArray",
133-
propertyName: filter.property_name,
134-
operator: filter.operator,
135-
value: filter.value,
136-
};
137-
138-
case "boolean":
139-
return {
140-
filterType: "boolean",
141-
propertyName: filter.property_name,
142-
operator: filter.operator,
143-
value: filter.value,
144-
};
145-
146-
case "boolean_array":
147-
return {
148-
filterType: "booleanArray",
149-
propertyName: filter.property_name,
150-
operator: filter.operator,
151-
value: filter.value,
152-
};
153-
154-
case "date_range":
155-
const value = mapDatePropertyFilter(filter.value);
156-
if (!value) {
157-
return {
158-
filterType: "unknown",
159-
propertyName: (filter as ApiPropertyFilter).property_name,
160-
};
161-
}
162-
return {
163-
filterType: "dateRange",
164-
propertyName: filter.property_name,
165-
value,
166-
};
167-
168-
case "date_array":
169-
return {
170-
filterType: "dateArray",
171-
propertyName: filter.property_name,
172-
operator: filter.operator,
173-
value: filter.value,
174-
};
175-
176-
case "geo":
177-
return {
178-
filterType: "geo",
179-
propertyName: filter.property_name,
180-
latitude: filter.latitude,
181-
longitude: filter.longitude,
182-
maxDistanceMeters: filter.max_distance_meters,
183-
};
184-
185-
case "is_null":
186-
return {
187-
filterType: "isNull",
188-
propertyName: filter.property_name,
189-
isNull: filter.is_null,
190-
};
191-
192-
default:
141+
const mapFilter = (
142+
filter: ApiPropertyFilter | ApiFilterAndOr,
143+
): PropertyFilter | FilterAndOr =>
144+
multiFilter(filter)
145+
? { combine: filter.combine, filters: filter.filters.map(mapFilter) }
146+
: mapPropertyFilter(filter);
147+
148+
const mapPropertyFilter = (filter: ApiPropertyFilter): PropertyFilter => {
149+
switch (filter.filter_type) {
150+
case "integer":
151+
return {
152+
filterType: "integer",
153+
propertyName: filter.property_name,
154+
operator: filter.operator,
155+
value: filter.value,
156+
};
157+
158+
case "integer_array":
159+
return {
160+
filterType: "integerArray",
161+
propertyName: filter.property_name,
162+
operator: filter.operator,
163+
value: filter.value,
164+
};
165+
166+
case "text":
167+
return {
168+
filterType: "text",
169+
propertyName: filter.property_name,
170+
operator: filter.operator,
171+
value: filter.value,
172+
};
173+
174+
case "text_array":
175+
return {
176+
filterType: "textArray",
177+
propertyName: filter.property_name,
178+
operator: filter.operator,
179+
value: filter.value,
180+
};
181+
182+
case "boolean":
183+
return {
184+
filterType: "boolean",
185+
propertyName: filter.property_name,
186+
operator: filter.operator,
187+
value: filter.value,
188+
};
189+
190+
case "boolean_array":
191+
return {
192+
filterType: "booleanArray",
193+
propertyName: filter.property_name,
194+
operator: filter.operator,
195+
value: filter.value,
196+
};
197+
198+
case "date_range":
199+
const value = mapDatePropertyFilter(filter.value);
200+
if (!value) {
193201
return {
194202
filterType: "unknown",
195203
propertyName: (filter as ApiPropertyFilter).property_name,
196204
};
197-
}
198-
});
205+
}
206+
return {
207+
filterType: "dateRange",
208+
propertyName: filter.property_name,
209+
value,
210+
};
211+
212+
case "date_array":
213+
return {
214+
filterType: "dateArray",
215+
propertyName: filter.property_name,
216+
operator: filter.operator,
217+
value: filter.value,
218+
};
219+
220+
case "geo":
221+
return {
222+
filterType: "geo",
223+
propertyName: filter.property_name,
224+
latitude: filter.latitude,
225+
longitude: filter.longitude,
226+
maxDistanceMeters: filter.max_distance_meters,
227+
};
228+
229+
case "is_null":
230+
return {
231+
filterType: "isNull",
232+
propertyName: filter.property_name,
233+
isNull: filter.is_null,
234+
};
235+
236+
default:
237+
return {
238+
filterType: "unknown",
239+
propertyName: (filter as ApiPropertyFilter).property_name,
240+
};
241+
}
242+
};
243+
244+
const multiFilter = (
245+
filter: ApiPropertyFilter | ApiFilterAndOr,
246+
): filter is ApiFilterAndOr => "combine" in filter;
199247

200248
const mapAggregations = (
201249
aggregations: ApiAggregationResult[][],
@@ -206,7 +254,7 @@ const mapAggregations = (
206254
searchQuery: result.search_query,
207255
groupbyProperty: result.groupby_property,
208256
aggregations: result.aggregations.map(mapPropertyAggregation),
209-
filters: mapPropertyFilters(result.filters),
257+
filters: result.filters.map(mapPropertyFilter),
210258
})),
211259
);
212260

@@ -235,10 +283,11 @@ const mapSources = (sources: ApiSource[]): Source[] =>
235283
collection: source.collection,
236284
}));
237285

238-
const display = (response: ResponseProperties) => {
286+
const display = (response: AskModeResponseProperties | ResponseProperties) => {
239287
console.log(JSON.stringify(response, undefined, 2));
240288
};
241289

290+
type AskModeResponseProperties = Omit<AskModeResponse, "display">;
242291
type ResponseProperties = Omit<QueryAgentResponse, "display">;
243292

244293
type ProgressMessageJSON = Omit<ProgressMessage, "outputType"> & {

0 commit comments

Comments
 (0)