@@ -12,10 +12,15 @@ import {
12
12
} from "./response/response-mapping.js" ;
13
13
import { mapApiResponse } from "./response/api-response-mapping.js" ;
14
14
import { fetchServerSentEvents } from "./response/server-sent-events.js" ;
15
- import { mapCollections , QueryAgentCollectionConfig } from "./collection.js" ;
15
+ import {
16
+ mapCollections ,
17
+ QueryAgentCollection ,
18
+ QueryAgentCollectionConfig ,
19
+ } from "./collection.js" ;
16
20
import { handleError } from "./response/error.js" ;
17
21
import { QueryAgentSearcher } from "./search.js" ;
18
22
import { SearchModeResponse } from "./response/response.js" ;
23
+ import { getHeaders } from "./connection.js" ;
19
24
20
25
/**
21
26
* An agent for executing agentic queries against Weaviate.
@@ -97,33 +102,23 @@ export class QueryAgent {
97
102
/**
98
103
* Ask query agent a question.
99
104
*
100
- * @param query - The natural language query string for the agent.
105
+ * @param query - The natural language query string or conversation context for the agent.
101
106
* @param options - Additional options for the run.
102
107
* @returns The response from the query agent.
103
108
*/
104
109
async ask (
105
- query : string ,
110
+ query : QueryAgentQuery ,
106
111
{ collections } : QueryAgentAskOptions = { } ,
107
112
) : Promise < QueryAgentResponse > {
108
- const targetCollections = collections ?? this . collections ;
109
- if ( ! targetCollections ) {
110
- throw Error ( "No collections provided to the query agent." ) ;
111
- }
112
-
113
- const { host, bearerToken, headers } =
114
- await this . client . getConnectionDetails ( ) ;
113
+ const targetCollections = this . validateCollections ( collections ) ;
114
+ const { requestHeaders, connectionHeaders } = await getHeaders ( this . client ) ;
115
115
116
116
const response = await fetch ( `${ this . agentsHost } /agent/query` , {
117
117
method : "POST" ,
118
- headers : {
119
- "Content-Type" : "application/json" ,
120
- Authorization : bearerToken ! ,
121
- "X-Weaviate-Cluster-Url" : host ,
122
- "X-Agent-Request-Origin" : "typescript-client" ,
123
- } ,
118
+ headers : requestHeaders ,
124
119
body : JSON . stringify ( {
125
- headers,
126
- query,
120
+ headers : connectionHeaders ,
121
+ query : typeof query === "string" ? query : { messages : query } ,
127
122
collections : mapCollections ( targetCollections ) ,
128
123
system_prompt : this . systemPrompt ,
129
124
} ) ,
@@ -238,40 +233,40 @@ export class QueryAgent {
238
233
/**
239
234
* Ask query agent a question and stream the response.
240
235
*
241
- * @param query - The natural language query string for the agent.
236
+ * @param query - The natural language query string or conversation context for the agent.
242
237
* @param options - Additional options for the run.
243
238
* @returns The response from the query agent.
244
239
*/
245
240
askStream (
246
- query : string ,
241
+ query : QueryAgentQuery ,
247
242
options : QueryAgentAskStreamOptions & {
248
243
includeProgress : false ;
249
244
includeFinalState : false ;
250
245
} ,
251
246
) : AsyncGenerator < StreamedTokens > ;
252
247
askStream (
253
- query : string ,
248
+ query : QueryAgentQuery ,
254
249
options : QueryAgentAskStreamOptions & {
255
250
includeProgress : false ;
256
251
includeFinalState ?: true ;
257
252
} ,
258
253
) : AsyncGenerator < StreamedTokens | QueryAgentResponse > ;
259
254
askStream (
260
- query : string ,
255
+ query : QueryAgentQuery ,
261
256
options : QueryAgentAskStreamOptions & {
262
257
includeProgress ?: true ;
263
258
includeFinalState : false ;
264
259
} ,
265
260
) : AsyncGenerator < ProgressMessage | StreamedTokens > ;
266
261
askStream (
267
- query : string ,
262
+ query : QueryAgentQuery ,
268
263
options ?: QueryAgentAskStreamOptions & {
269
264
includeProgress ?: true ;
270
265
includeFinalState ?: true ;
271
266
} ,
272
267
) : AsyncGenerator < ProgressMessage | StreamedTokens | QueryAgentResponse > ;
273
268
async * askStream (
274
- query : string ,
269
+ query : QueryAgentQuery ,
275
270
{
276
271
collections,
277
272
includeProgress,
@@ -299,7 +294,7 @@ export class QueryAgent {
299
294
} ,
300
295
body : JSON . stringify ( {
301
296
headers,
302
- query,
297
+ query : typeof query === "string" ? query : { messages : query } ,
303
298
collections : mapCollections ( targetCollections ) ,
304
299
system_prompt : this . systemPrompt ,
305
300
include_progress : includeProgress ?? true ,
@@ -336,28 +331,50 @@ export class QueryAgent {
336
331
* reuses the same underlying searches to ensure consistency across pages.
337
332
*/
338
333
async search (
339
- query : string ,
334
+ query : QueryAgentQuery ,
340
335
{ limit = 20 , collections } : QueryAgentSearchOnlyOptions = { } ,
341
336
) : Promise < SearchModeResponse > {
342
- const searcher = new QueryAgentSearcher ( this . client , query , {
343
- collections : collections ?? this . collections ,
344
- systemPrompt : this . systemPrompt ,
345
- agentsHost : this . agentsHost ,
346
- } ) ;
337
+ const searcher = new QueryAgentSearcher (
338
+ this . client ,
339
+ query ,
340
+ this . validateCollections ( collections ) ,
341
+ this . systemPrompt ,
342
+ this . agentsHost ,
343
+ ) ;
344
+
347
345
return searcher . run ( { limit, offset : 0 } ) ;
348
346
}
347
+
348
+ private validateCollections = (
349
+ collections ?: QueryAgentCollection [ ] ,
350
+ ) : QueryAgentCollection [ ] => {
351
+ const targetCollections = collections ?? this . collections ;
352
+
353
+ if ( ! targetCollections ) {
354
+ throw Error ( "No collections provided to the query agent." ) ;
355
+ }
356
+
357
+ return targetCollections ;
358
+ } ;
349
359
}
350
360
351
361
/** Options for the QueryAgent. */
352
362
export type QueryAgentOptions = {
353
363
/** List of collections to query. Will be overriden if passed in the `run` method. */
354
- collections ?: ( string | QueryAgentCollectionConfig ) [ ] ;
364
+ collections ?: QueryAgentCollection [ ] ;
355
365
/** System prompt to guide the agent's behavior. */
356
366
systemPrompt ?: string ;
357
367
/** Host of the agents service. */
358
368
agentsHost ?: string ;
359
369
} ;
360
370
371
+ export type QueryAgentQuery = string | ChatMessage [ ] ;
372
+
373
+ export type ChatMessage = {
374
+ role : "user" | "assistant" ;
375
+ content : string ;
376
+ } ;
377
+
361
378
/** Options for the QueryAgent run. */
362
379
export type QueryAgentRunOptions = {
363
380
/** List of collections to query. Will override any collections if passed in the constructor. */
0 commit comments