Skip to content

Add QueryAgent streaming #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 17, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add new stream method to query agent
  • Loading branch information
Dan Jones authored and Dan Jones committed Jun 12, 2025
commit 0f18868b2d7c9cbf58fc13b39d30a705e696c514
67 changes: 65 additions & 2 deletions src/query/agent.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { WeaviateClient } from "weaviate-client";
import { QueryAgentResponse } from "./response/response.js";
import { mapResponse } from "./response/response-mapping.js";
import { QueryAgentResponse, ProgressMessage, StreamedTokens } from "./response/response.js";
import { mapResponse, mapProgressMessageFromSSE, mapStreamedTokensFromSSE, mapResponseFromSSE } from "./response/response-mapping.js";
import { mapApiResponse } from "./response/api-response-mapping.js";
import { fetchServerSentEvents } from "./response/server-sent-events.js";
import { mapCollections, QueryAgentCollectionConfig } from "./collection.js";

/**
Expand Down Expand Up @@ -78,6 +79,58 @@ export class QueryAgent {

return mapResponse(await response.json());
}

/**
* Stream responses from the query agent.
*
* @param query - The natural language query string for the agent.
* @param options - Additional options for the run.
* @returns The response from the query agent.
*/
async *stream(
query: string,
{ collections, context, includeProgress }: QueryAgentStreamOptions = {}
): AsyncGenerator<ProgressMessage | StreamedTokens | QueryAgentResponse> {
const targetCollections = collections ?? this.collections;

if (!targetCollections) {
throw Error("No collections provided to the query agent.");
}

const { host, bearerToken, headers } =
await this.client.getConnectionDetails();

const sseStream = fetchServerSentEvents(`${this.agentsHost}/agent/stream_query`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: bearerToken!,
"X-Weaviate-Cluster-Url": host,
},
body: JSON.stringify({
headers,
query,
collections: mapCollections(targetCollections),
system_prompt: this.systemPrompt,
previous_response: context ? mapApiResponse(context) : undefined,
include_progress: includeProgress ?? true,
}),
});

for await (const event of sseStream) {
let output: ProgressMessage | StreamedTokens | QueryAgentResponse;
if (event.event === "progress_message") {
output = mapProgressMessageFromSSE(event);
} else if (event.event === "streamed_tokens") {
output = mapStreamedTokensFromSSE(event);
} else if (event.event === "final_state") {
output = mapResponseFromSSE(event);
} else {
throw new Error(`Unexpected event type: ${event.event}`);
}
yield output;
}
}
}

/** Options for the QueryAgent. */
Expand All @@ -97,3 +150,13 @@ export type QueryAgentRunOptions = {
/** Previous response from the agent. */
context?: QueryAgentResponse;
};

/** Options for the QueryAgent stream. */
export type QueryAgentStreamOptions = {
/** List of collections to query. Will override any collections if passed in the constructor. */
collections?: (string | QueryAgentCollectionConfig)[];
/** Previous response from the agent. */
context?: QueryAgentResponse;
/** Include progress messages in the stream. */
includeProgress?: boolean;
};