Building AI Applications with Microsoft Semantic Kernel

Book description

Unlock the power of GenAI by effortlessly linking your C# and Python apps with cutting-edge models, orchestrating diverse AI services with finesse, and crafting bespoke applications through immersive, real-world examples

Key Features

  • 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

Book 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.

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

Who this book is 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.

Table of contents

  1. Building AI Applications with Microsoft Semantic Kernel
  2. Contributors
  3. About the author
  4. About the reviewer
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Conventions used
    6. Get in touch
    7. Share Your Thoughts
    8. Download a free PDF copy of this book
  6. Part 1:Introduction to Generative AI and Microsoft Semantic Kernel
  7. Chapter 1: Introducing Microsoft Semantic Kernel
    1. Technical requirements
      1. Obtaining an OpenAI API key
      2. Obtaining an Azure OpenAI API key
    2. Generative AI and how to use it
      1. Text generation models
      2. Understanding the difference between applications and models
      3. Generating text using consumer applications
      4. Generating images
    3. Microsoft Semantic Kernel
      1. Installing the Microsoft Semantic Kernel package
    4. Using Semantic Kernel to connect to AI services
      1. Connecting to OpenAI Services using Python
      2. Connecting to OpenAI services using C#
    5. Running a simple prompt
      1. Running a simple prompt in Python
      2. Running a simple prompt in C#
    6. Using generative AI to solve simple problems
      1. Creating semantic functions
      2. Creating native functions
    7. Plugins
      1. The config.json file for the knock-knock joke function
      2. The skprompt.txt file for the knock-knock joke function
      3. The config.json file for the semantic function that explains jokes
      4. The skprompt.txt file for the explain joke function
      5. Loading the plugin from a directory into the kernel
    8. Using a planner to run a multistep task
      1. Calling the Function Calling Stepwise planner with Python
    9. Summary
    10. References
  8. Chapter 2: Creating Better Prompts
    1. Technical requirements
    2. A simple plugin template
      1. The skprompt.txt file
      2. The config.json file
      3. Calling the plugin from Python
      4. Calling the plugin from C#
      5. Results
    3. Improving the prompt to get better results
      1. Revising the skprompt.txt file
      2. The result
    4. Prompts with multiple variables
      1. Requesting a complex itinerary with Python
      2. Requesting a complex itinerary with C#
      3. The result of the complex itinerary
    5. Issues when answering math problems
    6. Multistage prompts
      1. CoT – “Let’s think step by step”
      2. Implementing CoT with Python
      3. Implementing CoT with C#
      4. Results for CoT
      5. An ensemble of answers
    7. Summary
    8. References
  9. Part 2: Creating AI Applications with Semantic Kernel
  10. Chapter 3: Extending Semantic Kernel
    1. Technical requirements
    2. Getting to know the core plugins
      1. An example – Using the TimePlugin
    3. Introducing the application – Validating grants
      1. Directory structure of our application
    4. Developing native plugins
      1. The directory structure of our plugins
      2. Checking the structure of our Excel spreadsheet
      3. Additional checks
      4. Evaluating the Word document
    5. Developing semantic plugins
      1. Evaluating the grant proposal with a semantic plugin
    6. Summary
  11. Chapter 4: Performing Complex Actions by Chaining Functions
    1. Technical requirements
    2. Creating a native plugin that generates images
      1. Writing a DALL-E 3 wrapper in Python
      2. Writing a DALL-E 3 wrapper in C#
    3. Using multiple steps to solve a problem
      1. Generating an image from a clue
      2. Chaining semantic and native functions with C#
      3. Chaining semantic and native functions with Python
    4. Dealing with larger, more complex chains
      1. Preparing our directory structure
      2. Understanding the flow of our process
      3. Creating the native function to process a folder
      4. Modifying the Excel native plugin
      5. Modifying the Word native plugin
      6. Modifying the semantic functions
    5. Creating and calling the pipeline
    6. Summary
    7. References
  12. Chapter 5: Programming with Planners
    1. Technical requirements
    2. What is a planner?
    3. When to use a planner
    4. Instantiating a planner
    5. Creating and running a plan
      1. An example of how a planner can help
    6. How do planners work?
    7. Controlling home automation with the planner
      1. Creating the native functions
      2. Adding a semantic function to suggest movies
      3. Invoking the planner
    8. Summary
  13. Chapter 6: Adding Memories to Your AI Application
    1. Technical requirements
    2. Defining memory and embeddings
      1. How does semantic memory work?
      2. Embeddings in action
    3. Using memory within chats and LLMs
      1. Using memory with Microsoft Semantic Kernel
      2. Using memory in chats
      3. Reducing history size with summarization
    4. Summary
  14. Part 3: Real-World Use Cases
  15. Chapter 7: Real-World Use Case – Retrieval-Augmented Generation
    1. Technical requirements
    2. Why would you need to customize GPT models?
    3. Retrieval-augmented generation
      1. Creating an index
      2. Uploading documents to the index
      3. Using the index to find academic articles
      4. Using RAG to create a summary of several articles on a topic
    4. Summary
    5. References
  16. Chapter 8: Real-World Use Case – Making Your Application Available on ChatGPT
    1. Technical requirements
    2. Custom GPT agents
      1. Creating a custom GPT
      2. Creating a custom GPT that supports actions
      3. Creating a web API wrapper for the native function
      4. Deploying your application to an Azure Web App
      5. Connecting the custom GPT with your custom GPT action
    3. Summary
  17. Index
    1. Why subscribe?
  18. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: Building AI Applications with Microsoft Semantic Kernel
  • Author(s): Lucas A. Meyer
  • Release date: June 2024
  • Publisher(s): Packt Publishing
  • ISBN: 9781835463703