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 76 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 54 commits
Commits
Show all changes
76 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
938f601
Merge branch 'main' into main
hoonman Sep 29, 2024
3b21b35
Merge branch 'main' into main
hoonman Oct 2, 2024
611e255
Update README.md
Tienbruh Oct 18, 2024
820d86f
Merge branch 'openai:main' into main
Tienbruh Oct 18, 2024
0eb98ca
Refactored README.md for autocompletion instructions
Tienbruh Oct 18, 2024
d72b501
Merge branch 'main' into main
hoonman Oct 23, 2024
f8df3ff
Merge branch 'main' into main
hoonman Oct 25, 2024
c6a76f3
Merge branch 'main' into main
hoonman Oct 30, 2024
c8c0b48
Merge branch 'main' into main
hoonman Nov 5, 2024
f851ea5
Merge branch 'main' into main
hoonman Nov 6, 2024
60e6486
Merge branch 'main' into main
hoonman Nov 7, 2024
f0645a1
Merge branch 'main' into main
hoonman Nov 12, 2024
57a37b4
Merge branch 'main' into main
hoonman Nov 19, 2024
445a287
Merge branch 'main' into main
hoonman Nov 21, 2024
20552fd
Merge branch 'main' into main
hoonman Dec 3, 2024
d17e8c9
Merge branch 'main' into main
hoonman Dec 4, 2024
0ca397f
Merge branch 'openai:main' into main
Tienbruh Dec 5, 2024
ec79b64
Merge branch 'main' into main
hoonman Dec 10, 2024
39f71d2
Merge branch 'main' into main
hoonman Dec 12, 2024
13c09ea
Merge branch 'openai:main' into main
Tienbruh Dec 14, 2024
3ab6ad9
Merge branch 'main' into main
hoonman Dec 28, 2024
4e5f49c
Merge branch 'main' into main
hoonman Jan 4, 2025
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
58 changes: 57 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,62 @@ 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.

Install `openai` with `[cli]` option to install argcomplete.
```shell
pip install openai[cli]
```

### 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

Copy link
Collaborator

Choose a reason for hiding this comment

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

cc @kwhinnery-openai for docs review

Choose a reason for hiding this comment

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

Hello @RobertCraigie, we have refactored our section of the readME file to utilize existing documentation from argcomplete. Let us know if anything else needs to be fixed.

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 @@ -712,4 +768,4 @@ print(openai.__version__)

## 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 @@ -39,6 +39,7 @@ classifiers = [

[project.optional-dependencies]
datalib = ["numpy >= 1", "pandas >= 1.2.3", "pandas-stubs >= 1.1.0.11"]
cli = ["argcomplete >= 1.12.0"]

[project.urls]
Homepage = "https://github.com/openai/openai-python"
Expand Down
7 changes: 7 additions & 0 deletions src/openai/cli/_cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
from __future__ import annotations

import sys
Expand Down Expand Up @@ -162,6 +164,11 @@ def _parse_args(parser: argparse.ArgumentParser) -> tuple[argparse.Namespace, Ar

def _main() -> None:
parser = _build_parser()
try:
import argcomplete
argcomplete.autocomplete(parser)
except ImportError:
pass
parsed, args, unknown = _parse_args(parser)

if args.verbosity != 0:
Expand Down