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 的有力竞争者。

部署使用

项目开源地址:

python 复制代码
https://github.com/microsoft/semantic-kernel

python教程:

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

安装最新软件包:

python 复制代码
python -m pip install --upgrade semantic-kernel

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

python 复制代码
python -m pip install --upgrade semantic-kernel[hugging_face]

或全部:

python 复制代码
python -m pip install --upgrade semantic-kernel[all]

使用示例:

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())
相关推荐
程序员的开发手册19 分钟前
新手教学系列——慎用Flask-SQLAlchemy慢日志记录
数据库·python·flask·sqlalchemy
李加号pluuuus2 小时前
【扩散模型】LCM LoRA:一个通用的Stable Diffusion加速模块
人工智能·stable diffusion
木觞清2 小时前
Django学习第三天
python·学习·django
电饭叔2 小时前
《python程序语言设计》2018版第5章第52题利用turtle绘制sin函数
开发语言·python
Alkali!3 小时前
2-5 softmax 回归的简洁实现
人工智能·数据挖掘·回归
YCCX_XFF213 小时前
ImportError: DLL load failed while importing _imaging: 操作系统无法运行 %1
开发语言·python
哥廷根数学学派4 小时前
基于Maximin的异常检测方法(MATLAB)
开发语言·人工智能·深度学习·机器学习
xrgs_shz4 小时前
人工智能、机器学习、神经网络、深度学习和卷积神经网络的概念和关系
人工智能·深度学习·神经网络·机器学习·卷积神经网络
FutureUniant5 小时前
GitHub每日最火火火项目(7.7)
python·计算机视觉·ai·github·视频
杰哥在此5 小时前
Java面试题:讨论持续集成/持续部署的重要性,并描述如何在项目中实施CI/CD流程
java·开发语言·python·面试·编程