- 
                Notifications
    
You must be signed in to change notification settings  - Fork 4.4k
 
Add CLI tools for chat fine-tuning #2659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            cnaples79
  wants to merge
  1
  commit into
  openai:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
cnaples79:add-chat-fine-tuning-cli-tools
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| from __future__ import annotations | ||
| 
     | 
||
| from typing import TYPE_CHECKING | ||
| from argparse import ArgumentParser | ||
| 
     | 
||
| from .chat_fine_tunes import jobs | ||
| 
     | 
||
| if TYPE_CHECKING: | ||
| from argparse import _SubParsersAction | ||
| 
     | 
||
| 
     | 
||
| def register(subparser: _SubParsersAction[ArgumentParser]) -> None: | ||
| jobs.register(subparser) | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # API commands for chat fine-tuning (convenience aliases) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| from __future__ import annotations | ||
| 
     | 
||
| import json | ||
| from typing import TYPE_CHECKING | ||
| from argparse import ArgumentParser | ||
| 
     | 
||
| from ..._utils import get_client, print_model | ||
| from ...._types import Omittable, omit | ||
| from ...._utils import is_given | ||
| from ..._models import BaseModel | ||
| from ....pagination import SyncCursorPage | ||
| from ....types.fine_tuning import ( | ||
| FineTuningJob, | ||
| FineTuningJobEvent, | ||
| ) | ||
| 
     | 
||
| if TYPE_CHECKING: | ||
| from argparse import _SubParsersAction | ||
| 
     | 
||
| 
     | 
||
| def register(subparser: _SubParsersAction[ArgumentParser]) -> None: | ||
| sub = subparser.add_parser("chat_fine_tunes.create") | ||
| sub.add_argument( | ||
| "-m", | ||
| "--model", | ||
| help="The model to fine-tune.", | ||
| required=True, | ||
| ) | ||
| sub.add_argument( | ||
| "-t", | ||
| "--training-file", | ||
| help="The training file to fine-tune the model on.", | ||
| required=True, | ||
| ) | ||
| sub.add_argument( | ||
| "-H", | ||
| "--hyperparameters", | ||
| help="JSON string of hyperparameters to use for fine-tuning.", | ||
| type=str, | ||
| ) | ||
| sub.add_argument( | ||
| "-s", | ||
| "--suffix", | ||
| help="A suffix to add to the fine-tuned model name.", | ||
| ) | ||
| sub.add_argument( | ||
| "-v", | ||
| "--validation-file", | ||
| help="The validation file to use for fine-tuning.", | ||
| ) | ||
| sub.set_defaults(func=CLIChatFineTunes.create, args_model=CLIChatFineTunesCreateArgs) | ||
| 
     | 
||
| sub = subparser.add_parser("chat_fine_tunes.get") | ||
| sub.add_argument( | ||
| "-i", | ||
| "--id", | ||
| help="The ID of the fine-tuning job to retrieve.", | ||
| required=True, | ||
| ) | ||
| sub.set_defaults(func=CLIChatFineTunes.retrieve, args_model=CLIChatFineTunesRetrieveArgs) | ||
| 
     | 
||
| sub = subparser.add_parser("chat_fine_tunes.list") | ||
| sub.add_argument( | ||
| "-a", | ||
| "--after", | ||
| help="Identifier for the last job from the previous pagination request. If provided, only jobs created after this job will be returned.", | ||
| ) | ||
| sub.add_argument( | ||
| "-l", | ||
| "--limit", | ||
| help="Number of fine-tuning jobs to retrieve.", | ||
| type=int, | ||
| ) | ||
| sub.set_defaults(func=CLIChatFineTunes.list, args_model=CLIChatFineTunesListArgs) | ||
| 
     | 
||
| sub = subparser.add_parser("chat_fine_tunes.cancel") | ||
| sub.add_argument( | ||
| "-i", | ||
| "--id", | ||
| help="The ID of the fine-tuning job to cancel.", | ||
| required=True, | ||
| ) | ||
| sub.set_defaults(func=CLIChatFineTunes.cancel, args_model=CLIChatFineTunesCancelArgs) | ||
| 
     | 
||
| 
     | 
||
| class CLIChatFineTunesCreateArgs(BaseModel): | ||
| model: str | ||
| training_file: str | ||
| hyperparameters: Omittable[str] = omit | ||
| suffix: Omittable[str] = omit | ||
| validation_file: Omittable[str] = omit | ||
| 
     | 
||
| 
     | 
||
| class CLIChatFineTunesRetrieveArgs(BaseModel): | ||
| id: str | ||
| 
     | 
||
| 
     | 
||
| class CLIChatFineTunesListArgs(BaseModel): | ||
| after: Omittable[str] = omit | ||
| limit: Omittable[int] = omit | ||
| 
     | 
