Skip to content

Commit 8d5742a

Browse files
Dan JonesDan Jones
authored andcommitted
Add test for ask mode
1 parent 9a26992 commit 8d5742a

File tree

1 file changed

+137
-2
lines changed

1 file changed

+137
-2
lines changed

src/query/agent.test.ts

Lines changed: 137 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({

0 commit comments

Comments
 (0)