code: def save_model(
      model,
      task,
      path,
      conda_env=None,
      code_paths=None,
      mlflow_model=None,
      signature: ModelSignature = None,
      input_example: ModelInputExample = None,
      pip_requirements=None,
      extra_pip_requirements=None,
      metadata=None,
      example_no_conversion=False,
      **kwargs,
  ):
      """
      Save an OpenAI model to a path on the local file system.
  
      Args:
          model: The OpenAI model name or reference instance, e.g.,
              ``openai.Model.retrieve("gpt-3.5-turbo")``.
          task: The task the model is performing, e.g., ``openai.ChatCompletion`` or
              ``'chat.completions'``.
          path: Local path where the model is to be saved.
          conda_env: {{ conda_env }}
          code_paths: A list of local filesystem paths to Python file dependencies (or directories
              containing file dependencies). These files are *prepended* to the system
              path when the model is loaded.
          mlflow_model: :py:mod:`mlflow.models.Model` this flavor is being added to.
          signature: :py:class:`ModelSignature <mlflow.models.ModelSignature>`
              describes model input and output :py:class:`Schema <mlflow.types.Schema>`.
              The model signature can be :py:func:`inferred <mlflow.models.infer_signature>`
              from datasets with valid model input (e.g. the training dataset with target
              column omitted) and valid model output (e.g. model predictions generated on
              the training dataset), for example:
  
              .. code-block:: python
  
                  from mlflow.models import infer_signature
  
                  train = df.drop_column("target_label")
                  predictions = ...  # compute model predictions
                  signature = infer_signature(train, predictions)
          input_example: {{ input_example }}
          pip_requirements: {{ pip_requirements }}
          extra_pip_requirements: {{ extra_pip_requirements }}
          metadata: Custom metadata dictionary passed to the model and stored in the MLmodel file.
  
              .. Note:: Experimental: This parameter may change or be removed in a future
                                      release without warning.
          example_no_conversion: {{ example_no_conversion }}
          kwargs: Keyword arguments specific to the OpenAI task, such as the ``messages`` (see
              :ref:`mlflow.openai.messages` for more details on this parameter)
              or ``top_p`` value to use for chat completion.
  
      .. code-block:: python
  
          import mlflow
          import openai
  
          # Chat
          mlflow.openai.save_model(
              model="gpt-3.5-turbo",
              task=openai.ChatCompletion,
              messages=[{"role": "user", "content": "Tell me a joke."}],
              path="model",
          )
  
          # Completions
          mlflow.openai.save_model(
              model="text-davinci-002",
              task=openai.Completion,
              prompt="{text}. The general sentiment of the text is",
              path="model",
          )
  
          # Embeddings
          mlflow.openai.save_model(
              model="text-embedding-ada-002",
              task=openai.Embedding,
              path="model",
          )
      """
      import numpy as np
  
      _validate_env_arguments(
          conda_env, pip_requirements, extra_pip_requirements
      )
      path = os.path.abspath(path)
      _validate_and_prepare_target_save_path(path)
      code_dir_subpath = _validate_and_copy_code_paths(code_paths, path)
      task = _get_task_name(task)
  
      if mlflow_model is None:
          mlflow_model = Model()
  
      if signature is not None:
          if signature.params:
              _validate_model_params(
                  task,
                  kwargs,
                  {p.name: p.default for p in signature.params.params},
              )
          mlflow_model.signature = signature
      elif task == "chat.completions":
          messages = kwargs.get("messages", [])
          if messages and not (
              all(isinstance(m, dict) for m in messages)
              and all(map(_is_valid_message, messages))
          ):
              raise mlflow.MlflowException.invalid_parameter_value(
                  "If `messages` is provided, it must be a list of dictionaries with keys "
                  "'role' and 'content'."
              )
  
          mlflow_model.signature = ModelSignature(
              inputs=_get_input_schema(task, messages),
              outputs=Schema([ColSpec(type="string", name=None)]),
          )
      elif task == "completions":
          prompt = kwargs.get("prompt")
          mlflow_model.signature = ModelSignature(
              inputs=_get_input_schema(task, prompt),
              outputs=Schema([ColSpec(type="string", name=None)]),
          )
      elif task == "embeddings":
          mlflow_model.signature = ModelSignature(
              inputs=Schema([ColSpec(type="string", name=None)]),
              outputs=Schema(
                  [TensorSpec(type=np.dtype("float64"), shape=(-1,))]
              ),
          )
  
      if input_example is not None:
          _save_example(
              mlflow_model, input_example, path, example_no_conversion
          )
      if metadata is not None:
          mlflow_model.metadata = metadata
      model_data_path = os.path.join(path, MODEL_FILENAME)
      model_dict = {
          "model": _get_model_name(model),
          "task": task,
          **kwargs,
      }
      with open(model_data_path, "w") as f:
          yaml.safe_dump(model_dict, f)
  
      if task in _PYFUNC_SUPPORTED_TASKS:
          pyfunc.add_to_model(
              mlflow_model,
              loader_module="mlflow.openai",
              data=MODEL_FILENAME,
              conda_env=_CONDA_ENV_FILE_NAME,
              python_env=_PYTHON_ENV_FILE_NAME,
              code=code_dir_subpath,
          )
      mlflow_model.add_flavor(
          FLAVOR_NAME,
          openai_version=_get_openai_package_version(),
          data=MODEL_FILENAME,
          code=code_dir_subpath,
      )
      mlflow_model.save(os.path.join(path, MLMODEL_FILE_NAME))
  
      if is_in_databricks_runtime():
          if scope := MLFLOW_OPENAI_SECRET_SCOPE.get():
              url = "https://docs.databricks.com/en/machine-learning/model-serving/store-env-variable-model-serving.html"
              warnings.warn(
                  "Specifying secrets for model serving with `MLFLOW_OPENAI_SECRET_SCOPE` is "
                  f"deprecated. Use secrets-based environment variables ({url}) instead.",
                  FutureWarning,
              )
              check_databricks_secret_scope_access(scope)
              _log_secrets_yaml(path, scope)
  
      if conda_env is None:
          if pip_requirements is None:
              default_reqs = get_default_pip_requirements()
              inferred_reqs = mlflow.models.infer_pip_requirements(
                  path, FLAVOR_NAME, fallback=default_reqs
              )
              default_reqs = sorted(
                  set(inferred_reqs).union(default_reqs)
              )
          else:
              default_reqs = None
          (
              conda_env,
              pip_requirements,
              pip_constraints,
          ) = _process_pip_requirements(
              default_reqs,
              pip_requirements,
              extra_pip_requirements,
          )
      else:
          (
              conda_env,
              pip_requirements,
              pip_constraints,
          ) = _process_conda_env(conda_env)
  
      with open(os.path.join(path, _CONDA_ENV_FILE_NAME), "w") as f:
          yaml.safe_dump(conda_env, stream=f, default_flow_style=False)
  
      # Save `constraints.txt` if necessary
      if pip_constraints:
          write_to(
              os.path.join(path, _CONSTRAINTS_FILE_NAME),
              "\n".join(pip_constraints),
          )
  
      # Save `requirements.txt`
      write_to(
          os.path.join(path, _REQUIREMENTS_FILE_NAME),
          "\n".join(pip_requirements),
      )
  
      _PythonEnv.current().to_yaml(
          os.path.join(path, _PYTHON_ENV_FILE_NAME)
      )
  
