Skip to main content

Prefect integrations for working with OpenAI.

Project description

prefect-openai

Visit the full docs here to see additional examples and the API reference.

PyPI

Welcome!

Prefect integrations for working with OpenAI.

Getting Started

Python setup

Requires an installation of Python 3.7+.

We recommend using a Python virtual environment manager such as pipenv, conda or virtualenv.

These tasks are designed to work with Prefect 2.0. For more information about how to use Prefect, please refer to the Prefect documentation.

Installation

Install prefect-openai with pip:

pip install prefect-openai

A list of available blocks in prefect-openai and their setup instructions can be found here.

Write and run a flow

Retrieve the flow run name to create a story about it and an image using that story.

from prefect import flow, get_run_logger
from prefect.context import get_run_context
from prefect_openai import OpenAICredentials, ImageModel, CompletionModel

@flow
def create_story_and_image_from_flow_run_name():
    logger = get_run_logger()
    context = get_run_context()
    flow_run_name = context.flow_run.name.replace("-", " ")

    credentials = OpenAICredentials.load("my-block")

    text_model = CompletionModel(openai_credentials=credentials, model="text-ada-001")
    text_prompt = f"Story about a {flow_run_name}"
    text_result = text_model.submit_prompt(text_prompt)

    image_prompt = text_result.choices[0].text.strip()
    image_model = ImageModel(openai_credentials=credentials)
    image_result = image_model.submit_prompt(image_prompt)
    image_url = image_result.data[0]["url"]

    logger.info(
        f"The story behind the image, {image_prompt!r}, "
        f"check it out here: {image_url}"
    )
    return image_url

create_image()

For example, a flow run named space-orangutan prompted a story about "A space orangutan is a species of monkey that live in and..." yielding this image: img-F2JuMSOh1c4rcLlKIVHfTI8B

For more tips on how to use tasks and flows in a Collection, check out Using Collections!

Resources

If you encounter any bugs while using prefect-openai, feel free to open an issue in the prefect-openai repository.

If you have any questions or issues while using prefect-openai, you can find help in either the Prefect Discourse forum or the Prefect Slack community.

Feel free to star or watch prefect-openai for updates too!

Contributing

If you'd like to help contribute to fix an issue or add a feature to prefect-openai, please propose changes through a pull request from a fork of the repository.

Here are the steps:

  1. Fork the repository
  2. Clone the forked repository
  3. Install the repository and its dependencies:
pip install -e ".[dev]"
  1. Make desired changes
  2. Add tests
  3. Insert an entry to CHANGELOG.md
  4. Install pre-commit to perform quality checks prior to commit:
pre-commit install
  1. git commit, git push, and create a pull request

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

prefect-openai-0.1.0.tar.gz (27.8 kB view hashes)

Uploaded Source

Built Distribution

prefect_openai-0.1.0-py3-none-any.whl (12.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page