openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.12.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

README.md

130lines · 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### Command-line interface
56
57This library additionally provides an `openai` command-line utility
58which makes it easy to interact with the API from your terminal. Run
59`openai api -h` for usage.
60
61```sh
62# list engines
63openai api engines.list
64
65# create a completion
66openai api completions.create -e ada -p "Hello world"
67```
68
69## Example code
70
71Examples 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).
72
73### Embeddings
74
75In 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.
76
77To get an embedding for a text string, you can use the embeddings method as follows in Python:
78
79```python
80import openai
81openai.api_key = "sk-..." # supply your API key however you choose
82
83# choose text to embed
84text_string = "sample text"
85
86# choose an embedding
87model_id = "text-similarity-davinci-001"
88
89# compute the embedding of the text
90embedding = openai.Embedding.create(input=text_string, engine=model_id)['data'][0]['embedding']
91```
92
93An 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).
94
95Examples of how to use embeddings are shared in the following Jupyter notebooks:
96
97- [Classification using embeddings](https://github.com/openai/openai-python/blob/main/examples/embeddings/Classification.ipynb)
98- [Clustering using embeddings](https://github.com/openai/openai-python/blob/main/examples/embeddings/Clustering.ipynb)
99- [Code search using embeddings](https://github.com/openai/openai-python/blob/main/examples/embeddings/Code_search.ipynb)
100- [Semantic text search using embeddings](https://github.com/openai/openai-python/blob/main/examples/embeddings/Semantic_text_search_using_embeddings.ipynb)
101- [User and product embeddings](https://github.com/openai/openai-python/blob/main/examples/embeddings/User_and_product_embeddings.ipynb)
102- [Zero-shot classification using embeddings](https://github.com/openai/openai-python/blob/main/examples/embeddings/Zero-shot_classification.ipynb)
103
104For 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.
105
106### Fine tuning
107
108Fine 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).
109
110Examples of fine tuning are shared in the following Jupyter notebooks:
111
112- [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)
113- Fine tuning a model that answers questions about the 2020 Olympics
114 - [Step 1: Collecting data](https://github.com/openai/openai-python/blob/main/examples/finetuning/olympics-1-collect-data.ipynb)
115 - [Step 2: Creating a synthetic Q&A dataset](https://github.com/openai/openai-python/blob/main/examples/finetuning/olympics-2-create-qa.ipynb)
116 - [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)
117
118For more information on fine tuning, read the [fine-tuning guide](https://beta.openai.com/docs/guides/fine-tuning) in the OpenAI documentation.
119
120## Requirements
121
122- Python 3.7.1+
123
124In general we want to support the versions of Python that our
125customers are using, so if you run into issues with any version
126issues, please let us know at support@openai.com.
127
128## Credit
129
130This library is forked from the [Stripe Python Library](https://github.com/stripe/stripe-python).
131