Skip to content

Commit 7067a1f

Browse files
committed
replace getenv with CONFIG object lookups; use Pathlib consistently
1 parent 55a7d2c commit 7067a1f

File tree

1 file changed

+6
-16
lines changed

1 file changed

+6
-16
lines changed

backend/scripts/create_vector_store.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1-
import os
21
from pathlib import Path
32
from openai import OpenAI
3+
from tenantfirstaid.shared import CONFIG
44

5-
6-
if Path(".env").exists():
7-
from dotenv import load_dotenv
8-
9-
load_dotenv(override=True)
10-
11-
API_KEY = os.getenv("OPENAI_API_KEY", os.getenv("GITHUB_API_KEY"))
12-
13-
client = OpenAI(api_key=API_KEY)
5+
client = OpenAI(api_key=CONFIG.openai_api_key or CONFIG.github_api_key)
146

157
# Note: we exit if the vector store already exists because
168
# OpenAI does not return the filenames of files in a vector store,
@@ -38,21 +30,19 @@
3830
vector_store = client.vector_stores.create(name="Oregon Housing Law")
3931

4032
# Get all the files in ./documents
41-
documents_path = Path("./scripts/documents")
33+
documents_path = Path(__file__).parent / "scripts/documents"
4234
file_paths = [
4335
f
44-
for f in os.listdir(documents_path)
45-
if os.path.isfile(os.path.join(documents_path, f))
36+
for f in documents_path.iterdir()
37+
if f.is_file() and f.suffix.lower() in [".txt"]
4638
]
4739

4840
if not file_paths:
4941
print("No text files found in the documents directory.")
5042
exit(1)
5143

5244
print("Uploading files to vector store...")
53-
file_streams = [
54-
open(os.path.join(documents_path, path), "rb") for path in file_paths
55-
]
45+
file_streams = [path.open("rb") for path in file_paths]
5646
# Add the files to the vector store
5747
file_batch = client.vector_stores.file_batches.upload_and_poll(
5848
vector_store_id=vector_store.id, files=file_streams

0 commit comments

Comments
 (0)