class_docstr: A wrapper class for openai client.
docstr: Create a completion for a given config using openai's client.
  
  Args:
      client: The openai client.
      params: The params for the completion.
  
  Returns:
      The completion.
docstr_tok: ["Create","a","completion","for","a","given","config","using","openai","'s","client",".","Args",":","client",":","The","openai","client",".","params",":","The","params","for","the","completion",".","Returns",":","The","completion","."]
code: def _completions_create(
      self, client: OpenAI, params: Dict[str, Any]
  ) -> ChatCompletion:
      """Create a completion for a given config using openai's client.
  
      Args:
          client: The openai client.
          params: The params for the completion.
  
      Returns:
          The completion.
      """
      completions: Completions = client.chat.completions if "messages" in params else client.completions  # type: ignore [attr-defined]
      # If streaming is enabled and has messages, then iterate over the chunks of the response.
      if params.get("stream", False) and "messages" in params:
          response_contents = [""] * params.get("n", 1)
          finish_reasons = [""] * params.get("n", 1)
          completion_tokens = 0
  
          # Set the terminal text color to green
          print("\033[32m", end="")
  
          # Prepare for potential function call
          full_function_call: Optional[Dict[str, Any]] = None
          full_tool_calls: Optional[
              List[Optional[Dict[str, Any]]]
          ] = None
  
          # Send the chat completion request to OpenAI's API and process the response in chunks
          for chunk in completions.create(**params):
              if chunk.choices:
                  for choice in chunk.choices:
                      content = choice.delta.content
                      tool_calls_chunks = choice.delta.tool_calls
                      finish_reasons[
                          choice.index
                      ] = choice.finish_reason
  
                      # todo: remove this after function calls are removed from the API
                      # the code should work regardless of whether function calls are removed or not, but test_chat_functions_stream should fail
                      # begin block
                      function_call_chunk = (
                          choice.delta.function_call
                          if hasattr(choice.delta, "function_call")
                          else None
                      )
                      # Handle function call
                      if function_call_chunk:
                          # Handle function call
                          if function_call_chunk:
                              (
                                  full_function_call,
                                  completion_tokens,
                              ) = self._update_function_call_from_chunk(
                                  function_call_chunk,
                                  full_function_call,
                                  completion_tokens,
                              )
                          if not content:
                              continue
                      # end block
  
                      # Handle tool calls
                      if tool_calls_chunks:
                          for tool_calls_chunk in tool_calls_chunks:
                              # the current tool call to be reconstructed
                              ix = tool_calls_chunk.index
                              if full_tool_calls is None:
                                  full_tool_calls = []
                              if ix >= len(full_tool_calls):
                                  # in case ix is not sequential
                                  full_tool_calls = full_tool_calls + [
                                      None
                                  ] * (ix - len(full_tool_calls) + 1)
  
                              (
                                  full_tool_calls[ix],
                                  completion_tokens,
                              ) = self._update_tool_calls_from_chunk(
                                  tool_calls_chunk,
                                  full_tool_calls[ix],
                                  completion_tokens,
                              )
                              if not content:
                                  continue
  
                      # End handle tool calls
  
                      # If content is present, print it to the terminal and update response variables
                      if content is not None:
                          print(content, end="", flush=True)
                          response_contents[choice.index] += content
                          completion_tokens += 1
                      else:
                          # print()
                          pass
  
          # Reset the terminal text color
          print("\033[0m\n")
  
          # Prepare the final ChatCompletion object based on the accumulated data
          model = chunk.model.replace(
              "gpt-35", "gpt-3.5"
          )  # hack for Azure API
          prompt_tokens = count_token(params["messages"], model)
          response = ChatCompletion(
              id=chunk.id,
              model=chunk.model,
              created=chunk.created,
              object="chat.completion",
              choices=[],
              usage=CompletionUsage(
                  prompt_tokens=prompt_tokens,
                  completion_tokens=completion_tokens,
                  total_tokens=prompt_tokens + completion_tokens,
              ),
          )
          for i in range(len(response_contents)):
              if OPENAIVERSION >= "1.5":  # pragma: no cover
                  # OpenAI versions 1.5.0 and above
                  choice = Choice(
                      index=i,
                      finish_reason=finish_reasons[i],
                      message=ChatCompletionMessage(
                          role="assistant",
                          content=response_contents[i],
                          function_call=full_function_call,
                          tool_calls=full_tool_calls,
                      ),
                      logprobs=None,
                  )
              else:
                  # OpenAI versions below 1.5.0
                  choice = Choice(  # type: ignore [call-arg]
                      index=i,
                      finish_reason=finish_reasons[i],
                      message=ChatCompletionMessage(
                          role="assistant",
                          content=response_contents[i],
                          function_call=full_function_call,
                          tool_calls=full_tool_calls,
                      ),
                  )
  
              response.choices.append(choice)
      else:
          # If streaming is not enabled, send a regular chat completion request
          params = params.copy()
          params["stream"] = False
          response = completions.create(**params)
  
      return response
  
imports: from __future__ import annotations
  import os
  import sys
  from typing import Any, List, Optional, Dict, Callable, Tuple, Union
  import logging
  import inspect
  from flaml.automl.logger import logger_formatter
  from pydantic import BaseModel
  from autogen.oai import completion
  from autogen.oai.openai_utils import DEFAULT_AZURE_API_VERSION, get_key, OAI_PRICE1K
  from autogen.token_count_utils import count_token
  from autogen._pydantic import model_dump
label_desc: Use image node_id 11 for calling the OpenAIWrapper obj's underlying member method code with example usage: obj._completions_create(client, params) and returns: response