|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import os |
| 5 | +from typing import Tuple |
| 6 | + |
| 7 | +import semantic_kernel as sk |
| 8 | +import semantic_kernel.connectors.ai.open_ai as sk_oai |
| 9 | +from semantic_kernel.connectors.ai.open_ai.semantic_functions.open_ai_chat_prompt_template import ( |
| 10 | + OpenAIChatPromptTemplate, |
| 11 | +) |
| 12 | +from semantic_kernel.connectors.ai.open_ai.utils import ( |
| 13 | + chat_completion_with_function_call, |
| 14 | + get_function_calling_object, |
| 15 | +) |
| 16 | +from semantic_kernel.core_skills import MathSkill |
| 17 | + |
| 18 | +system_message = """ |
| 19 | +You are a chat bot. Your name is Mosscap and |
| 20 | +you have one goal: figure out what people need. |
| 21 | +Your full name, should you need to know it, is |
| 22 | +Splendid Speckled Mosscap. You communicate |
| 23 | +effectively, but you tend to answer with long |
| 24 | +flowery prose. You are also a math wizard, |
| 25 | +especially for adding and subtracting. |
| 26 | +You also excel at joke telling, where your tone is often sarcastic. |
| 27 | +Once you have the answer I am looking for, |
| 28 | +you will return a full answer to me as soon as possible. |
| 29 | +""" |
| 30 | + |
| 31 | +kernel = sk.Kernel() |
| 32 | + |
| 33 | +deployment_name, api_key, endpoint = sk.azure_openai_settings_from_dot_env() |
| 34 | +api_version = "2023-07-01-preview" |
| 35 | +kernel.add_chat_service( |
| 36 | + "chat-gpt", |
| 37 | + sk_oai.AzureChatCompletion( |
| 38 | + deployment_name, |
| 39 | + endpoint, |
| 40 | + api_key, |
| 41 | + api_version=api_version, |
| 42 | + ), |
| 43 | +) |
| 44 | + |
| 45 | +skills_directory = os.path.join(__file__, "../../../../samples/skills") |
| 46 | +# adding skills to the kernel |
| 47 | +# the joke skill in the FunSkills is a semantic skill and has the function calling disabled. |
| 48 | +kernel.import_semantic_skill_from_directory(skills_directory, "FunSkill") |
| 49 | +# the math skill is a core skill and has the function calling enabled. |
| 50 | +kernel.import_skill(MathSkill(), skill_name="math") |
| 51 | + |
| 52 | +# enabling or disabling function calling is done by setting the function_call parameter for the completion. |
| 53 | +# when the function_call parameter is set to "auto" the model will decide which function to use, if any. |
| 54 | +# if you only want to use a specific function, set the name of that function in this parameter, |
| 55 | +# the format for that is 'SkillName-FunctionName', (i.e. 'math-Add'). |
| 56 | +# if the model or api version do not support this you will get an error. |
| 57 | +prompt_config = sk.PromptTemplateConfig.from_completion_parameters( |
| 58 | + max_tokens=2000, |
| 59 | + temperature=0.7, |
| 60 | + top_p=0.8, |
| 61 | + function_call="auto", |
| 62 | + chat_system_prompt=system_message, |
| 63 | +) |
| 64 | +prompt_template = OpenAIChatPromptTemplate( |
| 65 | + "{{$user_input}}", kernel.prompt_template_engine, prompt_config |
| 66 | +) |
| 67 | +prompt_template.add_user_message("Hi there, who are you?") |
| 68 | +prompt_template.add_assistant_message( |
| 69 | + "I am Mosscap, a chat bot. I'm trying to figure out what people need." |
| 70 | +) |
| 71 | + |
| 72 | +function_config = sk.SemanticFunctionConfig(prompt_config, prompt_template) |
| 73 | +chat_function = kernel.register_semantic_function("ChatBot", "Chat", function_config) |
| 74 | + |
| 75 | +# calling the chat, you could add a overloaded version of the settings here, |
| 76 | +# to enable or disable function calling or set the function calling to a specific skill. |
| 77 | +# see the openai_function_calling example for how to use this with a unrelated function definition |
| 78 | +filter = {"exclude_skill": ["ChatBot"]} |
| 79 | +functions = get_function_calling_object(kernel, filter) |
| 80 | + |
| 81 | + |
| 82 | +async def chat(context: sk.SKContext) -> Tuple[bool, sk.SKContext]: |
| 83 | + try: |
| 84 | + user_input = input("User:> ") |
| 85 | + context.variables["user_input"] = user_input |
| 86 | + except KeyboardInterrupt: |
| 87 | + print("\n\nExiting chat...") |
| 88 | + return False, None |
| 89 | + except EOFError: |
| 90 | + print("\n\nExiting chat...") |
| 91 | + return False, None |
| 92 | + |
| 93 | + if user_input == "exit": |
| 94 | + print("\n\nExiting chat...") |
| 95 | + return False, None |
| 96 | + |
| 97 | + context = await chat_completion_with_function_call( |
| 98 | + kernel, |
| 99 | + chat_skill_name="ChatBot", |
| 100 | + chat_function_name="Chat", |
| 101 | + context=context, |
| 102 | + functions=functions, |
| 103 | + ) |
| 104 | + print(f"Mosscap:> {context.result}") |
| 105 | + return True, context |
| 106 | + |
| 107 | + |
| 108 | +async def main() -> None: |
| 109 | + chatting = True |
| 110 | + context = kernel.create_new_context() |
| 111 | + print( |
| 112 | + "Welcome to the chat bot!\ |
| 113 | +\n Type 'exit' to exit.\ |
| 114 | +\n Try a math question to see the function calling in action (i.e. what is 3+3?)." |
| 115 | + ) |
| 116 | + while chatting: |
| 117 | + chatting, context = await chat(context) |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + asyncio.run(main()) |
0 commit comments