Skip to content

Commit 5f87786

Browse files
authored
Context (#5)
* add context * fix typo
1 parent 483cccc commit 5f87786

File tree

5 files changed

+113
-16
lines changed

5 files changed

+113
-16
lines changed

src/query/agent.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { WeaviateClient } from "weaviate-client";
22
import { QueryAgentResponse } from "./response/response.js";
33
import { mapResponse } from "./response/response-mapping.js";
4+
import { mapApiResponse } from "./response/api-reponse-mapping.js";
45

56
/**
67
* An agent for executing agentic queries against Weaviate.
@@ -43,7 +44,7 @@ export class QueryAgent {
4344
*/
4445
async run(
4546
query: string,
46-
{ viewProperties }: QueryAgentRunOptions = {}
47+
{ viewProperties, context }: QueryAgentRunOptions = {}
4748
): Promise<QueryAgentResponse> {
4849
const { host, bearerToken, headers } =
4950
await this.client.getConnectionDetails();
@@ -61,16 +62,15 @@ export class QueryAgent {
6162
collection_names: this.collections,
6263
collection_view_properties: viewProperties,
6364
system_prompt: this.systemPrompt,
65+
previous_response: context ? mapApiResponse(context) : undefined,
6466
}),
6567
});
6668

67-
const responseBody = await response.json();
68-
6969
if (!response.ok) {
70-
throw Error(`Query agent failed. ${JSON.stringify(responseBody)}`);
70+
throw Error(`Query agent failed. ${await response.text()}`);
7171
}
7272

73-
return mapResponse(responseBody);
73+
return mapResponse(await response.json());
7474
}
7575
}
7676

@@ -86,4 +86,6 @@ export type QueryAgentOptions = {
8686
export type QueryAgentRunOptions = {
8787
/** List of of property names the agent has the ability to view across all collections. */
8888
viewProperties?: string[];
89+
/** Previous response from the agent. */
90+
context?: QueryAgentResponse;
8991
};
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import {
2+
AggregationResult,
3+
PropertyAggregation,
4+
PropertyFilter,
5+
QueryAgentResponse,
6+
SearchResult,
7+
Source,
8+
Usage,
9+
} from "./response.js";
10+
11+
import {
12+
ApiAggregationResult,
13+
ApiPropertyAggregation,
14+
ApiPropertyFilter,
15+
ApiQueryAgentResponse,
16+
ApiSearchResult,
17+
ApiSource,
18+
ApiUsage,
19+
} from "./api-response.js";
20+
21+
export const mapApiResponse = (
22+
response: QueryAgentResponse
23+
): ApiQueryAgentResponse => ({
24+
original_query: response.originalQuery,
25+
collection_names: response.collectionNames,
26+
searches: mapApiSearches(response.searches),
27+
aggregations: mapApiAggregations(response.aggregations),
28+
usage: mapApiUsage(response.usage),
29+
total_time: response.totalTime,
30+
aggregation_answer: response.aggregationAnswer,
31+
has_aggregation_answer: response.hasAggregationAnswer,
32+
has_search_answer: response.hasSearchAnswer,
33+
is_partial_answer: response.isPartialAnswer,
34+
missing_information: response.missingInformation,
35+
final_answer: response.finalAnswer,
36+
sources: mapApiSources(response.sources),
37+
});
38+
39+
const mapApiSearches = (searches: SearchResult[][]): ApiSearchResult[][] =>
40+
searches.map((searchGroup) =>
41+
searchGroup.map((result) => ({
42+
collection: result.collection,
43+
queries: result.queries,
44+
filters: result.filters.map(mapApiPropertyFilters),
45+
filter_operators: result.filterOperators,
46+
}))
47+
);
48+
49+
const mapApiPropertyFilters = (
50+
filters: PropertyFilter[]
51+
): ApiPropertyFilter[] =>
52+
filters.map((filter) => ({
53+
property_name: filter.propertyName,
54+
operator: filter.operator,
55+
value: filter.value,
56+
}));
57+
58+
const mapApiAggregations = (
59+
aggregations: AggregationResult[][]
60+
): ApiAggregationResult[][] =>
61+
aggregations.map((aggregationGroup) =>
62+
aggregationGroup.map((result) => ({
63+
collection: result.collection,
64+
search_query: result.searchQuery,
65+
groupby_property: result.groupbyProperty,
66+
aggregations: result.aggregations.map(mapApiPropertyAggregation),
67+
filters: mapApiPropertyFilters(result.filters),
68+
}))
69+
);
70+
71+
const mapApiPropertyAggregation = (
72+
aggregation: PropertyAggregation
73+
): ApiPropertyAggregation => ({
74+
property_name: aggregation.propertyName,
75+
metrics: aggregation.metrics,
76+
top_occurrences_limit:
77+
"topOccurrencesLimit" in aggregation
78+
? aggregation.topOccurrencesLimit
79+
: undefined,
80+
});
81+
82+
const mapApiUsage = (usage: Usage): ApiUsage => ({
83+
requests: usage.requests,
84+
request_tokens: usage.requestTokens,
85+
response_tokens: usage.responseTokens,
86+
total_tokens: usage.totalTokens,
87+
details: usage.details,
88+
});
89+
90+
const mapApiSources = (sources: Source[]): ApiSource[] =>
91+
sources.map((source) => ({
92+
object_id: source.objectId,
93+
collection: source.collection,
94+
}));

src/query/response/api-response.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export type ApiQueryAgentResponse = {
2424
export type ApiSearchResult = {
2525
collection: string;
2626
queries: string[];
27-
filters: ApiPropertyFilter[];
27+
filters: ApiPropertyFilter[][];
2828
filter_operators: "AND" | "OR";
2929
};
3030

src/query/response/response-mapping.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,28 @@ const mapSearches = (searches: ApiSearchResult[][]): SearchResult[][] =>
4848
searchGroup.map((result) => ({
4949
collection: result.collection,
5050
queries: result.queries,
51-
filters: result.filters.map(mapPropertyFilter),
51+
filters: result.filters.map(mapPropertyFilters),
5252
filterOperators: result.filter_operators,
5353
}))
5454
);
5555

56-
const mapPropertyFilter = (filter: ApiPropertyFilter): PropertyFilter => ({
57-
propertyName: filter.property_name,
58-
operator: filter.operator,
59-
value: filter.value,
60-
});
56+
const mapPropertyFilters = (filters: ApiPropertyFilter[]): PropertyFilter[] =>
57+
filters.map((filter) => ({
58+
propertyName: filter.property_name,
59+
operator: filter.operator,
60+
value: filter.value,
61+
}));
6162

6263
const mapAggregations = (
6364
aggregations: ApiAggregationResult[][]
6465
): AggregationResult[][] =>
65-
aggregations.map((aggGroup) =>
66-
aggGroup.map((result) => ({
66+
aggregations.map((aggregationGroup) =>
67+
aggregationGroup.map((result) => ({
6768
collection: result.collection,
6869
searchQuery: result.search_query,
6970
groupbyProperty: result.groupby_property,
7071
aggregations: result.aggregations.map(mapPropertyAggregation),
71-
filters: result.filters.map(mapPropertyFilter),
72+
filters: mapPropertyFilters(result.filters),
7273
}))
7374
);
7475

src/query/response/response.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export type QueryAgentResponse = {
1818
export type SearchResult = {
1919
collection: string;
2020
queries: string[];
21-
filters: PropertyFilter[];
21+
filters: PropertyFilter[][];
2222
filterOperators: "AND" | "OR";
2323
};
2424

0 commit comments

Comments
 (0)