Skip to content

Commit 9a2c34e

Browse files
authored
V2 api (#33)
* swith ask mode to v2 API * swith ask stream to v2 API * swith search mode to v2 API * upgrade client version to v1
1 parent 53aa3d0 commit 9a2c34e

File tree

7 files changed

+281
-208
lines changed

7 files changed

+281
-208
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "weaviate-agents",
3-
"version": "0.1.4",
3+
"version": "1.0.0",
44
"description": "JS/TS client for Weaviate Agents",
55
"exports": "./dist/index.js",
66
"type": "module",

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: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import {
33
QueryAgentResponse,
44
ProgressMessage,
55
StreamedTokens,
6+
AskModeResponse,
67
} from "./response/response.js";
78
import {
89
mapResponse,
910
mapProgressMessageFromSSE,
1011
mapStreamedTokensFromSSE,
11-
mapResponseFromSSE,
12+
mapAskModeResponse,
1213
} from "./response/response-mapping.js";
1314
import { mapApiResponse } from "./response/api-response-mapping.js";
1415
import { fetchServerSentEvents } from "./response/server-sent-events.js";
@@ -109,11 +110,11 @@ export class QueryAgent {
109110
async ask(
110111
query: QueryAgentQuery,
111112
{ collections }: QueryAgentAskOptions = {},
112-
): Promise<QueryAgentResponse> {
113+
): Promise<AskModeResponse> {
113114
const targetCollections = this.validateCollections(collections);
114115
const { requestHeaders, connectionHeaders } = await getHeaders(this.client);
115116

116-
const response = await fetch(`${this.agentsHost}/agent/query`, {
117+
const response = await fetch(`${this.agentsHost}/query/ask`, {
117118
method: "POST",
118119
headers: requestHeaders,
119120
body: JSON.stringify({
@@ -128,7 +129,7 @@ export class QueryAgent {
128129
await handleError(await response.text());
129130
}
130131

131-
return mapResponse(await response.json());
132+
return mapAskModeResponse(await response.json());
132133
}
133134

134135
/**
@@ -221,7 +222,7 @@ export class QueryAgent {
221222
} else if (event.event === "streamed_tokens") {
222223
output = mapStreamedTokensFromSSE(event);
223224
} else if (event.event === "final_state") {
224-
output = mapResponseFromSSE(event);
225+
output = mapResponse(JSON.parse(event.data));
225226
} else {
226227
throw new Error(`Unexpected event type: ${event.event}: ${event.data}`);
227228
}
@@ -250,7 +251,7 @@ export class QueryAgent {
250251
includeProgress: false;
251252
includeFinalState?: true;
252253
},
253-
): AsyncGenerator<StreamedTokens | QueryAgentResponse>;
254+
): AsyncGenerator<StreamedTokens | AskModeResponse>;
254255
askStream(
255256
query: QueryAgentQuery,
256257
options: QueryAgentAskStreamOptions & {
@@ -264,15 +265,15 @@ export class QueryAgent {
264265
includeProgress?: true;
265266
includeFinalState?: true;
266267
},
267-
): AsyncGenerator<ProgressMessage | StreamedTokens | QueryAgentResponse>;
268+
): AsyncGenerator<ProgressMessage | StreamedTokens | AskModeResponse>;
268269
async *askStream(
269270
query: QueryAgentQuery,
270271
{
271272
collections,
272273
includeProgress,
273274
includeFinalState,
274275
}: QueryAgentAskStreamOptions = {},
275-
): AsyncGenerator<ProgressMessage | StreamedTokens | QueryAgentResponse> {
276+
): AsyncGenerator<ProgressMessage | StreamedTokens | AskModeResponse> {
276277
const targetCollections = collections ?? this.collections;
277278

278279
if (!targetCollections) {
@@ -283,7 +284,7 @@ export class QueryAgent {
283284
await this.client.getConnectionDetails();
284285

285286
const sseStream = fetchServerSentEvents(
286-
`${this.agentsHost}/agent/stream_query`,
287+
`${this.agentsHost}/query/stream_ask`,
287288
{
288289
method: "POST",
289290
headers: {
@@ -308,13 +309,13 @@ export class QueryAgent {
308309
await handleError(event.data);
309310
}
310311

311-
let output: ProgressMessage | StreamedTokens | QueryAgentResponse;
312+
let output: ProgressMessage | StreamedTokens | AskModeResponse;
312313
if (event.event === "progress_message") {
313314
output = mapProgressMessageFromSSE(event);
314315
} else if (event.event === "streamed_tokens") {
315316
output = mapStreamedTokensFromSSE(event);
316317
} else if (event.event === "final_state") {
317-
output = mapResponseFromSSE(event);
318+
output = mapAskModeResponse(JSON.parse(event.data));
318319
} else {
319320
throw new Error(`Unexpected event type: ${event.event}: ${event.data}`);
320321
}

src/query/response/api-response.ts

Lines changed: 37 additions & 3 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[];
@@ -212,9 +247,8 @@ export type ApiWeaviateReturn = {
212247
};
213248

214249
export type ApiSearchModeResponse = {
215-
original_query: string;
216-
searches?: ApiSearchResult[];
217-
usage: ApiUsage;
250+
searches?: ApiSearch[];
251+
usage: ApiModelUnitUsage;
218252
total_time: number;
219253
search_results: ApiWeaviateReturn;
220254
};

0 commit comments

Comments
 (0)