AI知识库

53AI知识库

学习大模型的前沿技术与行业应用场景


人工智能 (AI) 正以前所未有的速度改变着我们的世界
发布日期:2024-06-29 22:07:35 浏览次数: 1788


人工智能 (AI) 正以前所未有的速度改变着我们的世界。从智能助手到自动驾驶汽车,AI 正在渗透到我们生活的方方面面。而对于开发者来说,如何快速构建出功能强大、性能卓越的 AI 应用成为了一个重要的课题。

LangChain 作为一款备受欢迎的 AI 应用开发框架,凭借其丰富的预制组件和易用性,吸引了众多开发者的目光。然而,AI 领域的发展日新月异,微软开源的 Semantic Kernel 框架横空出世,以其独特的优势和强大的功能,为开发者带来了全新的选择,或将成为 LangChain 的有力竞争者。

本文将深入探讨 Semantic Kernel 的特点、优势以及与 LangChain 的比较,帮助开发者选择最适合自己的 AI 应用开发框架。

Semantic Kernel:AI 应用开发的新选择

什么是 Semantic Kernel?

Semantic Kernel 是一个轻量级的开源软件开发工具包 (SDK),旨在帮助开发者轻松地将 AI 功能集成到他们的应用程序中。它提供了一个简单易用的 API,可以连接到各种 AI 服务,例如 OpenAI、Azure OpenAI 和 Hugging Face 等。

Semantic Kernel 的主要特点:

  • • 灵活的插件架构: 允许开发者定义可以链接在一起的插件,仅需几行代码 即可实现复杂的功能。

  • • AI 驱动的插件编排: 可以使用 Semantic Kernel 规划器 让 LLM 生成一个计划来实现用户的独特目标,然后 Semantic Kernel 将为用户执行该计划。

  • • 多语言支持: 支持 C#、Python 和 Java 编程语言,方便不同技术背景的开发者使用。

Semantic Kernel 的优势

  • • 企业级:

    • • 微软和其他财富 500 强公司已经在利用 Semantic Kernel,因为它灵活、模块化且可观察。

    • • 它还具备安全性增强功能,如遥测支持、钩子和过滤器,因此您可以放心地大规模交付负责任的 AI 解决方案。

    • • 版本 1.0+ 支持 C#、Python 和 Java,这意味着它是可靠的,致力于不间断的更改。任何现有的基于聊天的 API 都可以轻松扩展以支持其他模式,如语音和视频。

  • • 自动化业务流程:

    • • Semantic Kernel 将提示与现有 API 相结合来自动执行操作。

    • • 通过向 AI 模型描述您现有的代码,它们将被调用来处理请求。

    • • 当发出请求时,模型会调用一个函数,而 Semantic Kernel 是将模型的请求转换为函数调用并将结果传递回模型的中间件。

  • • 模块化和可扩展性:

    • • 通过将现有代码添加为插件,您可以灵活地通过一组现成的连接器集成 AI 服务,从而最大限度地提高投资回报率。

    • • Semantic Kernel 使用 OpenAPI 规范(如 Microsoft 365 Copilot),因此您可以与公司中的其他专业或低代码开发人员共享任何扩展。

  • • 面向未来:

    • • Semantic Kernel 旨在面向未来,可以轻松地将您的代码连接到最新的 AI 模型,并随着技术的进步而发展。

    • • 当新模型发布时,您只需将它们替换掉,而无需重写整个代码库。

快速入门指南

安装 SDK

您可以使用以下命令轻松安装 Semantic Kernel:

pip install semantic-kernel

Semantic Kernel Notebooks

微软提供了丰富的 Semantic Kernel Notebooks,帮助开发者快速上手。这些 Notebooks 包含详细的代码示例和说明,涵盖了从基础概念到高级应用的各种主题,即使是 AI 初学者也能轻松理解和使用。

  • • C# 入门 Notebook

  • • Python 入门 Notebook

编写你的第一个 Semantic Kernel 控制台应用

以下是一个使用 Python 编写的简单控制台应用程序示例:

import asyncio

