class_docstr: Enables text generation using OpenAI's large language models (LLMs). It supports gpt-4 and gpt-3.5-turbo
  family of models.
  
  Users can pass any text generation parameters valid for the `openai.ChatCompletion.create` method
  directly to this component via the `**generation_kwargs` parameter in __init__ or the `**generation_kwargs`
  parameter in `run` method.
  
  For more details on the parameters supported by the OpenAI API, refer to the OpenAI
  [documentation](https://platform.openai.com/docs/api-reference/chat).
  
  ```python
  from haystack.components.generators import OpenAIGenerator
  client = OpenAIGenerator()
  response = client.run("What's Natural Language Processing? Be brief.")
  print(response)
  
  >> {'replies': ['Natural Language Processing (NLP) is a branch of artificial intelligence that focuses on
  >> the interaction between computers and human language. It involves enabling computers to understand, interpret,
  >> and respond to natural human language in a way that is both meaningful and useful.'], 'meta': [{'model':
  >> 'gpt-3.5-turbo-0613', 'index': 0, 'finish_reason': 'stop', 'usage': {'prompt_tokens': 16,
  >> 'completion_tokens': 49, 'total_tokens': 65}}]}
  ```
  
   Key Features and Compatibility:
       - **Primary Compatibility**: Designed to work seamlessly with gpt-4, gpt-3.5-turbo family of models.
       - **Streaming Support**: Supports streaming responses from the OpenAI API.
       - **Customizability**: Supports all parameters supported by the OpenAI API.
  
   Input and Output Format:
       - **String Format**: This component uses the strings for both input and output.
docstr: Converts the list of ChatMessage to the list of messages in the format expected by the OpenAI API.
  :param messages: The list of ChatMessage.
  :return: The list of messages in the format expected by the OpenAI API.
docstr_tok: ["Converts","the","list","of","ChatMessage","to","the","list","of","messages","in","the","format","expected","by","the","OpenAI","API",".",":","param","messages",":","The","list","of","ChatMessage",".",":","return",":","The","list","of","messages","in","the","format","expected","by","the","OpenAI","API","."]
code: def _convert_to_openai_format(
      self, messages: List[ChatMessage]
  ) -> List[Dict[str, Any]]:
      """
      Converts the list of ChatMessage to the list of messages in the format expected by the OpenAI API.
      :param messages: The list of ChatMessage.
      :return: The list of messages in the format expected by the OpenAI API.
      """
      openai_chat_message_format = {"role", "content", "name"}
      openai_formatted_messages = []
      for m in messages:
          message_dict = dataclasses.asdict(m)
          filtered_message = {
              k: v
              for k, v in message_dict.items()
              if k in openai_chat_message_format and v
          }
          openai_formatted_messages.append(filtered_message)
      return openai_formatted_messages
  
imports: import dataclasses
  import logging
  import warnings
  from typing import Optional, List, Callable, Dict, Any, Union
  from openai import OpenAI, Stream
  from openai.types.chat import ChatCompletionChunk, ChatCompletion
  from haystack import component, default_from_dict, default_to_dict
  from haystack.components.generators.utils import serialize_callback_handler, deserialize_callback_handler
  from haystack.dataclasses import StreamingChunk, ChatMessage
label_desc: Use image node_id 6 for calling the OpenAIGenerator obj's underlying member method code with example usage: obj._convert_to_openai_format(messages) and returns: openai_formatted_messages