Skip to content

Commit

Permalink
spiffy
Browse files Browse the repository at this point in the history
doesnt need to be a cf flow

wrap it in task
  • Loading branch information
zzstoatzz committed Nov 13, 2024
1 parent 329cf8e commit 6aa3b39
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions examples/read_hn.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
from typing import Annotated, TypedDict

import httpx
from prefect import flow, task
from prefect.artifacts import create_markdown_artifact
from prefect.blocks.system import Secret
from prefect.docker import DockerImage
from prefect.runner.storage import GitCredentials, GitRepository
from pydantic import AnyHttpUrl, Field
from pydantic import AnyHttpUrl, Field, TypeAdapter

import controlflow as cf

Expand All @@ -32,24 +32,22 @@ def analyze_article(id: str) -> HNArticleSummary:
return f"here is the article content: {content}" # type: ignore


@cf.task()
def summarize_article_briefs(
briefs: list[HNArticleSummary],
) -> Annotated[str, Field(description="markdown summary")]:
"""Summarize a list of article briefs"""
return f"here are the article briefs: {briefs}" # type: ignore


@cf.flow(retries=2)
def analyze_hn_articles(n: int = 5):
@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(
task(create_markdown_artifact)(
key="hn-article-exec-summary",
markdown=summarize_article_briefs(briefs),
markdown=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__":
Expand Down Expand Up @@ -96,4 +94,5 @@ def analyze_hn_articles(n: int = 5):
)
else:
print(f"just running the code\n\n\n\n\n\n")
analyze_hn_articles(5)
briefs = analyze_hn_articles(5) # type: ignore
TypeAdapter(list[HNArticleSummary]).validate_python(briefs)

0 comments on commit 6aa3b39

Please sign in to comment.