🪄

Semantic Kernel Getting Started Part1 基礎編

2023/09/20に公開

早いものでChatGPTが登場して1年近くになる。この1年のAIの進化はものすごく、さまざまなプロダクトが誕生した。そして個人的に注目しているのが MicrosoftのSemantic Kernelである。以前から時々触ってはいたが、改めて復習もかねてまとめていきたい。

Microsoft Learn
クイックスタートガイド

1. Semantic Kernel の環境構築(python版)

インストール

Semantic Kernel のpythonライブラリはpipでインストールできる。今回はWSL2にDebian12をインストールしvenv環境で利用

(myvenv):~/myvenv$ pip install semantic-kernel

レポジトリのクローン

Semantic Kernel(SK)の公式レポジトリをクローン

(myvenv):~/myvenv/work$ git clone https://github.com/microsoft/semantic-kernel

2. notebookで基本を学ぶ

学習用のnotebookがいくつかあるので、まずはそれをやってみる。

.env(環境変数)の準備

openaiのキーなどの情報を.envファイルに書いておくとSKはそれを読み込むことができるようになる。notebooksディレクトリに .env.example というファイルがあるのでそれをコピーして使うとよいだろう。とりあえずOPENAI_API_KEYが入っていればよい。.envファイルは念のためmodeを600に変更しておく

vscodeでWSL上のファイルを扱えるようにする

vscodeでもnotebookを扱うことができる

仮想環境上でipykernelをインストール

仮想環境上でipykernelをインストール

(myvenv) :~/myvenv/work/semantic-kernel/python/notebooks$ pip install ipykernel

jupyterにカーネルを追加

(myvenv) :~/myvenv/work/semantic-kernel/python/notebooks$ ipython kernel install --user --name=myvenv

これでカーネル選択で myvenv内の pythonが選べるようになる

あとはnotebookを実行するだけだ

00-getting-started.ipynb

手始めに最初のnotebookを動かしてみる

SKのインストール

!python -m pip install semantic-kernel==0.3.10.dev0

これを実行するとsemantic-kernel が古いバージョンで上書きされてしまうのでやらなくてもよい

ライブラリのインポート

import semantic_kernel as sk
kernel = sk.Kernel()

カーネルにChatCompletionサービスを追加

from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
api_key, org_id = sk.openai_settings_from_dot_env()
kernel.add_chat_service("chat-gpt", OpenAIChatCompletion("gpt-3.5-turbo", api_key, org_id))

OpenAIのChatCompletionをロードする。以前はTextCompetion(Davinci)だったような気がするが、ChatCompletionに統一されたのだろう

.envファイルからキーを読みだし、カーネルに設定

Semantic関数の実行

skill = kernel.import_semantic_skill_from_directory("../../samples/skills", "FunSkill")
joke_function = skill["Joke"]
print(joke_function("恐竜の世界へタイムトラベル"))

Funskillをファイルからインポートし、Jokeスキルを実行するSemantic関数を作成する

Semantic関数の引数として何か文言を入れてやると、それに関するジョークを出力する

skill = kernel.import_semantic_skill_from_directory("../../samples/skills", "FunSkill")
joke_function = skill["Joke"]
print(joke_function("恐竜の世界へタイムトラベル"))

日本語で応答させる

以上で動いたことは動いたのだが、引数に日本語を入れたにもかかわらず、応答は英語のままであった。GPTに日本語で応答させるため、JokeスキルのプロンプトにIN JAPANESEを追加する

編集前

(myvenv) :~/myvenv/work/semantic-kernel/samples/skills/FunSkill/Joke$ cat skprompt.txt

WRITE EXACTLY ONE JOKE or HUMOROUS STORY ABOUT THE TOPIC BELOW

JOKE MUST BE:
- G RATED
- WORKPLACE/FAMILY SAFE
NO SEXISM, RACISM OR OTHER BIAS/BIGOTRY

BE CREATIVE AND FUNNY. I WANT TO LAUGH.
Incorporate the style suggestion, if provided: {{$style}}
+++++

{{$input}}
+++++

編集後

WRITE EXACTLY ONE JOKE or HUMOROUS STORY ABOUT THE TOPIC BELOW

JOKE MUST BE:
- IN JAPANESE
- G RATED
- WORKPLACE/FAMILY SAFE
NO SEXISM, RACISM OR OTHER BIAS/BIGOTRY

