-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.py
63 lines (48 loc) · 1.66 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Copyright 2025 GlyphyAI
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.
import asyncio
from pydantic import BaseModel
from liteswarm.core import Swarm
from liteswarm.types import LLM, Agent, Message
class MathResult(BaseModel):
result: int
thoughts: str
async def run() -> None:
agent = Agent(
id="math_expert",
instructions="You are a math expert.",
llm=LLM(
model="gpt-4o",
response_format=MathResult,
),
output_type=MathResult,
)
swarm = Swarm()
# Method 1: Use streaming API to get structured outputs
stream = swarm.stream(
agent=agent,
messages=[Message(role="user", content="What is 2 + 2 * 2?")],
final_output_type=MathResult,
)
# Get streaming partial parsed responses
async for event in stream:
if event.type == "agent_response_chunk":
if event.chunk.parsed:
print(event.chunk.parsed)
# Get final parsed response
result = await stream.get_return_value()
if result.final_response.output:
print(result.final_response.output.model_dump_json(indent=2, exclude_none=True))
# # Method 2: Use convenience method to get final parsed response
# result = await swarm.run(
# agent,
# messages=[Message(role="user", content="What is 2 + 2 * 2?")],
# final_output_type=MathResult,
# )
# if result.final_response.output:
# print(result.final_response.output.model_dump_json(indent=2, exclude_none=True))
if __name__ == "__main__":
asyncio.run(run())