Skip to content

Commit de1577f

Browse files
refactor: add early environment variable validation for weaviate tool
Co-Authored-By: root@a10k.co <root@a10k.co>
1 parent 5e3509e commit de1577f

1 file changed

Lines changed: 23 additions & 16 deletions

File tree

agentstack/tools/weaviate/__init__.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,29 @@
55
from weaviate.classes.config import Configure
66
from weaviate.classes.init import Auth
77

8+
# Required environment variables
9+
url = os.getenv("WEAVIATE_URL")
10+
api_key = os.getenv("WEAVIATE_API_KEY")
11+
openai_key = os.getenv("WEAVIATE_OPENAI_API_KEY") or os.getenv("OPENAI_API_KEY")
12+
13+
if not url:
14+
raise Exception((
15+
"Weaviate URL has not been provided.\n"
16+
"Did you set the WEAVIATE_URL in your project's .env file?"
17+
))
18+
19+
if not api_key:
20+
raise Exception((
21+
"Weaviate API key has not been provided.\n"
22+
"Did you set the WEAVIATE_API_KEY in your project's .env file?"
23+
))
24+
25+
if not openai_key:
26+
raise Exception((
27+
"OpenAI API key has not been provided.\n"
28+
"Did you set either WEAVIATE_OPENAI_API_KEY or OPENAI_API_KEY in your project's .env file?"
29+
))
30+
831
def search_collection(
932
collection_name: str,
1033
query: str,
@@ -22,14 +45,6 @@ def search_collection(
2245
Returns:
2346
str: JSON string containing search results
2447
"""
25-
url = os.environ.get("WEAVIATE_URL")
26-
api_key = os.environ.get("WEAVIATE_API_KEY")
27-
openai_key = os.environ.get("WEAVIATE_OPENAI_API_KEY") or \
28-
os.environ.get("OPENAI_API_KEY")
29-
30-
if not url or not api_key or not openai_key:
31-
raise ValueError("Missing required environment variables")
32-
3348
headers = {"X-OpenAI-Api-Key": openai_key}
3449
vectorizer = Configure.Vectorizer.text2vec_openai(model=model)
3550

@@ -70,14 +85,6 @@ def create_collection(
7085
Returns:
7186
str: Success message
7287
"""
73-
url = os.environ.get("WEAVIATE_URL")
74-
api_key = os.environ.get("WEAVIATE_API_KEY")
75-
openai_key = os.environ.get("WEAVIATE_OPENAI_API_KEY") or \
76-
os.environ.get("OPENAI_API_KEY")
77-
78-
if not url or not api_key or not openai_key:
79-
raise ValueError("Missing required environment variables")
80-
8188
headers = {"X-OpenAI-Api-Key": openai_key}
8289
vectorizer = Configure.Vectorizer.text2vec_openai(model=model)
8390

0 commit comments

Comments
 (0)