BE CREATIVE AND FUNNY. I WANT TO LAUGH.
Incorporate the style suggestion, if provided: {{$style}}
+++++

{{$input}}
+++++         

これで再度実行する

ある日、恐竜の世界へタイムトラベルすることになったサラリーマンがいました。
サラリーマン:「やった!恐竜の世界に行けるんだ!」
タイムマシンに乗り込んだサラリーマンは、一瞬で恐竜の時代へと飛ばされました。
すると、目の前には巨大なティラノサウルスが現れました。
サラリーマン:「うわっ!ティラノサウルスだ!」
ティラノサウルス:「こんにちは!」
サラリーマン:「えっ、ティラノサウルスが話せるの?」
ティラノサウルス:「そうだよ!でも、ちょっと困ってるんだ。」
サラリーマン:「どうしたんですか?」
ティラノサウルス:「最近、恐竜の友達がみんな私のことを『おっさんザウルス』って呼ぶんだよ。」
サラリーマン:「それは大変ですね。でも、私が言っても分かってもらえないかもしれませんね。」
ティラノサウルス:「そうだよね…でも、君は人間だから、何かアドバイスをくれないかな?」
サラリーマン:「それなら、私が考えたニックネームを使ってみてはどうですか?」
ティラノサウルス:「ニックネーム?何かいいのがあるの?」
サラリーマン:「えーと…『ティラノサーラリーマン』ってどうですか?」
ティラノサウルス:「おお!それはいいね!私、ティラノサーラリーマンになる!ありがとう!」
サラリーマン:「どういたしまして!これからはみんなが尊敬する存在になれるよ!」
ティラノサーラリーマンは、新しいニックネームを胸に、恐竜の世界で活躍することになったのでした。
笑ってくれてありがとう!

日本語で応答させることができた。

しかしジョークにしては長い。しかもあんまり面白くないし。

01-basic-loading-the-kernel.ipynb

次のBasic Loading of the Kernelは、単にカーネルのロード方法を書いてるだけだ

!python -m pip install semantic-kernel==0.3.10.dev0

import semantic_kernel as sk
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion, OpenAIChatCompletion

ここまでは前と同じ

# Simple instance
kernel_1 = sk.Kernel()

単にインスタンスを作成する。もしくは、

# Instance with a custom logger
my_logger = sk.NullLogger()
kernel_2 = sk.Kernel(log=my_logger)

カスタムロガーを設定したインスタンスを作成

そしてこんな感じで、複数のChat Serviceを設定することができる

kernel = sk.Kernel()

kernel.add_chat_service(                      # We are adding a text service
    "Azure_curie",                            # The alias we can use in prompt templates' config.json
    AzureChatCompletion(
	"my-finetuned-Curie",                 # Azure OpenAI *Deployment name*
	"https://contoso.openai.azure.com/",  # Azure OpenAI *Endpoint*
	"...your Azure OpenAI Key..."         # Azure OpenAI *Key*
    )
)

kernel.add_chat_service(                      # We are adding a text service
    "OpenAI_chat_gpt",                        # The alias we can use in prompt templates' config.json
    OpenAIChatCompletion(
	"gpt-3.5-turbo",                      # OpenAI Model Name
	"...your OpenAI API Key...",          # OpenAI API key
	"...your OpenAI Org ID..."            # *optional* OpenAI Organization ID
    )
)	

最初のサービスがDefaultとして使われるが、Defaultを変更したい場合は以下を実行

kernel.set_default_text_completion_service("Azure_curie")

このファイルはこれで終わり

02-running-prompts-from-file.ipynb

次のnotebookはファイルからセマンティックスキルを実行する方法、である。しかしこれ、最初にやったのと同じなので、省略

03-semantic-function-inline.ipynb

今度はファイルではなくインラインでのSemantic関数の実行方法について

import semantic_kernel as sk
from semantic_kernel.connectors.ai.open_ai import AzureTextCompletion, OpenAITextCompletion

kernel = sk.Kernel()

useAzureOpenAI = False

# Configure AI service used by the kernel
if useAzureOpenAI:
    deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()
    kernel.add_text_completion_service("dv", AzureTextCompletion(deployment, endpoint, api_key))
else:
    api_key, org_id = sk.openai_settings_from_dot_env()
    kernel.add_text_completion_service("dv", OpenAITextCompletion("text-davinci-003", api_key, org_id))

ここまではいつものやつ

prompt = """{{$input}}
Summarize the content above.
"""

