openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
README.md
166lines · 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 | | |
f288b001Sorin Suciu4 years ago | 55 | ### Microsoft Azure Endpoints |
| 56 | | |
| 57 | In order to use the library with Microsoft Azure endpoints, you need to set the api_type, api_base and api_version in addition to the api_key. The api_type must be set to 'azure' and the others correspond to the properites of your endpoint. | |
| 58 | In addition, the deployment name must be passed as the engine parameter. | |
| 59 | | |
| 60 | ```python | |
| 61 | import openai | |
| 62 | openai.api_type = "azure" | |
| 63 | openai.api_key = "..." | |
| 64 | openai.api_base = "https://example-endpoint.openai.azure.com" | |
| 65 | openai.api_version = "2021-11-01-preview" | |
| 66 | | |
| 67 | # create a completion | |
| 68 | completion = openai.Completion.create(engine="deployment-namme", prompt="Hello world") | |
| 69 | | |
| 70 | # print the completion | |
| 71 | print(completion.choices[0].text) | |
| 72 | | |
| 73 | # create a search and pass the deployment-name as the engine Id. | |
| 74 | search = openai.Engine(id="deployment-namme").search(documents=["White House", "hospital", "school"], query ="the president") | |
| 75 | | |
| 76 | # print the search | |
| 77 | print(search) | |
| 78 | ``` | |
62b51ca0Boris Dayma4 years ago | 79 | |
02e4008dSorin Suciu4 years ago | 80 | Please note that for the moment, the Microsoft Azure endpoints can only be used for completion, search and fine-tuning operations. |
95fa7d07Sorin Suciu4 years ago | 81 | For a detailed example on how to use fine-tuning and other operations using Azure endpoints, please check out the following Jupyter notebook: |
| 82 | [Using Azure fine-tuning](https://github.com/openai/openai-python/blob/main/examples/azure/finetuning.ipynb) | |
f288b001Sorin Suciu4 years ago | 83 | |
d53d9efbRachel Lim5 years ago | 84 | ### Command-line interface |
| 85 | | |
| 86 | This library additionally provides an `openai` command-line utility | |
| 87 | which makes it easy to interact with the API from your terminal. Run | |
| 88 | `openai api -h` for usage. | |
| 89 | | |
0e21703eTed Sanders4 years ago | 90 | ```sh |
d53d9efbRachel Lim5 years ago | 91 | # list engines |
| 92 | openai api engines.list | |
| 93 | | |
| 94 | # create a completion | |
| 95 | openai api completions.create -e ada -p "Hello world" | |
| 96 | ``` | |
| 97 | | |
0e21703eTed Sanders4 years ago | 98 | ## Example code |
| 99 | | |
| 100 | Examples of how to use [embeddings](https://github.com/openai/openai-python/tree/main/examples/embeddings), [fine tuning](https://github.com/openai/openai-python/tree/main/examples/finetuning), [semantic search](https://github.com/openai/openai-python/tree/main/examples/semanticsearch), and [codex](https://github.com/openai/openai-python/tree/main/examples/codex) can be found in the [examples folder](https://github.com/openai/openai-python/tree/main/examples). | |
| 101 | | |
| 102 | ### Embeddings | |
| 103 | | |
| 104 | In the OpenAI Python library, an embedding represents a text string as a fixed-length vector of floating point numbers. Embeddings are designed to measure the similarity or relevance between text strings. | |
| 105 | | |
| 106 | To get an embedding for a text string, you can use the embeddings method as follows in Python: | |
| 107 | | |
| 108 | ```python | |
| 109 | import openai | |
| 110 | openai.api_key = "sk-..." # supply your API key however you choose | |
| 111 | | |
| 112 | # choose text to embed | |
| 113 | text_string = "sample text" | |
| 114 | | |
| 115 | # choose an embedding | |
f4be8f2fhallacy4 years ago | 116 | model_id = "text-similarity-davinci-001" |
0e21703eTed Sanders4 years ago | 117 | |
| 118 | # compute the embedding of the text | |
f4be8f2fhallacy4 years ago | 119 | embedding = openai.Embedding.create(input=text_string, engine=model_id)['data'][0]['embedding'] |
0e21703eTed Sanders4 years ago | 120 | ``` |
| 121 | | |
| 122 | An example of how to call the embeddings method is shown in the [get embeddings notebook](https://github.com/openai/openai-python/blob/main/examples/embeddings/Get_embeddings.ipynb). | |
| 123 | | |
| 124 | Examples of how to use embeddings are shared in the following Jupyter notebooks: | |
| 125 | | |
| 126 | - [Classification using embeddings](https://github.com/openai/openai-python/blob/main/examples/embeddings/Classification.ipynb) | |
| 127 | - [Clustering using embeddings](https://github.com/openai/openai-python/blob/main/examples/embeddings/Clustering.ipynb) | |
| 128 | - [Code search using embeddings](https://github.com/openai/openai-python/blob/main/examples/embeddings/Code_search.ipynb) | |
| 129 | - [Semantic text search using embeddings](https://github.com/openai/openai-python/blob/main/examples/embeddings/Semantic_text_search_using_embeddings.ipynb) | |
| 130 | - [User and product embeddings](https://github.com/openai/openai-python/blob/main/examples/embeddings/User_and_product_embeddings.ipynb) | |
| 131 | - [Zero-shot classification using embeddings](https://github.com/openai/openai-python/blob/main/examples/embeddings/Zero-shot_classification.ipynb) | |
eabf01f0Ted Sanders4 years ago | 132 | - [Recommendation using embeddings](https://github.com/openai/openai-python/blob/main/examples/embeddings/Recommendation.ipynb) |
0e21703eTed Sanders4 years ago | 133 | |
| 134 | For more information on embeddings and the types of embeddings OpenAI offers, read the [embeddings guide](https://beta.openai.com/docs/guides/embeddings) in the OpenAI documentation. | |
| 135 | | |
| 136 | ### Fine tuning | |
| 137 | | |
| 138 | Fine tuning a model on training data can both improve the results (by giving the model more examples to learn from) and reduce the cost & latency of API calls (by reducing the need to include training examples in prompts). | |
| 139 | | |
| 140 | Examples of fine tuning are shared in the following Jupyter notebooks: | |
| 141 | | |
| 142 | - [Classification with fine tuning](https://github.com/openai/openai-python/blob/main/examples/finetuning/finetuning-classification.ipynb) (a simple notebook that shows the steps required for fine tuning) | |
| 143 | - Fine tuning a model that answers questions about the 2020 Olympics | |
| 144 | - [Step 1: Collecting data](https://github.com/openai/openai-python/blob/main/examples/finetuning/olympics-1-collect-data.ipynb) | |
| 145 | - [Step 2: Creating a synthetic Q&A dataset](https://github.com/openai/openai-python/blob/main/examples/finetuning/olympics-2-create-qa.ipynb) | |
| 146 | - [Step 3: Train a fine-tuning model specialized for Q&A](https://github.com/openai/openai-python/blob/main/examples/finetuning/olympics-3-train-qa.ipynb) | |
| 147 | | |
62b51ca0Boris Dayma4 years ago | 148 | Sync your fine-tunes to [Weights & Biases](https://wandb.me/openai-docs) to track experiments, models, and datasets in your central dashboard with: |
| 149 | | |
| 150 | ```bash | |
| 151 | openai wandb sync | |
| 152 | ``` | |
| 153 | | |
0e21703eTed Sanders4 years ago | 154 | For more information on fine tuning, read the [fine-tuning guide](https://beta.openai.com/docs/guides/fine-tuning) in the OpenAI documentation. |
| 155 | | |
3c6d4cd6Greg Brockman5 years ago | 156 | ## Requirements |
| 157 | | |
62f8d40fMadeleine Thompson4 years ago | 158 | - Python 3.7.1+ |
3c6d4cd6Greg Brockman5 years ago | 159 | |
| 160 | In general we want to support the versions of Python that our | |
| 161 | customers are using, so if you run into issues with any version | |
| 162 | issues, please let us know at support@openai.com. | |
| 163 | | |
| 164 | ## Credit | |
| 165 | | |
| 166 | This library is forked from the [Stripe Python Library](https://github.com/stripe/stripe-python). |