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. --> Bumped the version to 1.8.1, slight updates in pyproject and make to build automatically. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### 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 - [x] I didn't break anyone 😄
13 KiB
13 KiB
In [ ]:
# Note: if using a virtual environment, do not run this cell
%pip install -U semantic-kernel
from semantic_kernel import __version__
__version__In [ ]:
# Make sure paths are correct for the imports
import os
import sys
notebook_dir = os.path.abspath("")
parent_dir = os.path.dirname(notebook_dir)
grandparent_dir = os.path.dirname(parent_dir)
sys.path.append(grandparent_dir)In [ ]:
from services import Service
from samples.service_settings import ServiceSettings
service_settings = ServiceSettings.create()
# Select a service to use for this notebook (available services: OpenAI, AzureOpenAI, HuggingFace)
selectedService = (
Service.AzureOpenAI
if service_settings.global_llm_service is None
else Service(service_settings.global_llm_service.lower())
)
print(f"Using service type: {selectedService}")In [ ]:
from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import (
AzureChatCompletion,
AzureChatPromptExecutionSettings, # noqa: F401
AzureTextCompletion,
OpenAIChatCompletion,
OpenAIChatPromptExecutionSettings, # noqa: F401
OpenAITextCompletion,
OpenAITextPromptExecutionSettings, # noqa: F401
)
from semantic_kernel.contents import ChatHistory # noqa: F401
kernel = Kernel()
service_id = None
if selectedService == Service.OpenAI:
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
service_id = "default"
oai_chat_service = OpenAIChatCompletion(
service_id="oai_chat",
)
oai_text_service = OpenAITextCompletion(
service_id="oai_text",
)
elif selectedService == Service.AzureOpenAI:
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
service_id = "default"
aoai_chat_service = AzureChatCompletion(
service_id="aoai_chat",
)
aoai_text_service = AzureTextCompletion(
service_id="aoai_text",
)
# Configure Hugging Face service
if selectedService == Service.HuggingFace:
from semantic_kernel.connectors.ai.hugging_face import (
HuggingFacePromptExecutionSettings, # noqa: F401
HuggingFaceTextCompletion,
)
hf_text_service = HuggingFaceTextCompletion(ai_model_id="distilgpt2", task="text-generation")In [ ]:
oai_prompt_execution_settings = OpenAITextPromptExecutionSettings(
service_id="oai_text",
max_tokens=150,
temperature=0.7,
top_p=1,
frequency_penalty=0.5,
presence_penalty=0.5,
)In [ ]:
if selectedService == Service.OpenAI:
prompt = "What is the purpose of a rubber duck?"
stream = oai_text_service.get_streaming_text_contents(prompt=prompt, settings=oai_prompt_execution_settings)
async for message in stream:
print(str(message[0]), end="") # end = "" to avoid newlinesIn [ ]:
if selectedService == Service.AzureOpenAI:
prompt = "provide me a list of possible meanings for the acronym 'ORLD'"
stream = aoai_text_service.get_streaming_text_contents(prompt=prompt, settings=oai_prompt_execution_settings)
async for message in stream:
print(str(message[0]), end="")In [ ]:
if selectedService == Service.HuggingFace:
hf_prompt_execution_settings = HuggingFacePromptExecutionSettings(
service_id="hf_text",
extension_data={
"max_new_tokens": 80,
"top_p": 1,
"eos_token_id": 11,
"pad_token_id": 0,
},
)In [ ]:
if selectedService == Service.HuggingFace:
prompt = "The purpose of a rubber duck is"
stream = hf_text_service.get_streaming_text_contents(
prompt=prompt, prompt_execution_settings=hf_prompt_execution_settings
)
async for text in stream:
print(str(text[0]), end="") # end = "" to avoid newlinesIn [ ]:
oai_chat_prompt_execution_settings = OpenAIChatPromptExecutionSettings(
service_id="oai_chat",
max_tokens=150,
temperature=0.7,
top_p=1,
frequency_penalty=0.5,
presence_penalty=0.5,
)In [ ]:
if selectedService == Service.OpenAI:
content = "You are an AI assistant that helps people find information."
chat = ChatHistory()
chat.add_system_message(content)
stream = oai_chat_service.get_streaming_chat_message_contents(
chat_history=chat, settings=oai_chat_prompt_execution_settings
)
async for text in stream:
print(str(text[0]), end="") # end = "" to avoid newlinesIn [ ]:
az_oai_chat_prompt_execution_settings = AzureChatPromptExecutionSettings(
service_id="aoai_chat",
max_tokens=150,
temperature=0.7,
top_p=1,
frequency_penalty=0.5,
presence_penalty=0.5,
)In [ ]:
if selectedService == Service.AzureOpenAI:
content = "You are an AI assistant that helps people find information."
chat = ChatHistory()
chat.add_system_message(content)
chat.add_user_message("What is the purpose of a rubber duck?")
stream = aoai_chat_service.get_streaming_chat_message_contents(
chat_history=chat, settings=az_oai_chat_prompt_execution_settings
)
async for text in stream:
print(str(text[0]), end="") # end = "" to avoid newlines