from semantic_kernel importKernel
from semantic_kernel.functions import kernel_function
from semantic_kernel.connectors.ai.open_ai importAzureChatCompletion
from semantic_kernel.connectors.ai.function_call_behavior importFunctionCallBehavior
from semantic_kernel.connectors.ai.chat_completion_client_base importChatCompletionClientBase
from semantic_kernel.contents.chat_history importChatHistory
from semantic_kernel.functions.kernel_arguments importKernelArguments

from semantic_kernel.connectors.ai.open_ai.prompt_execution_settings.azure_chat_prompt_execution_settings import(
AzureChatPromptExecutionSettings,
)

asyncdefmain():
# Initialize the kernel
    kernel =Kernel()

# Add Azure OpenAI chat completion
    kernel.add_service(AzureChatCompletion(
        deployment_name="your_models_deployment_name",
        api_key="your_api_key",
        base_url="your_base_url",
))

# Set the logging level for  semantic_kernel.kernel to DEBUG.
    logging.basicConfig(
format="[%(asctime)s - %(name)s:%(lineno)d - %(levelname)s] %(message)s",
        datefmt="%Y-%m-%d %H:%M:%S",
)
    logging.getLogger("kernel").setLevel(logging.DEBUG)

# Add a plugin (the LightsPlugin class is defined below)
    kernel.add_plugin(
LightsPlugin(),
        plugin_name="Lights",
)

    chat_completion :AzureChatCompletion= kernel.get_service(type=ChatCompletionClientBase)

# Enable planning
    execution_settings =AzureChatPromptExecutionSettings(tool_choice="auto")
    execution_settings.function_call_behavior =FunctionCallBehavior.EnableFunctions(auto_invoke=True, filters={})

# Create a history of the conversation
    history =ChatHistory()

# Initiate a back-and-forth chat
    userInput =None
whileTrue:
# Collect user input
        userInput =input("User > ")

# Terminate the loop if the user says "exit"
if userInput =="exit":
break

# Add user input to the history
        history.add_user_message(userInput)

# Get the response from the AI
        result =(await chat_completion.get_chat_message_contents(
            chat_history=history,
            settings=execution_settings,
            kernel=kernel,
            arguments=KernelArguments(),
))[0]

# Print the results
print("Assistant > "+str(result))

# Add the message from the agent to the chat history
        history.add_message(result)

# Run the main function
if __name__ =="__main__":
    asyncio.run(main())

Semantic Kernel 与 LangChain 的比较

特性Semantic KernelLangChain
开发语言C#, Python, JavaPython, JavaScript
插件生态仍在发展中拥有大量可用的集成
易用性对传统开发者友好,易于集成到现有应用程序中对熟悉 LLM 和提示工程的开发者友好
功能提供规划器,允许 LLM 生成计划并执行提供链接各种工具和 LLM 的方法
文档和社区支持提供官方文档和 GitHub 社区支持拥有活跃的社区和丰富的文档
企业级功能专注于提供企业级功能,例如安全性增强功能和与企业系统的集成更加注重快速原型设计和实验
学习曲线对传统开发者来说学习曲线相对平缓对熟悉 LLM 和提示工程的开发者来说学习曲线相对平缓
最佳使用场景构建需要与现有系统集成、注重安全性和可维护性的企业级 AI 应用程序快速构建原型、实验不同的 LLM 和提示工程技术、构建不需要与现有系统深度集成的小型 AI 应用程序

总结

Semantic Kernel 是微软开源的一款强大的 AI SDK,它为开发者构建智能应用提供了全新的解决方案。凭借其企业级、自动化、模块化和面向未来的特性,Semantic Kernel 将成为 AI 应用开发领域的重要工具,引领 AI 应用开发的新潮流。对于正在寻找 LangChain 替代品的开发者来说,Semantic Kernel 无疑是一个值得考虑的强大选择。


  • • Semantic Kernel GitHub 仓库:https://github.com/microsoft/semantic-kernel




53AI,企业落地应用大模型首选服务商

产品:大模型应用平台+智能体定制开发+落地咨询服务

承诺:先做场景POC验证,看到效果再签署服务协议。零风险落地应用大模型,已交付160+中大型企业

联系我们

售前咨询
186 6662 7370
预约演示
185 8882 0121

微信扫码

与创始人交个朋友

回到顶部

 
扫码咨询