Skip to content
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

Add shell auto-completion #1603

Open
wants to merge 54 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
bf189a9
Added argcomplete to the dependencies
hoonman Jul 31, 2024
01d72f3
Merge pull request #1 from Tienbruh/hoonbranch
hoonman Jul 31, 2024
f75f8a5
Added argcomplete to _cli.py
Tienbruh Jul 31, 2024
30f8013
Merge pull request #2 from Tienbruh/tienbranch
Tienbruh Jul 31, 2024
5a7d06e
Added bash command requirement for bash
hoonman Aug 1, 2024
f95433c
edit readme structure
hoonman Aug 2, 2024
080a818
Merge pull request #3 from Tienbruh/hoonbranch
hoonman Aug 2, 2024
f724c8b
edit readme structure
hoonman Aug 2, 2024
f734f73
Merge pull request #4 from Tienbruh/hoonbranch
hoonman Aug 2, 2024
38dc4ad
simplified step to register argcomplete
hoonman Aug 2, 2024
0e74b73
Merge pull request #5 from Tienbruh/hoonbranch
hoonman Aug 2, 2024
9bd76a1
Merge branch 'openai:main' into main
JesusG2022 Aug 2, 2024
9ba029d
refactored readme structure
Tienbruh Aug 2, 2024
11f3670
modified ordering of readme sections
Tienbruh Aug 2, 2024
5a65355
Merge pull request #6 from Tienbruh/tienbranch
Tienbruh Aug 2, 2024
9742678
Modified readme to include both temporary and permanent autocomplete …
hoonman Aug 4, 2024
f3d9071
Merge pull request #7 from Tienbruh/hoonbranch
hoonman Aug 4, 2024
1ce382c
Added subsection for different shells
hoonman Aug 4, 2024
cbb5052
removed global completion command
hoonman Aug 4, 2024
f02e7ea
Merge pull request #8 from Tienbruh/hoonbranch
hoonman Aug 4, 2024
016be6e
Fixed vague wording in readme
Tienbruh Aug 4, 2024
0566711
Modified readme layout
Tienbruh Aug 4, 2024
bdd7b7b
Merge pull request #9 from Tienbruh/tienbranch
Tienbruh Aug 4, 2024
3c22cda
Fixed readme typo
Tienbruh Aug 4, 2024
faa5dd6
Merge pull request #10 from Tienbruh/tienbranch
Tienbruh Aug 4, 2024
e112898
Merge pull request #11 from openai/main
hoonman Aug 5, 2024
177fbea
resolve merge conflict with pyproject.toml file
hoonman Aug 6, 2024
4430b99
Merge pull request #13 from Tienbruh/openai-main
hoonman Aug 6, 2024
1c75144
Merge branch 'openai:main' into main
Tienbruh Aug 8, 2024
6a41a59
Merge branch 'openai:main' into main
Tienbruh Aug 8, 2024
5e8ec65
Merge branch 'main' into main
hoonman Aug 10, 2024
9b097bb
Merge branch 'openai:main' into main
Tienbruh Aug 13, 2024
37d90ba
Merge branch 'openai:main' into main
Tienbruh Aug 15, 2024
995a13d
Merge branch 'main' into main
hoonman Aug 16, 2024
3701916
Merge branch 'openai:main' into main
Tienbruh Aug 16, 2024
51d8d52
Merge branch 'main' into main
hoonman Aug 21, 2024
4adbc2d
Merge branch 'openai:main' into main
Tienbruh Aug 29, 2024
361f10d
Moved argcomplete to become an optional dependency
Tienbruh Sep 3, 2024
984a02e
Merge pull request #14 from Tienbruh/tienbranch
hoonman Sep 3, 2024
8e70ec3
add proxy for argcomplete and use it in _cli.py file
hoonman Sep 3, 2024
889c3b9
add argcomplete proxy in the __init__ file
hoonman Sep 3, 2024
8ef585d
add try except block and remove proxy
hoonman Sep 3, 2024
ca2bd27
include guide on installing argcomplete optionally
hoonman Sep 3, 2024
77c2841
edit convention and guide on installing autocomplete optionally
hoonman Sep 3, 2024
6d2e5fa
remove unnecessary newline
hoonman Sep 3, 2024
5eda534
Merge pull request #15 from Tienbruh/hoonbranch
Tienbruh Sep 4, 2024
6041733
remove whiteline
hoonman Sep 4, 2024
f333c28
Merge branch 'openai:main' into main
Tienbruh Sep 5, 2024
8e8acfc
Merge branch 'openai:main' into main
Tienbruh Sep 6, 2024
a78746d
Merge branch 'openai:main' into main
Tienbruh Sep 9, 2024
b0d6047
Merge branch 'openai:main' into main
Tienbruh Sep 12, 2024
2f6b763
Merge branch 'openai:main' into main
Tienbruh Sep 16, 2024
f7bc7ae
Merge branch 'openai:main' into main
Tienbruh Sep 17, 2024
9ab6959
Merge branch 'openai:main' into main
Tienbruh Sep 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,57 @@ In addition to the options provided in the base `OpenAI` client, the following o

