# Coordinate and use AI in your dataflow with `prefect-openai`
<p align="center">
<img src="https://user-images.githubusercontent.com/15331990/213825004-eedb25b3-0520-4f55-95d3-3a3fc8b235ff.png">
<br>
<a href="https://pypi.python.org/pypi/prefect-openai/" alt="PyPI version">
<img alt="PyPI" src="https://img.shields.io/pypi/v/prefect-openai?color=0052FF&labelColor=090422"></a>
<a href="https://github.com/PrefectHQ/prefect-openai/" alt="Stars">
<img src="https://img.shields.io/github/stars/PrefectHQ/prefect-openai?color=0052FF&labelColor=090422" /></a>
<a href="https://pepy.tech/badge/prefect-openai/" alt="Downloads">
<img src="https://img.shields.io/pypi/dm/prefect-openai?color=0052FF&labelColor=090422" /></a>
<a href="https://github.com/PrefectHQ/prefect-openai/pulse" alt="Activity">
<img src="https://img.shields.io/github/commit-activity/m/PrefectHQ/prefect-openai?color=0052FF&labelColor=090422" /></a>
<br>
<a href="https://prefect-community.slack.com" alt="Slack">
<img src="https://img.shields.io/badge/slack-join_community-red.svg?color=0052FF&labelColor=090422&logo=slack" /></a>
<a href="https://discourse.prefect.io/" alt="Discourse">
<img src="https://img.shields.io/badge/discourse-browse_forum-red.svg?color=0052FF&labelColor=090422&logo=discourse" /></a>
</p>
Visit the full docs [here](https://PrefectHQ.github.io/prefect-openai) to see additional examples and the API reference.
The `prefect-openai` collection makes it easy to leverage the capabilities of AI in your flows. Check out the examples below to get started!
## Getting Started
### Summarize tracebacks with GPT3
Tracebacks--it's quintessential in programming. They are a record of every line of code leading to the error, to help us, humans, determine what's wrong with the program and find a solution.
However, tracebacks can be extraordinarily complex, especially for someone new to the codebase.
To streamline this process, we could add AI to the mix, to offer a more human-readable summary of the issue, so it's easier for the developer to understand what went wrong and implement a fix.
After installing `prefect-openai` and [saving an OpenAI key](#saving-an-openai-key), you can easily incorporate OpenAI within your flows to help you achieve the aforementioned benefits!
```python
from prefect import flow, get_run_logger
from prefect_openai import OpenAICredentials, CompletionModel
@flow
def summarize_traceback(traceback: str) -> str:
logger = get_run_logger()
openai_credentials = OpenAICredentials.load("openai-credentials")
completion_model = CompletionModel(
openai_credentials=openai_credentials,
model="text-curie-001",
max_tokens=512,
)
prompt = f"Summarize cause of error from this traceback: ```{traceback}```"
summary = completion_model.submit_prompt(traceback).choices[0]["text"]
logger.info(f"Summary of the traceback: {summary}")
return summary
if __name__ == "__main__":
traceback = """
ParameterBindError: Error binding parameters for function 'summarize_traceback': missing a required argument: 'traceback'.
Function 'summarize_traceback' has signature 'traceback: str) -> str' but received args: () and kwargs: {}.
"""
summarize_traceback(traceback)
```
```bash hl_lines="4"
...
12:29:32.085 | INFO | Flow run 'analytic-starling' - Finished text completion using the 'text-curie-001' model with 113 tokens, creating 1 choice(s).
12:29:32.089 | INFO | Flow run 'analytic-starling' - Summary of the traceback:
This error is caused by the missing argument traceback. The function expects a traceback object as its first argument, but received nothing.
...
```
Notice how the original traceback was quite long and confusing.
On the flip side, the Curie GPT3 model was able to summarize the issue eloquently!
!!! info "Built-in decorator"
No need to build this yourself, `prefect-openai` features a
[built-in decorator](completion/#prefect_openai.completion.interpret_exception)
to help you automatically catch and interpret exceptions in flows, tasks, and even
vanilla Python functions.
```python
import httpx
from prefect_openai.completion import interpret_exception
@interpret_exception("COMPLETION_MODEL_BLOCK_NAME_PLACEHOLDER")
def example_func():
resp = httpx.get("https://httpbin.org/status/403")
resp.raise_for_status()
example_func()
```
### Create a story around a flow run name with GPT3 and DALL-E
Have you marveled at all the AI-generated images and wondered how others did it?
After installing `prefect-openai` and [saving an OpenAI key](#saving-an-openai-key), you, too, can create AI-generated art.
Here's an example on how to create a story and an image based off a flow run name.
```python
from prefect import flow, task, get_run_logger
from prefect.context import get_run_context
from prefect_openai import OpenAICredentials, ImageModel, CompletionModel
@task
def create_story_from_name(credentials: OpenAICredentials, flow_run_name: str) -> str:
"""
Create a fun story about the flow run name.
"""
text_model = CompletionModel(
openai_credentials=credentials, model="text-curie-001", max_tokens=288
)
text_prompt = f"Provide a fun story about a {flow_run_name}"
story = text_model.submit_prompt(text_prompt).choices[0].text.strip()
return story
@task
def create_image_from_story(credentials: OpenAICredentials, story: str) -> str:
"""
Create an image associated with the story.
"""
image_model = ImageModel(openai_credentials=credentials, size="512x512")
image_result = image_model.submit_prompt(story)
image_url = image_result.data[0]["url"]
return image_url
@flow
def create_story_and_image_from_flow_run_name() -> str:
"""
Get the flow run name and create a story and image associated with it.
"""
context = get_run_context()
flow_run_name = context.flow_run.name.replace("-", " ")
credentials = OpenAICredentials.load("openai-credentials")
story = create_story_from_name(credentials=credentials, flow_run_name=flow_run_name)
image_url = create_image_from_story(credentials=credentials, story=story)
story_and_image = (
f"The story about a {flow_run_name}: '{story}' "
f"And its image: {image_url}"
)
print(story_and_image)
return story_and_image
create_story_and_image_from_flow_run_name()
```
### Saving an OpenAI key
It's easy to set up an `OpenAICredentials` block!
1. Head over to https://beta.openai.com/account/api-keys
2. Login to your OpenAI account
3. Click "+ Create new secret key"
4. Copy the generated API key
5. Create a short script, replacing the placeholders (or do so in the UI)
```python
from prefect_openai import OpenAICredentials`
OpenAICredentials(api_key="API_KEY_PLACEHOLDER").save("BLOCK_NAME_PLACEHOLDER")
```
Congrats! You can now easily load the saved block, which holds your OpenAI API key:
```python
from prefect_openai import OpenAICredentials
OpenAICredentials.load("BLOCK_NAME_PLACEHOLDER")
```
Visit [Flow Run Name Art](flow_run_name_art) to see some example output!
## Resources
For more tips on how to use tasks and flows in a Collection, check out [Using Collections](https://orion-docs.prefect.io/collections/usage/)!
### Installation
Install `prefect-openai` with `pip`:
```bash
pip install prefect-openai
```
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](https://orion-docs.prefect.io/).
### Feedback
If you encounter any bugs while using `prefect-openai`, feel free to open an issue in the [prefect-openai](https://github.com/PrefectHQ/prefect-openai) repository.
If you have any questions or issues while using `prefect-openai`, you can find help in either the [Prefect Discourse forum](https://discourse.prefect.io/) or the [Prefect Slack community](https://prefect.io/slack).
Feel free to star or watch [`prefect-openai`](https://github.com/PrefectHQ/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](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request-from-a-fork).
Here are the steps:
1. [Fork the repository](https://docs.github.com/en/get-started/quickstart/fork-a-repo#forking-a-repository)
2. [Clone the forked repository](https://docs.github.com/en/get-started/quickstart/fork-a-repo#cloning-your-forked-repository)
3. Install the repository and its dependencies:
```
pip install -e ".[dev]"
```
4. Make desired changes
5. Add tests
6. Insert an entry to [CHANGELOG.md](https://github.com/PrefectHQ/prefect-openai/blob/main/CHANGELOG.md)
7. Install `pre-commit` to perform quality checks prior to commit:
```
pre-commit install
```
8. `git commit`, `git push`, and create a pull request
Raw data
{
"_id": null,
"home_page": "https://github.com/PrefectHQ/prefect-openai",
"name": "prefect-openai",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "prefect",
"author": "Prefect Technologies, Inc.",
"author_email": "help@prefect.io",
"download_url": "https://files.pythonhosted.org/packages/7c/d7/7779e7fd28d79446e494909e5eba49ea148a23ba9cb968846281ebc31c90/prefect-openai-0.2.2.tar.gz",
"platform": null,
"description": "# Coordinate and use AI in your dataflow with `prefect-openai`\n\n<p align=\"center\">\n <img src=\"https://user-images.githubusercontent.com/15331990/213825004-eedb25b3-0520-4f55-95d3-3a3fc8b235ff.png\">\n <br>\n <a href=\"https://pypi.python.org/pypi/prefect-openai/\" alt=\"PyPI version\">\n <img alt=\"PyPI\" src=\"https://img.shields.io/pypi/v/prefect-openai?color=0052FF&labelColor=090422\"></a>\n <a href=\"https://github.com/PrefectHQ/prefect-openai/\" alt=\"Stars\">\n <img src=\"https://img.shields.io/github/stars/PrefectHQ/prefect-openai?color=0052FF&labelColor=090422\" /></a>\n <a href=\"https://pepy.tech/badge/prefect-openai/\" alt=\"Downloads\">\n <img src=\"https://img.shields.io/pypi/dm/prefect-openai?color=0052FF&labelColor=090422\" /></a>\n <a href=\"https://github.com/PrefectHQ/prefect-openai/pulse\" alt=\"Activity\">\n <img src=\"https://img.shields.io/github/commit-activity/m/PrefectHQ/prefect-openai?color=0052FF&labelColor=090422\" /></a>\n <br>\n <a href=\"https://prefect-community.slack.com\" alt=\"Slack\">\n <img src=\"https://img.shields.io/badge/slack-join_community-red.svg?color=0052FF&labelColor=090422&logo=slack\" /></a>\n <a href=\"https://discourse.prefect.io/\" alt=\"Discourse\">\n <img src=\"https://img.shields.io/badge/discourse-browse_forum-red.svg?color=0052FF&labelColor=090422&logo=discourse\" /></a>\n</p>\n\nVisit the full docs [here](https://PrefectHQ.github.io/prefect-openai) to see additional examples and the API reference.\n\nThe `prefect-openai` collection makes it easy to leverage the capabilities of AI in your flows. Check out the examples below to get started!\n\n## Getting Started\n\n### Summarize tracebacks with GPT3\n\nTracebacks--it's quintessential in programming. They are a record of every line of code leading to the error, to help us, humans, determine what's wrong with the program and find a solution.\n\nHowever, tracebacks can be extraordinarily complex, especially for someone new to the codebase.\n\nTo streamline this process, we could add AI to the mix, to offer a more human-readable summary of the issue, so it's easier for the developer to understand what went wrong and implement a fix.\n\nAfter installing `prefect-openai` and [saving an OpenAI key](#saving-an-openai-key), you can easily incorporate OpenAI within your flows to help you achieve the aforementioned benefits!\n\n```python\nfrom prefect import flow, get_run_logger\nfrom prefect_openai import OpenAICredentials, CompletionModel\n\n\n@flow\ndef summarize_traceback(traceback: str) -> str:\n logger = get_run_logger()\n openai_credentials = OpenAICredentials.load(\"openai-credentials\")\n completion_model = CompletionModel(\n openai_credentials=openai_credentials,\n model=\"text-curie-001\",\n max_tokens=512,\n )\n prompt = f\"Summarize cause of error from this traceback: ```{traceback}```\"\n summary = completion_model.submit_prompt(traceback).choices[0][\"text\"]\n logger.info(f\"Summary of the traceback: {summary}\")\n return summary\n\n\nif __name__ == \"__main__\":\n traceback = \"\"\"\n ParameterBindError: Error binding parameters for function 'summarize_traceback': missing a required argument: 'traceback'.\n Function 'summarize_traceback' has signature 'traceback: str) -> str' but received args: () and kwargs: {}.\n \"\"\"\n summarize_traceback(traceback)\n```\n\n```bash hl_lines=\"4\"\n...\n12:29:32.085 | INFO | Flow run 'analytic-starling' - Finished text completion using the 'text-curie-001' model with 113 tokens, creating 1 choice(s).\n12:29:32.089 | INFO | Flow run 'analytic-starling' - Summary of the traceback: \nThis error is caused by the missing argument traceback. The function expects a traceback object as its first argument, but received nothing.\n...\n```\nNotice how the original traceback was quite long and confusing.\n\nOn the flip side, the Curie GPT3 model was able to summarize the issue eloquently!\n\n!!! info \"Built-in decorator\"\n\n No need to build this yourself, `prefect-openai` features a\n [built-in decorator](completion/#prefect_openai.completion.interpret_exception)\n to help you automatically catch and interpret exceptions in flows, tasks, and even\n vanilla Python functions.\n\n ```python\n import httpx\n from prefect_openai.completion import interpret_exception\n\n @interpret_exception(\"COMPLETION_MODEL_BLOCK_NAME_PLACEHOLDER\")\n def example_func():\n resp = httpx.get(\"https://httpbin.org/status/403\")\n resp.raise_for_status()\n\n example_func()\n ```\n\n### Create a story around a flow run name with GPT3 and DALL-E\n\nHave you marveled at all the AI-generated images and wondered how others did it?\n\nAfter installing `prefect-openai` and [saving an OpenAI key](#saving-an-openai-key), you, too, can create AI-generated art.\n\nHere's an example on how to create a story and an image based off a flow run name.\n\n```python\nfrom prefect import flow, task, get_run_logger\nfrom prefect.context import get_run_context\nfrom prefect_openai import OpenAICredentials, ImageModel, CompletionModel\n\n\n@task\ndef create_story_from_name(credentials: OpenAICredentials, flow_run_name: str) -> str:\n \"\"\"\n Create a fun story about the flow run name.\n \"\"\"\n text_model = CompletionModel(\n openai_credentials=credentials, model=\"text-curie-001\", max_tokens=288\n )\n text_prompt = f\"Provide a fun story about a {flow_run_name}\"\n story = text_model.submit_prompt(text_prompt).choices[0].text.strip()\n return story\n\n\n@task\ndef create_image_from_story(credentials: OpenAICredentials, story: str) -> str:\n \"\"\"\n Create an image associated with the story.\n \"\"\"\n image_model = ImageModel(openai_credentials=credentials, size=\"512x512\")\n image_result = image_model.submit_prompt(story)\n image_url = image_result.data[0][\"url\"]\n return image_url\n\n\n@flow\ndef create_story_and_image_from_flow_run_name() -> str:\n \"\"\"\n Get the flow run name and create a story and image associated with it.\n \"\"\"\n context = get_run_context()\n flow_run_name = context.flow_run.name.replace(\"-\", \" \")\n\n credentials = OpenAICredentials.load(\"openai-credentials\")\n story = create_story_from_name(credentials=credentials, flow_run_name=flow_run_name)\n image_url = create_image_from_story(credentials=credentials, story=story)\n\n story_and_image = (\n f\"The story about a {flow_run_name}: '{story}' \"\n f\"And its image: {image_url}\"\n )\n print(story_and_image)\n return story_and_image\n\n\ncreate_story_and_image_from_flow_run_name()\n```\n\n### Saving an OpenAI key\n\nIt's easy to set up an `OpenAICredentials` block!\n\n1. Head over to https://beta.openai.com/account/api-keys\n2. Login to your OpenAI account\n3. Click \"+ Create new secret key\"\n4. Copy the generated API key\n5. Create a short script, replacing the placeholders (or do so in the UI)\n\n```python\nfrom prefect_openai import OpenAICredentials`\nOpenAICredentials(api_key=\"API_KEY_PLACEHOLDER\").save(\"BLOCK_NAME_PLACEHOLDER\")\n```\n\nCongrats! You can now easily load the saved block, which holds your OpenAI API key:\n\n```python\nfrom prefect_openai import OpenAICredentials\nOpenAICredentials.load(\"BLOCK_NAME_PLACEHOLDER\")\n```\n\nVisit [Flow Run Name Art](flow_run_name_art) to see some example output!\n\n## Resources\n\nFor more tips on how to use tasks and flows in a Collection, check out [Using Collections](https://orion-docs.prefect.io/collections/usage/)!\n\n### Installation\n\nInstall `prefect-openai` with `pip`:\n\n```bash\npip install prefect-openai\n```\n\nRequires an installation of Python 3.7+.\n\nWe recommend using a Python virtual environment manager such as pipenv, conda or virtualenv.\n\nThese tasks are designed to work with Prefect 2.0. For more information about how to use Prefect, please refer to the [Prefect documentation](https://orion-docs.prefect.io/).\n\n### Feedback\n\nIf you encounter any bugs while using `prefect-openai`, feel free to open an issue in the [prefect-openai](https://github.com/PrefectHQ/prefect-openai) repository.\n\nIf you have any questions or issues while using `prefect-openai`, you can find help in either the [Prefect Discourse forum](https://discourse.prefect.io/) or the [Prefect Slack community](https://prefect.io/slack).\n\nFeel free to star or watch [`prefect-openai`](https://github.com/PrefectHQ/prefect-openai) for updates too!\n\n### Contributing\n\nIf 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](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request-from-a-fork).\n\nHere are the steps:\n\n1. [Fork the repository](https://docs.github.com/en/get-started/quickstart/fork-a-repo#forking-a-repository)\n2. [Clone the forked repository](https://docs.github.com/en/get-started/quickstart/fork-a-repo#cloning-your-forked-repository)\n3. Install the repository and its dependencies:\n```\npip install -e \".[dev]\"\n```\n4. Make desired changes\n5. Add tests\n6. Insert an entry to [CHANGELOG.md](https://github.com/PrefectHQ/prefect-openai/blob/main/CHANGELOG.md)\n7. Install `pre-commit` to perform quality checks prior to commit:\n```\npre-commit install\n```\n8. `git commit`, `git push`, and create a pull request\n",
"bugtrack_url": null,
"license": "Apache License 2.0",
"summary": "Prefect integrations for working with OpenAI.",
"version": "0.2.2",
"project_urls": {
"Homepage": "https://github.com/PrefectHQ/prefect-openai"
},
"split_keywords": [
"prefect"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "1c385e85e7a2e3df70a549e2ef552911817177903bfc0dc2bffe2bb30477002d",
"md5": "124c385a0c86a231535b425325fdaf3f",
"sha256": "7feb0e80e7717b880b8dc363d4319bb07b6c3ed599548e702431cfabd8a8bcac"
},
"downloads": -1,
"filename": "prefect_openai-0.2.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "124c385a0c86a231535b425325fdaf3f",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 16480,
"upload_time": "2023-11-29T19:54:44",
"upload_time_iso_8601": "2023-11-29T19:54:44.888369Z",
"url": "https://files.pythonhosted.org/packages/1c/38/5e85e7a2e3df70a549e2ef552911817177903bfc0dc2bffe2bb30477002d/prefect_openai-0.2.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "7cd77779e7fd28d79446e494909e5eba49ea148a23ba9cb968846281ebc31c90",
"md5": "01f9d65de3c9e33f4e8fe5d61e3e7091",
"sha256": "ded7492c362aaaf17ddc92254f2dc53b6c8215018d246eb2aac22443259e4918"
},
"downloads": -1,
"filename": "prefect-openai-0.2.2.tar.gz",
"has_sig": false,
"md5_digest": "01f9d65de3c9e33f4e8fe5d61e3e7091",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 36888,
"upload_time": "2023-11-29T19:54:46",
"upload_time_iso_8601": "2023-11-29T19:54:46.545041Z",
"url": "https://files.pythonhosted.org/packages/7c/d7/7779e7fd28d79446e494909e5eba49ea148a23ba9cb968846281ebc31c90/prefect-openai-0.2.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-11-29 19:54:46",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "PrefectHQ",
"github_project": "prefect-openai",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "prefect-openai"
}