|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +from semantic_kernel.connectors.ai.open_ai.services.azure_chat_completion import AzureChatCompletion |
| 4 | +from semantic_kernel.connectors.ai.open_ai.services.open_ai_chat_completion import OpenAIChatCompletion |
| 5 | +from semantic_kernel.connectors.ai.prompt_execution_settings import PromptExecutionSettings |
| 6 | +from semantic_kernel.const import DEFAULT_SERVICE_NAME |
| 7 | +from semantic_kernel.contents.chat_history import ChatHistory |
| 8 | +from semantic_kernel.functions.kernel_arguments import KernelArguments |
| 9 | +from semantic_kernel.functions.kernel_function import KernelFunction |
| 10 | +from semantic_kernel.kernel import Kernel |
| 11 | +from semantic_kernel.kernel_types import AI_SERVICE_CLIENT_TYPE |
| 12 | +from semantic_kernel.services.ai_service_client_base import AIServiceClientBase |
| 13 | +from semantic_kernel.services.ai_service_selector import AIServiceSelector |
| 14 | +from semantic_kernel.services.kernel_services_extension import KernelServicesExtension |
| 15 | + |
| 16 | + |
| 17 | +class CustomServiceSelector(AIServiceSelector): |
| 18 | + def select_ai_service( |
| 19 | + self, |
| 20 | + kernel: "KernelServicesExtension", |
| 21 | + function: "KernelFunction", |
| 22 | + arguments: "KernelArguments", |
| 23 | + type_: type[AI_SERVICE_CLIENT_TYPE] | tuple[type[AI_SERVICE_CLIENT_TYPE], ...] | None = None, |
| 24 | + ) -> tuple["AIServiceClientBase", "PromptExecutionSettings"]: |
| 25 | + execution_settings_dict = arguments.execution_settings or {} |
| 26 | + if func_exec_settings := getattr(function, "prompt_execution_settings", None): |
| 27 | + for id, settings in func_exec_settings.items(): |
| 28 | + if id not in execution_settings_dict: |
| 29 | + execution_settings_dict[id] = settings |
| 30 | + if not execution_settings_dict: |
| 31 | + from semantic_kernel.connectors.ai.prompt_execution_settings import PromptExecutionSettings |
| 32 | + |
| 33 | + execution_settings_dict = {DEFAULT_SERVICE_NAME: PromptExecutionSettings()} |
| 34 | + |
| 35 | + gpt_4_settings = { |
| 36 | + service_name: settings |
| 37 | + for service_name, settings in execution_settings_dict.items() |
| 38 | + if "gpt-4" in service_name |
| 39 | + } |
| 40 | + if gpt_4_settings: |
| 41 | + service_id = list(gpt_4_settings.keys())[0] |
| 42 | + service = kernel.get_service(service_id, type=type_) |
| 43 | + service_settings = service.get_prompt_execution_settings_from_settings(gpt_4_settings[service_id]) |
| 44 | + return service, service_settings |
| 45 | + return super().select_ai_service(kernel, function, arguments, type_) |
| 46 | + |
| 47 | + |
| 48 | +kernel = Kernel(ai_service_selector=CustomServiceSelector()) |
| 49 | +kernel.add_service(AzureChatCompletion(service_id="gpt-4o")) |
| 50 | +kernel.add_service(OpenAIChatCompletion(service_id="gpt-3.5-turbo", ai_model_id="gpt-3.5-turbo")) |
| 51 | + |
| 52 | +kernel.add_function( |
| 53 | + plugin_name="selector", |
| 54 | + function_name="select_ai_service", |
| 55 | + prompt="Always respond with your name. {{$chat_history}}", |
| 56 | + prompt_execution_settings={ |
| 57 | + "gpt-4o": PromptExecutionSettings(service_id="gpt-4o", max_tokens=200, temperature=0.0), |
| 58 | + "gpt-3.5-turbo": PromptExecutionSettings(service_id="gpt-3.5-turbo", max_tokens=400, temperature=1.0), |
| 59 | + }, |
| 60 | +) |
| 61 | + |
| 62 | + |
| 63 | +async def main(): |
| 64 | + chat_history = ChatHistory() |
| 65 | + chat_history.add_user_message("I'm Eduard.") |
| 66 | + result = await kernel.invoke(plugin_name="selector", function_name="select_ai_service", chat_history=chat_history) |
| 67 | + print(result) |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == "__main__": |
| 71 | + import asyncio |
| 72 | + |
| 73 | + asyncio.run(main()) |
0 commit comments