Files
semantic-kernel/python/notebooks/01-basic-loading-the-kernel.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

136 lines
3.6 KiB
Plaintext

{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Basic Loading of the Kernel\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"To run the notebooks we recommend using Poetry and starting a shell with a virtual environment\n",
"prepared to use SK.\n",
"\n",
"See [DEV_SETUP.md](../../python/DEV_SETUP.md) for more information.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!python -m pip install semantic-kernel==0.9.6b1"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from semantic_kernel import Kernel, kernel\n",
"\n",
"kernel = Kernel()"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"When using the kernel for AI requests, the kernel needs some settings like URL and credentials to the AI models.\n",
"\n",
"The SDK currently supports OpenAI and Azure OpenAI, among other connectors.\n",
"\n",
"If you need an Azure OpenAI key, go [here](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/quickstart?pivots=rest-api).\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from services import Service\n",
"\n",
"# Select a service to use for this notebook (available services: OpenAI, AzureOpenAI, HuggingFace)\n",
"selectedService = Service.OpenAI"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"service_id = None\n",
"if selectedService == Service.OpenAI:\n",
" from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion\n",
" from semantic_kernel.utils.settings import openai_settings_from_dot_env\n",
"\n",
" api_key, org_id = openai_settings_from_dot_env()\n",
" service_id = \"oai_chat_gpt\"\n",
" kernel.add_service(\n",
" OpenAIChatCompletion(service_id=service_id, ai_model_id=\"gpt-3.5-turbo-1106\", api_key=api_key, org_id=org_id),\n",
" )\n",
"elif selectedService == Service.AzureOpenAI:\n",
" from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion\n",
" from semantic_kernel.utils.settings import azure_openai_settings_from_dot_env\n",
"\n",
" deployment, api_key, endpoint = azure_openai_settings_from_dot_env()\n",
" service_id = \"aoai_chat_completion\"\n",
" kernel.add_service(\n",
" AzureChatCompletion(service_id=service_id, deployment_name=deployment, endpoint=endpoint, api_key=api_key),\n",
" )"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Great, now that you're familiar with setting up the Semantic Kernel, let's see [how we can use it to run prompts](02-running-prompts-from-file.ipynb).\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
},
"polyglot_notebook": {
"kernelInfo": {
"items": [
{
"aliases": [
"frontend"
],
"name": "vscode"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}