{"id":3906,"date":"2025-01-14T16:26:44","date_gmt":"2025-01-15T00:26:44","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/semantic-kernel\/?p=3906"},"modified":"2025-01-16T11:29:33","modified_gmt":"2025-01-16T19:29:33","slug":"understanding-semantic-kernel-ai-connectors","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/agent-framework\/understanding-semantic-kernel-ai-connectors\/","title":{"rendered":"Understanding Semantic Kernel AI Connectors"},"content":{"rendered":"<div>\n<div>AI Connectors in Semantic Kernel are components that facilitate communication between the Kernel&#8217;s core functionalities and various AI services. They abstract the intricate details of service-specific protocols, allowing developers to seamlessly interact with AI services for tasks like text generation, chat interactions, and more.<\/div>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<div>\n<h3>Using AI Connectors in Semantic Kernel<\/h3>\n<\/div>\n<div>Developers utilize AI connectors to connect their applications to different AI services efficiently. The connectors manage the requests and responses, providing a streamlined way to leverage the power of these AI services without needing to handle the specific communication protocols each service requires.<\/div>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<h3>Creating Custom AI Connectors in Semantic Kernel<\/h3>\n<div>To create a custom AI connector in Semantic Kernel, one must extend the base classes provided, such as\u00a0<code>ChatCompletionClientBase<\/code>\u00a0and\u00a0<code>AIServiceClientBase<\/code>. Below is a guide and example for implementing a mock AI connector:<\/div>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<h4>Step-by-Step Walkthrough<\/h4>\n<div>\n<div>\n<ol dir=\"auto\">\n<li>\n<p dir=\"auto\"><strong>Understand the Base Classes<\/strong>: The foundational classes\u00a0<code>ChatCompletionClientBase<\/code>\u00a0and\u00a0<code>AIServiceClientBase<\/code>\u00a0provide necessary methods and structures for creating chat-based AI connectors.<\/p>\n<\/li>\n<li>\n<p dir=\"auto\"><strong>Implementing the Connector<\/strong>: Here&#8217;s a mock implementation example illustrating how to implement a connector without real service dependencies, ensuring compatibility with Pydantic&#8217;s expectations within the framework:<\/p>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div>\n<pre class=\"prettyprint language-py\"><code class=\"language-py\">from semantic_kernel.connectors.ai.chat_completion_client_base import ChatCompletionClientBase\r\n\r\nclass MockAIChatCompletionService(ChatCompletionClientBase):\r\n    def __init__(self, ai_model_id: str):\r\n        super().__init__(ai_model_id=ai_model_id)\r\n\r\n    async def _inner_get_chat_message_contents(self, chat_history, settings):\r\n        # Mock implementation: returns dummy chat message content for demonstration.\r\n        return [{\"role\": \"assistant\", \"content\": \"Mock response based on your history.\"}]\r\n\r\n    def service_url(self):\r\n        return \"http:\/\/mock-ai-service.com\"<\/code><\/pre>\n<\/div>\n<div>\n<h3>Usage Example<\/h3>\n<div>\n<p dir=\"auto\">The following example demonstrates how to integrate and use the\u00a0<code>MockAIChatCompletionService<\/code> in an application:<\/p>\n<\/div>\n<\/div>\n<div>\n<pre class=\"prettyprint language-py\"><code class=\"language-py\">import asyncio\r\nfrom semantic_kernel.contents.chat_history import ChatHistory\r\nfrom semantic_kernel.connectors.ai.prompt_execution_settings import PromptExecutionSettings\r\n\r\nasync def main():\r\n    chat_history = ChatHistory(messages=[{\"role\": \"user\", \"content\": \"Hello\"}])\r\n    settings = PromptExecutionSettings(model=\"mock-model\")\r\n    \r\n    service = MockAIChatCompletionService(ai_model_id=\"mock-model\")\r\n    \r\n    response = await service.get_chat_message_contents(chat_history, settings)\r\n    print(response)\r\n\r\n# Run the main function\r\nasyncio.run(main())<\/code><\/pre>\n<\/div>\n<div>\n<div>\n<h3>Conclusion<\/h3>\n<div>\n<div>By following the revised guide and understanding the base class functionalities, developers can effectively create custom connectors within Semantic Kernel. This structured approach enhances integration with various AI services while ensuring alignment with the framework&#8217;s architectural expectations. Custom connectors offer flexibility, allowing developers to adjust implementations to meet specific service needs, such as additional logging, authentication, or modifications tailored to specific protocols. This guide provides a strong foundation upon which more complex and service-specific extensions can be built, promoting robust and scalable AI service integration.<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<hr \/>\n<p><span style=\"font-size: 10pt;\"><em>* This blog post is created <strong>entirely<\/strong> by AI (including the code snippets), using Semantic Kernel&#8217;s agent framework. Multiple agents worked together to create this content by referencing the source code of SK, while ensuring the correctness, and incorporating user feedback.\u00a0<\/em><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>AI Connectors in Semantic Kernel are components that facilitate communication between the Kernel&#8217;s core functionalities and various AI services. They abstract the intricate details of service-specific protocols, allowing developers to seamlessly interact with AI services for tasks like text generation, chat interactions, and more. &nbsp; Using AI Connectors in Semantic Kernel Developers utilize AI connectors [&hellip;]<\/p>\n","protected":false},"author":165150,"featured_media":2365,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[48,111,63,9],"class_list":["post-3906","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-semantic-kernel","tag-ai","tag-ai-connectors","tag-microsoft-semantic-kernel","tag-semantic-kernel"],"acf":[],"blog_post_summary":"<p>AI Connectors in Semantic Kernel are components that facilitate communication between the Kernel&#8217;s core functionalities and various AI services. They abstract the intricate details of service-specific protocols, allowing developers to seamlessly interact with AI services for tasks like text generation, chat interactions, and more. &nbsp; Using AI Connectors in Semantic Kernel Developers utilize AI connectors [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/3906","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/users\/165150"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/comments?post=3906"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/posts\/3906\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/media\/2365"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/media?parent=3906"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/categories?post=3906"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/agent-framework\/wp-json\/wp\/v2\/tags?post=3906"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}