Sunday, June 30, 2024

Install Microsoft Semantic Kernel Locally and Integrate in Your App

 This video installs Semantic Kernel locally and shows how it works. Semantic Kernel is an SDK that integrates LLMs like OpenAI, Azure OpenAI, and Hugging Face with conventional programming languages like C#, Python, and Java.


import asyncio
from semantic_kernel import Kernel
from import OpenAIChatCompletion, AzureChatCompletion
from semantic_kernel.prompt_template import PromptTemplateConfig

import os
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())

kernel = Kernel()

# Prepare OpenAI service using credentials stored in the `.env` file

# Define the request settings
req_settings = kernel.get_prompt_execution_settings_from_service_id(service_id)
req_settings.max_tokens = 2000
req_settings.temperature = 0.7
req_settings.top_p = 0.8

prompt = """
1) A robot may not injure a human being or, through inaction,
allow a human being to come to harm.

2) A robot must obey orders given it by human beings except where
such orders would conflict with the First Law.

3) A robot must protect its own existence as long as such protection
does not conflict with the First or Second Law.

Give me the TLDR in exactly 5 words."""

prompt_template_config = PromptTemplateConfig(

function = kernel.add_function(

# Run your prompt
# Note: functions are run asynchronously
async def main():
    result = await kernel.invoke(function)
    print(result) # => Robots must not harm humans.

if __name__ == "__main__":
# If running from a jupyter-notebook:
# await main()

No comments: