Skip to content

SubramanyaNayak-github/openai-quickstart-python

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenAI API Quickstart - Python example app

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.

Basic request

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.

Setup

  1. If you don’t have Python installed, install it from Python.org.

  2. Clone this repository.

  3. Navigate into the project directory:

    $ cd openai-quickstart-python
  4. Create a new virtual environment:

    • macOS:

      $ python -m venv venv
      $ . venv/bin/activate
    • Windows:

      > python -m venv venv
      > .\venv\Scripts\activate
  5. Install the requirements:

    $ pip install -r requirements.txt
  6. Make a copy of the example environment variables file:

    $ cp .env.example .env
  7. Add your API key to the newly created .env file.

  8. 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!

About

Python example app from the OpenAI API quickstart tutorial

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages

  • HTML 49.5%
  • CSS 30.1%
  • Python 20.4%