||
| 
     | 
||
| class CLIChatFineTunesCancelArgs(BaseModel): | ||
| id: str | ||
| 
     | 
||
| 
     | 
||
| class CLIChatFineTunes: | ||
| @staticmethod | ||
| def create(args: CLIChatFineTunesCreateArgs) -> None: | ||
| hyperparameters = json.loads(str(args.hyperparameters)) if is_given(args.hyperparameters) else omit | ||
| fine_tuning_job: FineTuningJob = get_client().fine_tuning.jobs.create( | ||
| model=args.model, | ||
| training_file=args.training_file, | ||
| hyperparameters=hyperparameters, | ||
| suffix=args.suffix, | ||
| validation_file=args.validation_file, | ||
| ) | ||
| print_model(fine_tuning_job) | ||
| 
     | 
||
| @staticmethod | ||
| def retrieve(args: CLIChatFineTunesRetrieveArgs) -> None: | ||
| fine_tuning_job: FineTuningJob = get_client().fine_tuning.jobs.retrieve(fine_tuning_job_id=args.id) | ||
| print_model(fine_tuning_job) | ||
| 
     | 
||
| @staticmethod | ||
| def list(args: CLIChatFineTunesListArgs) -> None: | ||
| fine_tuning_jobs: SyncCursorPage[FineTuningJob] = get_client().fine_tuning.jobs.list( | ||
| after=args.after or omit, limit=args.limit or omit | ||
| ) | ||
| print_model(fine_tuning_jobs) | ||
| 
     | 
||
| @staticmethod | ||
| def cancel(args: CLIChatFineTunesCancelArgs) -> None: | ||
| fine_tuning_job: FineTuningJob = get_client().fine_tuning.jobs.cancel(fine_tuning_job_id=args.id) | ||
| print_model(fine_tuning_job) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| from __future__ import annotations | ||
| 
     | 
||
| import sys | ||
| from typing import TYPE_CHECKING | ||
| from argparse import ArgumentParser | ||
| 
     | 
||
| from .._models import BaseModel | ||
| from ...lib._validators import ( | ||
| get_chat_validators, | ||
| write_out_file, | ||
| read_any_format, | ||
| apply_validators, | ||
| apply_necessary_remediation, | ||
| ) | ||
| 
     | 
||
| if TYPE_CHECKING: | ||
| from argparse import _SubParsersAction | ||
| 
     | 
||
| 
     | 
||
| def register(subparser: _SubParsersAction[ArgumentParser]) -> None: | ||
| sub = subparser.add_parser("chat_fine_tunes.prepare_data") | ||
| sub.add_argument( | ||
| "-f", | ||
| "--file", | ||
| required=True, | ||
| help="JSONL, JSON file containing chat messages with roles (system, user, assistant) to be analyzed." | ||
| "This should be the local file path.", | ||
| ) | ||
| sub.add_argument( | ||
| "-q", | ||
| "--quiet", | ||
| required=False, | ||
| action="store_true", | ||
| help="Auto accepts all suggestions, without asking for user input. To be used within scripts.", | ||
| ) | ||
| sub.set_defaults(func=prepare_data, args_model=PrepareDataArgs) | ||
| 
     | 
||
| 
     | 
||
| class PrepareDataArgs(BaseModel): | ||
| file: str | ||
| 
     | 
||
| quiet: bool | ||
| 
     | 
||
| 
     | 
||
| def prepare_data(args: PrepareDataArgs) -> None: | ||
| sys.stdout.write("Analyzing chat fine-tuning data...\n") | ||
| fname = args.file | ||
| auto_accept = args.quiet | ||
| df, remediation = read_any_format(fname) | ||
| apply_necessary_remediation(None, remediation) | ||
| 
     | 
||
| validators = get_chat_validators() | ||
| 
     | 
||
| assert df is not None | ||
| 
     | 
||
| apply_validators( | ||
| df, | ||
| fname, | ||
| remediation, | ||
| validators, | ||
| auto_accept, | ||
| write_out_file_func=write_out_file, | ||
| ) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Avoid circular import in chat_fine_tunes CLI module
The new alias module is defined in
cli/_api/chat_fine_tunes.pywhile a package with the same name (cli/_api/chat_fine_tunes/) containsjobs.py. When_api/_mainimportschat_fine_tunes, Python loads this file and the statementfrom .chat_fine_tunes import jobsresolves to the module itself rather than the sibling package, leavingjobsundefined and raisingImportError: cannot import name 'jobs' from partially initialized module .... As a result, none of thechat_fine_tunescommands can be registered. The alias module needs a different name or should be removed in favour of the package so the import resolves correctly.Useful? React with 👍 / 👎.