Skip to main content

Flask-OpenAI Extension

The Flask-OpenAI extension provides a simple and intuitive way to integrate OpenAI's API into your Flask applications. With minimal setup, you can start leveraging the power of AI models in your Flask projects, whether it's for generating text, code, or any other application supported by OpenAI.

Features

  • Easy integration of OpenAI's API into Flask applications.
  • Automatic management of OpenAI API keys.
  • Efficient handling of API client instances throughout the application lifecycle.

Installation

Install Flask-OpenAI using pip:

pip install flask-openai

Quick Start

  1. Set up your Flask application

First, ensure you have Flask installed. If not, you can install it using pip:

pip install Flask

Then, set up a basic Flask application:

from flask import Flask
app = Flask(__name__)
  1. Configure the Flask-OpenAI extension

Import and initialize the OpenAI extension, passing your Flask app object to it. Don't forget to set the OPENAI_API_KEY in your app's configuration:

from flask_openai import OpenAI

app.config['OPENAI_API_KEY'] = 'your_openai_api_key_here'
openai_extension = OpenAI(app)

Alternatively, if you are using a factory function to create your Flask app, you can set up the OpenAI extension like this:

openai_extension = OpenAI()

def create_app():
    app = Flask(__name__)
    app.config['OPENAI_API_KEY'] = 'your_openai_api_key_here'
    openai_extension.init_app(app)
    return app
  1. Use the OpenAI client in your application

Now, you can access the OpenAI client in your route handlers using openai_extension.client. Here's an example of how to generate text with the OpenAI API:

@app.route('/generate-text')
def generate_text():
    completion = openai.client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system",
             "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."},
            {"role": "user", "content": "Compose a poem that explains the concept of recursion in programming."}
        ]
    )

    return completion.choices[0].message.content

Documentation

For more information on OpenAI's API and its capabilities, visit OpenAI API documentation.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a pull request.

Support

If you have any questions or encounter any issues, please open an issue on the project's GitHub page.

Acknowledgements

This project is not officially associated with OpenAI. All trademarks are the property of their respective owners.

Metadata

Release files for flask-openai 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-openai 0.5.0
File Size Uploaded
flask_openai-0.5.0.tar.gz 4.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flask-openai 0.5.0
File Interpreter ABI Platform
flask_openai-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 7.9 kB

Release files / flask_openai-0.5.0.tar.gz

Download URL flask_openai-0.5.0.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
fffc7ba19fb0c28dc8bf7062270c1039c1ab131b1a51ddb934b66dda54bcdaf6
BLAKE2b-256 checksum
How to use checksums
fa41ba37608c39424ecbdac6ee0e1b5f61a6de76ea977013bb56867d54b4e592
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.31.0

Release files / flask_openai-0.5.0-py3-none-any.whl

Download URL flask_openai-0.5.0-py3-none-any.whl
Size 3.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1670816b2addd569144a13cb8cdedddd90854cf2e6a31d252c5c10a3846db45b
BLAKE2b-256 checksum
How to use checksums
31ace9d98c43c9d0c55e70d0297bf20aff8f3c40de98aa7c28937980d5c7f536
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.31.0

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page