class_docstr: Enables text generation using OpenAI's large language models (LLMs). It supports gpt-4 and gpt-3.5-turbo
  family of models accessed through the chat completions API endpoint.
  
  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.chat import OpenAIChatGenerator
  from haystack.dataclasses import ChatMessage
  
  messages = [ChatMessage.from_user("What's Natural Language Processing?")]
  
  client = OpenAIChatGenerator()
  response = client.run(messages)
  print(response)
  
  >>{'replies': [ChatMessage(content='Natural Language Processing (NLP) is a branch of artificial intelligence
  >>that focuses on enabling computers to understand, interpret, and generate human language in a way that is
  >>meaningful and useful.', role=<ChatRole.ASSISTANT: 'assistant'>, name=None,
  >>meta={'model': 'gpt-3.5-turbo-0613', 'index': 0, 'finish_reason': 'stop',
  >>'usage': {'prompt_tokens': 15, 'completion_tokens': 36, 'total_tokens': 51}})]}
  
  ```
  
   Key Features and Compatibility:
       - **Primary Compatibility**: Designed to work seamlessly with the OpenAI API Chat Completion endpoint
          and gpt-4 and gpt-3.5-turbo family of models.
       - **Streaming Support**: Supports streaming responses from the OpenAI API Chat Completion endpoint.
       - **Customizability**: Supports all parameters supported by the OpenAI API Chat Completion endpoint.
  
   Input and Output Format:
       - **ChatMessage Format**: This component uses the ChatMessage format for structuring both input and output,
         ensuring coherent and contextually relevant responses in chat-based text generation scenarios. Details on the
         ChatMessage format can be found at: https://github.com/openai/openai-python/blob/main/chatml.md.
class_docstr_tok: ["Enables","text","generation","using","OpenAI","'s","large","language","models","(","LLMs",")",".","It","supports","gpt-4","and","gpt-3.5-turbo","family","of","models","accessed","through","the","chat","completions","API","endpoint",".","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.chat","import","OpenAIChatGenerator","from","haystack.dataclasses","import","ChatMessage","messages","=","[","ChatMessage.from_user","(","``","What","'s","Natural","Language","Processing","?","''",")","]","client","=","OpenAIChatGenerator","(",")","response","=","client.run","(","messages",")","print","(","response",")",">",">","{","'replies","'",":","[","ChatMessage","(","content='Natural","Language","Processing","(","NLP",")","is","a","branch","of","artificial","intelligence",">",">","that","focuses","on","enabling","computers","to","understand",",","interpret",",","and","generate","human","language","in","a","way","that","is",">",">","meaningful","and","useful",".","'",",","role=","<","ChatRole.ASSISTANT",":","'assistant","'",">",",","name=None",",",">",">","meta=","{","'model","'",":","'gpt-3.5-turbo-0613","'",",","'index","'",":","0",",","'finish_reason","'",":","'stop","'",",",">",">","'usage","'",":","{","'prompt_tokens","'",":","15",",","'completion_tokens","'",":","36",",","'total_tokens","'",":","51","}","}",")","]","}","``","`","Key","Features","and","Compatibility",":","-","*","*","Primary","Compatibility","*","*",":","Designed","to","work","seamlessly","with","the","OpenAI","API","Chat","Completion","endpoint","and","gpt-4","and","gpt-3.5-turbo","family","of","models",".","-","*","*","Streaming","Support","*","*",":","Supports","streaming","responses","from","the","OpenAI","API","Chat","Completion","endpoint",".","-","*","*","Customizability","*","*",":","Supports","all","parameters","supported","by","the","OpenAI","API","Chat","Completion","endpoint",".","Input","and","Output","Format",":","-","*","*","ChatMessage","Format","*","*",":","This","component","uses","the","ChatMessage","format","for","structuring","both","input","and","output",",","ensuring","coherent","and","contextually","relevant","responses","in","chat-based","text","generation","scenarios",".","Details","on","the","ChatMessage","format","can","be","found","at",":","https",":","\/\/github.com\/openai\/openai-python\/blob\/main\/chatml.md","."]
docstr: Converts the non-streaming response from the OpenAI API to a ChatMessage.
  :param completion: The completion returned by the OpenAI API.
  :param choice: The choice returned by the OpenAI API.
  :return: The ChatMessage.
docstr_tok: ["Converts","the","non-streaming","response","from","the","OpenAI","API","to","a","ChatMessage",".",":","param","completion",":","The","completion","returned","by","the","OpenAI","API",".",":","param","choice",":","The","choice","returned","by","the","OpenAI","API",".",":","return",":","The","ChatMessage","."]
code: def _build_message(
      self, completion: ChatCompletion, choice: Choice
  ) -> ChatMessage:
      """
      Converts the non-streaming response from the OpenAI API to a ChatMessage.
      :param completion: The completion returned by the OpenAI API.
      :param choice: The choice returned by the OpenAI API.
      :return: The ChatMessage.
      """
      message: ChatCompletionMessage = choice.message
      content = message.content or ""
      if message.function_call:
          # here we mimic the tools format response so that if user passes deprecated `functions` parameter
          # she'll get the same output as if new `tools` parameter was passed
          # use pydantic model dump to serialize the function call
          content = json.dumps(
              [
                  {
                      "function": message.function_call.model_dump(),
                      "type": "function",
                      "id": completion.id,
                  }
              ]
          )
      elif message.tool_calls:
          # new `tools` parameter was passed, use pydantic model dump to serialize the tool calls
          content = json.dumps(
              [tc.model_dump() for tc in message.tool_calls]
          )
  
      chat_message = ChatMessage.from_assistant(content)
      chat_message.meta.update(
          {
              "model": completion.model,
              "index": choice.index,
              "finish_reason": choice.finish_reason,
              "usage": dict(completion.usage or {}),
          }
      )
      return chat_message
  
imports: import copy
  import dataclasses
  import json
  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, ChatCompletionMessage
  from openai.types.chat.chat_completion import Choice
  from openai.types.chat.chat_completion_chunk import Choice
  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 8 for calling the OpenAIChatGenerator obj's underlying member method code with example usage: obj._build_message(completion, choice) and returns: chat_message