mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
This commit corrects the import paths for openai_settings_from_dot_env and azure_openai_settings_from_dot_env functions (and some more). Previously, these functions were incorrectly accessed directly from the semantic_kernel module. The updated paths point correctly to semantic_kernel.utils.settings, aligning with the package structure. This change ensures better code maintainability and error-free imports. ### Motivation and Context Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: **Why is this change required?** The change is required to correct incorrect import paths for configuration loading functions which were previously leading to AttributeError. This improvement ensures that the module imports function correctly and aligns with the expected package structure. **What problem does it solve?** This change solves the problem of AttributeError being thrown when attempting to access openai_settings_from_dot_env and azure_openai_settings_from_dot_env. These functions were incorrectly imported, causing module resolution errors in the user's environment. **What scenario does it contribute to?** This contributes to scenarios where developers need to integrate the semantic-kernel package seamlessly with their projects without encountering import errors. It facilitates easier setup and usage of the semantic-kernel functionalities, particularly in settings involving environmental configurations for AI services. ### Description This update corrects the import paths for **openai_settings_from_dot_env** and **azure_openai_settings_from_dot_env** by specifying their correct locations within the **semantic_kernel.utils.settings** module. Previously, attempts to import these functions directly from the **semantic_kernel** root module led to errors. The corrected paths ensure these essential configuration functions are accessible, supporting error-free configuration loading for OpenAI and Azure AI services integration. ### Contribution Checklist - [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 😄 Co-authored-by: Konstantin Kohl <[email protected]> Co-authored-by: Evan Mattson <[email protected]>
16 KiB
16 KiB
In [ ]:
!python -m pip install semantic-kernel==0.9.6b1In [ ]:
from services import Service
# Select a service to use for this notebook (available services: OpenAI, AzureOpenAI, HuggingFace)
selectedService = Service.OpenAIIn [ ]:
import semantic_kernel as sk
kernel = sk.Kernel()
service_id = None
if selectedService == Service.OpenAI:
from semantic_kernel.connectors.ai.open_ai import OpenAITextCompletion
from semantic_kernel.utils.settings import openai_settings_from_dot_env
api_key, org_id = openai_settings_from_dot_env()
service_id = "oai_text_completion"
kernel.add_service(
OpenAITextCompletion(
service_id=service_id, ai_model_id="gpt-3.5-turbo-instruct", api_key=api_key, org_id=org_id
),
)
elif selectedService == Service.AzureOpenAI:
from semantic_kernel.connectors.ai.open_ai import AzureTextCompletion
from semantic_kernel.utils.settings import azure_openai_settings_from_dot_env
deployment, api_key, endpoint = azure_openai_settings_from_dot_env()
service_id = "aoai_text_completion"
kernel.add_service(
AzureTextCompletion(service_id=service_id, deployment_name=deployment, endpoint=endpoint, api_key=api_key),
)In [ ]:
from semantic_kernel.connectors.ai.open_ai import OpenAITextPromptExecutionSettings
from semantic_kernel.prompt_template import PromptTemplateConfig, InputVariable
prompt = """{{$input}}
Summarize the content above.
"""
if selectedService == Service.OpenAI:
execution_settings = OpenAITextPromptExecutionSettings(
service_id=service_id,
ai_model_id="gpt-3.5-turbo-instruct",
max_tokens=2000,
temperature=0.7,
)
elif selectedService == Service.AzureOpenAI:
execution_settings = OpenAITextPromptExecutionSettings(
service_id=service_id,
ai_model_id=deployment,
max_tokens=2000,
temperature=0.7,
)
prompt_template_config = PromptTemplateConfig(
template=prompt,
name="summarize",
template_format="semantic-kernel",
input_variables=[
InputVariable(name="input", description="The user input", is_required=True),
],
execution_settings=execution_settings,
)
summarize = kernel.add_function(
function_name="summarizeFunc",
plugin_name="summarizePlugin",
prompt_template_config=prompt_template_config,
)In [ ]:
input_text = """
Demo (ancient Greek poet)
From Wikipedia, the free encyclopedia
Demo or Damo (Greek: Δεμώ, Δαμώ; fl. c. AD 200) was a Greek woman of the Roman period, known for a single epigram, engraved upon the Colossus of Memnon, which bears her name. She speaks of herself therein as a lyric poetess dedicated to the Muses, but nothing is known of her life.[1]
Identity
Demo was evidently Greek, as her name, a traditional epithet of Demeter, signifies. The name was relatively common in the Hellenistic world, in Egypt and elsewhere, and she cannot be further identified. The date of her visit to the Colossus of Memnon cannot be established with certainty, but internal evidence on the left leg suggests her poem was inscribed there at some point in or after AD 196.[2]
Epigram
There are a number of graffiti inscriptions on the Colossus of Memnon. Following three epigrams by Julia Balbilla, a fourth epigram, in elegiac couplets, entitled and presumably authored by "Demo" or "Damo" (the Greek inscription is difficult to read), is a dedication to the Muses.[2] The poem is traditionally published with the works of Balbilla, though the internal evidence suggests a different author.[1]
In the poem, Demo explains that Memnon has shown her special respect. In return, Demo offers the gift for poetry, as a gift to the hero. At the end of this epigram, she addresses Memnon, highlighting his divine status by recalling his strength and holiness.[2]
Demo, like Julia Balbilla, writes in the artificial and poetic Aeolic dialect. The language indicates she was knowledgeable in Homeric poetry—'bearing a pleasant gift', for example, alludes to the use of that phrase throughout the Iliad and Odyssey.[a][2]
"""In [ ]:
from semantic_kernel.functions import KernelArguments
summary = await kernel.invoke(summarize, KernelArguments(input=input_text))
print(summary)In [ ]:
kernel = sk.Kernel()
service_id = None
if selectedService == Service.OpenAI:
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
api_key, org_id = openai_settings_from_dot_env()
service_id = "oai_chat_gpt"
kernel.add_service(
OpenAIChatCompletion(service_id=service_id, ai_model_id="gpt-3.5-turbo-1106", api_key=api_key, org_id=org_id),
)
elif selectedService == Service.AzureOpenAI:
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
deployment, api_key, endpoint = azure_openai_settings_from_dot_env()
service_id = "aoai_chat_completion"
kernel.add_service(
AzureChatCompletion(service_id=service_id, deployment_name=deployment, endpoint=endpoint, api_key=api_key),
)In [ ]:
prompt = """
{{$input}}
Give me the TLDR in 5 words or less.
"""
text = """
1) A robot may not injure a human being or, through inaction,
allow a human being to come to harm.
2) A robot must obey orders given it by human beings except where
such orders would conflict with the First Law.
3) A robot must protect its own existence as long as such protection
does not conflict with the First or Second Law.
"""
from semantic_kernel.connectors.ai.open_ai.prompt_execution_settings.open_ai_prompt_execution_settings import (
OpenAIChatPromptExecutionSettings,
)
if selectedService == Service.OpenAI:
execution_settings = OpenAIChatPromptExecutionSettings(
service_id=service_id,
ai_model_id="gpt-3.5-turbo-1106",
max_tokens=2000,
temperature=0.7,
)
elif selectedService == Service.AzureOpenAI:
execution_settings = OpenAIChatPromptExecutionSettings(
service_id=service_id,
ai_model_id=deployment,
max_tokens=2000,
temperature=0.7,
)
prompt_template_config = PromptTemplateConfig(
template=prompt,
name="tldr",
template_format="semantic-kernel",
input_variables=[
InputVariable(name="input", description="The user input", is_required=True),
],
execution_settings=execution_settings,
)
tldr_function = kernel.add_function(
function_name="tldrFunction",
plugin_name="tldrPlugin",
prompt_template_config=prompt_template_config,
)
summary = await kernel.invoke(tldr_function, KernelArguments(input=text))
print(f"Output: {summary}")