File size: 2,647 Bytes
05e3517
7a2828d
0382412
05e3517
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a2828d
0382412
 
 
 
 
7a2828d
c1da4b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0382412
c1da4b1
 
 
 
 
 
 
 
 
7a2828d
0382412
05e3517
bb4d10d
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os
import json
import sys
from dotenv import load_dotenv

# Load environment variables from .env file if it exists
load_dotenv()

# Check for required environment variables and provide guidance if missing
required_vars = ["OPENAI_API_KEY", "PROJECT_ID", "DATASET_ID", "BIGQUERY_KEY_PATH"]
missing_vars = [var for var in required_vars if not os.environ.get(var)]

if missing_vars:
    print("ERROR: Missing required environment variables:")
    for var in missing_vars:
        print(f"- {var}")
    print("\nPlease set these variables in your environment or in a .env file.")
    print("Example .env file:")
    print("""
    OPENAI_API_KEY=your_openai_api_key
    PROJECT_ID=your_gcp_project_id
    DATASET_ID=your_bigquery_dataset_id
    BIGQUERY_KEY_PATH=path/to/your/bigquery/credentials.json
    """)

# Handle BigQuery credentials
print("DEBUG: Handling BigQuery credentials...")

if os.environ.get("BIGQUERY_KEY_PATH"):
    bigquery_key = os.environ.get("BIGQUERY_KEY_PATH")
    print(f"DEBUG: BIGQUERY_KEY_PATH is set, length: {len(bigquery_key)} characters")
    
    # Create a temporary credentials file from the environment variable
    credentials_path = "/tmp/bigquery_credentials.json"
    
    try:
        # Try to directly write the content to a file without JSON parsing
        with open(credentials_path, "w") as f:
            f.write(bigquery_key)
        
        print(f"DEBUG: Wrote credentials to {credentials_path}")
        

        os.environ["BIGQUERY_KEY_PATH"] = credentials_path
        print(f"DEBUG: Set  BIGQUERY_KEY_PATH to {credentials_path}")
        
        # Verify the file exists
        if os.path.exists(credentials_path):
            print(f"DEBUG: Confirmed credentials file exists at {credentials_path}")
            # Try to validate the JSON by reading it back
            try:
                with open(credentials_path, "r") as f:
                    json.load(f)
                print("DEBUG: Credentials file contains valid JSON")
            except json.JSONDecodeError as e:
                print(f"WARNING: Credentials file contains invalid JSON: {str(e)}")
                print("This might cause issues with BigQuery authentication")
        else:
            print(f"ERROR: Failed to create credentials file at {credentials_path}")
    except Exception as e:
        print(f"ERROR: Failed to process BigQuery credentials: {str(e)}")
else:
    print("WARNING: BIGQUERY_KEY_PATH environment variable is not set")

# BigQuery configuration
BIGQUERY_KEY_PATH = os.environ.get("BIGQUERY_KEY_PATH")
PROJECT_ID = os.environ.get("PROJECT_ID")
DATASET_ID = os.environ.get("DATASET_ID")