🔍 Code Extractor

class OpenAIChatLLM

Maturity: 49

Adapter class for interacting with OpenAI's Chat Completions API, supporting both GPT-4 and GPT-5 model families with automatic parameter adjustment based on model type.

File:
/tf/active/vicechatdev/docchat/llm_factory.py
Lines:
75 - 108
Complexity:
moderate

Purpose

This class provides a unified interface for making chat completion requests to OpenAI's API, handling the differences between GPT-4 and GPT-5 models automatically. It manages API client initialization, model-specific parameter configuration (GPT-5 uses max_completion_tokens and has fixed temperature, while GPT-4 uses max_tokens and configurable temperature), and response parsing. The class is designed to be used as an adapter in larger LLM-based systems, providing a consistent interface regardless of the underlying GPT model version.

Source Code

class OpenAIChatLLM:
    """Adapter using OpenAI Chat Completions API (for GPT-4 and GPT-5 families)."""

    def __init__(self, model: str, api_key: Optional[str] = None, temperature: float = 0.0, max_tokens: int = 4096):
        from openai import OpenAI  # lazy import
        self.client = OpenAI(api_key=api_key)
        self.model = model
        self.temperature = temperature
        self.max_tokens = max_tokens
        # GPT-5 requires max_completion_tokens instead of max_tokens
        self.is_gpt5 = 'gpt-5' in model.lower()

    @property
    def model_name(self) -> str:
        return self.model

    def invoke(self, prompt: str) -> LLMMessage:
        # Build parameters based on model
        params = {
            "model": self.model,
            "messages": [{"role": "user", "content": prompt}],
        }
        
        # GPT-5 uses max_completion_tokens and only supports temperature=1 (default)
        if self.is_gpt5:
            params["max_completion_tokens"] = self.max_tokens
            # GPT-5 only supports default temperature (1), so don't set it
        else:
            params["max_tokens"] = self.max_tokens
            params["temperature"] = self.temperature
        
        resp = self.client.chat.completions.create(**params)
        content = resp.choices[0].message.content or ''
        return LLMMessage(content=content.strip())

Parameters

Name Type Default Kind
bases - -

Parameter Details

model: The OpenAI model identifier string (e.g., 'gpt-4', 'gpt-4-turbo', 'gpt-5'). The class automatically detects GPT-5 models by checking if 'gpt-5' is in the model name (case-insensitive) and adjusts API parameters accordingly.

api_key: Optional OpenAI API key string. If None, the OpenAI client will attempt to use the OPENAI_API_KEY environment variable. Passed directly to the OpenAI client constructor.

temperature: Float value controlling randomness in responses (0.0 to 2.0). Lower values make output more deterministic. Note: This parameter is ignored for GPT-5 models, which only support the default temperature of 1.0.

max_tokens: Integer specifying the maximum number of tokens to generate in the completion. For GPT-4 models, this is passed as 'max_tokens'. For GPT-5 models, this is passed as 'max_completion_tokens'. Default is 4096.

Return Value

The __init__ method returns an instance of OpenAIChatLLM. The invoke() method returns an LLMMessage object containing the generated text response from the API. The LLMMessage has a 'content' attribute with the stripped string content from the model's response. The model_name property returns a string with the model identifier.

Class Interface

Methods

__init__(self, model: str, api_key: Optional[str] = None, temperature: float = 0.0, max_tokens: int = 4096)

Purpose: Initializes the OpenAI chat LLM adapter with specified model and configuration parameters

Parameters:

  • model: OpenAI model identifier (e.g., 'gpt-4', 'gpt-5')
  • api_key: Optional API key; uses environment variable if None
  • temperature: Response randomness (0.0-2.0); ignored for GPT-5
  • max_tokens: Maximum tokens to generate in completion

Returns: None (constructor)

@property model_name(self) -> str property

Purpose: Returns the model identifier string being used by this instance

Returns: String containing the model name (e.g., 'gpt-4', 'gpt-5-preview')

invoke(self, prompt: str) -> LLMMessage

