|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import logging |
| 5 | + |
| 6 | +import semantic_kernel as sk |
| 7 | +import semantic_kernel.connectors.ai.open_ai as sk_oai |
| 8 | +from semantic_kernel.contents.chat_history import ChatHistory |
| 9 | +from semantic_kernel.functions.kernel_arguments import KernelArguments |
| 10 | +from semantic_kernel.utils.settings import azure_openai_settings_from_dot_env_as_dict |
| 11 | + |
| 12 | +logging.basicConfig(level=logging.WARNING) |
| 13 | + |
| 14 | +system_message = """ |
| 15 | +You are a chat bot. Your name is Mosscap and |
| 16 | +you have one goal: figure out what people need. |
| 17 | +Your full name, should you need to know it, is |
| 18 | +Splendid Speckled Mosscap. You communicate |
| 19 | +effectively, but you tend to answer with long |
| 20 | +flowery prose. |
| 21 | +""" |
| 22 | + |
| 23 | +kernel = sk.Kernel() |
| 24 | + |
| 25 | +service_id = "chat-gpt" |
| 26 | +chat_service = sk_oai.AzureChatCompletion( |
| 27 | + service_id=service_id, **azure_openai_settings_from_dot_env_as_dict(include_api_version=True) |
| 28 | +) |
| 29 | +kernel.add_service(chat_service) |
| 30 | + |
| 31 | +req_settings = kernel.get_prompt_execution_settings_from_service_id(service_id=service_id) |
| 32 | +req_settings.max_tokens = 2000 |
| 33 | +req_settings.temperature = 0.7 |
| 34 | +req_settings.top_p = 0.8 |
| 35 | +req_settings.auto_invoke_kernel_functions = False |
| 36 | + |
| 37 | + |
| 38 | +chat_function = kernel.create_function_from_prompt( |
| 39 | + prompt="""{{system_message}}{% for item in chat_history %}{{ message(item) }}{% endfor %}""", |
| 40 | + function_name="chat", |
| 41 | + plugin_name="chat", |
| 42 | + template_format="jinja2", |
| 43 | + prompt_execution_settings=req_settings, |
| 44 | +) |
| 45 | + |
| 46 | +chat_history = ChatHistory() |
| 47 | +chat_history.add_user_message("Hi there, who are you?") |
| 48 | +chat_history.add_assistant_message("I am Mosscap, a chat bot. I'm trying to figure out what people need.") |
| 49 | + |
| 50 | + |
| 51 | +async def chat() -> bool: |
| 52 | + try: |
| 53 | + user_input = input("User:> ") |
| 54 | + except KeyboardInterrupt: |
| 55 | + print("\n\nExiting chat...") |
| 56 | + return False |
| 57 | + except EOFError: |
| 58 | + print("\n\nExiting chat...") |
| 59 | + return False |
| 60 | + |
| 61 | + if user_input == "exit": |
| 62 | + print("\n\nExiting chat...") |
| 63 | + return False |
| 64 | + chat_history.add_user_message(user_input) |
| 65 | + arguments = KernelArguments(system_message=system_message, chat_history=chat_history) |
| 66 | + |
| 67 | + stream = True |
| 68 | + if stream: |
| 69 | + answer = kernel.invoke_stream( |
| 70 | + chat_function, |
| 71 | + arguments=arguments, |
| 72 | + ) |
| 73 | + print("Mosscap:> ", end="") |
| 74 | + async for message in answer: |
| 75 | + print(str(message[0]), end="") |
| 76 | + print("\n") |
| 77 | + return True |
| 78 | + answer = await kernel.invoke( |
| 79 | + chat_function, |
| 80 | + arguments=arguments, |
| 81 | + ) |
| 82 | + print(f"Mosscap:> {answer}") |
| 83 | + chat_history.add_assistant_message(str(answer)) |
| 84 | + return True |
| 85 | + |
| 86 | + |
| 87 | +async def main() -> None: |
| 88 | + chatting = True |
| 89 | + while chatting: |
| 90 | + chatting = await chat() |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + asyncio.run(main()) |
0 commit comments