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())
相关推荐
cici158742 小时前
卡尔曼滤波器实现RBF神经网络训练
人工智能·深度学习·神经网络
哥本哈士奇(aspnetx)4 小时前
提升 Text2SQL 准确率
大模型
QQ2422199796 小时前
基于python+微信小程序的家教管理系统_mh3j9
开发语言·python·微信小程序
Neolnfra6 小时前
拒绝数据“裸奔”!把顶级AI装进自己的硬盘,这款神仙开源工具我粉了
人工智能·开源·蓝耘maas
code_li6 小时前
只花了几分钟,用AI开发了一个微信小程序!(附教程)
人工智能·微信小程序·小程序
飞Link6 小时前
瑞萨联姻 Irida Labs:嵌入式开发者如何玩转“端侧视觉 AI”新范式?
人工智能
RSTJ_16256 小时前
PYTHON+AI LLM DAY THREETY-SEVEN
开发语言·人工智能·python
郝学胜-神的一滴6 小时前
深度学习优化核心:梯度下降与网络训练全解析
数据结构·人工智能·python·深度学习·算法·机器学习
Aision_6 小时前
Agent 为什么需要 Checkpoint?
人工智能·python·gpt·langchain·prompt·aigc·agi
清水白石0086 小时前
《Python性能深潜:从对象分配开销到“小对象风暴”的破解之道(含实战与最佳实践)》
开发语言·python