Files
semantic-kernel/python/notebooks/00-getting-started.ipynb
T
Eduard van Valkenburg 66a3b5bfea Python: init cleanup (#5872)
### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

When the root init has to load a lot of stuff, loading the whole thing
becomes slower, only one concept there Kernel, all other things must now
be loaded from a sub package.

The following is not the guidance:
- imports within SK use the full path
- init files are created as much as possible on the root+1 level (like
semantic_kernel.functions) with the pieces that common developers need,
except in connectors and utils, there a developer needs to go deeper
- within the connectors folder this is further detailed first to ai,
memory, search_engine, within ai it is further spread into the different
connectors, for instance everything for openai and azure openai can be
imported using `from semantic_kernel.connectors.ai.open_ai import ...`,
the same within memory
- imports in samples use the abbreviated path as much as possible.

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
2024-04-16 13:21:20 +00:00

4.4 KiB

Setup

Step 1: Import Semantic Kernel SDK from pypi.org

In [ ]:
!python -m pip install semantic-kernel==0.9.6b1
In [ ]:
from semantic_kernel import Kernel

kernel = Kernel()

Configure the service you'd like to use via the Service Enum.

In [ ]:
from services import Service

# Select a service to use for this notebook (available services: OpenAI, AzureOpenAI, HuggingFace)
selectedService = Service.OpenAI

Option 1: using OpenAI

Step 2: Add your OpenAI Key key to a .env file in the same folder (org Id only if you have multiple orgs):

OPENAI_API_KEY="sk-..."
OPENAI_ORG_ID=""

Use "keyword arguments" to instantiate an OpenAI Chat Completion service and add it to the kernel:

Option 2: using Azure OpenAI

Step 2: Add your Azure Open AI Service key settings to a .env file in the same folder:

AZURE_OPENAI_API_KEY="..."
AZURE_OPENAI_ENDPOINT="https://..."
AZURE_OPENAI_DEPLOYMENT_NAME="..."

Use "keyword arguments" to instantiate an Azure OpenAI Chat Completion service and add it to the kernel:

In [ ]:
from semantic_kernel.utils.settings import azure_openai_settings_from_dot_env, openai_settings_from_dot_env

service_id = None
if selectedService == Service.OpenAI:
    from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion

    api_key, org_id = openai_settings_from_dot_env()
    service_id = "default"
    kernel.add_service(
        OpenAIChatCompletion(service_id=service_id, ai_model_id="gpt-3.5-turbo-1106", api_key=api_key, org_id=org_id),
    )
elif selectedService == Service.AzureOpenAI:
    from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion

    deployment, api_key, endpoint = azure_openai_settings_from_dot_env()
    service_id = "default"
    kernel.add_service(
        AzureChatCompletion(service_id=service_id, deployment_name=deployment, endpoint=endpoint, api_key=api_key),
    )

Run a Semantic Function

Step 3: Load a Plugin and run a semantic function:

In [ ]:
plugin = kernel.add_plugin(parent_directory="../../samples/plugins", plugin_name="FunPlugin")
In [ ]:
from semantic_kernel.functions import KernelArguments

joke_function = plugin["Joke"]

joke = await kernel.invoke(joke_function, KernelArguments(input="time travel to dinosaur age", style="super silly"))
print(joke)