diff --git a/python/notebooks/00-getting-started.ipynb b/python/notebooks/00-getting-started.ipynb index db08ad662d..2522202102 100644 --- a/python/notebooks/00-getting-started.ipynb +++ b/python/notebooks/00-getting-started.ipynb @@ -57,7 +57,9 @@ "\n", "api_key, org_id = sk.openai_settings_from_dot_env()\n", "\n", - "kernel.add_chat_service(\"chat-gpt\", OpenAIChatCompletion(ai_model_id=\"gpt-3.5-turbo-1106\", api_key=api_key, org_id=org_id))" + "kernel.add_chat_service(\n", + " \"chat-gpt\", OpenAIChatCompletion(ai_model_id=\"gpt-3.5-turbo-1106\", api_key=api_key, org_id=org_id)\n", + ")" ] }, { @@ -88,7 +90,9 @@ "\n", "deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()\n", "\n", - "kernel.add_chat_service(\"chat_completion\", AzureChatCompletion(deployment_name=deployment, endpoint=endpoint, api_key=api_key))\n" + "kernel.add_chat_service(\n", + " \"chat_completion\", AzureChatCompletion(deployment_name=deployment, endpoint=endpoint, api_key=api_key)\n", + ")" ] }, { diff --git a/python/notebooks/01-basic-loading-the-kernel.ipynb b/python/notebooks/01-basic-loading-the-kernel.ipynb index 87dbadb87f..2e8ad8be31 100644 --- a/python/notebooks/01-basic-loading-the-kernel.ipynb +++ b/python/notebooks/01-basic-loading-the-kernel.ipynb @@ -79,13 +79,13 @@ "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", + "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", + " 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", diff --git a/python/notebooks/07-hugging-face-for-skills.ipynb b/python/notebooks/07-hugging-face-for-skills.ipynb index c4ce3fb2d4..eb22dae2ba 100644 --- a/python/notebooks/07-hugging-face-for-skills.ipynb +++ b/python/notebooks/07-hugging-face-for-skills.ipynb @@ -1,192 +1,170 @@ { - "cells": [ - { - "attachments": {}, - "cell_type": "markdown", - "id": "68e1c158", - "metadata": {}, - "source": [ - "# Using Hugging Face With Skills\n", - "\n", - "In this notebook, we demonstrate using Hugging Face models for Skills using both SemanticMemory and text completions. \n", - "\n", - "SK supports downloading models from the Hugging Face that can perform the following tasks: text-generation, text2text-generation, summarization, and sentence-similarity. You can search for models by task at https://huggingface.co/models." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a77bdf89", - "metadata": {}, - "outputs": [], - "source": [ - "!python -m pip install semantic-kernel==0.4.4.dev0\n", - "\n", - "# Note that additional dependencies are required for the Hugging Face connectors:\n", - "!python -m pip install torch==2.0.0\n", - "!python -m pip install transformers==^4.28.1\n", - "!python -m pip install sentence-transformers==^2.2.2" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "508ad44f", - "metadata": {}, - "outputs": [], - "source": [ - "import semantic_kernel as sk\n", - "import semantic_kernel.connectors.ai.hugging_face as sk_hf" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "id": "d8ddffc1", - "metadata": {}, - "source": [ - "First, we will create a kernel and add both text completion and embedding services. \n", - "\n", - "For text completion, we are choosing GPT2. This is a text-generation model. (Note: text-generation will repeat the input in the output, text2text-generation will not.)\n", - "For embeddings, we are using sentence-transformers/all-MiniLM-L6-v2. Vectors generated for this model are of length 384 (compared to a length of 1536 from OpenAI ADA).\n", - "\n", - "The following step may take a few minutes when run for the first time as the models will be downloaded to your local machine." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "8f8dcbc6", - "metadata": {}, - "outputs": [], - "source": [ - "kernel = sk.Kernel()\n", - "\n", - "# Configure LLM service\n", - "kernel.add_text_completion_service(\n", - " service_id=\"gpt2\", \n", - " service=sk_hf.HuggingFaceTextCompletion(ai_model_id=\"gpt2\", task=\"text-generation\")\n", - ")\n", - "kernel.add_text_embedding_generation_service(\n", - " service_id=\"sentence-transformers/all-MiniLM-L6-v2\",\n", - " service=sk_hf.HuggingFaceTextEmbedding(ai_model_id=\"sentence-transformers/all-MiniLM-L6-v2\"),\n", - ")\n", - "kernel.register_memory_store(memory_store=sk.memory.VolatileMemoryStore())\n", - "kernel.import_skill(sk.core_skills.TextMemorySkill())" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "id": "2a7e7ca4", - "metadata": {}, - "source": [ - "### Add Memories and Define a skill to use them\n", - "\n", - "Most models available on huggingface.co are not as powerful as OpenAI GPT-3+. Your skills will likely need to be simpler to accommodate this." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "d096504c", - "metadata": {}, - "outputs": [], - "source": [ - "await kernel.memory.save_information_async(\n", - " collection=\"animal-facts\", id=\"info1\", text=\"Sharks are fish.\"\n", - ")\n", - "await kernel.memory.save_information_async(\n", - " collection=\"animal-facts\", id=\"info2\", text=\"Whales are mammals.\"\n", - ")\n", - "await kernel.memory.save_information_async(\n", - " collection=\"animal-facts\", id=\"info3\", text=\"Penguins are birds.\"\n", - ")\n", - "await kernel.memory.save_information_async(\n", - " collection=\"animal-facts\", id=\"info4\", text=\"Dolphins are mammals.\"\n", - ")\n", - "await kernel.memory.save_information_async(\n", - " collection=\"animal-facts\", id=\"info5\", text=\"Flies are insects.\"\n", - ")\n", - "\n", - "# Define semantic function using SK prompt template language\n", - "my_prompt = \"\"\"I know these animal facts: {{recall $query1}} {{recall $query2}} {{recall $query3}} and \"\"\"\n", - "\n", - "# Create the semantic function\n", - "my_function = kernel.create_semantic_function(\n", - " prompt_template=my_prompt, max_tokens=45, temperature=0.5, top_p=0.5\n", - ")\n" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "id": "2calf857", - "metadata": {}, - "source": [ - "Let's now see what the completion looks like! Remember, \"gpt2\" is nowhere near as large as ChatGPT, so expect a much simpler answer." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "628c843e", - "metadata": {}, - "outputs": [], - "source": [ - "context = kernel.create_new_context()\n", - "context[sk.core_skills.TextMemorySkill.COLLECTION_PARAM] = \"animal-facts\"\n", - "context[sk.core_skills.TextMemorySkill.RELEVANCE_PARAM] = \"0.3\"\n", - "\n", - "context[\"query1\"] = \"animal that swims\"\n", - "context[\"query2\"] = \"animal that flies\"\n", - "context[\"query3\"] = \"penguins are?\"\n", - "output = await kernel.run_async(my_function, input_vars=context.variables)\n", - "\n", - "output = str(output).strip()\n", - "\n", - "query_result1 = await kernel.memory.search_async(\n", - " collection=\"animal-facts\",\n", - " query=context[\"query1\"],\n", - " limit=1,\n", - " min_relevance_score=0.3\n", - ")\n", - "query_result2 = await kernel.memory.search_async(\n", - " collection=\"animal-facts\",\n", - " query=context[\"query2\"],\n", - " limit=1,\n", - " min_relevance_score=0.3\n", - ")\n", - "query_result3 = await kernel.memory.search_async(\n", - " collection=\"animal-facts\",\n", - " query=context[\"query3\"],\n", - " limit=1,\n", - " min_relevance_score=0.3\n", - ")\n", - "\n", - "print(f\"gpt2 completed prompt with: '{output}'\")" - ] - } - ], - "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" - } + "cells": [ + { + "attachments": {}, + "cell_type": "markdown", + "id": "68e1c158", + "metadata": {}, + "source": [ + "# Using Hugging Face With Skills\n", + "\n", + "In this notebook, we demonstrate using Hugging Face models for Skills using both SemanticMemory and text completions. \n", + "\n", + "SK supports downloading models from the Hugging Face that can perform the following tasks: text-generation, text2text-generation, summarization, and sentence-similarity. You can search for models by task at https://huggingface.co/models." + ] }, - "nbformat": 4, - "nbformat_minor": 5 + { + "cell_type": "code", + "execution_count": null, + "id": "a77bdf89", + "metadata": {}, + "outputs": [], + "source": [ + "!python -m pip install semantic-kernel==0.4.4.dev0\n", + "\n", + "# Note that additional dependencies are required for the Hugging Face connectors:\n", + "!python -m pip install torch==2.0.0\n", + "!python -m pip install transformers==^4.28.1\n", + "!python -m pip install sentence-transformers==^2.2.2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "508ad44f", + "metadata": {}, + "outputs": [], + "source": [ + "import semantic_kernel as sk\n", + "import semantic_kernel.connectors.ai.hugging_face as sk_hf" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "d8ddffc1", + "metadata": {}, + "source": [ + "First, we will create a kernel and add both text completion and embedding services. \n", + "\n", + "For text completion, we are choosing GPT2. This is a text-generation model. (Note: text-generation will repeat the input in the output, text2text-generation will not.)\n", + "For embeddings, we are using sentence-transformers/all-MiniLM-L6-v2. Vectors generated for this model are of length 384 (compared to a length of 1536 from OpenAI ADA).\n", + "\n", + "The following step may take a few minutes when run for the first time as the models will be downloaded to your local machine." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8f8dcbc6", + "metadata": {}, + "outputs": [], + "source": [ + "kernel = sk.Kernel()\n", + "\n", + "# Configure LLM service\n", + "kernel.add_text_completion_service(\n", + " service_id=\"gpt2\", service=sk_hf.HuggingFaceTextCompletion(ai_model_id=\"gpt2\", task=\"text-generation\")\n", + ")\n", + "kernel.add_text_embedding_generation_service(\n", + " service_id=\"sentence-transformers/all-MiniLM-L6-v2\",\n", + " service=sk_hf.HuggingFaceTextEmbedding(ai_model_id=\"sentence-transformers/all-MiniLM-L6-v2\"),\n", + ")\n", + "kernel.register_memory_store(memory_store=sk.memory.VolatileMemoryStore())\n", + "kernel.import_skill(sk.core_skills.TextMemorySkill())" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "2a7e7ca4", + "metadata": {}, + "source": [ + "### Add Memories and Define a skill to use them\n", + "\n", + "Most models available on huggingface.co are not as powerful as OpenAI GPT-3+. Your skills will likely need to be simpler to accommodate this." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d096504c", + "metadata": {}, + "outputs": [], + "source": [ + "await kernel.memory.save_information_async(collection=\"animal-facts\", id=\"info1\", text=\"Sharks are fish.\")\n", + "await kernel.memory.save_information_async(collection=\"animal-facts\", id=\"info2\", text=\"Whales are mammals.\")\n", + "await kernel.memory.save_information_async(collection=\"animal-facts\", id=\"info3\", text=\"Penguins are birds.\")\n", + "await kernel.memory.save_information_async(collection=\"animal-facts\", id=\"info4\", text=\"Dolphins are mammals.\")\n", + "await kernel.memory.save_information_async(collection=\"animal-facts\", id=\"info5\", text=\"Flies are insects.\")\n", + "\n", + "# Define semantic function using SK prompt template language\n", + "my_prompt = \"\"\"I know these animal facts: {{recall $query1}} {{recall $query2}} {{recall $query3}} and \"\"\"\n", + "\n", + "# Create the semantic function\n", + "my_function = kernel.create_semantic_function(prompt_template=my_prompt, max_tokens=45, temperature=0.5, top_p=0.5)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "2calf857", + "metadata": {}, + "source": [ + "Let's now see what the completion looks like! Remember, \"gpt2\" is nowhere near as large as ChatGPT, so expect a much simpler answer." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "628c843e", + "metadata": {}, + "outputs": [], + "source": [ + "context = kernel.create_new_context()\n", + "context[sk.core_skills.TextMemorySkill.COLLECTION_PARAM] = \"animal-facts\"\n", + "context[sk.core_skills.TextMemorySkill.RELEVANCE_PARAM] = \"0.3\"\n", + "\n", + "context[\"query1\"] = \"animal that swims\"\n", + "context[\"query2\"] = \"animal that flies\"\n", + "context[\"query3\"] = \"penguins are?\"\n", + "output = await kernel.run_async(my_function, input_vars=context.variables)\n", + "\n", + "output = str(output).strip()\n", + "\n", + "query_result1 = await kernel.memory.search_async(\n", + " collection=\"animal-facts\", query=context[\"query1\"], limit=1, min_relevance_score=0.3\n", + ")\n", + "query_result2 = await kernel.memory.search_async(\n", + " collection=\"animal-facts\", query=context[\"query2\"], limit=1, min_relevance_score=0.3\n", + ")\n", + "query_result3 = await kernel.memory.search_async(\n", + " collection=\"animal-facts\", query=context[\"query3\"], limit=1, min_relevance_score=0.3\n", + ")\n", + "\n", + "print(f\"gpt2 completed prompt with: '{output}'\")" + ] + } + ], + "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" + } + }, + "nbformat": 4, + "nbformat_minor": 5 } diff --git a/python/notebooks/08-native-function-inline.ipynb b/python/notebooks/08-native-function-inline.ipynb index 588cde4465..1f59cafa36 100644 --- a/python/notebooks/08-native-function-inline.ipynb +++ b/python/notebooks/08-native-function-inline.ipynb @@ -66,7 +66,9 @@ "# Configure AI service used by the kernel\n", "if useAzureOpenAI:\n", " deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()\n", - " azure_chat_service = AzureChatCompletion(deployment_name=\"turbo\", endpoint=endpoint, api_key=api_key) # set the deployment name to the value of your chat model\n", + " azure_chat_service = AzureChatCompletion(\n", + " deployment_name=\"turbo\", endpoint=endpoint, api_key=api_key\n", + " ) # set the deployment name to the value of your chat model\n", " kernel.add_chat_service(\"chat_completion\", azure_chat_service)\n", "else:\n", " api_key, org_id = sk.openai_settings_from_dot_env()\n", @@ -102,15 +104,13 @@ "import random\n", "from semantic_kernel.skill_definition import sk_function\n", "\n", + "\n", "class GenerateNumberSkill:\n", " \"\"\"\n", " Description: Generate a number between 3-x.\n", " \"\"\"\n", "\n", - " @sk_function(\n", - " description=\"Generate a random number between 3-x\",\n", - " name=\"GenerateNumberThreeOrHigher\"\n", - " )\n", + " @sk_function(description=\"Generate a random number between 3-x\", name=\"GenerateNumberThreeOrHigher\")\n", " def generate_number_three_or_higher(self, input: str) -> str:\n", " \"\"\"\n", " Generate a number between 3-\n", @@ -122,7 +122,7 @@ " int value\n", " \"\"\"\n", " try:\n", - " return str(random.randint(3, int(input))) \n", + " return str(random.randint(3, int(input)))\n", " except ValueError as e:\n", " print(f\"Invalid input {input}\")\n", " raise e" @@ -153,13 +153,15 @@ "- Be exactly {{$input}} paragraphs long\n", "\"\"\"\n", "\n", - "corgi_story = kernel.create_semantic_function(prompt_template=sk_prompt,\n", - " function_name=\"CorgiStory\",\n", - " skill_name=\"CorgiSkill\",\n", - " description=\"Write a short story about two Corgis on an adventure\",\n", - " max_tokens=500,\n", - " temperature=0.5,\n", - " top_p=0.5)\n", + "corgi_story = kernel.create_semantic_function(\n", + " prompt_template=sk_prompt,\n", + " function_name=\"CorgiStory\",\n", + " skill_name=\"CorgiSkill\",\n", + " description=\"Write a short story about two Corgis on an adventure\",\n", + " max_tokens=500,\n", + " temperature=0.5,\n", + " top_p=0.5,\n", + ")\n", "\n", "generate_number_skill = kernel.import_skill(GenerateNumberSkill())" ] @@ -217,6 +219,7 @@ { "cell_type": "code", "execution_count": null, + "id": "d54983d8", "metadata": {}, "outputs": [], "source": [ @@ -230,7 +233,9 @@ "# Configure AI service used by the kernel\n", "if useAzureOpenAI:\n", " deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()\n", - " azure_chat_service = AzureChatCompletion(deployment_name=\"turbo\", endpoint=endpoint, api_key=api_key) # set the deployment name to the value of your chat model\n", + " azure_chat_service = AzureChatCompletion(\n", + " deployment_name=\"turbo\", endpoint=endpoint, api_key=api_key\n", + " ) # set the deployment name to the value of your chat model\n", " kernel.add_chat_service(\"chat_completion\", azure_chat_service)\n", "else:\n", " api_key, org_id = sk.openai_settings_from_dot_env()\n", @@ -258,15 +263,13 @@ "from semantic_kernel.skill_definition import sk_function, sk_function_context_parameter\n", "from semantic_kernel import SKContext\n", "\n", + "\n", "class GenerateNumberSkill:\n", " \"\"\"\n", " Description: Generate a number between a min and a max.\n", " \"\"\"\n", "\n", - " @sk_function(\n", - " description=\"Generate a random number between min and max\",\n", - " name=\"GenerateNumber\"\n", - " )\n", + " @sk_function(description=\"Generate a random number between min and max\", name=\"GenerateNumber\")\n", " @sk_function_context_parameter(name=\"min\", description=\"Minimum number of paragraphs.\")\n", " @sk_function_context_parameter(name=\"max\", description=\"Maximum number of paragraphs.\", default_value=\"10\")\n", " def generate_number(self, context: SKContext) -> str:\n", @@ -281,7 +284,7 @@ " int value\n", " \"\"\"\n", " try:\n", - " return str(random.randint(int(context[\"min\"]), int(context[\"max\"]))) \n", + " return str(random.randint(int(context[\"min\"]), int(context[\"max\"])))\n", " except ValueError as e:\n", " print(f\"Invalid input {context['min']} {context['max']}\")\n", " raise e" @@ -324,13 +327,15 @@ "- Be written in this language: {{$language}}\n", "\"\"\"\n", "\n", - "corgi_story = kernel.create_semantic_function(prompt_template=sk_prompt,\n", - " function_name=\"CorgiStory\",\n", - " skill_name=\"CorgiSkill\",\n", - " description=\"Write a short story about two Corgis on an adventure\",\n", - " max_tokens=500,\n", - " temperature=0.5,\n", - " top_p=0.5)" + "corgi_story = kernel.create_semantic_function(\n", + " prompt_template=sk_prompt,\n", + " function_name=\"CorgiStory\",\n", + " skill_name=\"CorgiSkill\",\n", + " description=\"Write a short story about two Corgis on an adventure\",\n", + " max_tokens=500,\n", + " temperature=0.5,\n", + " top_p=0.5,\n", + ")" ] }, { @@ -349,11 +354,7 @@ "metadata": {}, "outputs": [], "source": [ - "context_variables = sk.ContextVariables(variables={\n", - " \"min\": \"1\",\n", - " \"max\": \"5\",\n", - " \"language\": \"Spanish\"\n", - "})" + "context_variables = sk.ContextVariables(variables={\"min\": \"1\", \"max\": \"5\", \"language\": \"Spanish\"})" ] }, { @@ -372,7 +373,7 @@ "metadata": {}, "outputs": [], "source": [ - "context_variables['paragraph_count'] = generate_number.invoke(variables=context_variables).result" + "context_variables[\"paragraph_count\"] = generate_number.invoke(variables=context_variables).result" ] }, { @@ -395,8 +396,11 @@ }, "outputs": [], "source": [ - "print(\"Generating a corgi story exactly {} paragraphs long in {} language: \".format(context_variables[\"paragraph_count\"],\n", - " context_variables[\"language\"]))\n", + "print(\n", + " \"Generating a corgi story exactly {} paragraphs long in {} language: \".format(\n", + " context_variables[\"paragraph_count\"], context_variables[\"language\"]\n", + " )\n", + ")\n", "print(\"=====================================================\")\n", "print(story)" ] @@ -426,6 +430,7 @@ "import random\n", "from semantic_kernel.skill_definition import sk_function\n", "\n", + "\n", "class GenerateNamesSkill:\n", " \"\"\"\n", " Description: Generate character names.\n", @@ -434,25 +439,14 @@ " # The default function name will be the name of the function itself, however you can override this\n", " # by setting the name= in the @sk_function decorator. In this case, we're using\n", " # the same name as the function name for simplicity.\n", - " @sk_function(\n", - " description=\"Generate character names\",\n", - " name=\"generate_names\"\n", - " )\n", + " @sk_function(description=\"Generate character names\", name=\"generate_names\")\n", " def generate_names(self) -> str:\n", " \"\"\"\n", " Generate two names.\n", " Returns:\n", " str\n", " \"\"\"\n", - " names = {\n", - " \"Hoagie\",\n", - " \"Hamilton\",\n", - " \"Bacon\",\n", - " \"Pizza\",\n", - " \"Boots\",\n", - " \"Shorts\",\n", - " \"Tuna\"\n", - " }\n", + " names = {\"Hoagie\", \"Hamilton\", \"Bacon\", \"Pizza\", \"Boots\", \"Shorts\", \"Tuna\"}\n", " first_name = random.choice(list(names))\n", " names.remove(first_name)\n", " second_name = random.choice(list(names))\n", @@ -496,13 +490,15 @@ "metadata": {}, "outputs": [], "source": [ - "corgi_story = kernel.create_semantic_function(prompt_template=sk_prompt,\n", - " function_name=\"CorgiStory\",\n", - " skill_name=\"CorgiSkill\",\n", - " description=\"Write a short story about two Corgis on an adventure\",\n", - " max_tokens=500,\n", - " temperature=0.5,\n", - " top_p=0.5)" + "corgi_story = kernel.create_semantic_function(\n", + " prompt_template=sk_prompt,\n", + " function_name=\"CorgiStory\",\n", + " skill_name=\"CorgiSkill\",\n", + " description=\"Write a short story about two Corgis on an adventure\",\n", + " max_tokens=500,\n", + " temperature=0.5,\n", + " top_p=0.5,\n", + ")" ] }, { @@ -512,12 +508,8 @@ "metadata": {}, "outputs": [], "source": [ - "context_variables = sk.ContextVariables(variables={\n", - " \"min\": \"1\",\n", - " \"max\": \"5\",\n", - " \"language\": \"Spanish\"\n", - "})\n", - "context_variables['paragraph_count'] = generate_number.invoke(variables=context_variables).result" + "context_variables = sk.ContextVariables(variables={\"min\": \"1\", \"max\": \"5\", \"language\": \"Spanish\"})\n", + "context_variables[\"paragraph_count\"] = generate_number.invoke(variables=context_variables).result" ] }, { @@ -538,8 +530,11 @@ "metadata": {}, "outputs": [], "source": [ - "print(\"Generating a corgi story exactly {} paragraphs long in {} language: \".format(context_variables[\"paragraph_count\"],\n", - " context_variables[\"language\"]))\n", + "print(\n", + " \"Generating a corgi story exactly {} paragraphs long in {} language: \".format(\n", + " context_variables[\"paragraph_count\"], context_variables[\"language\"]\n", + " )\n", + ")\n", "print(\"=====================================================\")\n", "print(story)" ] diff --git a/python/notebooks/09-groundedness-checking.ipynb b/python/notebooks/09-groundedness-checking.ipynb index cf9cbeed18..cbb1fe18a8 100644 --- a/python/notebooks/09-groundedness-checking.ipynb +++ b/python/notebooks/09-groundedness-checking.ipynb @@ -78,6 +78,7 @@ { "cell_type": "code", "execution_count": null, + "id": "7b22d324", "metadata": {}, "outputs": [], "source": [ @@ -101,7 +102,9 @@ "# Configure AI service used by the kernel\n", "if useAzureOpenAI:\n", " deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()\n", - " azure_chat_service = AzureChatCompletion(deployment_name=\"turbo\", endpoint=endpoint, api_key=api_key) # set the deployment name to the value of your chat model\n", + " azure_chat_service = AzureChatCompletion(\n", + " deployment_name=\"turbo\", endpoint=endpoint, api_key=api_key\n", + " ) # set the deployment name to the value of your chat model\n", " kernel.add_chat_service(\"chat_completion\", azure_chat_service)\n", "else:\n", " api_key, org_id = sk.openai_settings_from_dot_env()\n", diff --git a/python/notebooks/10-multiple-results-per-prompt.ipynb b/python/notebooks/10-multiple-results-per-prompt.ipynb index 5ad8b068e2..314558c239 100644 --- a/python/notebooks/10-multiple-results-per-prompt.ipynb +++ b/python/notebooks/10-multiple-results-per-prompt.ipynb @@ -40,10 +40,13 @@ " OpenAITextRequestSettings,\n", " OpenAIChatRequestSettings,\n", ")\n", - "from semantic_kernel.connectors.ai.open_ai.request_settings.azure_chat_request_settings import (\n", - " AzureChatRequestSettings\n", + "from semantic_kernel.connectors.ai.open_ai.request_settings.azure_chat_request_settings import AzureChatRequestSettings\n", + "from semantic_kernel.connectors.ai.open_ai import (\n", + " AzureTextCompletion,\n", + " AzureChatCompletion,\n", + " OpenAITextCompletion,\n", + " OpenAIChatCompletion,\n", ")\n", - "from semantic_kernel.connectors.ai.open_ai import AzureTextCompletion, AzureChatCompletion, OpenAITextCompletion, OpenAIChatCompletion\n", "from semantic_kernel.connectors.ai.hugging_face import HuggingFaceTextCompletion" ] }, @@ -67,8 +70,12 @@ "\n", "# Configure Azure LLM service\n", "deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()\n", - "azure_text_service = AzureTextCompletion(deployment_name=\"text-davinci-003\", endpoint=endpoint, api_key=api_key) # set the deployment name to the value of your text model\n", - "azure_chat_service = AzureChatCompletion(deployment_name=\"gpt-35-turbo\", endpoint=endpoint, api_key=api_key) # set the deployment name to the value of your chat model\n", + "azure_text_service = AzureTextCompletion(\n", + " deployment_name=\"text-davinci-003\", endpoint=endpoint, api_key=api_key\n", + ") # set the deployment name to the value of your text model\n", + "azure_chat_service = AzureChatCompletion(\n", + " deployment_name=\"gpt-35-turbo\", endpoint=endpoint, api_key=api_key\n", + ") # set the deployment name to the value of your chat model\n", "\n", "# Configure OpenAI service\n", "api_key, org_id = sk.openai_settings_from_dot_env()\n", @@ -95,7 +102,16 @@ "metadata": {}, "outputs": [], "source": [ - "oai_text_request_settings = OpenAITextRequestSettings(extension_data = { \"max_tokens\": 80, \"temperature\": 0.7, \"top_p\": 1, \"frequency_penalty\": 0.5, \"presence_penalty\": 0.5, \"number_of_responses\": 3})" + "oai_text_request_settings = OpenAITextRequestSettings(\n", + " extension_data={\n", + " \"max_tokens\": 80,\n", + " \"temperature\": 0.7,\n", + " \"top_p\": 1,\n", + " \"frequency_penalty\": 0.5,\n", + " \"presence_penalty\": 0.5,\n", + " \"number_of_responses\": 3,\n", + " }\n", + ")" ] }, { @@ -166,7 +182,7 @@ " HuggingFaceRequestSettings,\n", ")\n", "\n", - "hf_request_settings = HuggingFaceRequestSettings(extension_data = { \"max_new_tokens\": 80, \"temperature\": 0.7, \"top_p\": 1})" + "hf_request_settings = HuggingFaceRequestSettings(extension_data={\"max_new_tokens\": 80, \"temperature\": 0.7, \"top_p\": 1})" ] }, { @@ -198,12 +214,7 @@ "outputs": [], "source": [ "oai_chat_request_settings = OpenAIChatRequestSettings(\n", - " max_tokens=80,\n", - " temperature=0.7,\n", - " top_p=1,\n", - " frequency_penalty=0.5,\n", - " presence_penalty=0.5,\n", - " number_of_responses=3\n", + " max_tokens=80, temperature=0.7, top_p=1, frequency_penalty=0.5, presence_penalty=0.5, number_of_responses=3\n", ")" ] }, @@ -225,7 +236,7 @@ "source": [ "role = \"user\"\n", "content = \"It's a beautiful day outside, birds are singing, flowers are blooming. On days like these, kids like you...\"\n", - "message = { \"role\":role, \"content\":content }\n", + "message = {\"role\": role, \"content\": content}\n", "results = await oai_chat_service.complete_chat_async(messages=[message], settings=oai_chat_request_settings)\n", "i = 0\n", "for result in results:\n", @@ -250,12 +261,7 @@ "outputs": [], "source": [ "az_oai_request_settings = AzureChatRequestSettings(\n", - " max_tokens=80,\n", - " temperature=0.7,\n", - " top_p=1,\n", - " frequency_penalty=0.5,\n", - " presence_penalty=0.5,\n", - " number_of_responses=3\n", + " max_tokens=80, temperature=0.7, top_p=1, frequency_penalty=0.5, presence_penalty=0.5, number_of_responses=3\n", ")" ] }, @@ -268,7 +274,7 @@ "source": [ "role = \"user\"\n", "content = \"Tomorow is going to be a great day, I can feel it. I'm going to wake up early, go for a run, and then...\"\n", - "message = { \"role\":role, \"content\":content }\n", + "message = {\"role\": role, \"content\": content}\n", "results = await azure_chat_service.complete_chat_async(messages=[message], settings=az_oai_request_settings)\n", "i = 0\n", "for result in results:\n", diff --git a/python/semantic_kernel/orchestration/sk_function.py b/python/semantic_kernel/orchestration/sk_function.py index 5e8b7b0753..b75cd1eac5 100644 --- a/python/semantic_kernel/orchestration/sk_function.py +++ b/python/semantic_kernel/orchestration/sk_function.py @@ -372,8 +372,14 @@ class SKFunction(SKFunctionBase): loop = None if loop and loop.is_running(): - coroutine_function = self._invoke_semantic_async if self.is_semantic else self._invoke_native_async - return self.run_async_in_executor(lambda: coroutine_function(context, settings)) + + def run_coroutine(): + if self.is_semantic: + return self._invoke_semantic_async(context, settings) + else: + return self._invoke_native_async(context) + + return self.run_async_in_executor(run_coroutine) else: if self.is_semantic: return asyncio.run(self._invoke_semantic_async(context, settings))