File size: 1,475 Bytes
120d6fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af69422
 
 
 
 
 
 
 
 
 
 
 
120d6fd
af69422
120d6fd
 
 
 
 
 
 
 
 
 
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
from urllib.parse import urlparse
from dotenv import load_dotenv
import os
import chromadb
import streamlit as st
def get_credentials():
    load_dotenv()
    globals()["api_key"] = os.getenv("api_key", None)
    globals()["watsonx_project_id"] = os.getenv("project_id", None)

def load_css(file_name):
    with open(file_name) as file:
        st.markdown(f'<style>{file.read()}</style>', unsafe_allow_html=True)

def create_collection_name(url):
    parsed_url = urlparse(url)
    domain_parts = parsed_url.netloc.split('.')
    if len(domain_parts) >= 2:
        return domain_parts[-2]  # Extracting the second-level domain
    else:
        return "base"
    
def chromadb_client():
    import chromadb
    # Set up cache directory (consider user-defined location)
    current_dir = os.getcwd()
    # Replace 'my_custom_cache_path' with your desired location
    custom_cache_path = os.path.join(current_dir, ".cache")
    # Create settings object with custom cache path
    settings = chromadb.Settings(persist_directory=custom_cache_path)
     # Initialize client with custom settings
    client = chromadb.Client(settings)
    return client

def clear_collection(collection_name,client):
    try:
        collection = client.get_collection(collection_name)
        if collection:
            collection.delete()
            print(f"Collection '{collection_name}' cleared successfully!")
    except ValueError:
        pass  # collection does not exist, do nothing