class_docstr: (openai<1) A class for OpenAI completion API.
  
  It also supports: ChatCompletion, Azure OpenAI API.
docstr: Make a completion for a given context.
  
  Args:
      context (Dict, Optional): The context to instantiate the prompt.
          It needs to contain keys that are used by the prompt template or the filter function.
          E.g., `prompt="Complete the following sentence: {prefix}, context={"prefix": "Today I feel"}`.
          The actual prompt will be:
          "Complete the following sentence: Today I feel".
          More examples can be found at [templating](https://microsoft.github.io/autogen/docs/Use-Cases/enhanced_inference#templating).
      use_cache (bool, Optional): Whether to use cached responses.
      config_list (List, Optional): List of configurations for the completion to try.
          The first one that does not raise an error will be used.
          Only the differences from the default config need to be provided.
          E.g.,
  
  ```python
  response = oai.Completion.create(
      config_list=[
          {
              "model": "gpt-4",
              "api_key": os.environ.get("AZURE_OPENAI_API_KEY"),
              "api_type": "azure",
              "base_url": os.environ.get("AZURE_OPENAI_API_BASE"),
              "api_version": "2023-03-15-preview",
          },
          {
              "model": "gpt-3.5-turbo",
              "api_key": os.environ.get("OPENAI_API_KEY"),
              "api_type": "open_ai",
              "base_url": "https://api.openai.com/v1",
          },
          {
              "model": "llama-7B",
              "base_url": "http://127.0.0.1:8080",
              "api_type": "open_ai",
          }
      ],
      prompt="Hi",
  )
  ```
  
      filter_func (Callable, Optional): A function that takes in the context and the response and returns a boolean to indicate whether the response is valid. E.g.,
  
  ```python
  def yes_or_no_filter(context, config, response):
      return context.get("yes_or_no_choice", False) is False or any(
          text in ["Yes.", "No."] for text in oai.Completion.extract_text(response)
      )
  ```
  
      raise_on_ratelimit_or_timeout (bool, Optional): Whether to raise RateLimitError or Timeout when all configs fail.
          When set to False, -1 will be returned when all configs fail.
      allow_format_str_template (bool, Optional): Whether to allow format string template in the config.
      **config: Configuration for the openai API call. This is used as parameters for calling openai API.
          The "prompt" or "messages" parameter can contain a template (str or Callable) which will be instantiated with the context.
          Besides the parameters for the openai API call, it can also contain:
          - `max_retry_period` (int): the total time (in seconds) allowed for retrying failed requests.
          - `retry_wait_time` (int): the time interval to wait (in seconds) before retrying a failed request.
          - `cache_seed` (int) for the cache. This is useful when implementing "controlled randomness" for the completion.
  
  Returns:
      Responses from OpenAI API, with additional fields.
          - `cost`: the total cost.
      When `config_list` is provided, the response will contain a few more fields:
          - `config_id`: the index of the config in the config_list that is used to generate the response.
          - `pass_filter`: whether the response passes the filter function. None if no filter is provided.
code: def create(
      cls,
      context: Optional[Dict] = None,
      use_cache: Optional[bool] = True,
      config_list: Optional[List[Dict]] = None,
      filter_func: Optional[Callable[[Dict, Dict], bool]] = None,
      raise_on_ratelimit_or_timeout: Optional[bool] = True,
      allow_format_str_template: Optional[bool] = False,
      **config,
  ):
      """Make a completion for a given context.
  
      Args:
          context (Dict, Optional): The context to instantiate the prompt.
              It needs to contain keys that are used by the prompt template or the filter function.
              E.g., `prompt="Complete the following sentence: {prefix}, context={"prefix": "Today I feel"}`.
              The actual prompt will be:
              "Complete the following sentence: Today I feel".
              More examples can be found at [templating](https://microsoft.github.io/autogen/docs/Use-Cases/enhanced_inference#templating).
          use_cache (bool, Optional): Whether to use cached responses.
          config_list (List, Optional): List of configurations for the completion to try.
              The first one that does not raise an error will be used.
              Only the differences from the default config need to be provided.
              E.g.,
  
      ```python
      response = oai.Completion.create(
          config_list=[
              {
                  "model": "gpt-4",
                  "api_key": os.environ.get("AZURE_OPENAI_API_KEY"),
                  "api_type": "azure",
                  "base_url": os.environ.get("AZURE_OPENAI_API_BASE"),
                  "api_version": "2023-03-15-preview",
              },
              {
                  "model": "gpt-3.5-turbo",
                  "api_key": os.environ.get("OPENAI_API_KEY"),
                  "api_type": "open_ai",
                  "base_url": "https://api.openai.com/v1",
              },
              {
                  "model": "llama-7B",
                  "base_url": "http://127.0.0.1:8080",
                  "api_type": "open_ai",
              }
          ],
          prompt="Hi",
      )
      ```
  
          filter_func (Callable, Optional): A function that takes in the context and the response and returns a boolean to indicate whether the response is valid. E.g.,
  
      ```python
      def yes_or_no_filter(context, config, response):
          return context.get("yes_or_no_choice", False) is False or any(
              text in ["Yes.", "No."] for text in oai.Completion.extract_text(response)
          )
      ```
  
          raise_on_ratelimit_or_timeout (bool, Optional): Whether to raise RateLimitError or Timeout when all configs fail.
              When set to False, -1 will be returned when all configs fail.
          allow_format_str_template (bool, Optional): Whether to allow format string template in the config.
          **config: Configuration for the openai API call. This is used as parameters for calling openai API.
              The "prompt" or "messages" parameter can contain a template (str or Callable) which will be instantiated with the context.
              Besides the parameters for the openai API call, it can also contain:
              - `max_retry_period` (int): the total time (in seconds) allowed for retrying failed requests.
              - `retry_wait_time` (int): the time interval to wait (in seconds) before retrying a failed request.
              - `cache_seed` (int) for the cache. This is useful when implementing "controlled randomness" for the completion.
  
      Returns:
          Responses from OpenAI API, with additional fields.
              - `cost`: the total cost.
          When `config_list` is provided, the response will contain a few more fields:
              - `config_id`: the index of the config in the config_list that is used to generate the response.
              - `pass_filter`: whether the response passes the filter function. None if no filter is provided.
      """
      logger.warning(
          "Completion.create is deprecated in pyautogen v0.2 and openai>=1. "
          "The new openai requires initiating a client for inference. "
          "Please refer to https://microsoft.github.io/autogen/docs/Use-Cases/enhanced_inference#api-unification"
      )
      if ERROR:
          raise ERROR
  
      # Warn if a config list was provided but was empty
      if type(config_list) is list and len(config_list) == 0:
          logger.warning(
              "Completion was provided with a config_list, but the list was empty. Adopting default OpenAI behavior, which reads from the 'model' parameter instead."
          )
  
      if config_list:
          last = len(config_list) - 1
          cost = 0
          for i, each_config in enumerate(config_list):
              base_config = config.copy()
              base_config[
                  "allow_format_str_template"
              ] = allow_format_str_template
              base_config.update(each_config)
              if (
                  i < last
                  and filter_func is None
                  and "max_retry_period" not in base_config
              ):
                  # max_retry_period = 0 to avoid retrying when no filter is given
                  base_config["max_retry_period"] = 0
              try:
                  response = cls.create(
                      context,
                      use_cache,
                      raise_on_ratelimit_or_timeout=i < last
                      or raise_on_ratelimit_or_timeout,
                      **base_config,
                  )
                  if response == -1:
                      return response
                  pass_filter = filter_func is None or filter_func(
                      context=context, response=response
                  )
                  if pass_filter or i == last:
                      response["cost"] = cost + response["cost"]
                      response["config_id"] = i
                      response["pass_filter"] = pass_filter
                      return response
                  cost += response["cost"]
              except (
                  AuthenticationError,
                  RateLimitError,
                  Timeout,
                  BadRequestError,
              ):
                  logger.debug(f"failed with config {i}", exc_info=1)
                  if i == last:
                      raise
      params = cls._construct_params(
          context,
          config,
          allow_format_str_template=allow_format_str_template,
      )
      if not use_cache:
          return cls._get_response(
              params,
              raise_on_ratelimit_or_timeout=raise_on_ratelimit_or_timeout,
              use_cache=False,
          )
      cache_seed = cls.cache_seed
      if "cache_seed" in params:
          cls.set_cache(params.pop("cache_seed"))
      with diskcache.Cache(cls.cache_path) as cls._cache:
          cls.set_cache(cache_seed)
          return cls._get_response(
              params,
              raise_on_ratelimit_or_timeout=raise_on_ratelimit_or_timeout,
          )
  
imports: from time import sleep
  import logging
  import time
  from typing import List, Optional, Dict, Callable, Union
  import sys
  import shutil
  import numpy
  from flaml import tune, BlendSearch
  from flaml.tune.space import is_constant
  from flaml.automl.logger import logger_formatter
  from .openai_utils import get_key
  from collections import defaultdict
label_desc: Use image node_id 13 for calling the Completion obj's underlying member method code with example usage: obj.create(cls, context, use_cache, config_list, filter_func, raise_on_ratelimit_or_timeout, allow_format_str_template) and returns: cls, cls, response, response