class_docstr: Log OpenAI fine-tunes to [Weights & Biases](https://wandb.me/openai-docs).
class_docstr_tok: ["Log","OpenAI","fine-tunes","to","[","Weights","&","Biases","]","(","https",":","\/\/wandb.me\/openai-docs",")","."]
docstr: Sync fine-tunes to Weights & Biases.
  
  :param fine_tune_job_id: The id of the fine-tune (optional)
  :param openai_client: Pass the `OpenAI()` client (optional)
  :param num_fine_tunes: Number of most recent fine-tunes to log when an fine_tune_job_id is not provided. By default, every fine-tune is synced.
  :param project: Name of the project where you're sending runs. By default, it is "GPT-3".
  :param entity: Username or team name where you're sending runs. By default, your default entity is used, which is usually your username.
  :param overwrite: Forces logging and overwrite existing wandb run of the same fine-tune.
  :param wait_for_job_success: Waits for the fine-tune to be complete and then log metrics to W&B. By default, it is True.
docstr_tok: ["Sync","fine-tunes","to","Weights","&","Biases",".",":","param","fine_tune_job_id",":","The","id","of","the","fine-tune","(","optional",")",":","param","openai_client",":","Pass","the","`","OpenAI","(",")","`","client","(","optional",")",":","param","num_fine_tunes",":","Number","of","most","recent","fine-tunes","to","log","when","an","fine_tune_job_id","is","not","provided",".","By","default",",","every","fine-tune","is","synced",".",":","param","project",":","Name","of","the","project","where","you","'re","sending","runs",".","By","default",",","it","is","``","GPT-3","''",".",":","param","entity",":","Username","or","team","name","where","you","'re","sending","runs",".","By","default",",","your","default","entity","is","used",",","which","is","usually","your","username",".",":","param","overwrite",":","Forces","logging","and","overwrite","existing","wandb","run","of","the","same","fine-tune",".",":","param","wait_for_job_success",":","Waits","for","the","fine-tune","to","be","complete","and","then","log","metrics","to","W","&","B",".","By","default",",","it","is","True","."]
code: def sync(
      cls,
      fine_tune_job_id: Optional[str] = None,
      openai_client: Optional[OpenAI] = None,
      num_fine_tunes: Optional[int] = None,
      project: str = "OpenAI-Fine-Tune",
      entity: Optional[str] = None,
      overwrite: bool = False,
      wait_for_job_success: bool = True,
      **kwargs_wandb_init: Dict[str, Any],
  ) -> str:
      """Sync fine-tunes to Weights & Biases.
  
      :param fine_tune_job_id: The id of the fine-tune (optional)
      :param openai_client: Pass the `OpenAI()` client (optional)
      :param num_fine_tunes: Number of most recent fine-tunes to log when an fine_tune_job_id is not provided. By default, every fine-tune is synced.
      :param project: Name of the project where you're sending runs. By default, it is "GPT-3".
      :param entity: Username or team name where you're sending runs. By default, your default entity is used, which is usually your username.
      :param overwrite: Forces logging and overwrite existing wandb run of the same fine-tune.
      :param wait_for_job_success: Waits for the fine-tune to be complete and then log metrics to W&B. By default, it is True.
      """
      if openai_client is None:
          openai_client = OpenAI()
      cls.openai_client = openai_client
  
      if fine_tune_job_id:
          wandb.termlog("Retrieving fine-tune job...")
          fine_tune = openai_client.fine_tuning.jobs.retrieve(
              fine_tuning_job_id=fine_tune_job_id
          )
          fine_tunes = [fine_tune]
      else:
          # get list of fine_tune to log
          fine_tunes = openai_client.fine_tuning.jobs.list()
          if not fine_tunes or fine_tunes.data is None:
              wandb.termwarn("No fine-tune has been retrieved")
              return
          # Select the `num_fine_tunes` from the `fine_tunes.data` list.
          # If `num_fine_tunes` is None, it selects all items in the list (from start to end).
          # If for example, `num_fine_tunes` is 5, it selects the last 5 items in the list.
          # Note that the last items in the list are the latest fine-tune jobs.
          fine_tunes = fine_tunes.data[
              -num_fine_tunes if num_fine_tunes is not None else None :
          ]
  
      # log starting from oldest fine_tune
      show_individual_warnings = (
          fine_tune_job_id is not None or num_fine_tunes is not None
      )
      fine_tune_logged = []
      for fine_tune in fine_tunes:
          fine_tune_id = fine_tune.id
          # check run with the given `fine_tune_id` has not been logged already
          run_path = f"{project}/{fine_tune_id}"
          if entity is not None:
              run_path = f"{entity}/{run_path}"
          wandb_run = cls._get_wandb_run(run_path)
          if wandb_run:
              wandb_status = wandb_run.summary.get("status")
              if show_individual_warnings:
                  if wandb_status == "succeeded" and not overwrite:
                      wandb.termwarn(
                          f"Fine-tune {fine_tune_id} has already been logged successfully at {wandb_run.url}. "
                          "Use `overwrite=True` if you want to overwrite previous run"
                      )
                  elif wandb_status != "succeeded" or overwrite:
                      if wandb_status != "succeeded":
                          wandb.termwarn(
                              f"A run for fine-tune {fine_tune_id} was previously created but didn't end successfully"
                          )
                      wandb.termlog(
                          f"A new wandb run will be created for fine-tune {fine_tune_id} and previous run will be overwritten"
                      )
                      overwrite = True
              if wandb_status == "succeeded" and not overwrite:
                  return
  
          # check if the user has not created a wandb run externally
          if wandb.run is None:
              cls._run = wandb.init(
                  job_type="fine-tune",
                  project=project,
                  entity=entity,
                  name=fine_tune_id,
                  id=fine_tune_id,
                  **kwargs_wandb_init,
              )
          else:
              # if a run exits - created externally
              cls._run = wandb.run
  
          if wait_for_job_success:
              fine_tune = cls._wait_for_job_success(fine_tune)
  
          cls._log_fine_tune(
              fine_tune,
              project,
              entity,
              overwrite,
              show_individual_warnings,
              **kwargs_wandb_init,
          )
  
      if not show_individual_warnings and not any(fine_tune_logged):
          wandb.termwarn("No new successful fine-tunes were found")
  
      return "🎉 wandb sync completed successfully"
  
imports: import datetime
  import io
  import json
  import re
  import time
  from typing import Any, Dict, Optional, Tuple
  from pkg_resources import parse_version
  import wandb
  from wandb import util
  from wandb.data_types import Table
  from wandb.sdk.lib import telemetry
  from wandb.sdk.wandb_run import Run
  from openai import OpenAI
  from openai.types.fine_tuning import FineTuningJob
  from openai.types.fine_tuning.fine_tuning_job import Hyperparameters
label_desc: Use image node_id 1 for calling the WandbLogger obj's underlying member method code with example usage: obj.sync(cls, fine_tune_job_id, openai_client, num_fine_tunes, project, entity, overwrite, wait_for_job_success) and returns: str