imports: import itertools
  import logging
  import os
  import warnings
  from string import Formatter
  from typing import Any, Dict, Optional, Set
  import yaml
  import mlflow
  from mlflow import pyfunc
  from mlflow.environment_variables import _MLFLOW_TESTING, MLFLOW_OPENAI_SECRET_SCOPE
  from mlflow.exceptions import MlflowException
  from mlflow.models import Model, ModelInputExample, ModelSignature
  from mlflow.models.model import MLMODEL_FILE_NAME
  from mlflow.models.utils import _save_example
  from mlflow.protos.databricks_pb2 import INVALID_PARAMETER_VALUE
  from mlflow.tracking._model_registry import DEFAULT_AWAIT_MAX_SLEEP_SECONDS
  from mlflow.tracking.artifact_utils import _download_artifact_from_uri
  from mlflow.types import ColSpec, Schema, TensorSpec
  from mlflow.utils.annotations import experimental
  from mlflow.utils.databricks_utils import check_databricks_secret_scope_access, is_in_databricks_runtime
  from mlflow.utils.docstring_utils import LOG_MODEL_PARAM_DOCS, format_docstring
  from mlflow.utils.environment import _CONDA_ENV_FILE_NAME, _CONSTRAINTS_FILE_NAME, _PYTHON_ENV_FILE_NAME, _REQUIREMENTS_FILE_NAME, _mlflow_conda_env, _process_conda_env, _process_pip_requirements, _PythonEnv, _validate_env_arguments
  from mlflow.utils.file_utils import write_to
  from mlflow.utils.model_utils import _add_code_from_conf_to_system_path, _get_flavor_configuration, _validate_and_copy_code_paths, _validate_and_prepare_target_save_path
  from mlflow.utils.openai_utils import REQUEST_URL_CHAT, REQUEST_URL_COMPLETIONS, REQUEST_URL_EMBEDDINGS, _OAITokenHolder, _OpenAIApiConfig, _OpenAIEnvVar, _validate_model_params
  from mlflow.utils.requirements_utils import _get_pinned_requirement
label_desc: Use image node_id 12 for calling a global function with example usage: save_model(model, task, path, conda_env, code_paths, mlflow_model, signature, input_example, pip_requirements, extra_pip_requirements, metadata, example_no_conversion) without return types