openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
README.md
79lines · modeblame
3c6d4cd6Greg Brockman5 years ago | 1 | # OpenAI Python Library |
| 2 | | |
| 3 | The OpenAI Python library provides convenient access to the OpenAI API | |
| 4 | from applications written in the Python language. It includes a | |
| 5 | pre-defined set of classes for API resources that initialize | |
| 6 | themselves dynamically from API responses which makes it compatible | |
| 7 | with a wide range of versions of the OpenAI API. | |
| 8 | | |
| 9 | ## Documentation | |
| 10 | | |
d53d9efbRachel Lim5 years ago | 11 | See the [OpenAI API docs](https://beta.openai.com/docs/api-reference?lang=python). |
3c6d4cd6Greg Brockman5 years ago | 12 | |
| 13 | ## Installation | |
| 14 | | |
| 15 | You don't need this source code unless you want to modify the package. If you just | |
| 16 | want to use the package, just run: | |
| 17 | | |
| 18 | ```sh | |
| 19 | pip install --upgrade openai | |
| 20 | ``` | |
| 21 | | |
| 22 | Install from source with: | |
| 23 | | |
| 24 | ```sh | |
| 25 | python setup.py install | |
| 26 | ``` | |
| 27 | | |
d53d9efbRachel Lim5 years ago | 28 | ## Usage |
| 29 | | |
| 30 | The library needs to be configured with your account's secret key which is available on the [website](https://beta.openai.com/account/api-keys). Either set it as the `OPENAI_API_KEY` environment variable before using the library: | |
| 31 | | |
| 32 | ```bash | |
| 33 | export OPENAI_API_KEY='sk-...' | |
| 34 | ``` | |
| 35 | | |
| 36 | Or set `openai.api_key` to its value: | |
| 37 | | |
| 38 | ```python | |
| 39 | import openai | |
| 40 | openai.api_key = "sk-..." | |
| 41 | | |
| 42 | # list engines | |
| 43 | engines = openai.Engine.list() | |
| 44 | | |
| 45 | # print the first engine's id | |
| 46 | print(engines.data[0].id) | |
| 47 | | |
| 48 | # create a completion | |
| 49 | completion = openai.Completion.create(engine="ada", prompt="Hello world") | |
| 50 | | |
| 51 | # print the completion | |
| 52 | print(completion.choices[0].text) | |
| 53 | ``` | |
| 54 | | |
| 55 | ### Command-line interface | |
| 56 | | |
| 57 | This library additionally provides an `openai` command-line utility | |
| 58 | which makes it easy to interact with the API from your terminal. Run | |
| 59 | `openai api -h` for usage. | |
| 60 | | |
| 61 | ``` | |
| 62 | # list engines | |
| 63 | openai api engines.list | |
| 64 | | |
| 65 | # create a completion | |
| 66 | openai api completions.create -e ada -p "Hello world" | |
| 67 | ``` | |
| 68 | | |
3c6d4cd6Greg Brockman5 years ago | 69 | ## Requirements |
| 70 | | |
40c32f9fRachel Lim5 years ago | 71 | - Python 3.6+ |
3c6d4cd6Greg Brockman5 years ago | 72 | |
| 73 | In general we want to support the versions of Python that our | |
| 74 | customers are using, so if you run into issues with any version | |
| 75 | issues, please let us know at support@openai.com. | |
| 76 | | |
| 77 | ## Credit | |
| 78 | | |
| 79 | This library is forked from the [Stripe Python Library](https://github.com/stripe/stripe-python). |