Class instantiation
Instantiate the Query Agent against an authenticated Weaviate Cloud client. Configuration can be set at construction time (collections, system prompt, and — in Python — a request timeout) and most options can also be overridden per call to ask() or search().
Basic instantiation
The Query Agent requires only a target Weaviate Cloud instance to be initialised. First, set up a Weaviate client:
import os
import weaviate
from weaviate.agents.query import QueryAgent
from weaviate.classes.init import Auth
headers = {
# Provide your required API key(s), e.g. Cohere, OpenAI, etc. for the configured vectorizer(s)
"X-INFERENCE-PROVIDER-API-KEY": os.environ.get("YOUR_INFERENCE_PROVIDER_KEY", ""),
}
client = weaviate.connect_to_weaviate_cloud(
cluster_url=os.environ.get("WEAVIATE_URL"),
auth_credentials=Auth.api_key(os.environ.get("WEAVIATE_API_KEY")),
headers=headers,
)
Then pass that client to the Query Agent, and all Weaviate calls via agents will be routed through it.
qa = QueryAgent(client=client)
In Python, to instantiate the async Query Agent (AsyncQueryAgent), you must also pass an async Weaviate Client via weaviate.use_async_with_weaviate_cloud. In JavaScript/TypeScript, the Query Agent is async by default.
Additional configuration
Parameters
The QueryAgent constructor accepts the following arguments:
| Parameter | Type | Description |
|---|---|---|
client | WeaviateClient | Required. The Weaviate client connected to a Weaviate Cloud cluster. |
collections | list[str | QueryAgentCollectionConfig] | Optional. The collections to query. Can be overridden per call to ask() or search(). |
system_prompt | str | Optional. Prompt to provide extra instructions to the agents, as well as define the tone, format, and style of the agent's final response. See the page on the system prompt for more detail. |
timeout | int | None | Optional. The maximum time to wait for a response, in seconds. Defaults to 60 seconds. |
Collections
You can define which collections are available either
- at the instantiation of the Query Agent base class,
qa = QueryAgent(
client=client,
collections=["FinancialContracts", "Weather"]
) - or at runtime of the Query Agent's methods, either Ask Mode or Search Mode.
qa = QueryAgent(client=client)
qa.ask(
"What type of contracts have been signed and who were the authors?",
collections=["FinancialContracts", "Weather"]
)
If you provide both, then the collections specified at runtime will override those specified in the base class. See the page on collection configuration for more detail.
Questions and feedback
If you have any questions or feedback, let us know in the user forum.