Purpose: Sends a prompt to the OpenAI API and returns the model's response wrapped in an LLMMessage object

Parameters:

  • prompt: The user prompt/question to send to the model

Returns: LLMMessage object containing the stripped response content from the model

Attributes

Name Type Description Scope
client OpenAI The OpenAI API client instance used to make API requests instance
model str The model identifier string (e.g., 'gpt-4', 'gpt-5-preview') instance
temperature float The temperature setting for response randomness (0.0-2.0); not used for GPT-5 models instance
max_tokens int Maximum number of tokens to generate; mapped to max_tokens (GPT-4) or max_completion_tokens (GPT-5) instance
is_gpt5 bool Boolean flag indicating whether the model is a GPT-5 variant (determined by checking if 'gpt-5' is in model name) instance

Dependencies

  • openai
  • dataclasses
  • typing
  • logging
  • config

Required Imports

from typing import Optional
from dataclasses import dataclass

Conditional/Optional Imports

These imports are only needed under specific conditions:

from openai import OpenAI

Condition: Lazy import inside __init__ method, required when instantiating the class

Required (conditional)

Usage Example

# Basic usage with API key
llm = OpenAIChatLLM(model='gpt-4', api_key='sk-...')
response = llm.invoke('What is the capital of France?')
print(response.content)

# Using GPT-5 (automatically handles different parameters)
llm_gpt5 = OpenAIChatLLM(model='gpt-5-preview', api_key='sk-...')
response = llm_gpt5.invoke('Explain quantum computing')
print(response.content)

# Using environment variable for API key
import os
os.environ['OPENAI_API_KEY'] = 'sk-...'
llm = OpenAIChatLLM(model='gpt-4-turbo', temperature=0.7, max_tokens=2000)
response = llm.invoke('Write a haiku about coding')
print(response.content)

# Get model name
model_name = llm.model_name
print(f'Using model: {model_name}')

Best Practices

  • Always provide an API key either through the constructor or OPENAI_API_KEY environment variable
  • Be aware that GPT-5 models ignore the temperature parameter and use a fixed default of 1.0
  • The class automatically detects GPT-5 models by checking for 'gpt-5' in the model name (case-insensitive)
  • The invoke method creates a new API request each time it's called; consider caching or batching for multiple requests
  • The class strips whitespace from responses automatically; the original response may have leading/trailing whitespace
  • Handle potential API errors (rate limits, invalid API keys, network issues) when calling invoke()
  • The max_tokens parameter has different meanings for GPT-4 (max_tokens) vs GPT-5 (max_completion_tokens) but the class handles this automatically
  • The client is initialized once during instantiation and reused for all invoke() calls
  • For production use, implement retry logic and error handling around the invoke() method

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class OpenAIResponsesLLM 80.1% similar

    Adapter class for OpenAI's Responses API, specifically designed for GPT-5 family models with automatic fallback mechanisms to stable models when responses fail.

    From: /tf/active/vicechatdev/docchat/llm_factory.py
  • class AzureOpenAIChatLLM 78.3% similar

    Adapter class for interacting with Azure OpenAI's Chat Completions API, providing a simplified interface for generating chat responses using Azure-hosted OpenAI models.

    From: /tf/active/vicechatdev/docchat/llm_factory.py
  • class LLMClient_v1 77.0% similar

    A client class for interacting with Large Language Models (LLMs), specifically designed to work with OpenAI's chat completion API.

    From: /tf/active/vicechatdev/QA_updater/core/llm_client.py
  • class LLMClient_v1 71.4% similar

    Multi-LLM client that provides a unified interface for interacting with OpenAI GPT-4o, Azure OpenAI, Google Gemini, and Anthropic Claude models.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function get_llm_instance 65.7% similar

    Factory function that creates and returns an appropriate LLM (Large Language Model) instance based on the specified model name, automatically detecting the provider (OpenAI, Azure OpenAI, or Anthropic) and configuring it with the given parameters.

    From: /tf/active/vicechatdev/docchat/llm_factory.py
← Back to Browse