Skip to content

Commit e18db07

Browse files
author
Pierre
committed
refactor: update examples to use .run() method consistently
1 parent b737c85 commit e18db07

11 files changed

+41
-44
lines changed

examples/03_caching.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from pydantic import BaseModel, Field
1818

1919
import workflowai
20-
from workflowai import Model, Run
20+
from workflowai import Model
2121

2222
# Import CacheUsage type
2323
CacheUsage = Literal["auto", "always", "never"]
@@ -54,7 +54,7 @@ class SOAPNote(BaseModel):
5454
id="soap-extractor",
5555
model=Model.LLAMA_3_3_70B,
5656
)
57-
async def extract_soap_notes(soap_input: SOAPInput) -> Run[SOAPNote]:
57+
async def extract_soap_notes(soap_input: SOAPInput) -> SOAPNote:
5858
"""
5959
Extract SOAP notes from a medical consultation transcript.
6060
@@ -91,7 +91,7 @@ async def demonstrate_caching(transcript: str):
9191
for cache_option in cache_options:
9292
start_time = time.time()
9393

94-
run = await extract_soap_notes(
94+
run = await extract_soap_notes.run(
9595
SOAPInput(transcript=transcript),
9696
use_cache=cache_option,
9797
)

examples/04_audio_classifier_agent.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from pydantic import BaseModel, Field # pyright: ignore [reportUnknownVariableType]
1414

1515
import workflowai
16-
from workflowai import Model, Run
16+
from workflowai import Model
1717
from workflowai.fields import Audio
1818

1919

@@ -67,7 +67,7 @@ class AudioClassification(BaseModel):
6767
id="audio-spam-detector",
6868
model=Model.GEMINI_1_5_FLASH_LATEST,
6969
)
70-
async def classify_audio(audio_input: AudioInput) -> Run[AudioClassification]:
70+
async def classify_audio(audio_input: AudioInput) -> AudioClassification:
7171
"""
7272
Analyze the audio recording to determine if it's a spam/robocall.
7373
@@ -119,7 +119,7 @@ async def main():
119119
# )
120120

121121
# Classify the audio
122-
run = await classify_audio(AudioInput(audio=audio))
122+
run = await classify_audio.run(AudioInput(audio=audio))
123123

124124
# Print results including cost and latency information
125125
print(run)

examples/05_browser_text_uptime_agent.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from pydantic import BaseModel, Field # pyright: ignore [reportUnknownVariableType]
1212

1313
import workflowai
14-
from workflowai import Model, Run
14+
from workflowai import Model
1515

1616

1717
class UptimeInput(BaseModel):
@@ -40,7 +40,7 @@ class UptimeOutput(BaseModel):
4040
id="uptime-checker",
4141
model=Model.GPT_4O_MINI_LATEST,
4242
)
43-
async def check_uptime(uptime_input: UptimeInput, use_cache: str = "never") -> Run[UptimeOutput]:
43+
async def check_uptime(uptime_input: UptimeInput, use_cache: str = "never") -> UptimeOutput:
4444
"""
4545
Fetch and analyze uptime data from an API status page.
4646
Use @browser-text to get the page content.
@@ -68,7 +68,7 @@ async def main():
6868
print("-" * 50)
6969

7070
# Get uptime data with caching disabled
71-
run = await check_uptime(uptime_input, use_cache="never")
71+
run = await check_uptime.run(uptime_input, use_cache="never")
7272

7373
# Print the run
7474
print(run)

examples/06_streaming_summary.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
"""
77

88
import asyncio
9-
from collections.abc import AsyncIterator
109

1110
from pydantic import BaseModel, Field # pyright: ignore [reportUnknownVariableType]
1211

1312
import workflowai
14-
from workflowai import Model, Run
13+
from workflowai import Model
1514

1615

1716
class TranslationInput(BaseModel):
@@ -30,7 +29,7 @@ class TranslationOutput(BaseModel):
3029

3130

