|
| 1 | +import time |
| 2 | +import uuid |
| 3 | +from dataclasses import dataclass |
| 4 | +from datetime import date |
| 5 | + |
| 6 | +from flux0_core.agent_runners.api import Agent as FAgent |
| 7 | +from flux0_core.agent_runners.api import AgentRunner, Deps, agent_runner |
| 8 | +from flux0_core.agent_runners.context import Context |
| 9 | +from flux0_core.sessions import ( |
| 10 | + EventId, |
| 11 | + StatusEventData, |
| 12 | +) |
| 13 | +from flux0_stream.emitter.utils.events import send_processing_event |
| 14 | +from flux0_stream.types import ChunkEvent |
| 15 | +from pydantic_ai import Agent |
| 16 | +from pydantic_ai.messages import ( |
| 17 | + FinalResultEvent, |
| 18 | + FunctionToolCallEvent, |
| 19 | + FunctionToolResultEvent, |
| 20 | + PartDeltaEvent, |
| 21 | + PartStartEvent, |
| 22 | + TextPartDelta, |
| 23 | + ToolCallPartDelta, |
| 24 | +) |
| 25 | +from pydantic_ai.tools import RunContext |
| 26 | + |
| 27 | +from examples.utils.utils import read_user_input |
| 28 | + |
| 29 | + |
| 30 | +@dataclass |
| 31 | +class WeatherService: |
| 32 | + async def get_forecast(self, location: str, forecast_date: date) -> str: |
| 33 | + # In real code: call weather API, DB queries, etc. |
| 34 | + return f"The forecast in {location} on {forecast_date} is 24°C and sunny." |
| 35 | + |
| 36 | + async def get_historic_weather(self, location: str, forecast_date: date) -> str: |
| 37 | + # In real code: call a historical weather API or DB |
| 38 | + return f"The weather in {location} on {forecast_date} was 18°C and partly cloudy." |
| 39 | + |
| 40 | + |
| 41 | +weather_agent = Agent[WeatherService, str]( |
| 42 | + "openai:gpt-4.1-nano", |
| 43 | + deps_type=WeatherService, |
| 44 | + output_type=str, |
| 45 | + system_prompt="Providing a weather forecast at the locations the user provides.", |
| 46 | +) |
| 47 | + |
| 48 | + |
| 49 | +@weather_agent.tool |
| 50 | +async def weather_forecast( |
| 51 | + ctx: RunContext[WeatherService], |
| 52 | + location: str, |
| 53 | + forecast_date: date, |
| 54 | +) -> str: |
| 55 | + if forecast_date >= date.today(): |
| 56 | + return await ctx.deps.get_forecast(location, forecast_date) |
| 57 | + else: |
| 58 | + return await ctx.deps.get_historic_weather(location, forecast_date) |
| 59 | + |
| 60 | + |
| 61 | +async def send_message( |
| 62 | + deps: Deps, |
| 63 | + input: str, |
| 64 | + agent: FAgent, |
| 65 | + event_id: EventId, |
| 66 | +): |
| 67 | + await send_processing_event(deps, "thinking...") |
| 68 | + # Begin a node-by-node, streaming iteration |
| 69 | + async with weather_agent.iter(input, deps=WeatherService()) as run: |
| 70 | + async for node in run: |
| 71 | + if Agent.is_model_request_node(node): |
| 72 | + # A model request node => We can stream tokens from the model's request |
| 73 | + async with node.stream(run.ctx) as request_stream: |
| 74 | + async for event in request_stream: |
| 75 | + if isinstance(event, PartStartEvent): |
| 76 | + pass |
| 77 | + elif isinstance(event, PartDeltaEvent): |
| 78 | + if isinstance(event.delta, TextPartDelta): |
| 79 | + pass |
| 80 | + elif isinstance(event.delta, ToolCallPartDelta): |
| 81 | + pass |
| 82 | + elif isinstance(event, FinalResultEvent): |
| 83 | + pass |
| 84 | + elif Agent.is_call_tools_node(node): |
| 85 | + # A handle-response node => The model returned some data, potentially calls a tool |
| 86 | + async with node.stream(run.ctx) as handle_stream: |
| 87 | + async for event in handle_stream: |
| 88 | + if isinstance(event, FunctionToolCallEvent): |
| 89 | + value = f"[Tools] The LLM calls tool={event.part.tool_name!r} with args={event.part.args}" |
| 90 | + await send_processing_event(deps, value) |
| 91 | + elif isinstance(event, FunctionToolResultEvent): |
| 92 | + value = f"[Tools] Tool call {event.tool_call_id!r} returned => {event.result.content}" |
| 93 | + await send_processing_event(deps, value) |
| 94 | + elif Agent.is_end_node(node): |
| 95 | + assert run.result.output == node.data.output # type: ignore |
| 96 | + # Once an End node is reached, the agent run is complete |
| 97 | + await deps.event_emitter.enqueue_status_event( |
| 98 | + correlation_id=deps.correlator.correlation_id, |
| 99 | + data=StatusEventData(type="status", status="typing"), |
| 100 | + ) |
| 101 | + cec = ChunkEvent( |
| 102 | + correlation_id=deps.correlator.correlation_id, |
| 103 | + seq=0, |
| 104 | + event_id=event_id, |
| 105 | + patches=[ |
| 106 | + { |
| 107 | + "op": "add", |
| 108 | + "path": "/-", |
| 109 | + "value": run.result.output, # type: ignore |
| 110 | + } |
| 111 | + ], |
| 112 | + metadata={ |
| 113 | + "agent_id": agent.id, |
| 114 | + "agent_name": agent.name, |
| 115 | + }, |
| 116 | + timestamp=time.time(), |
| 117 | + ) |
| 118 | + await deps.event_emitter.enqueue_event_chunk(cec) |
| 119 | + await deps.event_emitter.enqueue_status_event( |
| 120 | + correlation_id=deps.correlator.correlation_id, |
| 121 | + data=StatusEventData(type="status", status="ready"), |
| 122 | + event_id=event_id, |
| 123 | + ) |
| 124 | + |
| 125 | + |
| 126 | +@agent_runner("pydantic_weather_agent") |
| 127 | +class WeatherAgentRunner(AgentRunner): |
| 128 | + async def run(self, context: Context, deps: Deps) -> bool: |
| 129 | + try: |
| 130 | + # read the agent object |
| 131 | + agent = await deps.read_agent(context.agent_id) |
| 132 | + if not agent: |
| 133 | + raise ValueError(f"Agent with id {context.agent_id} not found") |
| 134 | + |
| 135 | + # read session events and expect the last event to be the user input |
| 136 | + user_input = await read_user_input(deps, context) |
| 137 | + if not user_input: |
| 138 | + raise ValueError("No user input found in session events") |
| 139 | + |
| 140 | + deps.logger.info( |
| 141 | + f"User Input: {user_input} for session {context.session_id} and agent {agent.id}" |
| 142 | + ) |
| 143 | + |
| 144 | + await send_message( |
| 145 | + deps=deps, |
| 146 | + input=user_input, |
| 147 | + agent=agent, |
| 148 | + event_id=EventId(uuid.uuid4().hex), |
| 149 | + ) |
| 150 | + |
| 151 | + return True |
| 152 | + except Exception as e: |
| 153 | + await deps.event_emitter.enqueue_status_event( |
| 154 | + correlation_id=deps.correlator.correlation_id, |
| 155 | + data=StatusEventData(type="status", status="error", data=str(e)), |
| 156 | + ) |
| 157 | + |
| 158 | + return False |
| 159 | + finally: |
| 160 | + await deps.event_emitter.enqueue_status_event( |
| 161 | + correlation_id=deps.correlator.correlation_id, |
| 162 | + data=StatusEventData(type="status", status="completed"), |
| 163 | + ) |
0 commit comments