File size: 2,606 Bytes
dee16ac
 
 
 
 
 
 
f2b0741
 
 
 
 
 
 
 
dee16ac
 
 
f2b0741
dee16ac
 
 
 
 
 
 
 
 
 
f2b0741
 
dee16ac
f2b0741
 
dee16ac
f2b0741
 
dee16ac
f2b0741
 
dee16ac
 
f2b0741
dee16ac
 
 
 
 
 
 
 
 
 
 
 
f2b0741
dee16ac
 
 
 
 
 
 
 
 
 
 
 
 
f2b0741
dee16ac
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
from urllib.parse import urlparse
from dotenv import load_dotenv
import os
import chromadb
import logging

# Set up logging
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(levelname)s - %(message)s",
    handlers=[
        logging.FileHandler("app.log"),  # Save logs to a file
        logging.StreamHandler()         # Show logs in the console
    ]
)
logger = logging.getLogger(__name__)

def get_credentials():
    """Load credentials from the .env file."""
    load_dotenv()
    api_key = os.getenv("api_key", None)
    watsonx_project_id = os.getenv("project_id", None)

    if not api_key or not watsonx_project_id:
        raise EnvironmentError("Missing `api_key` or `project_id` in the .env file.")
    
    return api_key, watsonx_project_id

def load_css(file_name):
    """Load a CSS file for styling."""
    css_path = os.path.join("styles", file_name)  # Adjust to your folder
    try:
        with open(css_path) as file:
            return f'<style>{file.read()}</style>'
    except FileNotFoundError:
        logger.error(f"CSS file '{css_path}' not found.")
        raise
    except Exception as e:
        logger.error(f"Failed to load CSS file: {e}")
        raise

def create_collection_name(url):
    """Create a collection name from a URL."""
    try:
        parsed_url = urlparse(url)
        domain_parts = parsed_url.netloc.split('.')
        if len(domain_parts) >= 2:
            return domain_parts[-2]
        else:
            return "default_collection"
    except Exception as e:
        logger.warning(f"Invalid URL '{url}': {e}")
        return "invalid_url"

def chromadb_client():
    """Initialize a ChromaDB client with custom cache settings."""
    from chromadb.config import Settings
    
    current_dir = os.getcwd()
    custom_cache_path = os.path.join(current_dir, ".cache")
    
    try:
        settings = Settings(persist_directory=custom_cache_path)
        client = chromadb.Client(settings)
        return client
    except Exception as e:
        raise RuntimeError(f"Failed to initialize ChromaDB client: {e}")

def clear_collection(collection_name, client):
    """Clear a specific collection in ChromaDB."""
    try:
        collection = client.get_collection(collection_name)
        if collection:
            collection.delete()
            logger.info(f"Collection '{collection_name}' cleared successfully!")
    except ValueError:
        logger.warning(f"Collection '{collection_name}' does not exist, skipping.")
    except Exception as e:
        logger.error(f"Failed to clear collection '{collection_name}': {e}")