This is an example chat app intended to get you started with your first OpenAI API project. It uses the Chat Completions API to create a simple general purpose chat app with streaming.
To send your first API request with the OpenAI Python SDK, make sure you have the right dependencies installed and then run the following code:
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)
This quickstart app builds on top of the example code above, with streaming and a UI to visualize messages.
-
If you don’t have Python installed, install it from Python.org.
-
Clone this repository.
-
Navigate into the project directory:
$ cd openai-quickstart-python
-
Create a new virtual environment:
-
macOS:
$ python -m venv venv $ . venv/bin/activate
-
Windows:
> python -m venv venv > .\venv\Scripts\activate
-
-
Install the requirements:
$ pip install -r requirements.txt
-
Make a copy of the example environment variables file:
$ cp .env.example .env
-
Add your API key to the newly created
.env
file. -
Run the app:
$ flask run
You should now be able to access the app from your browser at the following URL: http:https://localhost:5000!