paint-brush
人工智能 100 天,第 8 天:使用 GPT-4 试验 Microsoft 语义内核经过@sindamnataraj
821 讀數
821 讀數

人工智能 100 天,第 8 天:使用 GPT-4 试验 Microsoft 语义内核

经过 Nataraj6m2024/01/31
Read on Terminal Reader

太長; 讀書

Semantic Kernel 是 Microsoft 的开源 SDK,可帮助开发人员创建 AI 应用程序,包括聊天机器人、RAG、Copilot 和代理。
featured image - 人工智能 100 天,第 8 天:使用 GPT-4 试验 Microsoft 语义内核
Nataraj HackerNoon profile picture
0-item
1-item


嘿大家! 我是 Nataraj和您一样,我也对人工智能的最新进展着迷。意识到我需要跟上所有正在发生的发展,我决定踏上个人学习之旅,于是人工智能100天诞生了!通过这个系列,我将学习法学硕士,并通过我的博客文章分享想法、实验、观点、趋势和学习。您可以在此处的HackerNoon 或此处的我的个人网站上跟踪整个旅程。在今天的文章中,我们将寻求在 GPT-4 的帮助下构建语义内核。


Semantic Kernel 是 Microsoft 的开源 SDK,可帮助开发人员创建 AI 应用程序,包括聊天机器人、RAG、Copilot 和代理。它与 langchain 的作用类似。我们或许可以将其称为微软对 langchain 的回答。


它旨在使现有软件可扩展并易于使用人工智能功能。它的设计还考虑到应用程序希望随着时间的推移将其人工智能模型更新到最新和最好的版本。

语义内核


尽管这个领域的发展非常迅速,但在我们探索语义内核时,需要记住一些定义。

  • 聊天机器人:与用户进行简单的聊天。
  • RAG:简单的聊天机器人,但基于实时和私人数据。
  • 副驾驶:旨在通过推荐和建议并肩协助我们完成任务。
  • 代理:在有限的人为干预下对刺激做出反应。代理代表用户执行发送电子邮件、订票等任务。

语义内核如何工作?

为了解释语义内核的工作原理,我们以一段文本并将其转换为 140 个字符的推文为例。但我们将使用语义内核来做到这一点。我们在之前的帖子中也做过类似的总结


我将使用 Semantic Kernel 的 python 库,但由于 Semantic Kernel 是由 Microsoft 创建的,因此您也可以在 C# 中执行此操作。查看 Microsoft 的公共文档了解如何执行此操作。

第 1 步:启动语义内核

下面我们通过告诉它使用 OpenAI 的 gpt-4 模型作为用于文本补全的 LLM 来启动语义内核并设置其文本补全服务。

 import semantic_kernel as sk from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion, OpenAIChatCompletion import os from IPython.display import display, Markdown import asyncio from dotenv import load_dotenv, find_dotenv _ = load_dotenv(find_dotenv()) # read local .env file api_key = os.environ['OPENAI_API_KEY'] kernel = sk.Kernel() kernel.add_text_completion_service("openai", OpenAIChatCompletion("gpt-4",api_key)) print("Kernel Initiated")

第 2 步:理解语义功能:

在语义内核世界中,我们有一个语义函数的概念,它不同于本机函数。本机函数是我们用任何编程语言编写的常规函数。语义函数是可重复的 LLM 提示的封装,可以由内核编排。在下一步我们将编写一个语义函数时,您将更好地了解什么是语义函数。


本机和语义功能


第 3 步:创建语义函数

在这里,我们创建一个提示sk_prompt ,用不到 140 个字符总结连接(这是我们本次练习的目标)。然后,我们将提示作为输入传递,以使用内核创建语义函数并存储,这会返回对象summary_function ,该对象表示我们创建的语义函数,并且可以通过内核重复访问。请注意,当我们创建语义函数时,我们使用客户提示,并提供 LLM 配置信息,例如 max_tokens、温度等,现在返回到上一个本机函数与语义函数的图像,它将更有意义。


 sk_prompt = """ {{$input}} Summarize the content above in less than 140 characters. """ summary_function = kernel.create_semantic_function(prompt_template = sk_prompt, description="Summarizes the input to length of an old tweet.", max_tokens=200, temperature=0.1, top_p=0.5) print("A semantic function for summarization has been registered.")

步骤 4:使用语义函数将文本总结为 140 个字符的推文。

现在我们使用变量sk_input创建要总结的文本,并通过 kernal 调用语义函数,然后显示结果。

 sk_input = """ Let me illustrate an example. Many weekends, I drive a few minutes from my house to a local pizza store to buy a slice of Hawaiian pizza from the gentleman that owns this pizza store. And his pizza is great, but he always has a lot of cold pizzas sitting around, and every weekend some different flavor of pizza is out of stock. But when I watch him operate his store, I get excited, because by selling pizza, he is generating data. And this is data that he can take advantage of if he had access to AI. AI systems are good at spotting patterns when given access to the right data, and perhaps an AI system could spot if Mediterranean pizzas sell really well on a Friday night, maybe it could suggest to him to make more of it on a Friday afternoon. Now you might say to me, "Hey, Andrew, this is a small pizza store. What's the big deal?" And I say, to the gentleman that owns this pizza store, something that could help him improve his revenues by a few thousand dollars a year, that will be a huge deal to him. """ # using async to run the semantic function async def run_summary_async(): summary_result = await kernel.run_async(summary_function, input_str=sk_input) display(summary_result) asyncio.run(run_summary_async())


这是我得到的输出:


人工智能可以分析销售数据,帮助小型披萨店老板优化库存,从而有可能增加他的年收入。


语义内核具有更多功能,例如结合使用语义函数和本机函数,旨在创建强大的人工智能应用程序。我将在以后的帖子中详细介绍它们。


这就是《人工智能 100 天》的第 8 天。


我写了一篇名为“高于平均水平”的时事通讯,其中讨论了大型科技领域正在发生的一切背后的二阶见解。如果您从事科技行业并且不想成为平庸的人,请订阅它


TwitterLinkedIn或 ** HackerNoon ** 上关注我,了解 AI 100 天的最新动态。如果您从事技术工作,您可能有兴趣加入我的技术专业人士社区


前一天 7:如何使用 Langchain 为您的数据构建 Chat-GPT?