mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
### Motivation and Context This PR makes creating a kernel instance more pythonic. The builder pattern is removed and the kernel is constructed using Kernel(...). Work originally started with #137 ### Description - Removed kernel_builder.py. - Instance creation pattern `kernel = sk.create_kernel() -> kernel = sk.Kernel()` - For notebook 6, there is no need to create a separate kernel for the GitHub files. Changed to use a separate collection. Co-authored-by: Devis Lucato <[email protected]>
7.4 KiB
7.4 KiB
In [ ]:
!python -m pip install -r requirements.txtIn [ ]:
import semantic_kernel as sk
from semantic_kernel.ai.open_ai import AzureTextCompletion, OpenAITextCompletion
kernel = sk.Kernel()
useAzureOpenAI = False
# Configure AI backend used by the kernel
if useAzureOpenAI:
deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()
kernel.config.add_text_backend("dv", AzureTextCompletion(deployment, endpoint, api_key))
else:
api_key, org_id = sk.openai_settings_from_dot_env()
kernel.config.add_text_backend("dv", OpenAITextCompletion("text-davinci-003", api_key, org_id))In [ ]:
prompt = """{{$input}}
Summarize the content above.
"""
summarize = kernel.create_semantic_function(prompt, max_tokens=2000, temperature=0.2, top_p=0.5)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 [ ]:
# If needed, async is available too: summary = await summarize.invoke_async(input_text)
summary = summarize(input_text)
print(summary)In [ ]:
sk_prompt = """
{{$input}}
Give me the TLDR in 5 words.
"""
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.
"""
tldr_function = kernel.create_semantic_function(sk_prompt, max_tokens=200, temperature=0, top_p=0.5)
summary = tldr_function(text)
print(f"Output: {summary}") # Output: Robots must not harm humans.