|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +import asyncio |
| 4 | + |
| 5 | +from semantic_kernel.agents import Agent, ChatCompletionAgent, SequentialOrchestration |
| 6 | +from semantic_kernel.agents.runtime import InProcessRuntime |
| 7 | +from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion |
| 8 | +from semantic_kernel.contents.streaming_chat_message_content import StreamingChatMessageContent |
| 9 | + |
| 10 | +""" |
| 11 | +The following sample demonstrates how to create a sequential orchestration for |
| 12 | +executing multiple agents in sequence, i.e. the output of one agent is the input |
| 13 | +to the next agent. |
| 14 | +
|
| 15 | +This sample demonstrates the basic steps of creating and starting a runtime, creating |
| 16 | +a sequential orchestration, invoking the orchestration, and finally waiting for the |
| 17 | +results. |
| 18 | +""" |
| 19 | + |
| 20 | + |
| 21 | +def get_agents() -> list[Agent]: |
| 22 | + """Return a list of agents that will participate in the sequential orchestration. |
| 23 | +
|
| 24 | + Feel free to add or remove agents. |
| 25 | + """ |
| 26 | + concept_extractor_agent = ChatCompletionAgent( |
| 27 | + name="ConceptExtractorAgent", |
| 28 | + instructions=( |
| 29 | + "You are a marketing analyst. Given a product description, identify:\n" |
| 30 | + "- Key features\n" |
| 31 | + "- Target audience\n" |
| 32 | + "- Unique selling points\n\n" |
| 33 | + ), |
| 34 | + service=AzureChatCompletion(), |
| 35 | + ) |
| 36 | + writer_agent = ChatCompletionAgent( |
| 37 | + name="WriterAgent", |
| 38 | + instructions=( |
| 39 | + "You are a marketing copywriter. Given a block of text describing features, audience, and USPs, " |
| 40 | + "compose a compelling marketing copy (like a newsletter section) that highlights these points. " |
| 41 | + "Output should be short (around 150 words), output just the copy as a single text block." |
| 42 | + ), |
| 43 | + service=AzureChatCompletion(), |
| 44 | + ) |
| 45 | + format_proof_agent = ChatCompletionAgent( |
| 46 | + name="FormatProofAgent", |
| 47 | + instructions=( |
| 48 | + "You are an editor. Given the draft copy, correct grammar, improve clarity, ensure consistent tone, " |
| 49 | + "give format and make it polished. Output the final improved copy as a single text block." |
| 50 | + ), |
| 51 | + service=AzureChatCompletion(), |
| 52 | + ) |
| 53 | + |
| 54 | + # The order of the agents in the list will be the order in which they are executed |
| 55 | + return [concept_extractor_agent, writer_agent, format_proof_agent] |
| 56 | + |
| 57 | + |
| 58 | +# Flag to indicate if a new message is being received |
| 59 | +is_new_message = True |
| 60 | + |
| 61 | + |
| 62 | +def streaming_agent_response_callback(message: StreamingChatMessageContent, is_final: bool) -> None: |
| 63 | + """Observer function to print the messages from the agents. |
| 64 | +
|
| 65 | + Args: |
| 66 | + message (StreamingChatMessageContent): The streaming message content from the agent. |
| 67 | + is_final (bool): Indicates if this is the final part of the message. |
| 68 | + """ |
| 69 | + global is_new_message |
| 70 | + if is_new_message: |
| 71 | + print(f"# {message.name}") |
| 72 | + is_new_message = False |
| 73 | + print(message.content, end="", flush=True) |
| 74 | + if is_final: |
| 75 | + print() |
| 76 | + is_new_message = True |
| 77 | + |
| 78 | + |
| 79 | +async def main(): |
| 80 | + """Main function to run the agents.""" |
| 81 | + # 1. Create a sequential orchestration with multiple agents and an agent |
| 82 | + # response callback to observe the output from each agent as they stream |
| 83 | + # their responses. |
| 84 | + agents = get_agents() |
| 85 | + sequential_orchestration = SequentialOrchestration( |
| 86 | + members=agents, |
| 87 | + streaming_agent_response_callback=streaming_agent_response_callback, |
| 88 | + ) |
| 89 | + |
| 90 | + # 2. Create a runtime and start it |
| 91 | + runtime = InProcessRuntime() |
| 92 | + runtime.start() |
| 93 | + |
| 94 | + # 3. Invoke the orchestration with a task and the runtime |
| 95 | + orchestration_result = await sequential_orchestration.invoke( |
| 96 | + task="An eco-friendly stainless steel water bottle that keeps drinks cold for 24 hours", |
| 97 | + runtime=runtime, |
| 98 | + ) |
| 99 | + |
| 100 | + # 4. Wait for the results |
| 101 | + value = await orchestration_result.get(timeout=20) |
| 102 | + print(f"***** Final Result *****\n{value}") |
| 103 | + |
| 104 | + # 5. Stop the runtime when idle |
| 105 | + await runtime.stop_when_idle() |
| 106 | + |
| 107 | + """ |
| 108 | + Sample output: |
| 109 | + # ConceptExtractorAgent |
| 110 | + **Key Features:** |
| 111 | + - Made from eco-friendly stainless steel |
| 112 | + - Insulation technology that keeps drinks cold for up to 24 hours |
| 113 | + - Reusable design, promoting sustainability |
| 114 | + - Possible variations in sizes and colors |
| 115 | +
|
| 116 | + **Target Audience:** |
| 117 | + - Environmentally conscious consumers |
| 118 | + - Active individuals and outdoor enthusiasts |
| 119 | + - Health-conscious individuals looking to stay hydrated |
| 120 | + - Students and professionals looking for stylish and functional drinkware |
| 121 | +
|
| 122 | + **Unique Selling Points:** |
| 123 | + - Combines eco-friendliness with high performance in temperature retention |
| 124 | + - Durable and reusable, reducing reliance on single-use plastics |
| 125 | + - Sleek design that appeals to modern aesthetics while being functional |
| 126 | + - Supporting sustainability initiatives through responsible manufacturing practices |
| 127 | + # WriterAgent |
| 128 | + Sip sustainably with our eco-friendly stainless steel water bottles, designed for the conscious consumer who values |
| 129 | + both performance and aesthetics. Our bottles feature advanced insulation technology that keeps your drinks icy cold |
| 130 | + for up to 24 hours, making them perfect for outdoor adventures, gym sessions, or a busy day at the office. Choose |
| 131 | + from various sizes and stunning colors to match your personal style while making a positive impact on the planet. |
| 132 | + Each reusable bottle helps reduce single-use plastics, supporting a cleaner, greener world. Join the movement toward |
| 133 | + sustainability without compromising on style or functionality. Stay hydrated, look great, and make a difference—get |
| 134 | + your eco-friendly water bottle today! |
| 135 | + # FormatProofAgent |
| 136 | + Sip sustainably with our eco-friendly stainless steel water bottles, designed for the conscious consumer who values |
| 137 | + both performance and aesthetics. Our bottles utilize advanced insulation technology to keep your beverages icy cold |
| 138 | + for up to 24 hours, making them perfect for outdoor adventures, gym sessions, or a busy day at the office. |
| 139 | +
|
| 140 | + Choose from a variety of sizes and stunning colors to match your personal style while positively impacting the |
| 141 | + planet. Each reusable bottle helps reduce single-use plastics, supporting a cleaner, greener world. |
| 142 | +
|
| 143 | + Join the movement towards sustainability without compromising on style or functionality. Stay hydrated, look great, |
| 144 | + and make a difference—get your eco-friendly water bottle today! |
| 145 | + ***** Final Result ***** |
| 146 | + Sip sustainably with our eco-friendly stainless steel water bottles, designed for the conscious consumer who values |
| 147 | + both performance and aesthetics. Our bottles utilize advanced insulation technology to keep your beverages icy cold |
| 148 | + for up to 24 hours, making them perfect for outdoor adventures, gym sessions, or a busy day at the office. |
| 149 | +
|
| 150 | + Choose from a variety of sizes and stunning colors to match your personal style while positively impacting the |
| 151 | + planet. Each reusable bottle helps reduce single-use plastics, supporting a cleaner, greener world. |
| 152 | +
|
| 153 | + Join the movement towards sustainability without compromising on style or functionality. Stay hydrated, look great, |
| 154 | + and make a difference—get your eco-friendly water bottle today! |
| 155 | + """ |
| 156 | + |
| 157 | + |
| 158 | +if __name__ == "__main__": |
| 159 | + asyncio.run(main()) |
0 commit comments