openai/openai-python

Public

mirrored fromhttps://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.22.1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

README.md

199lines · modecode

1# OpenAI Python Library
2
3The OpenAI Python library provides convenient access to the OpenAI API
4from applications written in the Python language. It includes a
5pre-defined set of classes for API resources that initialize
6themselves dynamically from API responses which makes it compatible
7with a wide range of versions of the OpenAI API.
8
9## Documentation
10
11See the [OpenAI API docs](https://beta.openai.com/docs/api-reference?lang=python).
12
13## Installation
14
15You don't need this source code unless you want to modify the package. If you just
16want to use the package, just run:
17
18```sh
19pip install --upgrade openai
20```
21
22Install from source with:
23
24```sh
25python setup.py install
26```
27
28## Usage
29
30The 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
33export OPENAI_API_KEY='sk-...'
34```
35
36Or set `openai.api_key` to its value:
37
38```python
39import openai
40openai.api_key = "sk-..."
41
42# list engines
43engines = openai.Engine.list()
44
45# print the first engine's id
46print(engines.data[0].id)
47
48# create a completion
49completion = openai.Completion.create(engine="ada", prompt="Hello world")
50
51# print the completion
52print(completion.choices[0].text)
53```
54
55### Microsoft Azure Endpoints
56
57In 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 properties of your endpoint.
58In addition, the deployment name must be passed as the engine parameter.
59
60```python
61import openai
62openai.api_type = "azure"
63openai.api_key = "..."
64openai.api_base = "https://example-endpoint.openai.azure.com"
65openai.api_version = "2021-11-01-preview"
66
67# create a completion
68completion = openai.Completion.create(engine="deployment-name", prompt="Hello world")
69
70# print the completion
71print(completion.choices[0].text)
72
73# create a search and pass the deployment-name as the engine Id.
74search = openai.Engine(id="deployment-name").search(documents=["White House", "hospital", "school"], query ="the president")
75
76# print the search
77print(search)
78```
79
80Please note that for the moment, the Microsoft Azure endpoints can only be used for completion, search and fine-tuning operations.
81For a detailed example on how to use fine-tuning and other operations using Azure endpoints, please check out the following Jupyter notebooks:
82* [Using Azure fine-tuning](https://github.com/openai/openai-cookbook/tree/main/examples/azure/finetuning.ipynb)
83* [Using Azure embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/azure/embeddings.ipynb)
84
85### Microsoft Azure Active Directory Authentication
86
87In order to use Microsoft Active Directory to authenticate to your Azure endpoint, you need to set the api_type to "azure_ad" and pass the acquired credential token to api_key. The rest of the parameters need to be set as specified in the previous section.
88
89
90```python
91from azure.identity import DefaultAzureCredential
92import openai
93
94# Request credential
95default_credential = DefaultAzureCredential()
96token = default_credential.get_token("https://cognitiveservices.azure.com")
97
98# Setup parameters
99openai.api_type = "azure_ad"
100openai.api_key = token.token
101openai.api_base = "https://example-endpoint.openai.azure.com/"
102openai.api_version = "2022-03-01-preview"
103
104# ...
105```
106### Command-line interface
107
108This library additionally provides an `openai` command-line utility
109which makes it easy to interact with the API from your terminal. Run
110`openai api -h` for usage.
111
112```sh
113# list engines
114openai api engines.list
115
116# create a completion
117openai api completions.create -e ada -p "Hello world"
118```
119
120## Example code
121
122Examples of how to use this Python library to accomplish various tasks can be found in the [OpenAI Cookbook](https://github.com/openai/openai-cookbook/). It contains code examples for:
123
124* Classification using fine-tuning
125* Clustering
126* Code search
127* Customizing embeddings
128* Question answering from a corpus of documents
129* Recommendations
130* Visualization of embeddings
131* And more
132
133Prior to July 2022, this OpenAI Python library hosted code examples in its examples folder, but since then all examples have been migrated to the [OpenAI Cookbook](https://github.com/openai/openai-cookbook/).
134
135### Embeddings
136
137In 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.
138
139To get an embedding for a text string, you can use the embeddings method as follows in Python:
140
141```python
142import openai
143openai.api_key = "sk-..." # supply your API key however you choose
144
145# choose text to embed
146text_string = "sample text"
147
148# choose an embedding
149model_id = "text-similarity-davinci-001"
150
151# compute the embedding of the text
152embedding = openai.Embedding.create(input=text_string, engine=model_id)['data'][0]['embedding']
153```
154
155An example of how to call the embeddings method is shown in this [get embeddings notebook](https://github.com/openai/openai-cookbook/blob/main/examples/Get_embeddings.ipynb).
156
157Examples of how to use embeddings are shared in the following Jupyter notebooks:
158
159- [Classification using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Classification_using_embeddings.ipynb)
160- [Clustering using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Clustering.ipynb)
161- [Code search using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Code_search.ipynb)
162- [Semantic text search using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Semantic_text_search_using_embeddings.ipynb)
163- [User and product embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/User_and_product_embeddings.ipynb)
164- [Zero-shot classification using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Zero-shot_classification_with_embeddings.ipynb)
165- [Recommendation using embeddings](https://github.com/openai/openai-cookbook/blob/main/examples/Recommendation_using_embeddings.ipynb)
166
167For 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.
168
169### Fine tuning
170
171Fine 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).
172
173Examples of fine tuning are shared in the following Jupyter notebooks:
174
175- [Classification with fine tuning](https://github.com/openai/openai-cookbook/blob/main/examples/Fine-tuned_classification.ipynb) (a simple notebook that shows the steps required for fine tuning)
176- Fine tuning a model that answers questions about the 2020 Olympics
177 - [Step 1: Collecting data](https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-1-collect-data.ipynb)
178 - [Step 2: Creating a synthetic Q&A dataset](https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb)
179 - [Step 3: Train a fine-tuning model specialized for Q&A](https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb)
180
181Sync your fine-tunes to [Weights & Biases](https://wandb.me/openai-docs) to track experiments, models, and datasets in your central dashboard with:
182
183```bash
184openai wandb sync
185```
186
187For more information on fine tuning, read the [fine-tuning guide](https://beta.openai.com/docs/guides/fine-tuning) in the OpenAI documentation.
188
189## Requirements
190
191- Python 3.7.1+
192
193In general we want to support the versions of Python that our
194customers are using, so if you run into issues with any version
195issues, please let us know at support@openai.com.
196
197## Credit
198
199This library is forked from the [Stripe Python Library](https://github.com/stripe/stripe-python).
200