File size: 1,340 Bytes
36183d4
 
 
535a3a5
36183d4
 
 
 
 
535a3a5
36183d4
535a3a5
36183d4
 
 
 
535a3a5
36183d4
 
3c4ab41
36183d4
 
 
 
 
3c4ab41
36183d4
 
 
 
 
535a3a5
36183d4
 
 
 
3c4ab41
36183d4
 
 
535a3a5
3c4ab41
 
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
from litellm import OpenAI
import os
from dotenv import load_dotenv
from typing import Optional, Tuple, Any

# Load environment variables
load_dotenv()

# Initialize client as None
client: Optional[OpenAI] = None

def get_client() -> Optional[OpenAI]:
    """Get the OpenAI client instance"""
    global client
    return client

def initialize_client(api_key: Optional[str] = None) -> Tuple[bool, str]:
    """Initialize the OpenAI client with an API key"""
    global client
    import logging
    
    # Use provided API key or get from environment
    api_key = api_key or os.environ.get("OPENAI_API_KEY")
    
    if not api_key:
        logging.error("No API key provided")
        return False, "No API key provided"
    
    try:
        client = OpenAI(api_key=api_key)
        # Test the connection with a simple request
        response: Any = client.chat.completions.create(
            model="gpt-3.5-turbo",
            messages=[{"role": "user", "content": "test"}],
            max_tokens=5,
        )
        logging.info("API Key updated and verified successfully")
        return True, "API Key updated and verified successfully"
    except Exception as e:
        client = None
        error_message: str = f"Failed to initialize client: {str(e)}"
        logging.error(error_message)
        return False, error_message