summarize = kernel.create_semantic_function(prompt, max_tokens=2000, temperature=0.2, top_p=0.5)

はじめにプロンプトを定義し、create_semantic_function関数に渡してSemantic関数を作成している。プロンプトの内容は見ればわかるが、ユーザーのインプットをサマライズするというもの

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] 
"""

この長いインプットを

# If needed, async is available too: summary = await summarize.invoke_async(input_text)
summary = summarize(input_text)

print(summary)

作成したSemantic関数に渡してサマライズしている。結果はこちら

Demo was a Greek poetess of the Roman period, known for an epigram inscribed on the Colossus of Memnon. Her identity is unknown, but her poem suggests she was knowledgeable in Homeric poetry. The poem is a dedication to the Muses, in which she offers the gift of poetry to the hero Memnon in return for his special respect.

Demoって何のことかと思ったら人名か

Using ChatCompletion for Semantic Skills

このファイルにはもう一つ例が入っている。今度はChatCompletionを使う例

import semantic_kernel as sk
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion, OpenAIChatCompletion

kernel = sk.Kernel()

useAzureOpenAI = False

# Configure AI service used by the kernel
if useAzureOpenAI:
    deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()
    kernel.add_chat_service(
	"chat_completion",
	AzureChatCompletion(deployment, endpoint, api_key),
    )
else:
    api_key, org_id = sk.openai_settings_from_dot_env()
    kernel.add_chat_service(
	"chat-gpt", OpenAIChatCompletion("gpt-3.5-turbo", api_key, org_id)
    )

sk_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.
"""

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.

tldr_functionというSemantic関数をインラインで作成して使っている。TLDRというのは「Too Long, Didn't Read」の略で長すぎる、とか要約とかいうこと

実行結果

Output: Robots must prioritize human safety.

04-context-variables-chat.ipynb

前半は同じなので省略

sk_prompt = """
ChatBot can have a conversation with you about any topic.
It can give explicit instructions or say 'I don't know' if it does not have an answer.

{{$history}}
User: {{$user_input}}
ChatBot: """

プロンプトを定義

chat_function = kernel.create_semantic_function(sk_prompt, "ChatBot", max_tokens=2000, temperature=0.7, top_p=0.5)

chat_functionというSemantic関数を作成

context = kernel.create_new_context()
context["history"] = ""

新しいコンテキストの作成。さて、ここでコンテキストという言葉が出てきたが、ここではセッション情報を保持するための変数の入れ物みたいなものか

context["user_input"] = "Hi, I'm looking for book suggestions"
bot_answer = await chat_function.invoke_async(context=context)
print(bot_answer)

ユーザーの入力をコンテキストに入れて、コンテキストごとSemantic関数に渡している。しかもawait。Semantic関数が非同期を扱えるようになっているのか

context["history"] += f"\nUser: {context['user_input']}\nChatBot: {bot_answer}\n"
print(context["history"])

historyつまり会話履歴を更新する

async def chat(input_text: str) -> None:
    # Save new message in the context variables
    print(f"User: {input_text}")
    context["user_input"] = input_text

    # Process the user message and get an answer
    answer = await chat_function.invoke_async(context=context)

    # Show the response
    print(f"ChatBot: {answer}")

    # Append the new interaction to the chat history
    context["history"] += f"\nUser: {input_text}\nChatBot: {answer}\n"

いよいよ今までのパーツを使った chat関数を定義

await chat("I love history and philosophy, I'd like to learn something new about Greece, any suggestion?")

質問一つ目

await chat("I love history and philosophy, I'd like to learn something new about Greece, any suggestion?")

回答

await chat("that sounds interesting, what is it about?")

次の質問

User: that sounds interesting, what is it about?
ChatBot: "The Greeks: A Portrait of Self and Others" explores the history and culture of ancient Greece. It delves into topics such as the development of democracy, the rise of Greek philosophy, the influence of Greek literature and drama, and the impact of Greek art and architecture. The book also examines the relationship between the Greeks and other ancient civilizations, such as the Persians and the Egyptians. Overall, it provides a comprehensive and engaging exploration of Greek civilization and its enduring legacy.

回答

await chat("if I read that book, what exactly will I learn about Greek history?")

続いての質問

