|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +import asyncio |
| 4 | + |
| 5 | +from semantic_kernel.agents.open_ai import AzureAssistantAgent, OpenAIAssistantAgent |
| 6 | +from semantic_kernel.contents.chat_message_content import ChatMessageContent |
| 7 | +from semantic_kernel.contents.utils.author_role import AuthorRole |
| 8 | +from semantic_kernel.functions.kernel_arguments import KernelArguments |
| 9 | +from semantic_kernel.kernel import Kernel |
| 10 | +from semantic_kernel.prompt_template.prompt_template_config import PromptTemplateConfig |
| 11 | + |
| 12 | +##################################################################### |
| 13 | +# The following sample demonstrates how to create an assistant # |
| 14 | +# agent using either Azure OpenAI or OpenAI within Semantic Kernel. # |
| 15 | +# It uses parameterized prompts and shows how to swap between # |
| 16 | +# "semantic-kernel," "jinja2," and "handlebars" template formats, # |
| 17 | +# This sample highlights how the agent’s threaded conversation # |
| 18 | +# state parallels the Chat History in Semantic Kernel, ensuring # |
| 19 | +# all responses and parameters remain consistent throughout the # |
| 20 | +# session. # |
| 21 | +##################################################################### |
| 22 | + |
| 23 | +inputs = [ |
| 24 | + ("Home cooking is great.", None), |
| 25 | + ("Talk about world peace.", "iambic pentameter"), |
| 26 | + ("Say something about doing your best.", "e. e. cummings"), |
| 27 | + ("What do you think about having fun?", "old school rap"), |
| 28 | +] |
| 29 | + |
| 30 | + |
| 31 | +async def invoke_assistant_agent(agent: OpenAIAssistantAgent | AzureAssistantAgent, inputs): |
| 32 | + """Invokes the given agent with each (input, style) in inputs.""" |
| 33 | + thread_id = await agent.create_thread() |
| 34 | + |
| 35 | + try: |
| 36 | + for user_input, style in inputs: |
| 37 | + # Add user message to the conversation |
| 38 | + await agent.add_chat_message( |
| 39 | + thread_id=thread_id, |
| 40 | + message=ChatMessageContent(role=AuthorRole.USER, content=user_input), |
| 41 | + ) |
| 42 | + print(f"[USER]: {user_input}") |
| 43 | + |
| 44 | + # If style is specified, override the 'style' argument |
| 45 | + argument_overrides = None |
| 46 | + if style: |
| 47 | + argument_overrides = KernelArguments(style=style) |
| 48 | + |
| 49 | + # Stream agent responses |
| 50 | + async for response in agent.invoke(thread_id=thread_id, arguments=argument_overrides): |
| 51 | + print(f"[AGENT]: {response.content}") |
| 52 | + finally: |
| 53 | + # Clean up |
| 54 | + await agent.delete_thread(thread_id) |
| 55 | + await agent.delete() |
| 56 | + |
| 57 | + |
| 58 | +async def invoke_agent_with_template( |
| 59 | + kernel: Kernel, template_str: str, template_format: str, default_style: str = "haiku" |
| 60 | +): |
| 61 | + """Creates an agent with the specified template and format, then invokes it using invoke_assistant_agent.""" |
| 62 | + # Switch between Azure or OpenAI as desired: |
| 63 | + use_azure_openai = False |
| 64 | + |
| 65 | + instructions = ( |
| 66 | + "You are a friendly poet, skilled at writing a one verse poem on any requested topic. " |
| 67 | + "Always include the poem style in the final output." |
| 68 | + ) |
| 69 | + |
| 70 | + # Configure the prompt template |
| 71 | + prompt_config = PromptTemplateConfig(template=template_str, template_format=template_format) |
| 72 | + |
| 73 | + if use_azure_openai: |
| 74 | + agent = await AzureAssistantAgent.create( |
| 75 | + kernel=kernel, |
| 76 | + service_id="agent", |
| 77 | + name="MyPoetAgent", |
| 78 | + instructions=instructions, |
| 79 | + prompt_template_config=prompt_config, |
| 80 | + arguments=KernelArguments(style=default_style), |
| 81 | + ) |
| 82 | + else: |
| 83 | + agent = await OpenAIAssistantAgent.create( |
| 84 | + kernel=kernel, |
| 85 | + service_id="agent", |
| 86 | + name="MyPoetAgent", |
| 87 | + instructions=instructions, |
| 88 | + prompt_template_config=prompt_config, |
| 89 | + arguments=KernelArguments(style=default_style), |
| 90 | + ) |
| 91 | + |
| 92 | + await invoke_assistant_agent(agent, inputs) |
| 93 | + |
| 94 | + |
| 95 | +async def main(): |
| 96 | + kernel = Kernel() |
| 97 | + |
| 98 | + # 1) Using "semantic-kernel" format |
| 99 | + print("\n===== SEMANTIC-KERNEL FORMAT =====\n") |
| 100 | + semantic_kernel_template = """ |
| 101 | +Write a one verse poem on the requested topic in the style of {{$style}}. |
| 102 | +Always state the requested style of the poem. |
| 103 | +""" |
| 104 | + await invoke_agent_with_template( |
| 105 | + kernel=kernel, |
| 106 | + template_str=semantic_kernel_template, |
| 107 | + template_format="semantic-kernel", |
| 108 | + default_style="haiku", |
| 109 | + ) |
| 110 | + |
| 111 | + # 2) Using "jinja2" format |
| 112 | + print("\n===== JINJA2 FORMAT =====\n") |
| 113 | + jinja2_template = """ |
| 114 | +Write a one verse poem on the requested topic in the style of {{style}}. |
| 115 | +Always state the requested style of the poem. |
| 116 | +""" |
| 117 | + await invoke_agent_with_template( |
| 118 | + kernel=kernel, template_str=jinja2_template, template_format="jinja2", default_style="haiku" |
| 119 | + ) |
| 120 | + |
| 121 | + # 3) Using "handlebars" format |
| 122 | + print("\n===== HANDLEBARS FORMAT =====\n") |
| 123 | + handlebars_template = """ |
| 124 | +Write a one verse poem on the requested topic in the style of {{style}}. |
| 125 | +Always state the requested style of the poem. |
| 126 | +""" |
| 127 | + await invoke_agent_with_template( |
| 128 | + kernel=kernel, template_str=handlebars_template, template_format="handlebars", default_style="haiku" |
| 129 | + ) |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == "__main__": |
| 133 | + asyncio.run(main()) |
0 commit comments