|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +import asyncio |
| 4 | +from typing import Annotated |
| 5 | + |
| 6 | +from semantic_kernel.agents import ChatCompletionAgent |
| 7 | +from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion |
| 8 | +from semantic_kernel.contents import ChatHistory |
| 9 | +from semantic_kernel.functions import kernel_function |
| 10 | + |
| 11 | +""" |
| 12 | +The following sample demonstrates how to create a chat completion agent that |
| 13 | +answers questions about a sample menu using a Semantic Kernel Plugin. The Chat |
| 14 | +Completion Service is passed directly via the ChatCompletionAgent constructor. |
| 15 | +Additionally, the plugin is supplied via the constructor. |
| 16 | +""" |
| 17 | + |
| 18 | + |
| 19 | +# Define a sample plugin for the sample |
| 20 | +class MenuPlugin: |
| 21 | + """A sample Menu Plugin used for the concept sample.""" |
| 22 | + |
| 23 | + @kernel_function(description="Provides a list of specials from the menu.") |
| 24 | + def get_specials(self) -> Annotated[str, "Returns the specials from the menu."]: |
| 25 | + return """ |
| 26 | + Special Soup: Clam Chowder |
| 27 | + Special Salad: Cobb Salad |
| 28 | + Special Drink: Chai Tea |
| 29 | + """ |
| 30 | + |
| 31 | + @kernel_function(description="Provides the price of the requested menu item.") |
| 32 | + def get_item_price( |
| 33 | + self, menu_item: Annotated[str, "The name of the menu item."] |
| 34 | + ) -> Annotated[str, "Returns the price of the menu item."]: |
| 35 | + return "$9.99" |
| 36 | + |
| 37 | + |
| 38 | +# Simulate a conversation with the agent |
| 39 | +USER_INPUTS = [ |
| 40 | + "Hello", |
| 41 | + "What is the special soup?", |
| 42 | + "What does that cost?", |
| 43 | + "Thank you", |
| 44 | +] |
| 45 | + |
| 46 | + |
| 47 | +async def main(): |
| 48 | + # 1. Create the agent |
| 49 | + agent = ChatCompletionAgent( |
| 50 | + service=AzureChatCompletion(), |
| 51 | + name="Host", |
| 52 | + instructions="Answer questions about the menu.", |
| 53 | + plugins=[MenuPlugin()], |
| 54 | + ) |
| 55 | + |
| 56 | + # 2. Create a chat history to hold the conversation |
| 57 | + chat_history = ChatHistory() |
| 58 | + |
| 59 | + for user_input in USER_INPUTS: |
| 60 | + # 3. Add the user input to the chat history |
| 61 | + chat_history.add_user_message(user_input) |
| 62 | + print(f"# User: {user_input}") |
| 63 | + # 4. Invoke the agent for a response |
| 64 | + response = await agent.get_response(chat_history) |
| 65 | + print(f"# {response.name}: {response.content} ") |
| 66 | + |
| 67 | + """ |
| 68 | + Sample output: |
| 69 | + # User: Hello |
| 70 | + # Host: Hello! How can I assist you today? |
| 71 | + # User: What is the special soup? |
| 72 | + # Host: The special soup is Clam Chowder. |
| 73 | + # User: What does that cost? |
| 74 | + # Host: The special soup, Clam Chowder, costs $9.99. |
| 75 | + # User: Thank you |
| 76 | + # Host: You're welcome! If you have any more questions, feel free to ask. Enjoy your day! |
| 77 | + """ |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + asyncio.run(main()) |
0 commit comments