-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
deploy example and fix typing #382
Open
zzstoatzz
wants to merge
9
commits into
main
Choose a base branch
from
example-deploy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b5bc9dd
deploy example and fix typing
zzstoatzz be41a7c
update example
zzstoatzz 041efb0
clarify
zzstoatzz 6e5a43d
add docker example
zzstoatzz 85fa4fd
make 3.9 happy
zzstoatzz 329cf8e
make friendly to any user
zzstoatzz ce25892
spiffy
zzstoatzz ca314f8
Merge branch 'main' into example-deploy
zzstoatzz 13600a9
Merge branch 'main' into example-deploy
jlowin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim | ||
|
||
RUN apt-get update && apt-get install -y git | ||
|
||
RUN rm -rf /var/lib/apt/lists/* | ||
|
||
WORKDIR /app | ||
|
||
ENV UV_SYSTEM_PYTHON=1 | ||
|
||
RUN uv pip install controlflow | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# /// script | ||
# dependencies = ["controlflow"] | ||
# /// | ||
|
||
import os | ||
import sys | ||
from pathlib import Path | ||
from typing import Annotated, TypedDict | ||
|
||
import httpx | ||
from prefect import flow, task | ||
from prefect.artifacts import create_markdown_artifact | ||
from prefect.docker import DockerImage | ||
from prefect.runner.storage import GitCredentials, GitRepository | ||
from pydantic import AnyHttpUrl, Field, TypeAdapter | ||
|
||
import controlflow as cf | ||
|
||
|
||
class HNArticleSummary(TypedDict): | ||
link: AnyHttpUrl | ||
title: str | ||
main_topics: Annotated[set[str], Field(min_length=1, max_length=5)] | ||
key_takeaways: Annotated[set[str], Field(min_length=1, max_length=5)] | ||
tech_domains: Annotated[set[str], Field(min_length=1, max_length=5)] | ||
|
||
|
||
@cf.task(instructions="concise, main details") | ||
def analyze_article(id: str) -> HNArticleSummary: | ||
"""Analyze a HackerNews article and return structured insights""" | ||
content = httpx.get(f"https://hacker-news.firebaseio.com/v0/item/{id}.json").json() | ||
return f"here is the article content: {content}" # type: ignore | ||
|
||
|
||
@flow(retries=2) | ||
def analyze_hn_articles(n: int = 5) -> list[HNArticleSummary]: | ||
top_article_ids = httpx.get( | ||
"https://hacker-news.firebaseio.com/v0/topstories.json" | ||
).json()[:n] | ||
briefs = analyze_article.map(top_article_ids).result() | ||
create_markdown_artifact( | ||
key="hn-article-exec-summary", | ||
markdown=task(task_run_name=f"make summary of {len(briefs)} articles")(cf.run)( | ||
objective="markdown summary of all extracted article briefs", | ||
result_type=Annotated[str, Field(description="markdown summary")], | ||
context=dict(briefs=briefs), | ||
), | ||
description="executive summary of all extracted article briefs", | ||
) | ||
return briefs | ||
|
||
|
||
if __name__ == "__main__": | ||
EVERY_12_HOURS_CRON = "0 */12 * * *" | ||
if len(sys.argv) > 1 and sys.argv[1] == "serve": | ||
analyze_hn_articles.serve( | ||
parameters={"n": 5}, | ||
cron=EVERY_12_HOURS_CRON, | ||
) | ||
elif len(sys.argv) > 1 and sys.argv[1] == "local_deploy": | ||
analyze_hn_articles.from_source( | ||
source=str((p := Path(__file__)).parent.resolve()), | ||
entrypoint=f"{p.name}:analyze_hn_articles", | ||
).deploy( | ||
name="local-deployment", | ||
work_pool_name="local", | ||
cron=EVERY_12_HOURS_CRON, | ||
) | ||
elif len(sys.argv) > 1 and sys.argv[1] == "docker_deploy": | ||
repo = GitRepository( | ||
url="https://github.com/PrefectHQ/controlflow.git", | ||
branch="main", | ||
credentials=None, # replace with `dict(username="", access_token="")` for private repos | ||
) | ||
analyze_hn_articles.from_source( | ||
source=repo, | ||
entrypoint="examples/read_hn.py:analyze_articles", | ||
).deploy( | ||
name="docker-deployment", | ||
# image=DockerImage( # uncomment and replace with your own image if desired | ||
# name="zzstoatzz/cf-read-hn", | ||
# tag="latest", | ||
# dockerfile=str(Path(__file__).parent.resolve() / "read-hn.Dockerfile"), | ||
# ), | ||
work_pool_name="docker-work", # uv pip install -U prefect-docker prefect worker start --pool docker-work --type docker | ||
cron=EVERY_12_HOURS_CRON, | ||
parameters={"n": 5}, | ||
job_variables={ | ||
"env": {"OPENAI_API_KEY": os.getenv("OPENAI_API_KEY")}, | ||
"image": "zzstoatzz/cf-read-hn:latest", # publicly available image on dockerhub | ||
}, | ||
build=False, | ||
push=False, | ||
) | ||
else: | ||
print(f"just running the code\n\n\n\n\n\n") | ||
briefs = analyze_hn_articles(5) # type: ignore | ||
TypeAdapter(list[HNArticleSummary]).validate_python(briefs) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
like this