[96a5a0]: / src / utils / openai_client.py

Download this file

57 lines (41 with data), 1.6 kB

 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
# utils/openai_client.py
"""
OpenAI Client Module for Clinical Trial Analysis
This module manages the initialization and configuration of the OpenAI client
used for LLM-based clinical trial eligibility criteria processing. It handles
API key loading and client setup.
The module ensures:
1. Environment variables are properly loaded
2. API key availability is verified
3. OpenAI client is correctly initialized
4. Errors are appropriately logged and handled
Functions:
get_openai_client: Initialize and return a configured OpenAI client.
"""
import logging
import os
from dotenv import load_dotenv
from openai import OpenAI
# Configure logging
logger = logging.getLogger(__name__)
# Load environment variables from .env file
load_dotenv()
def get_openai_client() -> OpenAI:
"""
Initialize and return an OpenAI client configured with the appropriate API key.
This function loads the API key from environment variables and creates a
properly configured OpenAI client for use throughout the application.
Returns:
OpenAI: The initialized OpenAI client.
Raises:
ValueError: If the OPENAI_API_KEY is not found in environment variables.
"""
# Retrieve API key from environment variables
api_key = os.getenv("OPENAI_API_KEY")
# Verify API key existence
if not api_key:
logger.error("OPENAI_API_KEY not found in environment variables.")
raise ValueError("OPENAI_API_KEY not found in environment variables.")
logger.info("OpenAI client initialized.")
# Create and return the OpenAI client
return OpenAI(api_key=api_key)