3231
@workflowai.agent(id="french-translator", model=Model.CLAUDE_3_5_SONNET_LATEST)
33-
def translate_to_english(_: TranslationInput) -> AsyncIterator[Run[TranslationOutput]]:
32+
def translate_to_english(_: TranslationInput) -> TranslationOutput:
3433
"""
3534
Translate French text into natural, fluent English.
3635
@@ -74,7 +73,7 @@ async def main():
7473
# This ensures we can see the streaming effect in the example
7574
# Otherwise, subsequent runs would return the cached result instantly,
7675
# making it hard to observe the incremental streaming behavior
77-
async for chunk in translate_to_english(TranslationInput(text=french_text), use_cache="never"):
76+
async for chunk in translate_to_english.stream(TranslationInput(text=french_text), use_cache="never"):
7877
print(f"--- Translation Progress (Chunk {chunk_num}) ---")
7978
print(chunk.output.translation)
8079
print("-" * 50)

examples/08_pdf_agent.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from pydantic import BaseModel, Field # pyright: ignore [reportUnknownVariableType]
66

77
import workflowai
8-
from workflowai import Run, WorkflowAIError
8+
from workflowai import WorkflowAIError
99
from workflowai.core.domain.model import Model
1010
from workflowai.fields import PDF
1111

@@ -21,7 +21,7 @@ class PDFAnswerOutput(BaseModel):
2121

2222

2323
@workflowai.agent(id="pdf-answer", model=Model.CLAUDE_3_5_SONNET_LATEST)
24-
async def answer_pdf_question(_: PDFQuestionInput) -> Run[PDFAnswerOutput]:
24+
async def answer_pdf_question(_: PDFQuestionInput) -> PDFAnswerOutput:
2525
"""
2626
Analyze the provided PDF document and answer the given question.
2727
Provide a clear and concise answer based on the content found in the PDF.
@@ -55,17 +55,15 @@ async def run_pdf_answer():
5555
question = "How many stocks were sold? What is the total amount in USD?"
5656

5757
try:
58-
agent_run = await answer_pdf_question(
58+
run = await answer_pdf_question.run(
5959
PDFQuestionInput(pdf=pdf, question=question),
6060
use_cache="auto",
6161
)
6262
except WorkflowAIError as e:
6363
print(f"Failed to run task. Code: {e.error.code}. Message: {e.error.message}")
6464
return
6565

66-
print("\n--------\nAgent output:\n", agent_run.output, "\n--------\n")
67-
print(f"Cost: ${agent_run.cost_usd:.10f}")
68-
print(f"Latency: {agent_run.duration_seconds:.2f}s")
66+
print(run)
6967

7068

7169
if __name__ == "__main__":

examples/09_reply.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from pydantic import BaseModel, Field # pyright: ignore [reportUnknownVariableType]
2121

2222
import workflowai
23-
from workflowai import Model, Run
23+
from workflowai import Model
2424

2525

2626
class NameExtractionInput(BaseModel):
@@ -43,7 +43,7 @@ class NameExtractionOutput(BaseModel):
4343

4444

4545
@workflowai.agent(id="name-extractor", model=Model.GPT_4O_MINI_LATEST)
46-
async def extract_name(_: NameExtractionInput) -> Run[NameExtractionOutput]:
46+
async def extract_name(_: NameExtractionInput) -> NameExtractionOutput:
4747
"""
4848
Extract a person's first and last name from a sentence.
4949
Be precise and consider cultural variations in name formats.
@@ -64,7 +64,7 @@ async def main():
6464
print(f"\nProcessing: {sentence}")
6565

6666
# Initial extraction
67-
run = await extract_name(NameExtractionInput(sentence=sentence))
67+
run = await extract_name.run(NameExtractionInput(sentence=sentence))
6868

6969
print(f"Extracted: {run.output.first_name} {run.output.last_name}")
7070

examples/10_calendar_event_extraction.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from pydantic import BaseModel, Field
1818

1919
import workflowai
20-
from workflowai import Model, Run
20+
from workflowai import Model
2121
from workflowai.fields import File
2222

2323

@@ -79,7 +79,7 @@ class CalendarEventOutput(BaseModel):
7979
id="calendar-event-extractor",
8080
model=Model.GPT_4O_MINI_LATEST,
8181
)
82-
async def extract_calendar_event_from_email(email_input: EmailInput) -> Run[CalendarEventOutput]:
82+
async def extract_calendar_event_from_email(email_input: EmailInput) -> CalendarEventOutput:
8383
"""
8484
Extract calendar event details from email content.
8585
@@ -112,7 +112,7 @@ async def extract_calendar_event_from_email(email_input: EmailInput) -> Run[Cale
112112
id="calendar-event-extractor",
113113
model=Model.GPT_4O_MINI_LATEST,
114114
)
115-
async def extract_calendar_event_from_image(image_input: ImageInput) -> Run[CalendarEventOutput]:
115+
async def extract_calendar_event_from_image(image_input: ImageInput) -> CalendarEventOutput:
116116
"""
117117
Extract calendar event details from an event poster or flyer image.
118118
@@ -160,7 +160,7 @@ async def main():
160160
""",
161161
)
162162

