Files
semantic-kernel/python/semantic_kernel/kernel_extensions/inline_definition.py
T
Jordan Henkel 407fb36e59 Python: cleanup/sync/fixes (for examples, docs, tests, notebooks) (#317)
### Motivation and Context

The `python-preview` branch is under active development and many
docs/tests/examples are getting a bit out of sync. There are also a few
small cleanup chores/refactors to make _usage_ easier (e.g., to make
examples cleaner). I've grouped these small fixes (plus a few bug fixes)
into this "cleanup" PR.

### Description

This PR does the following:

1. Updates the out-of-date example in `README.md`
2. Fixes all 5 of the notebooks we support right now (1,2,3,4, and 6;
notebook 5 is planner, I've blanked out most of the code there so we
don't confuse people, especially as the planner API is changing)
3. Fixes a bug w/ `stop_sequences` in `(Azure)OpenAITextCompletion`
4. Fixes a bug introduced in our upgrades to the embeddings cosine-sim
check (we should add more thorough tests here)
5. Cleans up tests
6. Ensures end-to-end tests are all runnable 
7. Fixes up the `kernel_extensions` to work more naturally (so that end
users can use them directly on the `kernel` object and get good
type-hinting)
2023-04-13 16:12:36 -07:00

71 lines
2.2 KiB
Python

# Copyright (c) Microsoft. All rights reserved.
from typing import TYPE_CHECKING, List, Optional
from uuid import uuid4
from semantic_kernel.diagnostics.verify import Verify
from semantic_kernel.kernel_extensions.extends_kernel import ExtendsKernel
from semantic_kernel.semantic_functions.prompt_template import PromptTemplate
from semantic_kernel.semantic_functions.prompt_template_config import (
PromptTemplateConfig,
)
from semantic_kernel.semantic_functions.semantic_function_config import (
SemanticFunctionConfig,
)
if TYPE_CHECKING:
from semantic_kernel.orchestration.sk_function_base import SKFunctionBase
class InlineDefinition(ExtendsKernel):
def create_semantic_function(
self,
prompt_template: str,
function_name: Optional[str] = None,
skill_name: Optional[str] = None,
description: Optional[str] = None,
max_tokens: int = 256,
temperature: float = 0.0,
top_p: float = 1.0,
presence_penalty: float = 0.0,
frequency_penalty: float = 0.0,
stop_sequences: Optional[List[str]] = None,
) -> "SKFunctionBase":
kernel = self.kernel()
function_name = (
function_name
if function_name is not None
else f"f_{str(uuid4()).replace('-', '_')}"
)
config = PromptTemplateConfig(
description=(
description
if description is not None
else "Generic function, unknown purpose"
),
type="completion",
completion=PromptTemplateConfig.CompletionConfig(
temperature,
top_p,
presence_penalty,
frequency_penalty,
max_tokens,
stop_sequences if stop_sequences is not None else [],
),
)
Verify.valid_function_name(function_name)
if skill_name is not None:
Verify.valid_skill_name(skill_name)
template = PromptTemplate(
prompt_template, kernel.prompt_template_engine, config
)
function_config = SemanticFunctionConfig(config, template)
return kernel.register_semantic_function(
skill_name, function_name, function_config
)