mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
### 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. --> Major work to replace the context with Kernel Arguments on the front-end and Function Results on the back. Updated the function decorator to a new approach, in line with dotnet. Revamps the way function are called, allowing native functions to be completely ignorant of SK, other then the decorator. This also moves things into the new folder structure in sync with dotnet. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Adds: - KernelArguments, a dict-like class that replaces the variables in KernelContext. Closes #4565 - FunctionResult, a class to hold the results of function executions, includes the function, the value and metadata, as well as two convenient function to get the value out of it (str and get_inner_content) the first is generic, the second specifically for KernelContent responses (from AI services). - AI Service Selector class, has a default, which is based on the order in the arguments followed by the order in the functions, can be overridden to implement your own strategy. Closes #4631 - Introduces ChatHistory and refactors the PromptTemplateConfig. Closes #4856, #4630 - Improves parsing of templates, will now all validate during creation and throw an error then, instead of some that do not check for validaty until used. - Introduces named_args block and thereby the ability to have multiple arguments for a function call in a template. Closes #5003 Updates: - kernel_function decorators, the parameter decorator was removed and instead we now use Annotated to add a description of a field and we get the type and required from the function definition. - core plugins, use the new approach to kernel_function decorators. - planners, template engines have all been updated to use the kernel and kernelarguments instead of Context. - Events have been updated, now use kernelarguments and function_result - Tokenizers support for named_args and improvements on parsing and checking. - Kernel examples and notebooks to use the latest code. - All unit and integration tests. There is more code coverage now than before. Removed: - kernelContext - kernel_function_parameter_decorator - delegate handling code for native functions - file_io_plugin and tests - SemanticFunctionConfig - ChatPromptTemplate ### 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 😄 --------- Co-authored-by: Evan Mattson <[email protected]> Co-authored-by: Evan Mattson <[email protected]> Co-authored-by: Evan Mattson <[email protected]>
69 lines
3.4 KiB
Python
69 lines
3.4 KiB
Python
# Copyright (c) Microsoft. All rights reserved.
|
|
from typing import Optional
|
|
|
|
from semantic_kernel.contents.chat_role import ChatRole
|
|
from semantic_kernel.contents.finish_reason import FinishReason
|
|
from semantic_kernel.contents.streaming_kernel_content import StreamingKernelContent
|
|
|
|
|
|
class StreamingChatMessageContent(StreamingKernelContent):
|
|
"""This is the base class for streaming chat message response content.
|
|
|
|
All Chat Completion Services should return a instance of this class as streaming response,
|
|
where each part of the response as it is streamed is converted to a instance of this class,
|
|
the end-user will have to either do something directly or gather them and combine them into a
|
|
new instance. A service can implement their own subclass of this class and return instances of that.
|
|
|
|
Args:
|
|
choice_index: int - The index of the choice that generated this response.
|
|
inner_content: Optional[Any] - The inner content of the response,
|
|
this should hold all the information from the response so even
|
|
when not creating a subclass a developer can leverage the full thing.
|
|
ai_model_id: Optional[str] - The id of the AI model that generated this response.
|
|
metadata: Dict[str, Any] - Any metadata that should be attached to the response.
|
|
role: Optional[ChatRole] - The role of the chat message, defaults to ASSISTANT.
|
|
content: Optional[str] - The text of the response.
|
|
encoding: Optional[str] - The encoding of the text.
|
|
|
|
Methods:
|
|
__str__: Returns the content of the response.
|
|
__bytes__: Returns the content of the response encoded in the encoding.
|
|
__add__: Combines two StreamingChatMessageContent instances.
|
|
"""
|
|
|
|
role: Optional[ChatRole] = ChatRole.ASSISTANT
|
|
content: Optional[str] = None
|
|
encoding: Optional[str] = None
|
|
finish_reason: Optional[FinishReason] = None
|
|
|
|
def __str__(self) -> str:
|
|
return self.content or ""
|
|
|
|
def __bytes__(self) -> bytes:
|
|
return self.content.encode(self.encoding if self.encoding else "utf-8") if self.content else b""
|
|
|
|
def __add__(self, other: "StreamingChatMessageContent") -> "StreamingChatMessageContent":
|
|
"""When combining two StreamingChatMessageContent instances, the content fields are combined.
|
|
|
|
The inner_content of the first one is used, ai_model_id and encoding should be the same,
|
|
if role is set, they should be the same.
|
|
"""
|
|
if self.choice_index != other.choice_index:
|
|
raise ValueError("Cannot add StreamingChatMessageContent with different choice_index")
|
|
if self.ai_model_id != other.ai_model_id:
|
|
raise ValueError("Cannot add StreamingChatMessageContent from different ai_model_id")
|
|
if self.encoding != other.encoding:
|
|
raise ValueError("Cannot add StreamingChatMessageContent with different encoding")
|
|
if self.role and other.role and self.role != other.role:
|
|
raise ValueError("Cannot add StreamingChatMessageContent with different role")
|
|
return StreamingChatMessageContent(
|
|
choice_index=self.choice_index,
|
|
inner_content=self.inner_content,
|
|
ai_model_id=self.ai_model_id,
|
|
metadata=self.metadata,
|
|
role=self.role,
|
|
content=(self.content or "") + (other.content or ""),
|
|
encoding=self.encoding,
|
|
finish_reason=self.finish_reason or other.finish_reason,
|
|
)
|