Skip to content

Commit 5eb96e8

Browse files
committed
swith search mode to v2 API
1 parent 1f8fd34 commit 5eb96e8

File tree

6 files changed

+63
-113
lines changed

6 files changed

+63
-113
lines changed

src/query/agent.test.ts

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -108,30 +108,22 @@ it("search-only mode success: caches searches and sends on subsequent request",
108108
const capturedBodies: ApiSearchModeResponse[] = [];
109109

110110
const apiSuccess: ApiSearchModeResponse = {
111-
original_query: "Test this search only mode!",
112111
searches: [
113112
{
114-
queries: ["search query"],
115-
filters: [
116-
[
117-
{
118-
filter_type: "integer",
119-
property_name: "test_property",
120-
operator: ComparisonOperator.GreaterThan,
121-
value: 0,
122-
},
123-
],
124-
],
125-
filter_operators: "AND",
113+
query: "search query",
114+
filters: {
115+
filter_type: "integer",
116+
property_name: "test_property",
117+
operator: ComparisonOperator.GreaterThan,
118+
value: 0,
119+
},
126120
collection: "test_collection",
127121
},
128122
],
129123
usage: {
130-
requests: 0,
131-
request_tokens: undefined,
132-
response_tokens: undefined,
133-
total_tokens: undefined,
134-
details: undefined,
124+
model_units: 1,
125+
usage_in_plan: true,
126+
remaining_plan_requests: 2,
135127
},
136128
total_time: 1.5,
137129
search_results: {
@@ -200,30 +192,22 @@ it("search-only mode success: caches searches and sends on subsequent request",
200192
collections: ["test_collection"],
201193
});
202194
expect(first).toMatchObject({
203-
originalQuery: apiSuccess.original_query,
204195
searches: [
205196
{
206197
collection: "test_collection",
207-
queries: ["search query"],
208-
filters: [
209-
[
210-
{
211-
filterType: "integer",
212-
propertyName: "test_property",
213-
operator: ComparisonOperator.GreaterThan,
214-
value: 0,
215-
},
216-
],
217-
],
218-
filterOperators: "AND",
198+
query: "search query",
199+
filters: {
200+
filterType: "integer",
201+
propertyName: "test_property",
202+
operator: ComparisonOperator.GreaterThan,
203+
value: 0,
204+
},
219205
},
220206
],
221207
usage: {
222-
requests: 0,
223-
requestTokens: undefined,
224-
responseTokens: undefined,
225-
totalTokens: undefined,
226-
details: undefined,
208+
modelUnits: 1,
209+
usageInPlan: true,
210+
remainingPlanRequests: 2,
227211
},
228212
totalTime: 1.5,
229213
searchResults: {
@@ -266,7 +250,6 @@ it("search-only mode success: caches searches and sends on subsequent request",
266250
expect(capturedBodies[1].searches).toEqual(apiSuccess.searches);
267251
// Response mapping should be the same (because response is mocked)
268252
expect(second).toMatchObject({
269-
originalQuery: apiSuccess.original_query,
270253
searches: first.searches,
271254
usage: first.usage,
272255
totalTime: first.totalTime,

src/query/agent.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
mapResponse,
1010
mapProgressMessageFromSSE,
1111
mapStreamedTokensFromSSE,
12-
mapResponseFromSSE,
1312
mapAskModeResponse,
1413
} from "./response/response-mapping.js";
1514
import { mapApiResponse } from "./response/api-response-mapping.js";

src/query/response/api-response.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,8 @@ export type ApiWeaviateReturn = {
247247
};
248248

249249
export type ApiSearchModeResponse = {
250-
original_query: string;
251-
searches?: ApiSearchResult[];
252-
usage: ApiUsage;
250+
searches?: ApiSearch[];
251+
usage: ApiModelUnitUsage;
253252
total_time: number;
254253
search_results: ApiWeaviateReturn;
255254
};

src/query/response/response-mapping.ts

Lines changed: 36 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ import { ReturnMetadata } from "weaviate-client";
22

33
import {
44
QueryAgentResponse,
5-
SearchResult,
65
PropertyFilter,
76
AggregationResult,
87
PropertyAggregation,
9-
Usage,
108
Source,
119
StreamedTokens,
1210
ProgressMessage,
@@ -16,22 +14,24 @@ import {
1614
SearchModeResponse,
1715
FilterAndOr,
1816
AskModeResponse,
17+
Search,
18+
ModelUnitUsage,
1919
} from "./response.js";
2020

2121
import {
2222
ApiQueryAgentResponse,
23-
ApiSearchResult,
2423
ApiPropertyFilter,
2524
ApiAggregationResult,
2625
ApiPropertyAggregation,
27-
ApiUsage,
2826
ApiSource,
2927
ApiDateFilterValue,
3028
ApiSearchModeResponse,
3129
ApiWeaviateObject,
3230
ApiWeaviateReturn,
3331
ApiFilterAndOr,
3432
ApiAskModeResponse,
33+
ApiSearch,
34+
ApiModelUnitUsage,
3535
} from "./api-response.js";
3636

3737
import { ServerSentEvent } from "./server-sent-events.js";
@@ -41,22 +41,14 @@ export const mapAskModeResponse = (
4141
): AskModeResponse => {
4242
const properties: AskModeResponseProperties = {
4343
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-
})),
44+
searches: mapSearches(response.searches),
4945
aggregations: response.aggregations.map((aggregation) => ({
5046
groupbyProperty: aggregation.groupby_property,
5147
aggregation: mapPropertyAggregation(aggregation.aggregation),
5248
filters: aggregation.filters ? mapFilter(aggregation.filters) : undefined,
5349
collection: aggregation.collection,
5450
})),
55-
usage: {
56-
modelUnits: response.usage.model_units,
57-
usageInPlan: response.usage.usage_in_plan,
58-
remainingPlanRequests: response.usage.remaining_plan_requests,
59-
},
51+
usage: mapUsage(response.usage),
6052
totalTime: response.total_time,
6153
isPartialAnswer: response.is_partial_answer,
6254
missingInformation: response.missing_information,
@@ -70,16 +62,42 @@ export const mapAskModeResponse = (
7062
};
7163
};
7264

65+
const mapSearches = (searches: ApiSearch[]): Search[] =>
66+
searches.map((search) => ({
67+
query: search.query,
68+
filters: search.filters ? mapFilter(search.filters) : undefined,
69+
collection: search.collection,
70+
}));
71+
72+
const mapUsage = (usage: ApiModelUnitUsage): ModelUnitUsage => ({
73+
modelUnits: usage.model_units,
74+
usageInPlan: usage.usage_in_plan,
75+
remainingPlanRequests: usage.remaining_plan_requests,
76+
});
77+
7378
export const mapResponse = (
7479
response: ApiQueryAgentResponse,
7580
): QueryAgentResponse => {
7681
const properties: ResponseProperties = {
7782
outputType: "finalState",
7883
originalQuery: response.original_query,
7984
collectionNames: response.collection_names,
80-
searches: mapSearches(response.searches),
85+
searches: response.searches.map((searches) =>
86+
searches.map((result) => ({
87+
collection: result.collection,
88+
queries: result.queries,
89+
filters: result.filters.map((filter) => filter.map(mapPropertyFilter)),
90+
filterOperators: result.filter_operators,
91+
})),
92+
),
8193
aggregations: mapAggregations(response.aggregations),
82-
usage: mapUsage(response.usage),
94+
usage: {
95+
requests: response.usage.requests,
96+
requestTokens: response.usage.request_tokens,
97+
responseTokens: response.usage.response_tokens,
98+
totalTokens: response.usage.total_tokens,
99+
details: response.usage.details,
100+
},
83101
totalTime: response.total_time,
84102
isPartialAnswer: response.is_partial_answer,
85103
missingInformation: response.missing_information,
@@ -93,17 +111,6 @@ export const mapResponse = (
93111
};
94112
};
95113

96-
const mapInnerSearches = (searches: ApiSearchResult[]): SearchResult[] =>
97-
searches.map((result) => ({
98-
collection: result.collection,
99-
queries: result.queries,
100-
filters: result.filters.map((filter) => filter.map(mapPropertyFilter)),
101-
filterOperators: result.filter_operators,
102-
}));
103-
104-
const mapSearches = (searches: ApiSearchResult[][]): SearchResult[][] =>
105-
searches.map((searchGroup) => mapInnerSearches(searchGroup));
106-
107114
const mapDatePropertyFilter = (
108115
filterValue: ApiDateFilterValue,
109116
): DateFilterValue | undefined => {
@@ -269,14 +276,6 @@ const mapPropertyAggregation = (
269276
: undefined,
270277
});
271278

272-
const mapUsage = (usage: ApiUsage): Usage => ({
273-
requests: usage.requests,
274-
requestTokens: usage.request_tokens,
275-
responseTokens: usage.response_tokens,
276-
totalTokens: usage.total_tokens,
277-
details: usage.details,
278-
});
279-
280279
const mapSources = (sources: ApiSource[]): Source[] =>
281280
sources.map((source) => ({
282281
objectId: source.object_id,
@@ -332,31 +331,6 @@ export const mapStreamedTokensFromSSE = (
332331
};
333332
};
334333

335-
export const mapResponseFromSSE = (
336-
sse: ServerSentEvent,
337-
): QueryAgentResponse => {
338-
const data: ApiQueryAgentResponse = JSON.parse(sse.data);
339-
340-
const properties: ResponseProperties = {
341-
outputType: "finalState",
342-
originalQuery: data.original_query,
343-
collectionNames: data.collection_names,
344-
searches: mapSearches(data.searches),
345-
aggregations: mapAggregations(data.aggregations),
346-
usage: mapUsage(data.usage),
347-
totalTime: data.total_time,
348-
isPartialAnswer: data.is_partial_answer,
349-
missingInformation: data.missing_information,
350-
finalAnswer: data.final_answer,
351-
sources: mapSources(data.sources),
352-
};
353-
354-
return {
355-
...properties,
356-
display: () => display(properties),
357-
};
358-
};
359-
360334
const mapWeaviateObject = (
361335
object: ApiWeaviateObject,
362336
): WeaviateObjectWithCollection => {
@@ -410,12 +384,11 @@ export const mapSearchOnlyResponse = (
410384
response: ApiSearchModeResponse,
411385
): {
412386
mappedResponse: Omit<SearchModeResponse, "next">;
413-
apiSearches: ApiSearchResult[] | undefined;
387+
apiSearches: ApiSearch[] | undefined;
414388
} => {
415389
const apiSearches = response.searches;
416390
const mappedResponse: Omit<SearchModeResponse, "next"> = {
417-
originalQuery: response.original_query,
418-
searches: apiSearches ? mapInnerSearches(apiSearches) : undefined,
391+
searches: apiSearches ? mapSearches(apiSearches) : undefined,
419392
usage: mapUsage(response.usage),
420393
totalTime: response.total_time,
421394
searchResults: mapWeviateSearchResults(response.search_results),

src/query/response/response.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,8 @@ export type SearchExecutionOptions = {
317317
};
318318

319319
export type SearchModeResponse = {
320-
originalQuery: string;
321-
searches?: SearchResult[];
322-
usage: Usage;
320+
searches?: Search[];
321+
usage: ModelUnitUsage;
323322
totalTime: number;
324323
searchResults: WeaviateReturnWithCollection;
325324
next: (options: SearchExecutionOptions) => Promise<SearchModeResponse>;

src/query/search.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ import {
66
import { mapSearchOnlyResponse } from "./response/response-mapping.js";
77
import { mapCollections } from "./collection.js";
88
import { handleError } from "./response/error.js";
9-
import {
10-
ApiSearchModeResponse,
11-
ApiSearchResult,
12-
} from "./response/api-response.js";
9+
import { ApiSearch, ApiSearchModeResponse } from "./response/api-response.js";
1310
import { QueryAgentQuery } from "./agent.js";
1411
import { QueryAgentCollection } from "./collection.js";
1512
import { getHeaders } from "./connection.js";
@@ -28,7 +25,7 @@ import { getHeaders } from "./connection.js";
2825
* For more information, see the [Weaviate Query Agent Docs](https://weaviate.io/developers/agents/query)
2926
*/
3027
export class QueryAgentSearcher {
31-
private cachedSearches?: ApiSearchResult[];
28+
private cachedSearches?: ApiSearch[];
3229

3330
constructor(
3431
private client: WeaviateClient,
@@ -85,7 +82,7 @@ export class QueryAgentSearcher {
8582
}
8683
const { requestHeaders, connectionHeaders } = await getHeaders(this.client);
8784

88-
const response = await fetch(`${this.agentsHost}/agent/search_only`, {
85+
const response = await fetch(`${this.agentsHost}/query/search_only`, {
8986
method: "POST",
9087
headers: requestHeaders,
9188
body: JSON.stringify(

0 commit comments

Comments
 (0)