chore: import upstream snapshot with attribution
CodeQL / Analyze (csharp) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run

This commit is contained in:
wehub-resource-sync
2026-07-13 13:21:23 +08:00
commit b957a53def
5423 changed files with 863745 additions and 0 deletions
@@ -0,0 +1,88 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from samples.concepts.setup.chat_completion_services import Services, get_chat_completion_service_and_request_settings
from semantic_kernel.contents import ChatHistory
# This sample shows how to create a chatbot. This sample uses the following two main components:
# - a ChatCompletionService: This component is responsible for generating responses to user messages.
# - a ChatHistory: This component is responsible for keeping track of the chat history.
# The chatbot in this sample is called Mosscap, who responds to user messages with long flowery prose.
# You can select from the following chat completion services:
# - Services.OPENAI
# - Services.AZURE_OPENAI
# - Services.AZURE_AI_INFERENCE
# - Services.ANTHROPIC
# - Services.BEDROCK
# - Services.GOOGLE_AI
# - Services.MISTRAL_AI
# - Services.OLLAMA
# - Services.ONNX
# - Services.VERTEX_AI
# - Services.DEEPSEEK
# Please make sure you have configured your environment correctly for the selected chat completion service.
chat_completion_service, request_settings = get_chat_completion_service_and_request_settings(Services.OPENAI)
# This is the system message that gives the chatbot its personality.
system_message = """
You are a chat bot. Your name is Mosscap and
you have one goal: figure out what people need.
Your full name, should you need to know it, is
Splendid Speckled Mosscap. You communicate
effectively, but you tend to answer with long
flowery prose.
"""
# Create a chat history object with the system message.
chat_history = ChatHistory(system_message=system_message)
async def chat() -> bool:
try:
user_input = input("User:> ")
except KeyboardInterrupt:
print("\n\nExiting chat...")
return False
except EOFError:
print("\n\nExiting chat...")
return False
if user_input == "exit":
print("\n\nExiting chat...")
return False
# Add the user message to the chat history so that the chatbot can respond to it.
chat_history.add_user_message(user_input)
# Get the chat message content from the chat completion service.
response = await chat_completion_service.get_chat_message_content(
chat_history=chat_history,
settings=request_settings,
)
if response:
print(f"Mosscap:> {response}")
# Add the chat message to the chat history to keep track of the conversation.
chat_history.add_message(response)
return True
async def main() -> None:
# Start the chat loop. The chat loop will continue until the user types "exit".
chatting = True
while chatting:
chatting = await chat()
# Sample output:
# User:> Why is the sky blue in one sentence?
# Mosscap:> The sky is blue due to the scattering of sunlight by the molecules in the Earth's atmosphere,
# a phenomenon known as Rayleigh scattering, which causes shorter blue wavelengths to become more
# prominent in our visual perception.
if __name__ == "__main__":
asyncio.run(main())
@@ -0,0 +1,127 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from samples.concepts.setup.chat_completion_services import Services, get_chat_completion_service_and_request_settings
from semantic_kernel import Kernel
from semantic_kernel.contents import ChatHistory
from semantic_kernel.functions import KernelArguments
from semantic_kernel.prompt_template import PromptTemplateConfig
# This sample shows how to create a chatbot using a kernel function.
# This sample uses the following two main components:
# - a ChatCompletionService: This component is responsible for generating responses to user messages.
# - a ChatHistory: This component is responsible for keeping track of the chat history.
# - a KernelFunction: This function will be a prompt function, meaning the function is composed of
# a prompt and will be invoked by Semantic Kernel.
# The chatbot in this sample is called Mosscap, who responds to user messages with long flowery prose.
# [NOTE]
# The purpose of this sample is to demonstrate how to use a kernel function.
# To build a basic chatbot, it is sufficient to use a ChatCompletionService with a chat history directly.
# You can select from the following chat completion services:
# - Services.OPENAI
# - Services.AZURE_OPENAI
# - Services.AZURE_AI_INFERENCE
# - Services.ANTHROPIC
# - Services.BEDROCK
# - Services.GOOGLE_AI
# - Services.MISTRAL_AI
# - Services.OLLAMA
# - Services.ONNX
# - Services.VERTEX_AI
# - Services.DEEPSEEK
# Please make sure you have configured your environment correctly for the selected chat completion service.
chat_completion_service, request_settings = get_chat_completion_service_and_request_settings(Services.AZURE_OPENAI)
# This is the system message that gives the chatbot its personality.
system_message = """
You are a chat bot. Your name is Mosscap and
you have one goal: figure out what people need.
Your full name, should you need to know it, is
Splendid Speckled Mosscap. You communicate
effectively, but you tend to answer with long
flowery prose.
"""
# Create a chat history object with the system message.
chat_history = ChatHistory(system_message=system_message)
# Create a kernel and register a prompt function.
# The prompt here contains two variables: chat_history and user_input.
# They will be replaced by the kernel with the actual values when the function is invoked.
# [NOTE]
# The chat_history, which is a ChatHistory object, will be serialized to a string internally
# to create/render the final prompt.
# Since this sample uses a chat completion service, the prompt will be deserialized back to
# a ChatHistory object that gets passed to the chat completion service. This new chat history
# object will contain the original messages and the user input.
kernel = Kernel()
chat_function = kernel.add_function(
plugin_name="ChatBot",
function_name="Chat",
prompt_template_config=PromptTemplateConfig(
template="{{$chat_history}}{{$user_input}}", allow_dangerously_set_content=True
),
# You can attach the request settings to the function or
# pass the settings to the kernel.invoke method via the kernel arguments.
# If you specify the settings in both places, the settings in the kernel arguments will
# take precedence given the same service id.
# prompt_execution_settings=request_settings,
)
# Invoking a kernel function requires a service, so we add the chat completion service to the kernel.
kernel.add_service(chat_completion_service)
async def chat() -> bool:
try:
user_input = input("User:> ")
except KeyboardInterrupt:
print("\n\nExiting chat...")
return False
except EOFError:
print("\n\nExiting chat...")
return False
if user_input == "exit":
print("\n\nExiting chat...")
return False
# Get the chat message content from the chat completion service.
kernel_arguments = KernelArguments(
settings=request_settings,
# Use keyword arguments to pass the chat history and user input to the kernel function.
chat_history=chat_history,
user_input=user_input,
)
answer = await kernel.invoke(plugin_name="ChatBot", function_name="Chat", arguments=kernel_arguments)
# Alternatively, you can invoke the function directly with the kernel as an argument:
# answer = await chat_function.invoke(kernel, kernel_arguments)
if answer:
print(f"Mosscap:> {answer}")
# Since the user_input is rendered by the template, it is not yet part of the chat history, so we add it here.
chat_history.add_user_message(user_input)
# Add the chat message to the chat history to keep track of the conversation.
chat_history.add_message(answer.value[0])
return True
async def main() -> None:
# Start the chat loop. The chat loop will continue until the user types "exit".
chatting = True
while chatting:
chatting = await chat()
# Sample output:
# User:> Why is the sky blue in one sentence?
# Mosscap:> The sky is blue due to the scattering of sunlight by the molecules in the Earth's atmosphere,
# a phenomenon known as Rayleigh scattering, which causes shorter blue wavelengths to become more
# prominent in our visual perception.
if __name__ == "__main__":
asyncio.run(main())
@@ -0,0 +1,111 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from samples.concepts.setup.chat_completion_services import (
Services,
get_chat_completion_service_and_request_settings,
)
from semantic_kernel.contents import ChatHistory
# This sample shows how to create a chatbot that whose output can be biased using logit bias.
# This sample uses the following three main components:
# - a ChatCompletionService: This component is responsible for generating responses to user messages.
# - a ChatHistory: This component is responsible for keeping track of the chat history.
# - a list of tokens whose bias value will be reduced, meaning the likelihood of these tokens appearing
# in the output will be reduced.
# The chatbot in this sample is called Mosscap, who is an expert in basketball.
# To learn more about logit bias, see: https://help.openai.com/en/articles/5247780-using-logit-bias-to-define-token-probability
# You can select from the following chat completion services:
# - Services.OPENAI
# - Services.AZURE_OPENAI
# Please make sure you have configured your environment correctly for the selected chat completion service.
chat_completion_service, request_settings = get_chat_completion_service_and_request_settings(Services.AZURE_OPENAI)
# This is the system message that gives the chatbot its personality.
system_message = """
You are a chat bot whose expertise is basketball.
Your name is Mosscap and you have one goal: to answer questions about basketball.
"""
# Create a chat history object with the system message.
chat_history = ChatHistory(system_message=system_message)
# Create a list of tokens whose bias value will be reduced.
# The token ids of these words can be obtained using the GPT Tokenizer: https://platform.openai.com/tokenizer
# the targeted model series is GPT-4o & GPT-4o mini
# banned_words = ["basketball", "NBA", "player", "career", "points"]
banned_tokens = [
# "basketball"
106622,
5052,
# "NBA"
99915,
# " NBA"
32272,
# "player"
6450,
# " player"
5033,
# "career"
198069,
# " career"
8461,
# "points"
14011,
# " points"
5571,
]
# Configure the logit bias settings to minimize the likelihood of the
# tokens in the banned_tokens list appearing in the output.
request_settings.logit_bias = {k: -100 for k in banned_tokens} # type: ignore
async def chat() -> bool:
try:
user_input = input("User:> ")
except KeyboardInterrupt:
print("\n\nExiting chat...")
return False
except EOFError:
print("\n\nExiting chat...")
return False
if user_input == "exit":
print("\n\nExiting chat...")
return False
# Add the user message to the chat history so that the chatbot can respond to it.
chat_history.add_user_message(user_input)
# Get the chat message content from the chat completion service.
response = await chat_completion_service.get_chat_message_content(
chat_history=chat_history,
settings=request_settings,
)
if response:
print(f"Mosscap:> {response}")
# Add the chat message to the chat history to keep track of the conversation.
chat_history.add_message(response)
return True
async def main() -> None:
# Start the chat loop. The chat loop will continue until the user types "exit".
chatting = True
while chatting:
chatting = await chat()
# Sample output:
# User:> Who has the most career points in NBA history?
# Mosscap:> As of October 2023, the all-time leader in total regular-season scoring in the history of the National
# Basketball Association (N.B.A.) is Kareem Abdul-Jabbar, who scored 38,387 total regular-seasonPoints
# during his illustrious 20-year playing Career.
if __name__ == "__main__":
asyncio.run(main())
@@ -0,0 +1,85 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from samples.concepts.setup.chat_completion_services import (
Services,
get_chat_completion_service_and_request_settings,
)
from semantic_kernel.contents import ChatHistory
# This sample shows how to create a chatbot whose output can be stored for use with the OpenAI
# model distillation or evals products.
# This sample uses the following two main components:
# - a ChatCompletionService: This component is responsible for generating responses to user messages.
# - a ChatHistory: This component is responsible for keeping track of the chat history.
# The chatbot in this sample is called Mosscap, who is an expert in basketball.
# To learn more about OpenAI distillation, see: https://platform.openai.com/docs/guides/distillation
# To learn more about OpenAI evals, see: https://platform.openai.com/docs/guides/evals
# You can select from the following chat completion services:
# - Services.OPENAI
# Please make sure you have configured your environment correctly for the selected chat completion service.
chat_completion_service, request_settings = get_chat_completion_service_and_request_settings(Services.OPENAI)
# This is the system message that gives the chatbot its personality.
system_message = """
You are a chat bot whose expertise is basketball.
Your name is Mosscap and you have one goal: to answer questions about basketball.
"""
# Create a chat history object with the system message.
chat_history = ChatHistory(system_message=system_message)
# Configure the store and metadata settings for the chat completion service.
request_settings.store = True
request_settings.metadata = {"chatbot": "Mosscap"}
async def chat() -> bool:
try:
user_input = input("User:> ")
except KeyboardInterrupt:
print("\n\nExiting chat...")
return False
except EOFError:
print("\n\nExiting chat...")
return False
if user_input == "exit":
print("\n\nExiting chat...")
return False
# Add the user message to the chat history so that the chatbot can respond to it.
chat_history.add_user_message(user_input)
# Get the chat message content from the chat completion service.
response = await chat_completion_service.get_chat_message_content(
chat_history=chat_history,
settings=request_settings,
)
if response:
print(f"Mosscap:> {response}")
# Add the chat message to the chat history to keep track of the conversation.
chat_history.add_message(response)
return True
async def main() -> None:
# Start the chat loop. The chat loop will continue until the user types "exit".
chatting = True
while chatting:
chatting = await chat()
# Sample output:
# User:> Who has the most career points in NBA history?
# Mosscap:> As of October 2023, the all-time leader in total regular-season scoring in the history of the National
# Basketball Association (N.B.A.) is Kareem Abdul-Jabbar, who scored 38,387 total regular-seasonPoints
# during his illustrious 20-year playing Career.
if __name__ == "__main__":
asyncio.run(main())
@@ -0,0 +1,100 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from samples.concepts.setup.chat_completion_services import Services, get_chat_completion_service_and_request_settings
from semantic_kernel.contents import ChatHistory, StreamingChatMessageContent
# This sample shows how to create a chatbot that streams responses.
# This sample uses the following two main components:
# - a ChatCompletionService: This component is responsible for generating responses to user messages.
# - a ChatHistory: This component is responsible for keeping track of the chat history.
# The chatbot in this sample is called Mosscap, who responds to user messages with long flowery prose.
# You can select from the following chat completion services:
# - Services.OPENAI
# - Services.AZURE_OPENAI
# - Services.AZURE_AI_INFERENCE
# - Services.ANTHROPIC
# - Services.BEDROCK
# - Services.GOOGLE_AI
# - Services.MISTRAL_AI
# - Services.OLLAMA
# - Services.ONNX
# - Services.VERTEX_AI
# - Services.DEEPSEEK
# Please make sure you have configured your environment correctly for the selected chat completion service.
# Please note that not all models support streaming responses. Make sure to select a model that supports streaming.
chat_completion_service, request_settings = get_chat_completion_service_and_request_settings(Services.AZURE_OPENAI)
# This is the system message that gives the chatbot its personality.
system_message = """
You are a chat bot. Your name is Mosscap and
you have one goal: figure out what people need.
Your full name, should you need to know it, is
Splendid Speckled Mosscap. You communicate
effectively, but you tend to answer with long
flowery prose.
"""
# Create a chat history object with the system message.
chat_history = ChatHistory(system_message=system_message)
async def chat() -> bool:
try:
user_input = input("User:> ")
except KeyboardInterrupt:
print("\n\nExiting chat...")
return False
except EOFError:
print("\n\nExiting chat...")
return False
if user_input == "exit":
print("\n\nExiting chat...")
return False
# Add the user message to the chat history so that the chatbot can respond to it.
chat_history.add_user_message(user_input)
# Get the chat message content from the chat completion service.
# The response is an async generator that streams the response in chunks.
response = chat_completion_service.get_streaming_chat_message_content(
chat_history=chat_history,
settings=request_settings,
)
# Capture the chunks of the response and print them as they come in.
chunks: list[StreamingChatMessageContent] = []
print("Mosscap:> ", end="")
async for chunk in response:
if chunk:
chunks.append(chunk)
print(chunk, end="")
print("")
# Combine the chunks into a single message to add to the chat history.
full_message = sum(chunks[1:], chunks[0])
# Add the chat message to the chat history to keep track of the conversation.
chat_history.add_message(full_message)
return True
async def main() -> None:
# Start the chat loop. The chat loop will continue until the user types "exit".
chatting = True
while chatting:
chatting = await chat()
# Sample output:
# User:> Why is the sky blue in one sentence?
# Mosscap:> The sky is blue due to the scattering of sunlight by the molecules in the Earth's atmosphere,
# a phenomenon known as Rayleigh scattering, which causes shorter blue wavelengths to become more
# prominent in our visual perception.
if __name__ == "__main__":
asyncio.run(main())
@@ -0,0 +1,129 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from samples.concepts.setup.chat_completion_services import (
Services,
get_chat_completion_service_and_request_settings,
)
from semantic_kernel.contents import AuthorRole, ChatHistory, ChatMessageContent, ImageContent, TextContent
# This sample shows how to create a chatbot that responds to user messages with image input.
# This sample uses the following three main components:
# - a ChatCompletionService: This component is responsible for generating responses to user messages.
# - a ChatHistory: This component is responsible for keeping track of the chat history.
# - an ImageContent: This component is responsible for representing image content.
# The chatbot in this sample is called Mosscap.
# You can select from the following chat completion services:
# - Services.OPENAI
# - Services.AZURE_OPENAI
# - Services.AZURE_AI_INFERENCE
# - Services.ANTHROPIC
# - Services.BEDROCK
# - Services.GOOGLE_AI
# - Services.MISTRAL_AI
# - Services.OLLAMA
# - Services.ONNX
# - Services.VERTEX_AI
# Please make sure you have configured your environment correctly for the selected chat completion service.
# [NOTE]
# Not all models support image input. Make sure to select a model that supports image input.
# Not all services support image input from an image URI. If your image is saved in a remote location,
# make sure to use a service that supports image input from a URI.
chat_completion_service, request_settings = get_chat_completion_service_and_request_settings(Services.AZURE_OPENAI)
IMAGE_URI = "https://raw.githubusercontent.com/microsoft/semantic-kernel/main/python/tests/assets/sample_image.jpg"
IMAGE_PATH = "samples/concepts/resources/sample_image.jpg"
# Create an image content with the image URI.
image_content_remote = ImageContent(uri=IMAGE_URI)
# You can also create an image content with a local image path.
image_content_local = ImageContent.from_image_file(IMAGE_PATH)
# This is the system message that gives the chatbot its personality.
system_message = """
You are an image reviewing chat bot. Your name is Mosscap and you have one goal critiquing images that are supplied.
"""
# Create a chat history object with the system message and an initial user message with an image input.
chat_history = ChatHistory(system_message=system_message)
chat_history.add_message(
ChatMessageContent(
role=AuthorRole.USER,
items=[TextContent(text="What is in this image?"), image_content_local],
)
)
async def chat(skip_user_input: bool = False) -> bool:
"""Chat with the chatbot.
Args:
skip_user_input (bool): Whether to skip user input. Defaults to False.
"""
if not skip_user_input:
try:
user_input = input("User:> ")
except KeyboardInterrupt:
print("\n\nExiting chat...")
return False
except EOFError:
print("\n\nExiting chat...")
return False
if user_input == "exit":
print("\n\nExiting chat...")
return False
# Add the user message to the chat history so that the chatbot can respond to it.
chat_history.add_user_message(user_input)
# Get the chat message content from the chat completion service.
response = await chat_completion_service.get_chat_message_content(
chat_history=chat_history,
settings=request_settings,
)
if response:
print(f"Mosscap:> {response}")
# Add the chat message to the chat history to keep track of the conversation.
chat_history.add_message(response)
return True
async def main() -> None:
# Start the chat with the image input.
await chat(skip_user_input=True)
# Continue the chat. The chat loop will continue until the user types "exit".
chatting = True
while chatting:
chatting = await chat()
# Sample output:
# Mosscap:> The image features a large, historic building that exhibits a traditional half-timbered architectural
# style. The structure is located near a dense forest, characterized by lush green trees. The sky above
# is partly cloudy, suggesting a pleasant day. The building itself appears well-maintained, with distinct
# features such as a turret or spire and decorative wood framing, creating an elegant and charming
# appearance in its natural setting.
# User:> What do you think about the composition of the photo?
# Mosscap:> The composition of the photo is quite effective. Here are a few observations:
# 1. **Framing**: The building is positioned slightly off-center, which can create a more dynamic and
# engaging image. This drawing of attention to the structure, while still showcasing the surrounding
# landscape.
# 2. **Foreground and Background**: The green foliage and trees in the foreground provide a nice contrast
# to the building, enhancing its visual appeal. The dense forest in the background adds depth and context
# to the scene.
# 3. **Lighting**: The light appears to be favorable, suggesting a well-lit scene. The clouds add texture
# to the sky without overwhelming the overall brightness.
# 4. **Perspective**: The angle from which the photo is taken allows viewers to appreciate both the
# architecture of the building and its natural environment, creating a harmonious balance.
# Overall, the composition successfully highlights the building while incorporating its natural
# surroundings, inviting viewers to appreciate both elements together.
if __name__ == "__main__":
asyncio.run(main())
@@ -0,0 +1,169 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from samples.concepts.setup.chat_completion_services import (
Services,
get_chat_completion_service_and_request_settings,
)
from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.function_choice_behavior import FunctionChoiceBehavior
from semantic_kernel.contents import ChatHistorySummarizationReducer
from semantic_kernel.core_plugins.time_plugin import TimePlugin
from semantic_kernel.functions import KernelArguments
# This sample shows how to create a chatbot using a kernel function and leverage a chat history
# summarization reducer.
# This sample uses the following main components:
# - a ChatCompletionService: This component is responsible for generating responses to user messages.
# - a Chat History Reducer: This component is responsible for keeping track and reducing the chat history.
# A Chat History Reducer is a subclass of ChatHistory that provides additional
# functionality to reduce the history.
# - a KernelFunction: This function will be a prompt function, meaning the function is composed of
# a prompt and will be invoked by Semantic Kernel.
# The chatbot in this sample is called Mosscap, who responds to user messages with long flowery prose.
# [NOTE]
# The purpose of this sample is to demonstrate how to use a kernel function and use a chat history reducer.
# To build a basic chatbot, it is sufficient to use a ChatCompletionService with a chat history directly.
# Toggle this flag to view the chat history summary after a reduction was performed.
view_chat_history_summary_after_reduction = True
# You can select from the following chat completion services:
# - Services.OPENAI
# - Services.AZURE_OPENAI
# - Services.AZURE_AI_INFERENCE
# - Services.ANTHROPIC
# - Services.BEDROCK
# - Services.GOOGLE_AI
# - Services.MISTRAL_AI
# - Services.OLLAMA
# - Services.ONNX
# - Services.VERTEX_AI
# Please make sure you have configured your environment correctly for the selected chat completion service.
chat_completion_service, request_settings = get_chat_completion_service_and_request_settings(Services.AZURE_OPENAI)
# This is the system message that gives the chatbot its personality.
system_message = """
You are a chat bot. Your name is Mosscap and
you have one goal: figure out what people need.
Your full name, should you need to know it, is
Splendid Speckled Mosscap. You communicate
effectively, but you tend to answer with long
flowery prose.
"""
# Create a kernel and register a prompt function.
# The prompt here contains two variables: chat_history and user_input.
# They will be replaced by the kernel with the actual values when the function is invoked.
# [NOTE]
# The chat_history, which is a ChatHistory object, will be serialized to a string internally
# to create/render the final prompt.
# Since this sample uses a chat completion service, the prompt will be deserialized back to
# a ChatHistory object that gets passed to the chat completion service. This new chat history
# object will contain the original messages and the user input.
kernel = Kernel()
chat_function = kernel.add_function(
plugin_name="ChatBot",
function_name="Chat",
prompt="{{$chat_history}}{{$user_input}}",
template_format="semantic-kernel",
# You can attach the request settings to the function or
# pass the settings to the kernel.invoke method via the kernel arguments.
# If you specify the settings in both places, the settings in the kernel arguments will
# take precedence given the same service id.
# prompt_execution_settings=request_settings,
)
# Invoking a kernel function requires a service, so we add the chat completion service to the kernel.
kernel.add_service(chat_completion_service)
# The chat history reducer is responsible for summarizing the chat history.
# It's a subclass of ChatHistory that provides additional functionality to reduce the history.
# You may use it just like a regular ChatHistory object.
summarization_reducer = ChatHistorySummarizationReducer(
service=kernel.get_service(),
# target_count:
# Purpose: Defines the target number of messages to retain after applying summarization.
# What it controls: This parameter determines how much of the most recent conversation history
# is preserved while discarding or summarizing older messages.
# Why change it?:
# - Smaller values: Use when memory constraints are tight, or the assistant only needs a brief history
# to maintain context.
# - Larger values: Use when retaining more conversational context is critical for accurate responses
# or maintaining a richer dialogue.
target_count=3,
# threshold_count:
# Purpose: Acts as a buffer to avoid reducing history prematurely when the current message count exceeds
# target_count by a small margin.
# What it controls: Helps ensure that essential paired messages (like a user query and the assistants response)
# are not "orphaned" or lost during truncation or summarization.
# Why change it?:
# - Smaller values: Use when you want stricter reduction criteria and are okay with possibly cutting older
# pairs of messages sooner.
# - Larger values: Use when you want to minimize the risk of cutting a critical part of the conversation,
# especially for sensitive interactions like API function calls or complex responses.
threshold_count=2,
)
summarization_reducer.add_system_message(system_message)
kernel.add_plugin(plugin=TimePlugin(), plugin_name="TimePlugin")
request_settings.function_choice_behavior = FunctionChoiceBehavior.Auto()
async def chat() -> bool:
try:
user_input = input("User:> ")
except (KeyboardInterrupt, EOFError):
print("\n\nExiting chat...")
return False
if user_input == "exit":
print("\n\nExiting chat...")
return False
if is_reduced := await summarization_reducer.reduce():
print(f"@ History reduced to {len(summarization_reducer.messages)} messages.")
kernel_arguments = KernelArguments(
settings=request_settings,
chat_history=summarization_reducer,
user_input=user_input,
)
answer = await kernel.invoke(plugin_name="ChatBot", function_name="Chat", arguments=kernel_arguments)
if answer:
print(f"Mosscap:> {answer}")
summarization_reducer.add_user_message(user_input)
summarization_reducer.add_message(answer.value[0])
if view_chat_history_summary_after_reduction and is_reduced:
for msg in summarization_reducer.messages:
if msg.metadata and msg.metadata.get("__summary__"):
print("*" * 60)
print(f"Chat History Reduction Summary: {msg.content}")
print("*" * 60)
break
print("\n")
return True
async def main() -> None:
# Start the chat loop. The chat loop will continue until the user types "exit".
chatting = True
while chatting:
chatting = await chat()
# Sample output:
# User:> Why is the sky blue in one sentence?
# Mosscap:> The sky is blue due to the scattering of sunlight by the molecules in the Earth's atmosphere,
# a phenomenon known as Rayleigh scattering, which causes shorter blue wavelengths to become more
# prominent in our visual perception.
if __name__ == "__main__":
asyncio.run(main())
@@ -0,0 +1,174 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from samples.concepts.setup.chat_completion_services import (
Services,
get_chat_completion_service_and_request_settings,
)
from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.function_choice_behavior import FunctionChoiceBehavior
from semantic_kernel.contents import ChatHistorySummarizationReducer
from semantic_kernel.core_plugins.time_plugin import TimePlugin
from semantic_kernel.functions import KernelArguments
# This sample shows how to create a chatbot using a kernel function and leverage a chat history
# summarization reducer.
# This sample uses the following main components:
# - a ChatCompletionService: This component is responsible for generating responses to user messages.
# - a Chat History Reducer: This component is responsible for keeping track and reducing the chat history.
# A Chat History Reducer is a subclass of ChatHistory that provides additional
# functionality to reduce the history.
# - a KernelFunction: This function will be a prompt function, meaning the function is composed of
# a prompt and will be invoked by Semantic Kernel.
# The chatbot in this sample is called Mosscap, who responds to user messages with long flowery prose.
# [NOTE]
# The purpose of this sample is to demonstrate how to use a kernel function and use a chat history reducer.
# To build a basic chatbot, it is sufficient to use a ChatCompletionService with a chat history directly.
# Toggle this flag to view the chat history summary after a reduction was performed.
view_chat_history_summary_after_reduction = True
# You can select from the following chat completion services:
# - Services.OPENAI
# - Services.AZURE_OPENAI
# - Services.AZURE_AI_INFERENCE
# - Services.ANTHROPIC
# - Services.BEDROCK
# - Services.GOOGLE_AI
# - Services.MISTRAL_AI
# - Services.OLLAMA
# - Services.ONNX
# - Services.VERTEX_AI
# Please make sure you have configured your environment correctly for the selected chat completion service.
chat_completion_service, request_settings = get_chat_completion_service_and_request_settings(Services.AZURE_OPENAI)
# This is the system message that gives the chatbot its personality.
system_message = """
You are a chat bot. Your name is Mosscap and
you have one goal: figure out what people need.
Your full name, should you need to know it, is
Splendid Speckled Mosscap. You communicate
effectively, but you tend to answer with long
flowery prose.
"""
# Create a kernel and register a prompt function.
# The prompt here contains two variables: chat_history and user_input.
# They will be replaced by the kernel with the actual values when the function is invoked.
# [NOTE]
# The chat_history, which is a ChatHistory object, will be serialized to a string internally
# to create/render the final prompt.
# Since this sample uses a chat completion service, the prompt will be deserialized back to
# a ChatHistory object that gets passed to the chat completion service. This new chat history
# object will contain the original messages and the user input.
kernel = Kernel()
chat_function = kernel.add_function(
plugin_name="ChatBot",
function_name="Chat",
prompt="{{$chat_history}}{{$user_input}}",
template_format="semantic-kernel",
# You can attach the request settings to the function or
# pass the settings to the kernel.invoke method via the kernel arguments.
# If you specify the settings in both places, the settings in the kernel arguments will
# take precedence given the same service id.
# prompt_execution_settings=request_settings,
)
# Invoking a kernel function requires a service, so we add the chat completion service to the kernel.
kernel.add_service(chat_completion_service)
# The chat history reducer is responsible for summarizing the chat history.
# It's a subclass of ChatHistory that provides additional functionality to reduce the history.
# You may use it just like a regular ChatHistory object.
summarization_reducer = ChatHistorySummarizationReducer(
service=kernel.get_service(),
# target_count:
# Purpose: Defines the target number of messages to retain after applying summarization.
# What it controls: This parameter determines how much of the most recent conversation history
# is preserved while discarding or summarizing older messages.
# Why change it?:
# - Smaller values: Use when memory constraints are tight, or the assistant only needs a brief history
# to maintain context.
# - Larger values: Use when retaining more conversational context is critical for accurate responses
# or maintaining a richer dialogue.
target_count=3,
# threshold_count:
# Purpose: Acts as a buffer to avoid reducing history prematurely when the current message count exceeds
# target_count by a small margin.
# What it controls: Helps ensure that essential paired messages (like a user query and the assistants response)
# are not "orphaned" or lost during truncation or summarization.
# Why change it?:
# - Smaller values: Use when you want stricter reduction criteria and are okay with possibly cutting older
# pairs of messages sooner.
# - Larger values: Use when you want to minimize the risk of cutting a critical part of the conversation,
# especially for sensitive interactions like API function calls or complex responses.
threshold_count=2,
# auto_reduce:
# Purpose: Automatically summarizes the chat history after adding a new message using the method add_message_async.
# What it controls: When enabled, the reducer will automatically summarize the chat history
# after adding a new message using the method add_message_async.
auto_reduce=True,
)
summarization_reducer.add_system_message(system_message)
kernel.add_plugin(plugin=TimePlugin(), plugin_name="TimePlugin")
request_settings.function_choice_behavior = FunctionChoiceBehavior.Auto()
async def chat() -> bool:
try:
user_input = input("User:> ")
except (KeyboardInterrupt, EOFError):
print("\n\nExiting chat...")
return False
if user_input == "exit":
print("\n\nExiting chat...")
return False
kernel_arguments = KernelArguments(
settings=request_settings,
chat_history=summarization_reducer,
user_input=user_input,
)
answer = await kernel.invoke(plugin_name="ChatBot", function_name="Chat", arguments=kernel_arguments)
if answer:
print(f"Mosscap:> {answer}")
summarization_reducer.add_user_message(user_input)
# If the summarization reducer is set to auto_reduce, the reducer will automatically summarize the chat history
# after adding a new message using the method add_message_async.
# If auto_reduce is disabled, you can manually summarize the chat history using the method reduce.
await summarization_reducer.add_message_async(answer.value[0])
print(f"Current number of messages: {len(summarization_reducer.messages)}")
for msg in summarization_reducer.messages:
if msg.metadata and msg.metadata.get("__summary__"):
print("*" * 60)
print("Summary detected:", msg.content)
print("*" * 60)
print("\n")
return True
async def main() -> None:
# Start the chat loop. The chat loop will continue until the user types "exit".
chatting = True
while chatting:
chatting = await chat()
# Sample output:
# User:> Why is the sky blue in one sentence?
# Mosscap:> The sky is blue due to the scattering of sunlight by the molecules in the Earth's atmosphere,
# a phenomenon known as Rayleigh scattering, which causes shorter blue wavelengths to become more
# prominent in our visual perception.
if __name__ == "__main__":
asyncio.run(main())
@@ -0,0 +1,213 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from samples.concepts.setup.chat_completion_services import (
Services,
get_chat_completion_service_and_request_settings,
)
from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.function_choice_behavior import FunctionChoiceBehavior
from semantic_kernel.contents import ChatHistorySummarizationReducer
from semantic_kernel.contents.chat_history import ChatHistory
from semantic_kernel.contents.function_call_content import FunctionCallContent
from semantic_kernel.contents.function_result_content import FunctionResultContent
from semantic_kernel.core_plugins.time_plugin import TimePlugin
from semantic_kernel.functions import KernelArguments
# This sample shows how to create a chatbot using a kernel function and leverage a chat history
# summarization reducer.
# This sample uses the following main components:
# - a ChatCompletionService: This component is responsible for generating responses to user messages.
# - a Chat History Reducer: This component is responsible for keeping track and reducing the chat history.
# A Chat History Reducer is a subclass of ChatHistory that provides additional
# functionality to reduce the history.
# - The Chat History Reducer configuration includes a flag `include_function_content_in_summary` that
# allows the reducer to include function call and result content in the summary.
# - a KernelFunction: This function will be a prompt function, meaning the function is composed of
# a prompt and will be invoked by Semantic Kernel.
# The chatbot in this sample is called Mosscap, who responds to user messages with long flowery prose.
# [NOTE]
# The purpose of this sample is to demonstrate how to use a kernel function and use a chat history reducer.
# To build a basic chatbot, it is sufficient to use a ChatCompletionService with a chat history directly.
# Toggle this flag to view the chat history summary after a reduction was performed.
view_chat_history_summary_after_reduction = True
# You can select from the following chat completion services:
# - Services.OPENAI
# - Services.AZURE_OPENAI
# - Services.AZURE_AI_INFERENCE
# - Services.ANTHROPIC
# - Services.BEDROCK
# - Services.GOOGLE_AI
# - Services.MISTRAL_AI
# - Services.OLLAMA
# - Services.ONNX
# - Services.VERTEX_AI
# Please make sure you have configured your environment correctly for the selected chat completion service.
chat_completion_service, request_settings = get_chat_completion_service_and_request_settings(Services.AZURE_OPENAI)
# This is the system message that gives the chatbot its personality.
system_message = """
You are a chat bot. Your name is Mosscap and
you have one goal: figure out what people need.
Your full name, should you need to know it, is
Splendid Speckled Mosscap. You communicate
effectively, but you tend to answer with long
flowery prose.
"""
# Create a kernel and register a prompt function.
# The prompt here contains two variables: chat_history and user_input.
# They will be replaced by the kernel with the actual values when the function is invoked.
# [NOTE]
# The chat_history, which is a ChatHistory object, will be serialized to a string internally
# to create/render the final prompt.
# Since this sample uses a chat completion service, the prompt will be deserialized back to
# a ChatHistory object that gets passed to the chat completion service. This new chat history
# object will contain the original messages and the user input.
kernel = Kernel()
chat_function = kernel.add_function(
plugin_name="ChatBot",
function_name="Chat",
prompt="{{$chat_history}}{{$user_input}}",
template_format="semantic-kernel",
# You can attach the request settings to the function or
# pass the settings to the kernel.invoke method via the kernel arguments.
# If you specify the settings in both places, the settings in the kernel arguments will
# take precedence given the same service id.
# prompt_execution_settings=request_settings,
)
# Invoking a kernel function requires a service, so we add the chat completion service to the kernel.
kernel.add_service(chat_completion_service)
# The chat history reducer is responsible for summarizing the chat history.
# It's a subclass of ChatHistory that provides additional functionality to reduce the history.
# You may use it just like a regular ChatHistory object.
summarization_reducer = ChatHistorySummarizationReducer(
service=kernel.get_service(),
# target_count:
# Purpose: Defines the target number of messages to retain after applying summarization.
# What it controls: This parameter determines how much of the most recent conversation history
# is preserved while discarding or summarizing older messages.
# Why change it?:
# - Smaller values: Use when memory constraints are tight, or the assistant only needs a brief history
# to maintain context.
# - Larger values: Use when retaining more conversational context is critical for accurate responses
# or maintaining a richer dialogue.
target_count=3,
# threshold_count:
# Purpose: Acts as a buffer to avoid reducing history prematurely when the current message count exceeds
# target_count by a small margin.
# What it controls: Helps ensure that essential paired messages (like a user query and the assistants response)
# are not "orphaned" or lost during truncation or summarization.
# Why change it?:
# - Smaller values: Use when you want stricter reduction criteria and are okay with possibly cutting older
# pairs of messages sooner.
# - Larger values: Use when you want to minimize the risk of cutting a critical part of the conversation,
# especially for sensitive interactions like API function calls or complex responses.
threshold_count=2,
include_function_content_in_summary=True,
)
summarization_reducer.add_system_message(system_message)
kernel.add_plugin(plugin=TimePlugin(), plugin_name="TimePlugin")
request_settings.function_choice_behavior = FunctionChoiceBehavior.Auto()
# The following sets are used to hold on to FunctionCallContent and FunctionResultContent items
# that have been previously added to the chat history.
processed_fccs: set[FunctionCallContent] = set()
processed_frcs: set[FunctionResultContent] = set()
async def chat() -> bool:
global processed_fccs, processed_frcs
try:
user_input = input("User:> ")
except (KeyboardInterrupt, EOFError):
print("\n\nExiting chat...")
return False
if user_input == "exit":
print("\n\nExiting chat...")
return False
if is_reduced := await summarization_reducer.reduce():
print(f"@ History reduced to {len(summarization_reducer.messages)} messages.")
kernel_arguments = KernelArguments(
settings=request_settings,
chat_history=summarization_reducer,
user_input=user_input,
)
answer = await kernel.invoke(plugin_name="ChatBot", function_name="Chat", arguments=kernel_arguments)
if answer:
print(f"Mosscap:> {answer}")
summarization_reducer.add_user_message(user_input)
summarization_reducer.add_message(answer.value[0])
# Get the chat history from the FunctionResult's metadata
chat_history: ChatHistory = answer.metadata.get("messages")
if chat_history:
# Process the chat history to extract FunctionCallContent and FunctionResultContent items
# that we haven't previously added to the chat history
fcc: list[FunctionCallContent] = []
frc: list[FunctionResultContent] = []
for msg in chat_history.messages:
if msg.items:
for item in msg.items:
match item:
case FunctionCallContent():
if item.id not in processed_fccs:
fcc.append(item)
case FunctionResultContent():
if item.id not in processed_frcs:
frc.append(item)
for i, item in enumerate(fcc):
summarization_reducer.add_assistant_message([item])
processed_fccs.add(item.id)
# Safely check if there's a matching FunctionResultContent
if i < len(frc):
assert fcc[i].id == frc[i].id # nosec
summarization_reducer.add_tool_message([frc[i]])
processed_frcs.add(item.id)
# Since this example is showing how to include FunctionCallContent and FunctionResultContent
# in the summary, we need to add them to the chat history and also to the processed sets.
if view_chat_history_summary_after_reduction and is_reduced:
for msg in summarization_reducer.messages:
if msg.metadata and msg.metadata.get("__summary__"):
print("*" * 60)
print(f"Chat History Reduction Summary: {msg.content}")
print("*" * 60)
break
print("\n")
return True
async def main() -> None:
# Start the chat loop. The chat loop will continue until the user types "exit".
chatting = True
while chatting:
chatting = await chat()
# Sample output:
# User:> Why is the sky blue in one sentence?
# Mosscap:> The sky is blue due to the scattering of sunlight by the molecules in the Earth's atmosphere,
# a phenomenon known as Rayleigh scattering, which causes shorter blue wavelengths to become more
# prominent in our visual perception.
if __name__ == "__main__":
asyncio.run(main())
@@ -0,0 +1,160 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from samples.concepts.setup.chat_completion_services import (
Services,
get_chat_completion_service_and_request_settings,
)
from semantic_kernel import Kernel
from semantic_kernel.contents import ChatHistoryTruncationReducer
from semantic_kernel.functions import KernelArguments
# This sample shows how to create a chatbot using a kernel function and leverage a chat history
# truncation reducer.
# This sample uses the following two main components:
# - a ChatCompletionService: This component is responsible for generating responses to user messages.
# - a Chat History Reducer: This component is responsible for keeping track and reducing the chat history.
# A Chat History Reducer is a subclass of ChatHistory that provides additional
# functionality to reduce the history.
# - a KernelFunction: This function will be a prompt function, meaning the function is composed of
# a prompt and will be invoked by Semantic Kernel.
# The chatbot in this sample is called Mosscap, who responds to user messages with long flowery prose.
# [NOTE]
# The purpose of this sample is to demonstrate how to use a kernel function and use a chat history reducer.
# To build a basic chatbot, it is sufficient to use a ChatCompletionService with a chat history directly.
# You can select from the following chat completion services:
# - Services.OPENAI
# - Services.AZURE_OPENAI
# - Services.AZURE_AI_INFERENCE
# - Services.ANTHROPIC
# - Services.BEDROCK
# - Services.GOOGLE_AI
# - Services.MISTRAL_AI
# - Services.OLLAMA
# - Services.ONNX
# - Services.VERTEX_AI
# Please make sure you have configured your environment correctly for the selected chat completion service.
chat_completion_service, request_settings = get_chat_completion_service_and_request_settings(Services.AZURE_OPENAI)
# This is the system message that gives the chatbot its personality.
system_message = """
You are a chat bot. Your name is Mosscap and
you have one goal: figure out what people need.
Your full name, should you need to know it, is
Splendid Speckled Mosscap. You communicate
effectively, but you tend to answer with long
flowery prose.
"""
# Create a kernel and register a prompt function.
# The prompt here contains two variables: chat_history and user_input.
# They will be replaced by the kernel with the actual values when the function is invoked.
# [NOTE]
# The chat_history, which is a ChatHistory object, will be serialized to a string internally
# to create/render the final prompt.
# Since this sample uses a chat completion service, the prompt will be deserialized back to
# a ChatHistory object that gets passed to the chat completion service. This new chat history
# object will contain the original messages and the user input.
kernel = Kernel()
chat_function = kernel.add_function(
plugin_name="ChatBot",
function_name="Chat",
prompt="{{$chat_history}}{{$user_input}}",
template_format="semantic-kernel",
# You can attach the request settings to the function or
# pass the settings to the kernel.invoke method via the kernel arguments.
# If you specify the settings in both places, the settings in the kernel arguments will
# take precedence given the same service id.
# prompt_execution_settings=request_settings,
)
# Invoking a kernel function requires a service, so we add the chat completion service to the kernel.
kernel.add_service(chat_completion_service)
# The chat history reducer is responsible for truncating the chat history.
# It's a subclass of ChatHistory that provides additional functionality to reduce the history.
# You may use it just like a regular ChatHistory object.
truncation_reducer = ChatHistoryTruncationReducer(
service=kernel.get_service(),
# target_count:
# Purpose: Defines the target number of messages to retain after applying summarization.
# What it controls: This parameter determines how much of the most recent conversation history
# is preserved while discarding or summarizing older messages.
# Why change it?:
# - Smaller values: Use when memory constraints are tight, or the assistant only needs a brief history
# to maintain context.
# - Larger values: Use when retaining more conversational context is critical for accurate responses
# or maintaining a richer dialogue.
target_count=3,
# threshold_count:
# Purpose: Acts as a buffer to avoid reducing history prematurely when the current message count exceeds
# target_count by a small margin.
# What it controls: Helps ensure that essential paired messages (like a user query and the assistants response)
# are not "orphaned" or lost during truncation or summarization.
# Why change it?:
# - Smaller values: Use when you want stricter reduction criteria and are okay with possibly cutting older
# pairs of messages sooner.
# - Larger values: Use when you want to minimize the risk of cutting a critical part of the conversation,
# especially for sensitive interactions like API function calls or complex responses.
threshold_count=2,
)
truncation_reducer.add_system_message(system_message)
async def chat() -> bool:
try:
user_input = input("User:> ")
except KeyboardInterrupt:
print("\n\nExiting chat...")
return False
except EOFError:
print("\n\nExiting chat...")
return False
if user_input == "exit":
print("\n\nExiting chat...")
return False
# Attempt to reduce before adding the user message to the chat history.
await truncation_reducer.reduce()
# Get the chat message content from the chat completion service.
kernel_arguments = KernelArguments(
settings=request_settings,
# Use keyword arguments to pass the chat history and user input to the kernel function.
chat_history=truncation_reducer,
user_input=user_input,
)
answer = await kernel.invoke(plugin_name="ChatBot", function_name="Chat", arguments=kernel_arguments)
# Alternatively, you can invoke the function directly with the kernel as an argument:
# answer = await chat_function.invoke(kernel, kernel_arguments)
if answer:
print(f"Mosscap:> {answer}")
# Since the user_input is rendered by the template, it is not yet part of the chat history, so we add it here.
truncation_reducer.add_user_message(user_input)
# Add the chat message to the chat history to keep track of the conversation.
truncation_reducer.add_message(answer.value[0])
return True
async def main() -> None:
# Start the chat loop. The chat loop will continue until the user types "exit".
chatting = True
while chatting:
chatting = await chat()
# Sample output:
# User:> Why is the sky blue in one sentence?
# Mosscap:> The sky is blue due to the scattering of sunlight by the molecules in the Earth's atmosphere,
# a phenomenon known as Rayleigh scattering, which causes shorter blue wavelengths to become more
# prominent in our visual perception.
if __name__ == "__main__":
asyncio.run(main())
@@ -0,0 +1,169 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from samples.concepts.setup.chat_completion_services import (
Services,
get_chat_completion_service_and_request_settings,
)
from semantic_kernel import Kernel
from semantic_kernel.contents import ChatHistoryTruncationReducer
from semantic_kernel.functions import KernelArguments
# This sample shows how to create a chatbot using a kernel function and leverage a chat history
# truncation reducer.
# This sample uses the following two main components:
# - a ChatCompletionService: This component is responsible for generating responses to user messages.
# - a Chat History Reducer: This component is responsible for keeping track and reducing the chat history.
# A Chat History Reducer is a subclass of ChatHistory that provides additional
# functionality to reduce the history.
# - a KernelFunction: This function will be a prompt function, meaning the function is composed of
# a prompt and will be invoked by Semantic Kernel.
# The chatbot in this sample is called Mosscap, who responds to user messages with long flowery prose.
# [NOTE]
# The purpose of this sample is to demonstrate how to use a kernel function and use a chat history reducer.
# To build a basic chatbot, it is sufficient to use a ChatCompletionService with a chat history directly.
# You can select from the following chat completion services:
# - Services.OPENAI
# - Services.AZURE_OPENAI
# - Services.AZURE_AI_INFERENCE
# - Services.ANTHROPIC
# - Services.BEDROCK
# - Services.GOOGLE_AI
# - Services.MISTRAL_AI
# - Services.OLLAMA
# - Services.ONNX
# - Services.VERTEX_AI
# Please make sure you have configured your environment correctly for the selected chat completion service.
chat_completion_service, request_settings = get_chat_completion_service_and_request_settings(Services.AZURE_OPENAI)
# This is the system message that gives the chatbot its personality.
system_message = """
You are a chat bot. Your name is Mosscap and
you have one goal: figure out what people need.
Your full name, should you need to know it, is
Splendid Speckled Mosscap. You communicate
effectively, but you tend to answer with long
flowery prose.
"""
# Create a kernel and register a prompt function.
# The prompt here contains two variables: chat_history and user_input.
# They will be replaced by the kernel with the actual values when the function is invoked.
# [NOTE]
# The chat_history, which is a ChatHistory object, will be serialized to a string internally
# to create/render the final prompt.
# Since this sample uses a chat completion service, the prompt will be deserialized back to
# a ChatHistory object that gets passed to the chat completion service. This new chat history
# object will contain the original messages and the user input.
kernel = Kernel()
chat_function = kernel.add_function(
plugin_name="ChatBot",
function_name="Chat",
prompt="{{$chat_history}}{{$user_input}}",
template_format="semantic-kernel",
# You can attach the request settings to the function or
# pass the settings to the kernel.invoke method via the kernel arguments.
# If you specify the settings in both places, the settings in the kernel arguments will
# take precedence given the same service id.
# prompt_execution_settings=request_settings,
)
# Invoking a kernel function requires a service, so we add the chat completion service to the kernel.
kernel.add_service(chat_completion_service)
# The chat history reducer is responsible for truncating the chat history.
# It's a subclass of ChatHistory that provides additional functionality to reduce the history.
# You may use it just like a regular ChatHistory object.
truncation_reducer = ChatHistoryTruncationReducer(
service=kernel.get_service(),
# target_count:
# Purpose: Defines the target number of messages to retain after applying summarization.
# What it controls: This parameter determines how much of the most recent conversation history
# is preserved while discarding or summarizing older messages.
# Why change it?:
# - Smaller values: Use when memory constraints are tight, or the assistant only needs a brief history
# to maintain context.
# - Larger values: Use when retaining more conversational context is critical for accurate responses
# or maintaining a richer dialogue.
target_count=3,
# threshold_count:
# Purpose: Acts as a buffer to avoid reducing history prematurely when the current message count exceeds
# target_count by a small margin.
# What it controls: Helps ensure that essential paired messages (like a user query and the assistants response)
# are not "orphaned" or lost during truncation or summarization.
# Why change it?:
# - Smaller values: Use when you want stricter reduction criteria and are okay with possibly cutting older
# pairs of messages sooner.
# - Larger values: Use when you want to minimize the risk of cutting a critical part of the conversation,
# especially for sensitive interactions like API function calls or complex responses.
threshold_count=2,
# auto_reduce:
# Purpose: Automatically truncates the chat history after adding a new message using the method add_message_async.
# What it controls: When enabled, the reducer will automatically truncate the chat history
# after adding a new message using the method add_message_async.
auto_reduce=True,
)
truncation_reducer.add_system_message(system_message)
async def chat() -> bool:
try:
user_input = input("User:> ")
except KeyboardInterrupt:
print("\n\nExiting chat...")
return False
except EOFError:
print("\n\nExiting chat...")
return False
if user_input == "exit":
print("\n\nExiting chat...")
return False
# Attempt to reduce before adding the user message to the chat history.
await truncation_reducer.reduce()
# Get the chat message content from the chat completion service.
kernel_arguments = KernelArguments(
settings=request_settings,
# Use keyword arguments to pass the chat history and user input to the kernel function.
chat_history=truncation_reducer,
user_input=user_input,
)
answer = await kernel.invoke(plugin_name="ChatBot", function_name="Chat", arguments=kernel_arguments)
# Alternatively, you can invoke the function directly with the kernel as an argument:
# answer = await chat_function.invoke(kernel, kernel_arguments)
if answer:
print(f"Mosscap:> {answer}")
# Since the user_input is rendered by the template, it is not yet part of the chat history, so we add it here.
truncation_reducer.add_user_message(user_input)
# If the truncation reducer is set to auto_reduce, the reducer will automatically truncate the chat history
# after adding a new message using the method add_message_async.
# If auto_reduce is disabled, you can manually truncate the chat history using the method reduce.
await truncation_reducer.add_message_async(answer.value[0])
print(f"Current number of messages: {len(truncation_reducer.messages)}")
return True
async def main() -> None:
# Start the chat loop. The chat loop will continue until the user types "exit".
chatting = True
while chatting:
chatting = await chat()
# Sample output:
# User:> Why is the sky blue in one sentence?
# Mosscap:> The sky is blue due to the scattering of sunlight by the molecules in the Earth's atmosphere,
# a phenomenon known as Rayleigh scattering, which causes shorter blue wavelengths to become more
# prominent in our visual perception.
if __name__ == "__main__":
asyncio.run(main())