|
1 | 1 | import { WeaviateClient } from "weaviate-client"; |
2 | 2 | import { QueryAgent } from "./agent.js"; |
3 | 3 | 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"; |
6 | 13 | import { QueryAgentError } from "./response/error.js"; |
7 | 14 |
|
8 | 15 | it("runs the query agent", async () => { |
@@ -96,6 +103,134 @@ it("runs the query agent", async () => { |
96 | 103 | }); |
97 | 104 | }); |
98 | 105 |
|
| 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 | + |
99 | 234 | it("search-only mode success: caches searches and sends on subsequent request", async () => { |
100 | 235 | const mockClient = { |
101 | 236 | getConnectionDetails: jest.fn().mockResolvedValue({ |
|
0 commit comments