From 8a99ea2723f54b8ac7802912d8fb9586c25e0dc5 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Fri, 20 Dec 2024 23:05:36 +0000 Subject: [PATCH 1/3] feat: add framework-agnostic weaviate tool Co-Authored-By: root@a10k.co --- agentstack/tools/weaviate/__init__.py | 99 +++++++++++++++++++++++++++ agentstack/tools/weaviate/config.json | 19 +++++ 2 files changed, 118 insertions(+) create mode 100644 agentstack/tools/weaviate/__init__.py create mode 100644 agentstack/tools/weaviate/config.json diff --git a/agentstack/tools/weaviate/__init__.py b/agentstack/tools/weaviate/__init__.py new file mode 100644 index 00000000..3aa982d4 --- /dev/null +++ b/agentstack/tools/weaviate/__init__.py @@ -0,0 +1,99 @@ +import os +import json +import weaviate +from typing import Optional +from weaviate.classes.config import Configure +from weaviate.classes.init import Auth + +def search_collection( + collection_name: str, + query: str, + limit: int = 3, + model: str = "nomic-embed-text" +) -> str: + """Search a Weaviate collection using near-text queries. + + Args: + collection_name: Name of the collection to search + query: The search query + limit: Maximum number of results (default: 3) + model: Text embedding model to use (default: nomic-embed-text) + + Returns: + str: JSON string containing search results + """ + url = os.environ.get("WEAVIATE_TOOL_URL") + api_key = os.environ.get("WEAVIATE_TOOL_API_KEY") + openai_key = os.environ.get("WEAVIATE_TOOL_OPENAI_API_KEY") + + if not url or not api_key or not openai_key: + raise ValueError("Missing required environment variables") + + headers = {"X-OpenAI-Api-Key": openai_key} + vectorizer = Configure.Vectorizer.text2vec_openai(model=model) + + client = weaviate.connect_to_weaviate_cloud( + cluster_url=url, + auth_credentials=Auth.api_key(api_key), + headers=headers + ) + + try: + collection = client.collections.get(collection_name) + if not collection: + raise ValueError(f"Collection {collection_name} not found") + + response = collection.query.near_text( + query=query, + limit=limit + ) + + results = [] + for obj in response.objects: + results.append(obj.properties) + + return json.dumps(results, indent=2) + finally: + client.close() + +def create_collection( + collection_name: str, + model: str = "nomic-embed-text" +) -> str: + """Create a new Weaviate collection. + + Args: + collection_name: Name of the collection to create + model: Text embedding model to use (default: nomic-embed-text) + + Returns: + str: Success message + """ + url = os.environ.get("WEAVIATE_TOOL_URL") + api_key = os.environ.get("WEAVIATE_TOOL_API_KEY") + openai_key = os.environ.get("WEAVIATE_TOOL_OPENAI_API_KEY") + + if not url or not api_key or not openai_key: + raise ValueError("Missing required environment variables") + + headers = {"X-OpenAI-Api-Key": openai_key} + vectorizer = Configure.Vectorizer.text2vec_openai(model=model) + + client = weaviate.connect_to_weaviate_cloud( + cluster_url=url, + auth_credentials=Auth.api_key(api_key), + headers=headers + ) + + try: + collection = client.collections.get(collection_name) + if collection: + return f"Collection {collection_name} already exists" + + client.collections.create( + name=collection_name, + vectorizer_config=vectorizer + ) + return f"Created collection {collection_name}" + finally: + client.close() diff --git a/agentstack/tools/weaviate/config.json b/agentstack/tools/weaviate/config.json new file mode 100644 index 00000000..67225595 --- /dev/null +++ b/agentstack/tools/weaviate/config.json @@ -0,0 +1,19 @@ +{ + "name": "weaviate", + "url": "https://github.com/weaviate/weaviate-python-client", + "category": "vector-store", + "env": { + "WEAVIATE_TOOL_URL": null, + "WEAVIATE_TOOL_API_KEY": null, + "WEAVIATE_TOOL_OPENAI_API_KEY": null + }, + "dependencies": [ + "weaviate-client>=3.0.0", + "openai>=1.0.0" + ], + "tools": [ + "search_collection", + "create_collection" + ], + "cta": "🔗 Create your Weaviate cluster here: https://console.weaviate.cloud/" +} From 5e3509e8d28be5121a9e2a2ded503f3b44f9dc5f Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Fri, 20 Dec 2024 23:15:23 +0000 Subject: [PATCH 2/3] refactor: update weaviate tool environment variable names Co-Authored-By: root@a10k.co --- agentstack/tools/weaviate/__init__.py | 14 ++++++++------ agentstack/tools/weaviate/config.json | 6 +++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/agentstack/tools/weaviate/__init__.py b/agentstack/tools/weaviate/__init__.py index 3aa982d4..2127ebda 100644 --- a/agentstack/tools/weaviate/__init__.py +++ b/agentstack/tools/weaviate/__init__.py @@ -22,9 +22,10 @@ def search_collection( Returns: str: JSON string containing search results """ - url = os.environ.get("WEAVIATE_TOOL_URL") - api_key = os.environ.get("WEAVIATE_TOOL_API_KEY") - openai_key = os.environ.get("WEAVIATE_TOOL_OPENAI_API_KEY") + url = os.environ.get("WEAVIATE_URL") + api_key = os.environ.get("WEAVIATE_API_KEY") + openai_key = os.environ.get("WEAVIATE_OPENAI_API_KEY") or \ + os.environ.get("OPENAI_API_KEY") if not url or not api_key or not openai_key: raise ValueError("Missing required environment variables") @@ -69,9 +70,10 @@ def create_collection( Returns: str: Success message """ - url = os.environ.get("WEAVIATE_TOOL_URL") - api_key = os.environ.get("WEAVIATE_TOOL_API_KEY") - openai_key = os.environ.get("WEAVIATE_TOOL_OPENAI_API_KEY") + url = os.environ.get("WEAVIATE_URL") + api_key = os.environ.get("WEAVIATE_API_KEY") + openai_key = os.environ.get("WEAVIATE_OPENAI_API_KEY") or \ + os.environ.get("OPENAI_API_KEY") if not url or not api_key or not openai_key: raise ValueError("Missing required environment variables") diff --git a/agentstack/tools/weaviate/config.json b/agentstack/tools/weaviate/config.json index 67225595..1323a10f 100644 --- a/agentstack/tools/weaviate/config.json +++ b/agentstack/tools/weaviate/config.json @@ -3,9 +3,9 @@ "url": "https://github.com/weaviate/weaviate-python-client", "category": "vector-store", "env": { - "WEAVIATE_TOOL_URL": null, - "WEAVIATE_TOOL_API_KEY": null, - "WEAVIATE_TOOL_OPENAI_API_KEY": null + "WEAVIATE_URL": null, + "WEAVIATE_API_KEY": null, + "WEAVIATE_OPENAI_API_KEY": null }, "dependencies": [ "weaviate-client>=3.0.0", From de1577f63345db29e7e66fb89486a338cba669e5 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Fri, 20 Dec 2024 23:43:14 +0000 Subject: [PATCH 3/3] refactor: add early environment variable validation for weaviate tool Co-Authored-By: root@a10k.co --- agentstack/tools/weaviate/__init__.py | 39 ++++++++++++++++----------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/agentstack/tools/weaviate/__init__.py b/agentstack/tools/weaviate/__init__.py index 2127ebda..8f38de1c 100644 --- a/agentstack/tools/weaviate/__init__.py +++ b/agentstack/tools/weaviate/__init__.py @@ -5,6 +5,29 @@ from weaviate.classes.config import Configure from weaviate.classes.init import Auth +# Required environment variables +url = os.getenv("WEAVIATE_URL") +api_key = os.getenv("WEAVIATE_API_KEY") +openai_key = os.getenv("WEAVIATE_OPENAI_API_KEY") or os.getenv("OPENAI_API_KEY") + +if not url: + raise Exception(( + "Weaviate URL has not been provided.\n" + "Did you set the WEAVIATE_URL in your project's .env file?" + )) + +if not api_key: + raise Exception(( + "Weaviate API key has not been provided.\n" + "Did you set the WEAVIATE_API_KEY in your project's .env file?" + )) + +if not openai_key: + raise Exception(( + "OpenAI API key has not been provided.\n" + "Did you set either WEAVIATE_OPENAI_API_KEY or OPENAI_API_KEY in your project's .env file?" + )) + def search_collection( collection_name: str, query: str, @@ -22,14 +45,6 @@ def search_collection( Returns: str: JSON string containing search results """ - url = os.environ.get("WEAVIATE_URL") - api_key = os.environ.get("WEAVIATE_API_KEY") - openai_key = os.environ.get("WEAVIATE_OPENAI_API_KEY") or \ - os.environ.get("OPENAI_API_KEY") - - if not url or not api_key or not openai_key: - raise ValueError("Missing required environment variables") - headers = {"X-OpenAI-Api-Key": openai_key} vectorizer = Configure.Vectorizer.text2vec_openai(model=model) @@ -70,14 +85,6 @@ def create_collection( Returns: str: Success message """ - url = os.environ.get("WEAVIATE_URL") - api_key = os.environ.get("WEAVIATE_API_KEY") - openai_key = os.environ.get("WEAVIATE_OPENAI_API_KEY") or \ - os.environ.get("OPENAI_API_KEY") - - if not url or not api_key or not openai_key: - raise ValueError("Missing required environment variables") - headers = {"X-OpenAI-Api-Key": openai_key} vectorizer = Configure.Vectorizer.text2vec_openai(model=model)