-
Notifications
You must be signed in to change notification settings - Fork 481
Add WeaviateQueryAgentAskTool / WeaviateQueryAgentSearchModeTool
#474
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
Open
CShorten
wants to merge
1
commit into
crewAIInc:main
Choose a base branch
from
CShorten:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| import json | ||
| import os | ||
| from typing import Any, Optional, Type, List | ||
|
|
||
| try: | ||
| import weaviate | ||
| from weaviate.classes.init import Auth | ||
| from weaviate.agents.query import QueryAgent | ||
| from weaviate.agents.classes import ChatMessage | ||
|
|
||
| WEAVIATE_AVAILABLE = True | ||
| except ImportError: | ||
| WEAVIATE_AVAILABLE = False | ||
| weaviate = Any | ||
| Auth = Any | ||
| QueryAgent = Any | ||
| ChatMessage = Any | ||
|
|
||
| from crewai.tools import BaseTool | ||
| from pydantic import BaseModel, Field | ||
|
|
||
|
|
||
| class WeaviateQueryAgentAskToolSchema(BaseModel): | ||
| query: str = Field( | ||
| ..., | ||
| description="The natural language question to ask the Weaviate Query Agent.", | ||
| ) | ||
|
|
||
|
|
||
| class WeaviateQueryAgentAskTool(BaseTool): | ||
| name: str = "WeaviateQueryAgentAskTool" | ||
| description: str = ( | ||
| "A tool to ask natural language questions to the Weaviate Query Agent. " | ||
| "The agent will process the question, search Weaviate, and return a generated answer." | ||
| ) | ||
| args_schema: Type[BaseModel] = WeaviateQueryAgentAskToolSchema | ||
| collection_names: List[str] = Field( | ||
| ..., | ||
| description="List of collection names to query", | ||
| ) | ||
| weaviate_cluster_url: str = Field( | ||
| ..., | ||
| description="The URL of the Weaviate cluster", | ||
| ) | ||
| weaviate_api_key: str = Field( | ||
| ..., | ||
| description="The API key for the Weaviate cluster", | ||
| ) | ||
| package_dependencies: List[str] = ["weaviate-client", "weaviate-agents"] | ||
|
|
||
| def __init__(self, **kwargs): | ||
| super().__init__(**kwargs) | ||
| if not WEAVIATE_AVAILABLE: | ||
| import click | ||
|
|
||
| if click.confirm( | ||
| "You are missing the 'weaviate-client' and 'weaviate-agents' packages. Would you like to install them?" | ||
| ): | ||
| import subprocess | ||
|
|
||
| subprocess.run(["uv", "pip", "install", "weaviate-client", "weaviate-agents"], check=True) | ||
| else: | ||
| raise ImportError( | ||
| "You are missing the 'weaviate-client' and 'weaviate-agents' packages. Please install them to use this tool." | ||
| ) | ||
|
|
||
| def _run(self, query: str) -> str: | ||
| if not WEAVIATE_AVAILABLE: | ||
| raise ImportError( | ||
| "You are missing the 'weaviate-client' and 'weaviate-agents' packages. Please install them to use this tool." | ||
| ) | ||
|
|
||
| if not self.weaviate_cluster_url or not self.weaviate_api_key: | ||
| raise ValueError("WEAVIATE_URL or WEAVIATE_API_KEY is not set") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Mismatched Config Names in Error MessagesThe Additional Locations (1) |
||
|
|
||
| client = weaviate.connect_to_weaviate_cloud( | ||
| cluster_url=self.weaviate_cluster_url, | ||
| auth_credentials=Auth.api_key(self.weaviate_api_key), | ||
| ) | ||
|
|
||
| try: | ||
| qa = QueryAgent( | ||
| client=client, | ||
| collections=self.collection_names | ||
| ) | ||
|
|
||
| response = qa.ask(query) | ||
|
|
||
| return response.final_answer | ||
|
|
||
| finally: | ||
| client.close() | ||
|
|
||
|
|
||
| class WeaviateQueryAgentSearchModeToolSchema(BaseModel): | ||
| query: str = Field( | ||
| ..., | ||
| description="The natural language search query for the Weaviate Query Agent.", | ||
| ) | ||
|
|
||
|
|
||
| class WeaviateQueryAgentSearchModeTool(BaseTool): | ||
| name: str = "WeaviateQueryAgentSearchModeTool" | ||
| description: str = ( | ||
| "A tool to search Weaviate using natural language queries via the Query Agent. " | ||
| "Returns relevant objects without generating an answer (retrieval only)." | ||
| ) | ||
| args_schema: Type[BaseModel] = WeaviateQueryAgentSearchModeToolSchema | ||
| collection_names: List[str] = Field( | ||
| ..., | ||
| description="List of collection names to search", | ||
| ) | ||
| limit: Optional[int] = Field(default=10, description="Maximum number of results to retrieve") | ||
| weaviate_cluster_url: str = Field( | ||
| ..., | ||
| description="The URL of the Weaviate cluster", | ||
| ) | ||
| weaviate_api_key: str = Field( | ||
| ..., | ||
| description="The API key for the Weaviate cluster", | ||
| ) | ||
| package_dependencies: List[str] = ["weaviate-client", "weaviate-agents"] | ||
|
|
||
| def __init__(self, **kwargs): | ||
| super().__init__(**kwargs) | ||
| if not WEAVIATE_AVAILABLE: | ||
| import click | ||
|
|
||
| if click.confirm( | ||
| "You are missing the 'weaviate-client' and 'weaviate-agents' packages. Would you like to install them?" | ||
| ): | ||
| import subprocess | ||
|
|
||
| subprocess.run(["uv", "pip", "install", "weaviate-client", "weaviate-agents"], check=True) | ||
| else: | ||
| raise ImportError( | ||
| "You are missing the 'weaviate-client' and 'weaviate-agents' packages. Please install them to use this tool." | ||
| ) | ||
|
|
||
| def _run(self, query: str) -> str: | ||
| if not WEAVIATE_AVAILABLE: | ||
| raise ImportError( | ||
| "You are missing the 'weaviate-client' and 'weaviate-agents' packages. Please install them to use this tool." | ||
| ) | ||
|
|
||
| if not self.weaviate_cluster_url or not self.weaviate_api_key: | ||
| raise ValueError("WEAVIATE_URL or WEAVIATE_API_KEY is not set") | ||
|
|
||
| client = weaviate.connect_to_weaviate_cloud( | ||
| cluster_url=self.weaviate_cluster_url, | ||
| auth_credentials=Auth.api_key(self.weaviate_api_key), | ||
| ) | ||
|
|
||
| try: | ||
| qa = QueryAgent( | ||
| client=client, | ||
| collections=self.collection_names | ||
| ) | ||
|
|
||
| search_response = qa.search(query, limit=self.limit) | ||
|
|
||
| results = [] | ||
| for obj in search_response.search_results.objects: | ||
| results.append({ | ||
| "properties": obj.properties, | ||
| "uuid": str(obj.uuid) if hasattr(obj, 'uuid') else None | ||
| }) | ||
|
|
||
| return json.dumps(results, indent=2) | ||
|
|
||
| finally: | ||
| client.close() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Dynamic Imports Fail to Update Availability Flag
After dynamically installing missing Weaviate packages in
__init__, theWEAVIATE_AVAILABLEflag and module imports aren't re-evaluated. This causes the flag to remainFalse, leading the_runmethod in bothWeaviateQueryAgentAskToolandWeaviateQueryAgentSearchModeToolto incorrectly raise anImportError, making the tools unusable within the same process.Additional Locations (1)
crewai_tools/tools/weaviate_tool/query_agent.py#L123-L144