An example of using the client with Microsoft Entra ID (formerly known as Azure Active Directory) can be found [here](https://github.com/openai/openai-python/blob/main/examples/azure_ad.py).

## Activating CLI Autocomplete
Use the following commands to activate CLI autocompletion for each shell.

### Bash & Zsh
Copy link
Collaborator

@RobertCraigie RobertCraigie Sep 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: are there existing docs we could link to here instead of listing out all the possible environments here? maybe argcomplete's docs?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both temporary and permanent argcomplete activation instructions for Fish and Powershell are available on argcomplete's contrib directory. However, only the temporary activation commands for Bash and Zsh are listed on argcomplete's README file. The commands to activate autocompletion permanently for Bash and Zsh are not found anywhere on argcomplete's docs.

What are your opinions on replacing all the other environment activation instructions with the appropriate links to argcomplete's docs, while keeping these lines for permanent activation on Bash and Zsh?

For Bash:

register-python-argcomplete openai >> ~/.bashrc

For Zsh:

register-python-argcomplete openai >> ~/.zshrc

Use this command to register `openai` for argument completion using argcomplete for the current terminal session:
```shell
eval "$(register-python-argcomplete openai)"
```
#### Permanent Argument Completion Setup for Bash/Zsh
Alternatively, run this command to register `openai` for argument completion permanently, eliminating the need to run the `eval` command each time the terminal is restarted or killed.
For Bash:
```shell
register-python-argcomplete openai >> ~/.bashrc
```
For Zsh:
```shell
register-python-argcomplete openai >> ~/.zshrc
```

### Powershell
To activate completions for Powershell use:
```shell
register-python-argcomplete --shell powershell openai | Out-String | Invoke-Expression
```
#### Permanent Argument Completion Setup for Powershell
Alternatively, create a new completion file. This command will persist even if the terminal is killed or restarted.
```shell
register-python-argcomplete --shell powershell openai > ~/openai.psm1
```
To activate the completions file, open the [``$PROFILE``](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-7.4&viewFallbackFrom=powershell-7.3#how-to-create-a-profile) by running ``notepad $PROFILE`` and add this following line:
```shell
Import-Module "~/openai.psm1"
```
### Fish
To activate completions for fish use:
```shell
register-python-argcomplete --shell fish openai | source
```
#### Permanent Argument Completion Setup for Fish
Alternatively, create a new completion file. This command will persist even if the terminal is killed or restarted.
```shell
register-python-argcomplete --shell fish openai > ~/.config/fish/config.fish
```

### Usage
After completing the appropriate steps for your shell, try the following action:
>_If you have activated permanent argument completion, restart your terminal before proceeding._
```shell
openai <TAB>
```

## Versioning

This package generally follows [SemVer](https://semver.org/spec/v2.0.0.html) conventions, though certain backwards-incompatible changes may be released as minor versions:
Expand All @@ -641,4 +692,4 @@ We are keen for your feedback; please open an [issue](https://www.github.com/ope

## Requirements

Python 3.7 or higher.
Python 3.7 or higher.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies = [
"cached-property; python_version < '3.8'",
"tqdm > 4",
"jiter>=0.4.0, <1",
"argcomplete >= 1.12.0"
Copy link
Collaborator

@RobertCraigie RobertCraigie Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to avoid adding a new required dependency, can we add this as an optional dependency?

I'm thinking something like this, thoughts?

pip install openai[cli]

Copy link
Author

@hoonman hoonman Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the feedback! Our team discussed this and wanted to share our approach to add argcomplete as an optional dependency.

To avoid making argcomplete a required dependency, we plan to:

  1. Change argcomplete from a required to an optional dependency in pyproject.toml.
  2. Add a proxy file for argcomplete to handle optional imports.
  3. Update the __init__.py file in the extras directory to reflect these changes.
  4. Modify the README to instruct users to use the [cli] option to install argcomplete.

Please let us know if there are any additional changes we should consider.

Copy link
Collaborator

@RobertCraigie RobertCraigie Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds good to me! I don't think there's any other changes needed.

For what it's worth I don't think we need the complexity of a proxy here though as the dependency is only ever used in one place anyway we can just change that call site to try ... except ImportError

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have included the changes for making argcomplete an optional dependency, the try...except ImportError to the _cli.py, as well as the pip install openai[cli] command in the README. Let me know if there's anything else that needs modification. Thank you!

]
requires-python = ">= 3.7.1"
classifiers = [
Expand Down
4 changes: 4 additions & 0 deletions src/openai/cli/_cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#!usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
from __future__ import annotations

import sys
import logging
import argparse
import argcomplete
from typing import Any, List, Type, Optional
from typing_extensions import ClassVar

Expand Down Expand Up @@ -162,6 +165,7 @@ def _parse_args(parser: argparse.ArgumentParser) -> tuple[argparse.Namespace, Ar

def _main() -> None:
parser = _build_parser()
argcomplete.autocomplete(parser)
parsed, args, unknown = _parse_args(parser)

if args.verbosity != 0:
Expand Down
Loading