{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Basic Loading of the Kernel" ] }, { "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!python -m pip install semantic-kernel==0.5.1.dev0" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import semantic_kernel as sk\n", "from semantic_kernel.connectors.ai.open_ai import (\n", " AzureChatCompletion,\n", " OpenAIChatCompletion,\n", ")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "You can instantiate the kernel in a few ways, depending on your use case." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Simple instance\n", "kernel_1 = sk.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, other services will be added over time.\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)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "kernel = sk.Kernel()\n", "\n", "kernel.add_chat_service( # We are adding a text service\n", " \"Azure_curie\", # The alias we can use in prompt templates' config.json\n", " AzureChatCompletion(\n", " deployment_name=\"my-finetuned-Curie\", # Azure OpenAI *Deployment name*\n", " endpoint=\"https://contoso.openai.azure.com/\", # Azure OpenAI *Endpoint*\n", " api_key=\"...your Azure OpenAI Key...\", # Azure OpenAI *Key*\n", " ),\n", ")\n", "\n", "kernel.add_chat_service( # We are adding a text service\n", " \"OpenAI_chat_gpt\", # The alias we can use in prompt templates' config.json\n", " OpenAIChatCompletion(\n", " ai_model_id=\"gpt-3.5-turbo\", # OpenAI Model Name\n", " api_key=\"...your OpenAI API Key...\", # OpenAI API key\n", " org_id=\"...your OpenAI Org ID...\", # *optional* OpenAI Organization ID\n", " ),\n", ")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "When working with multiple services and multiple models, the **first service** defined\n", "is also the \"**default**\" used in these scenarios:\n", "\n", "* a prompt configuration doesn't specify which AI service to use\n", "* a prompt configuration requires a service unknown to the kernel\n", "\n", "The default can be set and changed programmatically:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "kernel.set_default_text_completion_service(\"Azure_curie\")" ] }, { "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)." ] } ], "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 }