Semantic Kernel:微软大模型开发框架——LangChain 替代

大模型相关目录

大模型,包括部署微调prompt/Agent应用开发、知识库增强、数据库增强、知识图谱增强、自然语言处理、多模态等大模型应用开发内容
从0起步,扬帆起航。

  1. 基于Dify的QA数据集构建(附代码)
  2. Qwen-2-7B和GLM-4-9B:大模型届的比亚迪秦L
  3. 文擎毕昇和Dify:大模型开发平台模式对比
  4. Qwen-VL图文多模态大模型微调指南
  5. 从零开始的Ollama指南:部署私域大模型
  6. 基于Dify的智能分类方案:大模型结合KNN算法(附代码)
  7. OpenCompass:大模型测评工具
  8. 一文读懂多模态大模型基础架构
  9. 大模型管理平台:one-api使用指南
  10. 大模型RAG、ROG、RCG概念科普
  11. RAGOnMedicalKG:大模型结合知识图谱的RAG实现
  12. DSPy:变革式大模型应用开发
  13. 最简明的Few-shot Prompt指南
  14. Semantic Kernel:微软大模型开发框架——LangChain 替代


Semantic Kernel介绍

Semantic Kernel:一个集成大型语言模型 (LLM) 的 SDK,如 OpenAI、Azure OpenAI、 以及使用 C#、Python 和 Java 等传统编程语言的 Hugging Face。语义内核实现了这一点 通过允许您定义可以链接在一起的插件 只需几行代码。

然而,语义内核的特别之处在于它能够自动编排 带有 AI 的插件。使用语义内核规划器,您可以 可以要求 LLM 生成实现用户唯一目标的计划。之后 语义内核将为用户执行计划。

Semantic Kernel 的主要特点:

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

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

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

在这里插入图片描述

与Langchain对比

LangChain 作为一款备受欢迎的 AI 应用开发框架,凭借其丰富的预制组件和易用性,吸引了众多开发者的目光。然而,微软开源的 Semantic Kernel 框架以其独特的优势和强大的功能,为开发者带来了全新的选择,或将成为 LangChain 的有力竞争者。
在这里插入图片描述

部署使用

项目开源地址:

https://github.com/microsoft/semantic-kernel

python教程:

https://github.com/microsoft/semantic-kernel/blob/main/python/samples/getting_started/03-prompt-function-inline.ipynb

在这里插入图片描述
安装最新软件包:

python -m pip install --upgrade semantic-kernel

如果要使用一些可选依赖项(默认安装 OpenAI),可以使用以下命令进行安装:

python -m pip install --upgrade semantic-kernel[hugging_face]

或全部:

python -m pip install --upgrade semantic-kernel[all]

使用示例:

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())
  • 30
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

写代码的中青年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值