Skip to content

Commit 86513d9

Browse files
authored
use nested data model for collection configuration (#8)
* use nested data model for collection configuration * rename variable to camelCase
1 parent bdecbbb commit 86513d9

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

src/query/agent.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { WeaviateClient } from "weaviate-client";
22
import { QueryAgentResponse } from "./response/response.js";
33
import { mapResponse } from "./response/response-mapping.js";
44
import { mapApiResponse } from "./response/api-response-mapping.js";
5+
import { mapCollections, QueryAgentCollectionConfig } from "./collection.js";
56

67
/**
78
* An agent for executing agentic queries against Weaviate.
@@ -13,24 +14,25 @@ import { mapApiResponse } from "./response/api-response-mapping.js";
1314
* For more information, see the [Weaviate Query Agent Docs](https://weaviate.io/developers/agents/query)
1415
*/
1516
export class QueryAgent {
17+
private collections?: (string | QueryAgentCollectionConfig)[];
1618
private systemPrompt?: string;
1719
private agentsHost: string;
1820

1921
/**
2022
* Creates a new QueryAgent instance.
2123
*
2224
* @param client - The Weaviate client instance.
23-
* @param collections - The collections to query.
2425
* @param options - Additional options for the QueryAgent.
2526
*/
2627
constructor(
2728
private client: WeaviateClient,
28-
private collections: string[],
2929
{
30+
collections,
3031
systemPrompt,
3132
agentsHost = "https://api.agents.weaviate.io",
3233
}: QueryAgentOptions = {}
3334
) {
35+
this.collections = collections;
3436
this.systemPrompt = systemPrompt;
3537
this.agentsHost = agentsHost;
3638
}
@@ -44,8 +46,13 @@ export class QueryAgent {
4446
*/
4547
async run(
4648
query: string,
47-
{ viewProperties, context, targetVector }: QueryAgentRunOptions = {}
49+
{ collections, context }: QueryAgentRunOptions = {}
4850
): Promise<QueryAgentResponse> {
51+
const targetCollections = collections ?? this.collections;
52+
if (!targetCollections) {
53+
throw Error("No collections provided to the query agent.");
54+
}
55+
4956
const { host, bearerToken, headers } =
5057
await this.client.getConnectionDetails();
5158

@@ -59,11 +66,9 @@ export class QueryAgent {
5966
body: JSON.stringify({
6067
headers,
6168
query,
62-
collection_names: this.collections,
63-
collection_view_properties: viewProperties,
69+
collections: mapCollections(targetCollections),
6470
system_prompt: this.systemPrompt,
6571
previous_response: context ? mapApiResponse(context) : undefined,
66-
target_vector: targetVector,
6772
}),
6873
});
6974

@@ -77,6 +82,8 @@ export class QueryAgent {
7782

7883
/** Options for the QueryAgent. */
7984
export type QueryAgentOptions = {
85+
/** List of collections to query. Will be overriden if passed in the `run` method. */
86+
collections?: (string | QueryAgentCollectionConfig)[];
8087
/** System prompt to guide the agent's behavior. */
8188
systemPrompt?: string;
8289
/** Host of the agents service. */
@@ -85,16 +92,8 @@ export type QueryAgentOptions = {
8592

8693
/** Options for the QueryAgent run. */
8794
export type QueryAgentRunOptions = {
88-
/** List of of property names the agent has the ability to view across all collections. */
89-
viewProperties?: string[];
90-
/**
91-
* Target vector for the query if a collection uses named vector.
92-
* When multiple collections are provided to the query agent,
93-
* a mapping must be used to map collection names to target vectors.
94-
*/
95-
targetVector?: TargetVector | Record<string, TargetVector>;
95+
/** List of collections to query. Will override any collections if passed in the constructor. */
96+
collections?: (string | QueryAgentCollectionConfig)[];
9697
/** Previous response from the agent. */
9798
context?: QueryAgentResponse;
9899
};
99-
100-
type TargetVector = string | string[];

src/query/collection.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export const mapCollections = (
2+
collections: (string | QueryAgentCollectionConfig)[]
3+
) =>
4+
collections.map((collection) =>
5+
typeof collection === "string"
6+
? collection
7+
: {
8+
name: collection.name,
9+
view_properties: collection.viewProperties,
10+
target_vector: collection.targetVector,
11+
}
12+
);
13+
14+
/** Configuration for a collection to query. */
15+
export type QueryAgentCollectionConfig = {
16+
/** The name of the collection to query. */
17+
name: string;
18+
/** List of of property names the agent has the ability to view for the collection. */
19+
viewProperties?: string[];
20+
/** Target vector for the query if a collection uses named vector. */
21+
targetVector?: string | string[];
22+
};

src/query/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./agent.js";
2+
export { QueryAgentCollectionConfig } from "./collection.js";
23
export * from "./response/index.js";

0 commit comments

Comments
 (0)