|
3 | 3 | import asyncio
|
4 | 4 | from typing import Annotated
|
5 | 5 |
|
| 6 | +from pydantic import BaseModel |
| 7 | + |
6 | 8 | from semantic_kernel import Kernel
|
7 | 9 | from semantic_kernel.agents import AgentRegistry, ChatHistoryAgentThread
|
8 | 10 | from semantic_kernel.agents.chat_completion.chat_completion_agent import ChatCompletionAgent
|
9 |
| -from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion |
10 |
| -from semantic_kernel.functions import kernel_function |
| 11 | +from semantic_kernel.connectors.ai.open_ai import ( |
| 12 | + AzureChatCompletion, |
| 13 | + AzureChatPromptExecutionSettings, |
| 14 | +) |
| 15 | +from semantic_kernel.functions import KernelArguments, kernel_function |
11 | 16 |
|
12 | 17 | """
|
13 | 18 | The following sample demonstrates how to create a chat completion agent using a
|
14 | 19 | declarative approach. The Chat Completion Agent is created from a YAML spec,
|
15 | 20 | with a specific service and plugins. The agent is then used to answer user questions.
|
| 21 | +
|
| 22 | +This sample also demonstrates how to properly pass execution settings (like response format) |
| 23 | +when using AgentRegistry.create_from_yaml(). |
16 | 24 | """
|
17 | 25 |
|
18 | 26 |
|
| 27 | +# Example structure for structured output |
| 28 | +class StructuredResult(BaseModel): |
| 29 | + """Example structure for demonstrating response format.""" |
| 30 | + |
| 31 | + response: str |
| 32 | + category: str |
| 33 | + |
| 34 | + |
19 | 35 | # 1. Define a Sample Plugin
|
20 | 36 | class MenuPlugin:
|
21 | 37 | """A sample Menu Plugin used for the concept sample."""
|
@@ -66,24 +82,44 @@ async def main():
|
66 | 82 | kernel = Kernel()
|
67 | 83 | kernel.add_plugin(MenuPlugin(), plugin_name="MenuPlugin")
|
68 | 84 |
|
69 |
| - # 5. Create the agent from YAML + inject the AI service |
| 85 | + # 5. Create execution settings with structured output |
| 86 | + execution_settings = AzureChatPromptExecutionSettings() |
| 87 | + execution_settings.response_format = StructuredResult |
| 88 | + |
| 89 | + # 6. Create KernelArguments with the execution settings |
| 90 | + arguments = KernelArguments(settings=execution_settings) |
| 91 | + |
| 92 | + # 7. Create the agent from YAML + inject the AI service |
70 | 93 | agent: ChatCompletionAgent = await AgentRegistry.create_from_yaml(
|
71 |
| - AGENT_YAML, kernel=kernel, service=OpenAIChatCompletion() |
| 94 | + AGENT_YAML, kernel=kernel, service=AzureChatCompletion(), arguments=arguments |
72 | 95 | )
|
73 | 96 |
|
74 |
| - # 6. Create a thread to hold the conversation |
| 97 | + # 8. Create a thread to hold the conversation |
75 | 98 | thread: ChatHistoryAgentThread | None = None
|
76 | 99 |
|
77 | 100 | for user_input in USER_INPUTS:
|
78 | 101 | print(f"# User: {user_input}")
|
79 |
| - # 7. Invoke the agent for a response |
| 102 | + # 9. Invoke the agent for a response |
80 | 103 | response = await agent.get_response(messages=user_input, thread=thread)
|
81 | 104 | print(f"# {response.name}: {response}")
|
82 | 105 | thread = response.thread
|
83 | 106 |
|
84 |
| - # 8. Cleanup the thread |
| 107 | + # 10. Cleanup the thread |
85 | 108 | await thread.delete() if thread else None
|
86 | 109 |
|
| 110 | + """ |
| 111 | + # Sample output: |
| 112 | +
|
| 113 | + # User: Hello |
| 114 | + # Assistant: {"response":"Hello! How can I help you today? If you have any questions about the menu, feel free to ask!","category":"Greeting"} |
| 115 | + # User: What is the special soup? |
| 116 | + # Assistant: {"response":"Today's special soup is Clam Chowder. Would you like to know more about it or see other specials?","category":"Menu Specials"} |
| 117 | + # User: What does that cost? |
| 118 | + # Assistant: {"response":"The Clam Chowder special soup costs $9.99.","category":"Menu Pricing"} |
| 119 | + # User: Thank you |
| 120 | + # Assistant: {"response":"You're welcome! If you have any more questions or need assistance with the menu, just let me know. Enjoy your meal!","category":"Polite Closing"} |
| 121 | + """ # noqa: E501 |
| 122 | + |
87 | 123 |
|
88 | 124 | if __name__ == "__main__":
|
89 | 125 | asyncio.run(main())
|
0 commit comments