163-
run = await extract_calendar_event_from_email(email1)
163+
run = await extract_calendar_event_from_email.run(email1)
164164
print(run)
165165

166166
# Example 2: Virtual meeting with more details
@@ -238,7 +238,7 @@ async def main():
238238
)
239239

240240
try:
241-
run = await extract_calendar_event_from_email(email4)
241+
run = await extract_calendar_event_from_email.run(email4)
242242
print(run)
243243
except workflowai.WorkflowAIError as e:
244244
print(f"As expected, no calendar event found: {e!s}")

examples/12_contextual_retrieval.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pydantic import BaseModel, Field
1010

1111
import workflowai
12-
from workflowai import Model, Run
12+
from workflowai import Model
1313

1414

1515
class ContextGeneratorInput(BaseModel):
@@ -37,7 +37,7 @@ class ContextGeneratorOutput(BaseModel):
3737
id="context-generator",
3838
model=Model.CLAUDE_3_5_SONNET_LATEST,
3939
)
40-
async def generate_chunk_context(context_input: ContextGeneratorInput) -> Run[ContextGeneratorOutput]:
40+
async def generate_chunk_context(context_input: ContextGeneratorInput) -> ContextGeneratorOutput:
4141
"""
4242
Here is the chunk we want to situate within the whole document.
4343
Please give a short succinct context to situate this chunk within the overall document
@@ -80,9 +80,9 @@ async def main():
8080
chunk_content=chunk_content,
8181
)
8282

83-
run = await generate_chunk_context(context_input)
83+
run = await generate_chunk_context.run(context_input)
8484
print("\nGenerated Context:")
85-
print(run.output.context)
85+
print(run)
8686

8787

8888
if __name__ == "__main__":

examples/14_templated_instructions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from pydantic import BaseModel, Field
2424

2525
import workflowai
26-
from workflowai import Model, Run
26+
from workflowai import Model
2727

2828

2929
class CodeReviewInput(BaseModel):
@@ -77,7 +77,7 @@ class CodeReviewOutput(BaseModel):
7777
id="templated-code-reviewer",
7878
model=Model.CLAUDE_3_5_SONNET_LATEST,
7979
)
80-
async def review_code(review_input: CodeReviewInput) -> Run[CodeReviewOutput]:
80+
async def review_code(review_input: CodeReviewInput) -> CodeReviewOutput:
8181
"""
8282
Review code based on specified parameters and guidelines.
8383
@@ -142,7 +142,7 @@ def calculate_sum(numbers):
142142
return result
143143
"""
144144

145-
run = await review_code(
145+
run = await review_code.run(
146146
CodeReviewInput(
147147
language="python",
148148
code=python_code,

examples/15_text_to_sql.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from pydantic import BaseModel, Field
1818

1919
import workflowai
20-
from workflowai import Model, Run
20+
from workflowai import Model
2121

2222

2323
class SQLGenerationInput(BaseModel):
@@ -49,7 +49,7 @@ class SQLGenerationOutput(BaseModel):
4949
id="text-to-sql",
5050
model=Model.CLAUDE_3_5_SONNET_LATEST,
5151
)
52-
async def generate_sql(review_input: SQLGenerationInput) -> Run[SQLGenerationOutput]:
52+
async def generate_sql(review_input: SQLGenerationInput) -> SQLGenerationOutput:
5353
"""
5454
Convert natural language questions to SQL queries based on the provided schema.
5555
@@ -121,7 +121,7 @@ async def main():
121121
# Example 1: Simple SELECT with conditions
122122
print("\nExample 1: Find expensive products")
123123
print("-" * 50)
124-
run = await generate_sql(
124+
run = await generate_sql.run(
125125
SQLGenerationInput(
126126
db_schema=schema,
127127
question="Show me all products that cost more than $100, ordered by price descending",
@@ -132,7 +132,7 @@ async def main():
132132
# Example 2: JOIN with aggregation
133133
print("\nExample 2: Customer order summary")
134134
print("-" * 50)
135-
run = await generate_sql(
135+
run = await generate_sql.run(
136136
SQLGenerationInput(
137137
db_schema=schema,
138138
question=(
@@ -146,7 +146,7 @@ async def main():
146146
# Example 3: Complex query
147147
print("\nExample 3: Product category analysis")
148148
print("-" * 50)
149-
run = await generate_sql(
149+
run = await generate_sql.run(
150150
SQLGenerationInput(
151151
db_schema=schema,
152152
question=(

0 commit comments

Comments
 (0)