Repository: oumi-ai/oumi
File: src/oumi/inference/openai_inference_engine.py (Python)

Code before:
from typing import Any, Optional

from typing_extensions import override

from oumi.core.configs import GenerationParams
from oumi.core.types.conversation import Conversation
from oumi.inference.remote_inference_engine import RemoteInferenceEngine


class OpenAIInferenceEngine(RemoteInferenceEngine):
    """Engine for running inference against the OpenAI API."""

    @property
    @override
    def base_url(self) -> Optional[str]:
        """Return the default base URL for the OpenAI API."""
        return "https://api.openai.com/v1/chat/completions"

    @property
    @override
    def api_key_env_varname(self) -> Optional[str]:
        """Return the default environment variable name for the OpenAI API key."""
        return "OPENAI_API_KEY"

    @override
    def _convert_conversation_to_api_input(
        self, conversation: Conversation, generation_params: GenerationParams
    ) -> dict[str, Any]:
        """Converts a conversation to an OpenAI input.

        Documentation: https://platform.openai.com/docs/api-reference/chat/create

        Args:
            conversation: The conversation to convert.
            generation_params: Parameters for generation during inference.

        Returns:
            Dict[str, Any]: A dictionary representing the OpenAI input.
        """
        # o1-preview does NOT support logit_bias.
        if self._model and self._model == "o1-preview":
            generation_params = copy.deepcopy(generation_params)
            generation_params.logit_bias = {}

        return super()._convert_conversation_to_api_input(
            conversation=conversation, generation_params=generation_params
        )


Review comment (bug):
This function could be omitted, since `logit_bias` is empty by default & the super function has been updated to NOT include `logit_bias` in the API call if empty. This fn will only protect the user if they unintentionally set this. It is questionable though if it's a better experience to silently ignore this or just fail. 

Code after:
from typing import Any, Optional

from typing_extensions import override

from oumi.core.configs import GenerationParams, ModelParams
from oumi.core.types.conversation import Conversation
from oumi.inference.remote_inference_engine import RemoteInferenceEngine


class OpenAIInferenceEngine(RemoteInferenceEngine):
    """Engine for running inference against the OpenAI API."""

    @property
    @override
    def base_url(self) -> Optional[str]:
        """Return the default base URL for the OpenAI API."""
        return "https://api.openai.com/v1/chat/completions"

    @property
    @override
    def api_key_env_varname(self) -> Optional[str]:
        """Return the default environment variable name for the OpenAI API key."""
        return "OPENAI_API_KEY"

    @override
    def _convert_conversation_to_api_input(
        self,
        conversation: Conversation,
        generation_params: GenerationParams,
        model_params: ModelParams,
    ) -> dict[str, Any]:
        """Converts a conversation to an OpenAI input.

        Documentation: https://platform.openai.com/docs/api-reference/chat/create

        Args:
            conversation: The conversation to convert.
            generation_params: Parameters for generation during inference.
            model_params: Model parameters to use during inference.

        Returns:
            Dict[str, Any]: A dictionary representing the OpenAI input.
        """
        # o1-preview does NOT support logit_bias.
        if model_params.model_name == "o1-preview":
            generation_params = copy.deepcopy(generation_params)
            generation_params.logit_bias = {}

        return super()._convert_conversation_to_api_input(
            conversation=conversation,
            generation_params=generation_params,