User: if I read that book, what exactly will I learn about Greek history?
ChatBot: If you read "The Greeks: A Portrait of Self and Others," you will learn about various aspects of Greek history. Some of the key topics covered in the book include the development of democracy in ancient Athens, the rise of Greek philosophy and the contributions of philosophers like Socrates, Plato, and Aristotle, the influence of Greek literature and drama with works by writers like Homer, Sophocles, and Euripides, and the impact of Greek art and architecture with famous examples like the Parthenon. Additionally, the book explores the relationship between the Greeks and other ancient civilizations, shedding light on their interactions and cultural exchanges. Overall, reading this book will provide you with a comprehensive understanding of Greek history and its significance in shaping Western civilization.

回答

await chat("could you list some more books I could read about this topic?")

また次の質問

User: could you list some more books I could read about this topic?
ChatBot: Certainly! Here are a few more books you might find interesting:

1. "The Peloponnesian War" by Thucydides - This ancient Greek historian's account of the war between Athens and Sparta provides valuable insights into the politics and military strategies of the time.

2. "The Iliad" by Homer - This epic poem tells the story of the Trojan War and offers a glimpse into ancient Greek society and mythology.

3. "The Republic" by Plato - In this philosophical work, Plato explores the concept of justice and presents his ideal society, providing valuable insights into ancient Greek philosophy.

4. "The Histories" by Herodotus - Often considered the first work of history, this book by Herodotus covers a wide range of topics, including the Persian Wars and the cultures of various ancient civilizations.

5. "The Birth of the West: Rome, Germany, France, and the Creation of Europe in the Tenth Century" by Paul Collins - While not solely focused on Greece, this book provides a broader perspective on the development of Western civilization, including its Greek roots.

These books should provide you with a deeper understanding of Greek history and its impact on the world. Happy reading!

回答。いいかんじですな

print(context["history"])

最後にhistoryの中身を見てみる

User: Hi, I'm looking for book suggestions
ChatBot: Of course! What genre or type of books are you interested in?

User: I love history and philosophy, I'd like to learn something new about Greece, any suggestion?
ChatBot: I would recommend "The Greeks: A Portrait of Self and Others" by Paul Cartledge. It provides a comprehensive overview of Greek history and culture, exploring various aspects such as politics, philosophy, and literature. It's a great book to gain a deeper understanding of Greece and its impact on the world.

User: that sounds interesting, what is it about?
ChatBot: "The Greeks: A Portrait of Self and Others" explores the history and culture of ancient Greece. It delves into topics such as the development of democracy, the rise of Greek philosophy, the influence of Greek literature and drama, and the impact of Greek art and architecture. The book also examines the relationship between the Greeks and other ancient civilizations, such as the Persians and the Egyptians. Overall, it provides a comprehensive and engaging exploration of Greek civilization and its enduring legacy.

User: if I read that book, what exactly will I learn about Greek history?
ChatBot: If you read "The Greeks: A Portrait of Self and Others," you will learn about various aspects of Greek history. Some of the key topics covered in the book include the development of democracy in ancient Athens, the rise of Greek philosophy and the contributions of philosophers like Socrates, Plato, and Aristotle, the influence of Greek literature and drama with works by writers like Homer, Sophocles, and Euripides, and the impact of Greek art and architecture with famous examples like the Parthenon. Additionally, the book explores the relationship between the Greeks and other ancient civilizations, shedding light on their interactions and cultural exchanges. Overall, reading this book will provide you with a comprehensive understanding of Greek history and its significance in shaping Western civilization.

User: could you list some more books I could read about this topic?
ChatBot: Certainly! Here are a few more books you might find interesting:

1. "The Peloponnesian War" by Thucydides - This ancient Greek historian's account of the war between Athens and Sparta provides valuable insights into the politics and military strategies of the time.

2. "The Iliad" by Homer - This epic poem tells the story of the Trojan War and offers a glimpse into ancient Greek society and mythology.

3. "The Republic" by Plato - In this philosophical work, Plato explores the concept of justice and presents his ideal society, providing valuable insights into ancient Greek philosophy.

4. "The Histories" by Herodotus - Often considered the first work of history, this book by Herodotus covers a wide range of topics, including the Persian Wars and the cultures of various ancient civilizations.

5. "The Birth of the West: Rome, Germany, France, and the Creation of Europe in the Tenth Century" by Paul Collins - While not solely focused on Greece, this book provides a broader perspective on the development of Western civilization, including its Greek roots.

These books should provide you with a deeper understanding of Greek history and its impact on the world. Happy reading!

会話履歴がすべて入っている

さて01から04まで終わったが意外と長くなってしまったので、続きは別記事にしよう

ではまた!

Discussion