Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Building AI Applications with Microsoft Semantic Kernel
Building AI Applications with Microsoft Semantic Kernel

Building AI Applications with Microsoft Semantic Kernel : Easily integrate generative AI capabilities and copilot experiences into your applications

Arrow left icon
Profile Icon Lucas A. Meyer
Arrow right icon
$19.99 per month
Full star icon Full star icon Full star icon Full star icon Half star icon 4.3 (8 Ratings)
Paperback Jun 2024 252 pages 1st Edition
eBook
$9.99 $35.99
Paperback
$44.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon Lucas A. Meyer
Arrow right icon
$19.99 per month
Full star icon Full star icon Full star icon Full star icon Half star icon 4.3 (8 Ratings)
Paperback Jun 2024 252 pages 1st Edition
eBook
$9.99 $35.99
Paperback
$44.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$9.99 $35.99
Paperback
$44.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Building AI Applications with Microsoft Semantic Kernel

Introducing Microsoft Semantic Kernel

The generative artificial intelligence (GenAI) space is evolving quickly, with dozens of new products and services being launched weekly; it is becoming hard for developers to keep up with the ever-changing features and application programming interfaces (APIs) for each of the services. In this book, you will learn about Microsoft Semantic Kernel, an API that will make it a lot easier for you to use GenAI as a developer, making your code shorter, simpler, and more maintainable. Microsoft Semantic Kernel will allow you, as a developer, to use a single interface to connect with several different GenAI providers. Microsoft used Semantic Kernel to develop its copilots, such as Microsoft 365 Copilot.

Billions of people already use GenAI as consumers, and you are probably one of them. We will start this chapter by showing some examples of what you can do with GenAI as a consumer. Then, you will learn how you can start using GenAI as a developer to...

Technical requirements

To complete this chapter, you will need to have a recent, supported version of your preferred Python or C# development environment:

  • For Python, the minimum supported version is Python 3.10, and the recommended version is Python 3.11
  • For C#, the minimum supported version is .NET 8

Important note

The examples are presented in C# and Python, and you can choose to only read the examples of your preferred language. Occasionally, a feature is available in only one of the languages. In such cases, we provide an alternative in the other language for how to achieve the same objectives.

In this chapter, we will call OpenAI services. Given the amount that companies spend on training these large language models (LLMs), it’s no surprise that using these services is not free. You will need an OpenAI API key, obtained either directly through OpenAI or Microsoft, via the Azure OpenAI service.

Important: Using the OpenAI services is not free...

Generative AI and how to use it

Generative AI refers to a subset of artificial intelligence programs that are capable of creating content that is similar to what humans can produce. These systems use training from very large datasets to learn their patterns, styles, and structures. Then, they can generate entirely new content, such as synthesized images, music, and text.

Using GenAI as consumer or end-user is very easy, and as a technical person, you probably have already done it. There are many consumer-facing AI products. The most famous is OpenAI’s ChatGPT, but there are many others that have hundreds of millions of users every day, such as Microsoft Copilot, Google Gemini (formerly Bard), and Midjourney. As of October 2023, Meta, the parent company of Facebook, WhatsApp, and Instagram, is making GenAI services available to all its users, increasing the number of GenAI daily users to billions.

While the concept of GenAI has existed for a while, it gained a lot of users...

Microsoft Semantic Kernel

