Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/openai/openai-quickstart-python
Python example app from the OpenAI API quickstart tutorial
https://github.com/openai/openai-quickstart-python
openai openai-api
Last synced: 4 days ago
JSON representation
Python example app from the OpenAI API quickstart tutorial
- Host: GitHub
- URL: https://github.com/openai/openai-quickstart-python
- Owner: openai
- License: mit
- Created: 2021-11-30T22:25:27.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2024-06-14T11:24:57.000Z (8 months ago)
- Last Synced: 2025-01-15T23:48:56.759Z (11 days ago)
- Topics: openai, openai-api
- Homepage: https://platform.openai.com/docs/quickstart?context=python
- Size: 131 KB
- Stars: 1,721
- Watchers: 107
- Forks: 1,320
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-gpt - openai-quickstart-python
- jimsghstars - openai/openai-quickstart-python - Python example app from the OpenAI API quickstart tutorial (Others)
README
# OpenAI API Quickstart - Python
This repository hosts multiple quickstart apps for different OpenAI API endpoints (chat, assistants, etc). Check out the `examples` folder to try out different examples and get started using the OpenAI API.
## Basic request
To send your first API request with the [OpenAI Python SDK](https://github.com/openai/openai-python), make sure you have the right [dependencies installed](https://platform.openai.com/docs/quickstart?context=python) and then run the following code:
```python
from openai import OpenAI
client = OpenAI()completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
]
)print(completion.choices[0].message)
```## Setup
1. If you don’t have Python installed, install it [from Python.org](https://www.python.org/downloads/).
2. [Clone](https://docs.github.com/en/repositories/creating-and-managing-repositories/cloning-a-repository) this repository.
3. Navigate into the project directory:
```bash
$ cd openai-quickstart-python
```4. Create a new virtual environment:
- macOS:
```bash
$ python -m venv venv
$ . venv/bin/activate
```- Windows:
```cmd
> python -m venv venv
> .\venv\Scripts\activate
```5. Install the requirements:
```bash
$ pip install -r requirements.txt
```6. Make a copy of the example environment variables file:
```bash
$ cp .env.example .env
```7. Add your [API key](https://platform.openai.com/api-keys) to the newly created `.env` file.
8. Run the app:
This step depends on the app itself. If the code uses flask (like the chat-basic example), you can run:
```bash
$ flask run
```You should now be able to access the app from your browser at the following URL: [http://localhost:5000](http://localhost:5000)!
If the code is just a simple Python script, you can run it with:
```bash
$ python my_file.py
```