chat / agent_system /vector_store.py
rejig-ai's picture
code with working websearch agent
9fc2f18
"""
Vector Store Manager for OpenAI Vector Store API integration.
"""
import os
import json
import time
from typing import Dict, List, Optional, Any, Union
from dotenv import load_dotenv
# Note: In a real implementation, you would use the OpenAI client
# This is a mock implementation for demonstration purposes
# from openai import OpenAI
class VectorStoreManager:
"""
Manages interactions with OpenAI Vector Stores for user authentication and data.
"""
def __init__(self):
"""Initialize the Vector Store Manager."""
load_dotenv()
self.api_key = os.getenv("OPENAI_API_KEY")
# In a real implementation, initialize the OpenAI client
# self.client = OpenAI()
# Store IDs for different vector stores
self.auth_index_store_id = None
self.user_profiles_store_id = None
self.user_content_store_id = None
# Cache for frequently accessed data
self.email_path_cache = {}
# Base paths for local data
self.data_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "data")
self.indexes_dir = os.path.join(self.data_dir, "indexes")
self.users_dir = os.path.join(self.data_dir, "users")
self.profiles_dir = os.path.join(self.users_dir, "profiles")
self.history_dir = os.path.join(self.users_dir, "history")
async def initialize_stores(self):
"""
Initialize vector stores if they don't exist.
In a real implementation, this would create OpenAI Vector Stores.
"""
# For demonstration, we'll use mock store IDs
self.auth_index_store_id = "vs_auth_index_123"
self.user_profiles_store_id = "vs_user_profiles_456"
self.user_content_store_id = "vs_user_content_789"
# Create and upload the email index if it doesn't exist
email_index_path = os.path.join(self.indexes_dir, "user_email_index.json")
if not os.path.exists(email_index_path):
self._create_email_index()
# In a real implementation, you would:
# 1. Check if stores exist by listing them
# 2. Create them if they don't exist
# 3. Set expiration policies
# 4. Upload initial files
return {
"auth_index_store_id": self.auth_index_store_id,
"user_profiles_store_id": self.user_profiles_store_id,
"user_content_store_id": self.user_content_store_id
}
def _create_email_index(self):
"""Create the email index file if it doesn't exist."""
os.makedirs(self.indexes_dir, exist_ok=True)
email_index_path = os.path.join(self.indexes_dir, "user_email_index.json")
# Create an empty index
email_index = {}
with open(email_index_path, 'w') as f:
json.dump(email_index, f, indent=2)
async def verify_email(self, email: str) -> bool:
"""
Verify if an email exists in the authentication index.
Args:
email: The email to verify
Returns:
True if email is registered, False otherwise
"""
# In a real implementation, you would use the OpenAI Vector Store API for search
# For demonstration, we'll read directly from the file
email_index_path = os.path.join(self.indexes_dir, "user_email_index.json")
if not os.path.exists(email_index_path):
return False
try:
with open(email_index_path, 'r') as f:
email_index = json.load(f)
# Check if email exists in the index
normalized_email = email.lower()
return normalized_email in email_index
except (json.JSONDecodeError, FileNotFoundError):
return False
async def get_user_profile(self, email: str) -> Optional[Dict[str, Any]]:
"""
Get a user's profile data.
Args:
email: The user's email
Returns:
User profile data or None if not found
"""
# In a real implementation, you would use the OpenAI Vector Store API
# For demonstration, we'll read directly from the file
normalized_email = email.lower()
# Get the profile path from the index or use a default path
email_index_path = os.path.join(self.indexes_dir, "user_email_index.json")
profile_path = None
try:
with open(email_index_path, 'r') as f:
email_index = json.load(f)
if normalized_email in email_index:
profile_path = email_index[normalized_email]
else:
# Default path based on email
profile_path = os.path.join(self.profiles_dir, f"{normalized_email}.json")
except (json.JSONDecodeError, FileNotFoundError):
# Default path based on email
profile_path = os.path.join(self.profiles_dir, f"{normalized_email}.json")
# Check if profile exists
if not os.path.exists(profile_path):
return None
try:
with open(profile_path, 'r') as f:
profile_data = json.load(f)
return profile_data
except (json.JSONDecodeError, FileNotFoundError):
return None
async def add_new_user(self, email: str, profile_data: Dict[str, Any]) -> bool:
"""
Add a new user to the system.
Args:
email: The user's email
profile_data: The user's profile data
Returns:
True if successful, False otherwise
"""
normalized_email = email.lower()
# Create profile directory if it doesn't exist
os.makedirs(self.profiles_dir, exist_ok=True)
# Create a profile file for the user
profile_path = os.path.join(self.profiles_dir, f"{normalized_email}.json")
try:
with open(profile_path, 'w') as f:
json.dump(profile_data, f, indent=2)
# Update the email index
email_index_path = os.path.join(self.indexes_dir, "user_email_index.json")
email_index = {}
if os.path.exists(email_index_path):
with open(email_index_path, 'r') as f:
try:
email_index = json.load(f)
except json.JSONDecodeError:
email_index = {}
# Add the email to the index with the profile path
email_index[normalized_email] = profile_path
with open(email_index_path, 'w') as f:
json.dump(email_index, f, indent=2)
return True
except (IOError, json.JSONDecodeError):
return False
async def update_user_profile(self, email: str, profile_data: Dict[str, Any]) -> bool:
"""
Update a user's profile data.
Args:
email: The user's email
profile_data: The updated profile data
Returns:
True if successful, False otherwise
"""
# Check if user exists
if not await self.verify_email(email):
return False
# Update the profile file
normalized_email = email.lower()
profile_path = os.path.join(self.profiles_dir, f"{normalized_email}.json")
try:
with open(profile_path, 'w') as f:
json.dump(profile_data, f, indent=2)
return True
except (IOError, json.JSONDecodeError):
return False
# In a real implementation, you would initialize this in your application setup
# vector_store_manager = VectorStoreManager()