Microsoft Semantic Kernel (https://github.com/microsoft/semantic-kernel) is a thin, open source software development toolkit (SDK) that makes it easier for applications developed in C# and Python to interact with AI services such as the ones made available through OpenAI, Azure OpenAI, and Hugging Face. Semantic Kernel can receive requests from your application and route them to different AI services. Furthermore, if you extend the functionality of Semantic Kernel by adding your own functions, which we will explore in Chapter 3, Semantic Kernel can automatically discover which functions need to be used, and in which order, to fulfill a request. The request can come directly from the user and be passed through directly from your application, or your application can modify and enrich the user request before sending it to Semantic Kernel.

It was originally designed to power different versions of Microsoft Copilot, such as Microsoft 365 Copilot and the Bing...

Using Semantic Kernel to connect to AI services

To complete this section, you must have an API key. The process to obtain an API key was described at the beginning of this chapter.

In the upcoming subsections, we are only going to connect to the OpenAI text models GPT-3.5 and GPT-4. If you have access to the OpenAI models through Azure, you will need to make minor modifications to your code.

Although it would be simpler to connect to a single model, we are already going to show a simple but powerful Microsoft Semantic Kernel feature: we’re going to connect to two different models and run a simple prompt using the simpler but less expensive model, GPT-3.5, and a more complex prompt on the more advanced but also more expensive model, GPT-4.

This process of sending simpler requests to simpler models and more complex requests to more complex models is something that you will frequently do when creating your own applications. This approach is called LLM cascade, and it was...

Running a simple prompt

This section assumes you completed the prior sections and builds upon the same code. By now, you should have instantiated Semantic Kernel and loaded both the GPT-3.5 and the GPT-4 services into it in that order. When you submit a prompt, it will default to the first service, and will run the prompt on GPT-3.5.

When we send the prompt to the service, we will also send a parameter called temperature. The temperature parameter goes from 0.0 to 1.0, and it controls how random the responses are. We’re going to explain the temperature parameter in more detail in later chapters. A temperature parameter of 0.8 generates a more creative response, while a temperature parameter of 0.2 generates a more precise response.

To send the prompt to the service, we will use a method called create_semantic_function. For now, don’t worry about what a semantic function is. We’re going to explain it in the Using generative AI to solve simple problems section...

Using generative AI to solve simple problems

Microsoft Semantic Kernel distinguishes between two types of functions that can be loaded into it: semantic functions and native functions.

Semantic functions are functions that connect to AI services, usually LLMs, to perform a task. The service is not part of your codebase. Native functions are regular functions written in the language of your application.

The reason to differentiate a native function from any other regular function in your code is that the native function will have additional attributes that will tell the kernel what it does. When you load a native function into the kernel, you can use it in chains that combine native and semantic functions. In addition, Semantic Kernel planner can use the function when creating a plan to achieve a user goal.

Creating semantic functions

We have already created a semantic function (knock) in the previous section. Now, we’re going to add a parameter to it. The default...

Plugins

One of the greatest strengths of Microsoft Semantic Kernel is that you can create semantic plugins that are language agnostic. Semantic plugins are collections of semantic functions that can be imported into the kernel. Creating semantic plugins allows you to separate your code from the AI function, which makes your application easier to maintain. It also allows other people to work on the prompts, making it easier to implement prompt engineering, which will be explored in Chapter 2.

Each function is defined by a directory containing two text files: config.json, which contains the configuration for the semantic function, and skprompt.txt, which contains its prompt.

The configuration of the semantic function includes the preferred engine to use, the temperature parameter, and a description of what the semantic function does and its inputs.

The text file contains the prompt that will be sent to the AI service to generate the response.

In this section, we are going...

Using a planner to run a multistep task

Instead of calling functions yourself, you can let Microsoft Semantic Kernel choose the functions for you. This can make your code a lot simpler and can give your users the ability to combine your code in ways that you haven’t considered.

Right now, this will not seem very useful because we only have a few functions and plugins. However, in a large application, such as Microsoft Office, you may have hundreds or even thousands of plugins, and your users may want to combine them in ways that you can’t yet imagine. For example, you may be creating a copilot that helps a user be more efficient when learning about a subject, so you write a function that downloads the latest news about that subject from the web. You may also have independently created a function that explains a piece of text to the user so that the user can paste content to learn more about it. The user may decide to combine them both with “download the news...

Summary

In this chapter, you learned about Generative AI and the main components of Microsoft Semantic Kernel. You learned how to create a prompt and submit it to a service and how to embed that prompt into a semantic function. You also learned how to execute multistep requests by using a planner.

In the next chapter, we are going to learn how to make our prompts better through a topic called prompt engineering. This will help you create prompts that get your users the correct result faster and use fewer tokens, therefore reducing costs.

References

[1] A. Vaswani et al., “Attention Is All You Need,” Jun. 2017.

[2] OpenAI, “GPT-4 Technical Report.” arXiv, Mar. 27, 2023. doi: 10.48550/arXiv.2303.08774.

[3] L. Chen, M. Zaharia, and J. Zou, “FrugalGPT: How to Use Large Language Models While Reducing Cost and Improving Performance.” arXiv, May 09, 2023. doi: 10.48550/arXiv.2305.05176.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Link your C# and Python applications with the latest AI models from OpenAI
  • Combine and orchestrate different AI services such as text and image generators
  • Create your own AI apps with real-world use case examples that show you how to use basic generative AI, create images, process documents, use a vector database
  • Purchase of the print or Kindle book includes a free PDF eBook

Description

In the fast-paced world of AI, developers are constantly seeking efficient ways to integrate AI capabilities into their apps. Microsoft Semantic Kernel simplifies this process by using the GenAI features from Microsoft and OpenAI. Written by Lucas A. Meyer, a Principal Research Scientist in Microsoft’s AI for Good Lab, this book helps you get hands on with Semantic Kernel. It begins by introducing you to different generative AI services such as GPT-3.5 and GPT-4, demonstrating their integration with Semantic Kernel. You’ll then learn to craft prompt templates for reuse across various AI services and variables. Next, you’ll learn how to add functionality to Semantic Kernel by creating your own plugins. The second part of the book shows you how to combine multiple plugins to execute complex actions, and how to let Semantic Kernel use its own AI to solve complex problems by calling plugins, including the ones made by you. The book concludes by teaching you how to use vector databases to expand the memory of your AI services and how to help AI remember the context of earlier requests. You’ll also be guided through several real-world examples of applications, such as RAG and custom GPT agents. By the end of this book, you'll have gained the knowledge you need to start using Semantic Kernel to add AI capabilities to your applications.

Who is this book for?

This book is for beginner-level to experienced .NET or Python software developers who want to quickly incorporate the latest AI technologies into their applications, without having to learn the details of every new AI service. Product managers with some development experience will find this book helpful while creating proof-of-concept applications. This book requires working knowledge of programming basics.

What you will learn

  • Write reusable AI prompts and connect to different AI providers
  • Create new plugins that extend the capabilities of AI services
  • Understand how to combine multiple plugins to execute complex actions
  • Orchestrate multiple AI services to accomplish a task
  • Leverage the powerful planner to automatically create appropriate AI calls
  • Use vector databases as additional memory for your AI tasks
  • Deploy your application to ChatGPT, making it available to hundreds of millions of users

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jun 21, 2024
Length: 252 pages
Edition : 1st
Language : English
ISBN-13 : 9781835463703
Category :
Languages :
Concepts :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Jun 21, 2024
Length: 252 pages
Edition : 1st
Language : English
ISBN-13 : 9781835463703
Category :
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 149.97
Building LLM Powered  Applications
$49.99
Building AI Applications with Microsoft Semantic Kernel
$44.99
Web API Development with ASP.NET Core 8
$54.99
Total $ 149.97 Stars icon
Banner background image

Table of Contents

13 Chapters
Part 1:Introduction to Generative AI and Microsoft Semantic Kernel Chevron down icon Chevron up icon
Chapter 1: Introducing Microsoft Semantic Kernel Chevron down icon Chevron up icon
Chapter 2: Creating Better Prompts Chevron down icon Chevron up icon
Part 2: Creating AI Applications with Semantic Kernel Chevron down icon Chevron up icon
Chapter 3: Extending Semantic Kernel Chevron down icon Chevron up icon
Chapter 4: Performing Complex Actions by Chaining Functions Chevron down icon Chevron up icon
Chapter 5: Programming with Planners Chevron down icon Chevron up icon
Chapter 6: Adding Memories to Your AI Application Chevron down icon Chevron up icon
Part 3: Real-World Use Cases Chevron down icon Chevron up icon
Chapter 7: Real-World Use Case – Retrieval-Augmented Generation Chevron down icon Chevron up icon
Chapter 8: Real-World Use Case – Making Your Application Available on ChatGPT Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.3
(8 Ratings)
5 star 62.5%
4 star 25%
3 star 0%
2 star 0%
1 star 12.5%
Filter icon Filter
Top Reviews

Filter reviews by




Om S Jul 26, 2024
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is a fantastic resource for anyone looking to integrate AI into their applications. Lucas A. Meyer breaks down complex concepts into easy-to-understand steps, making it accessible even for beginners. The book covers how to link C# and Python applications with OpenAI models and use various AI services, such as text and image generators.What I loved most were the real-world examples. They provide practical insights on using generative AI, creating images, processing documents, and using a vector database. The inclusion of these examples helps you see the potential applications in a tangible way.The book is well-structured, starting from basic introductions to more advanced topics like creating and combining plugins. It also explains how to use vector databases for memory expansion, which is crucial for more complex AI tasks. Great addition to my tech library!!
Amazon Verified review Amazon
Lucas Puskaric Jun 26, 2024
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I did the technical review on this book, but it's truly a banger. Highly recommend
Amazon Verified review Amazon
Mojeed Abisiga Sep 09, 2024
Full star icon Full star icon Full star icon Full star icon Full star icon 5
If you are a professional in the Gen AI space looking to up your game, this Lucas's book is a must have! It offers a great deep dive into prompt engineering and building robust AI applications with Microsoft's Semantic Kernel SDK.What makes it stand out?Novel Code Design: A fresh approach to managing AI functionalities, similar to the leap classes brought to object-oriented programming.Seamless AI Integration: Streamlines the use of LLMs and other AI models for more efficient and powerful development.Flexible & Familiar: Innovates while allowing for the use of traditional Python functions - which makes the transition smooth for developers.This book is highly recommended for professionals eager to enhance their Gen AI skills.
Amazon Verified review Amazon
Chandra Jul 28, 2024
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is a great resource for Generative AI professionals seeking to build applications that require prompt engineering to construct RAGs and custom OpenAI apps. Lucas provides a high-level overview of Microsoft's Semantic Kernel SDK. This SDK integrates OpenAI, Azure OpenAI, and Hugging Face with Java, C#, and Python. Consequently, it's a valuable tool for software engineers to leverage their programming language expertise for developing Gen AI applications. Lucas offers lucid explanations of various topics including prompt engineering, image generation, document processing, and RAGs. Highly recommended for professionals eager to enhance their Gen AI skills.
Amazon Verified review Amazon
Alexander Ashish Sep 09, 2024
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I recently read this book which is an amazing guide for Gen AI professionals seeking to build applications that require prompt engineering to construct RAGs and custom OpenAI apps.I am sharing some key points from this book here 👩🏻‍💻📗 This book provides a proper details about the Semantic Kernel’s design which supports independent development and testing of components, perfect for building scalable and maintainable large-scale applications.📘 The kernel can dynamically combine functions which enables the creation of new workflows without additional coding which is ideal for complex applications like MS Office.📙 The book talk about the supports for both Core Plugins and custom semantic plugins which allows developers to create amazing workflows using AI services like OpenAI, without impacting the overall system.📔 The book offers practical guidance on CoT prompt templating and RAG application development within the Semantic Kernel framework.Well this book is around 250 pages only which makes it easier to explain the GEN AI usages and copilot experience in our applications .
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.