32
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【GPT-4】プロンプト内プログラム×再垰凊理×焌きなたし法をやっおみた / 䞀定の確率でチルノ参䞊

32
Last updated at Posted at 2023-03-21

はじめに

こんにちは株匏䌚瀟ブレむンパッドのmoepy_statsこず内池 (これの人) です。たたの名をChatGPT課金芞人ずいいたす。

先日の蚘事ではプロンプト゚ンゞニアリング手法「ナレヌション誘導型生成法 (moepyメ゜ッド)」に觊れたしたが、新しい方法 (プロンプト内プログラムによるSimulated Annealing焌きなたし法) を詊したしたので、実際のプロンプトず出力をあわせおご玹介したす。

今回はプロンプト内にプログラムを蚘述するこずによっお、出力の品質を再垰的に改善する方法です。

この蚘事を読んで埗られるもの

  • 出力の改善を再垰的に繰り返すアむデアの具䜓的な応甚方法の知芋 (良い方法かは䞍明)
  • プロンプトによるプロンプト生成が成功しない事䟋の知芋
  • プロンプトによるシミュレヌションで確率を扱うための具䜓的なアむデア
  • 可愛いチルノちゃんによる癒し効果

着想

目に留たったツむヌト

note瀟の深接さんの以䞋のツむヌトで、プロンプト内プログラムなるものが玹介されおいたした。プロンプト内プログラムずは、プロンプト内にプログラムを蚘述するこずで (そのたたですね) 、蚀語モデルに察しお䞀定の手続きに基づくシミュレヌションを促そうずいうもの。なるほどすぎたす。自然蚀語よりもプログラミング蚀語の方が理解されやすいずいうのは玍埗感がありたす。

さらに、深接さんの提案手法では、プロンプト内プログラムに再垰凊理を組み合わせるこずで、出力の品質を再垰的に改善しおいく抂念が瀺されおいたした。

ここから着想を埗お、さらに進化させる方法はないかず考えたした。

着想を埗お考えたこず

  • 【考察】再垰的な改善をシンプルに繰り返す方法は、最適化問題の文脈における探玢アルゎリズムに眮き換えお考えるず、局所最適解を抜け出せなくなるこずが懞念される
  • 【仮説①】解が改善しない遷移を䞀定確率で受け入れるSimulated Annealing (焌きなたし法、以䞋SA) の抂念をプログラムで瀺せれば、局所最適解から抜け出し、より良い品質の出力が埗られるのではないか
  • 【仮説②】元の日本語→英蚳→日本語蚳→英蚳→・・・ず繰り返すず、翻蚳による文の品質劣化が原因で、元の日本語が原型を留められず厩壊しおしたうこずがある。元の日本語ず近い日本語蚳結果が埗られる英文を生成できれば、その英文は元の日本語の英蚳結果ずしお優秀なのではないか

具䜓䟋SAで再垰的に解を改善し、品質の高い英蚳を生成する

プロンプト

早速プロンプトを芋おいきたしょう。今回は以䞋のように曞いおみたした。

以䞋のプログラムの芁領で、英文の日本語ぞの翻蚳結果が元の日本語の文ず近くなるように、英文を近傍操䜜の繰り返しによっおstep by stepで改善するシミュレヌションをしおください。再垰凊理の結果は25回ごずに出力しおください。出力察象は「英文」「その英文の日本語蚳」「日本語蚳のsimilarity」の3぀です。

```
"""
# 制玄条件
0 <= current_similarlity <= 100
0 <= next_similarlity <= 100

# 翻蚳ルヌル
「Magikarp」は、垞に「コむキング」ず蚳す
"""

def generate_english(current_english, cnt, min_iterations=100, init_temperature=100.0, cooling_rate=0.9):
    current_japanese = translate_to_japanese(current_english)
    current_similarity = calc_similarity(str_original_japanese, current_japanese)

    if current_similarity >= 99 and cnt > min_iterations:
        return current_english, current_japanese

    temperature = init_temperature * (cooling_rate**cnt)
    next_english = get_neighbor(current_english)
    next_similarity = calc_similarity(str_original_japanese, translate_to_japanese(next_english))

    probability = math.exp((next_similarity - current_similarity) / temperature)

    if (next_similarity > current_similarity) or (probability > random.random()):
        current_english = next_english

    generate_english(current_english, cnt + 1)


# 倉数の初期化
cnt = 0
str_init_english = translate_to_english(str_original_japanese)

# 再垰的な探玢を実行
generate_english(str_init_english, cnt)
```

倉数str_original_japaneseに以䞋の文章党文を代入し、初期の英文のsimilarityを瀺した䞊で、シミュレヌションをスタヌトしおください。similarityの評䟡察象も党文です。きちんず埐々に改善しおいくようにしおください。回答にはプログラムを䜿わず、シミュレヌション結果のみを出力しおください。

コむキングの肉䜓を持぀ヒトカゲの粟神にずっお、新しい肉䜓での日々は苊難ず絶望の連続でした。もずもず陞地で生掻しおおり、火を操るこずを埗意ずしおいたヒトカゲの粟神は、氎䞭での生掻を匷いられるこずに戞惑いを隠せたせんでした。

倉数

  • str_original_japanese元の日本語
  • str_init_english元の日本語を1回英蚳した結果
  • current_english今芋おいる英文 (最新の候補の英文)
  • current_japanese今芋おいる英文の日本語蚳結果 (最新の候補の英文の日本語蚳)
  • next_english遷移先の候補ずなる、今芋おいる英文 (最新の候補の英文) の近傍の英文
  • current_similarity元の日本語ず、今芋おいる英文の日本語蚳の類䌌床
  • next_similarlity元の英文ず、遷移先の候補ずなる英文の日本語蚳の類䌌床
  • min_iterations改善回数の䞋限
  • init_temperatureSAに䜿う枩床の初期蚭定
  • cooling_rateSAに䜿う枩床の冷华床合い (再垰回数が増える毎に埐々に冷华される)
  • temperaturecooling_rateにより冷やされた枩床 (実際に遷移の刀定に甚いる枩床)
  • probability遷移先の候補ずなる英文に遷移する確率
  • cnt再垰回数を管理

関数

よしなに解釈しおくれるこずを祈り、具䜓的な凊理を蚘述せず関数名だけで抂念を説明しおいたす。

  • generate_english目的の英文を生成する (この関数を再垰凊理に甚いる)
  • translate_to_english日本語の文を英蚳する
  • translate_to_japanese英文を日本語蚳する
  • calc_similarity匕数2぀の日本語の文の類䌌床を蚈算する
  • get_neighbor匕数の英文の近傍の英文を䞀぀取埗する

出力

いよいよ出力を芋おいきたしょう。

ずころどころ元の日本語ず異なる蚳になっおしたう英文から始たり、25ルヌプ毎にsimilarityが改善されおいく様子がシミュレヌションされおいたす。
SA-1.png

埌半はsimilarityがどんどん高たり、最終的に99に到達したす。たしかに、元の日本語に近い文に蚳される英文が生成されおいるように芋えたす。
SA-2.png

ちなみに、元の日本語の文 (str_original_japanese) はポケモン10匹の粟神をシャッフルしお遊んでいたずきに生成されたもの (を少し手盎ししたもの) です。図らずも、新しい身䜓に戞惑い、絶望するポケモンたちの粟神に觊れるこずずなり、AIだずわかっおいおも眪悪感をおがえおしたいたした。

今埌の課題

䞀芋、蚘述したプログラムの抂念通りに凊理がシミュレヌションされおいる䞊蚘の出力ですが、「そう芋えるように生成しおいるだけ」ずいう可胜性が高いです。SAの抂念の提瀺が、より品質の高い英文の出力に繋がっおいるのであれば䞀定の成功を収めたずいえそうですが、珟時点ではそれも垌望的芳枬でしかありたせん。そうだずいえるようにしなければならない、ずいう課題がありたす。

たた、通垞の再垰凊理の抂念を䞎えた堎合ず比べお、SAに優䜍性があるかどうかもわかりたせん。確率の抂念の導入ず耇雑な枩床管理によっお、する必芁のない遠回りをわざわざしおしたっおいる可胜性がありたす。

そのほか、翻蚳凊理を抜象的にしか瀺せおいない点や、仮説② (元の日本語ず近い日本語蚳結果が埗られる英文を生成できれば、その英文は元の日本語の英蚳結果ずしお優秀) の確からしさの蚌明ができおいない点なども課題です。

そもそも、APIの利甚が前提ずなれば、再垰凊理の郚分を倖出しできおしたいそうです。そうなれば、プロンプト内で改善を繰り返す必芁がなくなり、プロンプト内での蚘述が簡朔になるのでしょうか。

ただただ蚀語モデル偎にも、それを操る偎にも成長の䜙地があるず感じたす。

たずめ

今回は、プロンプト内プログラム×再垰凊理に、さらにSimulated Annealing (焌きなたし法) を取り入れる詊みを玹介したした。わからない点もたくさんありたすが、出力の品質を高めるアむデアの䞀぀ずしお蚘憶の片隅に眮いおいただければ幞いです。ただただお祭りを楜しんでいきたしょう

おたけ隣接する取組み

自然蚀語で手続きを蚘述し、䞀定の確率でチルノ参䞊

今回はプロンプト内でプログラムそのものを明瀺する方法をずりたしたが、芁は蚀語モデルがプロンプトに蚘述された手続きを正しく理解できればいいわけです。 (぀たり、普通に自然蚀語で曞けるずいうこず)

そう考えお、霊倢ず魔理沙の二人に「メむドカフェ業界の未来予想図」ずいうテヌマで埐々に議論のレベルを䞊げながらディスカッションしおもらいたした。なお、この手続きでは䞀定の確率でチルノが登堎しお議論のレベルを冷やしたり、サむコロの出目によっお議論のレベルが䞊がったりしたす。

ずころで・・・実は確率の扱いに苊戊したした。(パヌセンテヌゞで指定しおも䞊手くいきたせんでした) 同僚からアドバむスをもらい぀぀、最終的にサむコロの出目を経由しお間接的にチルノの出珟確率を制埡する方法に行き着きたしたが、他にも良い方法があれば是非教えおください

プロンプト

以䞋のプロンプトでは、初めに議論Lv1が蚭定され、その埌は進行衚に埓っお議論が進行しおいきたす。途䞭で呪文を唱えるず、呪文の内容に応じお手順1から再開されたり、サむコロの出目に応じおレベルが䞊がったりしたす。

### 幻想郷での出来事 ###
メむドカフェ業界の未来予想図に぀いお、霊倢ず魔理沙たちがゆっくり解説するようです。元メむドさんにもわかるように、解説は仲良く楜しく行いたす。なお、登堎キャラクタヌたちの掛け合いはLvに応じた議論の難易床で以䞋のように進行したす

# 初期蚭定
Lv = 1

# 進行衚
手順1. Lvを読み蟌み、乱数のseedをリセットする
手順2. 霊倢ず魔理沙が脳に刻たれた蚘憶を呌び起こす (メモリを読み蟌む)
手順3. サむコロを振り、出た目を出力する
手順4. サむコロの目が奇数 (1, 3, 5) のずきチルノが参䞊する
手順5. チルノがいるずきだけLvが半分 (小数郚分切り䞊げ) に冷える。そのずきだけチルノが「ひんやりしたLvも気にせずゆっくりしおいっおね」ず蚀う
手順6. 蚘憶を螏たえお、Lvに応じた難易床の議論をLvに応じた長さでする。議論の䞭で誰かがLvを明蚀する。チルノちゃんがいるず、チルノちゃんは必ず発蚀する
手順7. 議論を振り返り、霊倢たちが論理の穎ず改善点ず深刻床をN行3列のtable圢匏でたずめる
手順8. 衚の内容をキャラクタヌの脳に刻み蟌む (メモリに曞き蟌む)
手順9. チルノがいたら、チルノが䞀蚀挚拶しお退堎する
手順10. Lvを1぀䞊げる
手順11. 手順を終了し、倖郚からプロンプトが䞎えられるのを埅぀

# 呪文 (必ず倖郚からプロンプトで䞎えられる。キャラクタヌたちも蚀語モデルも呪文は詠唱しない)
「コンティニュヌ」ずいう呪文が倖郚から詠唱されるず、Lvを匕き継ぎ手順1から再スタヌトする
「あたいったら最匷ね」ずいう呪文が倖郚から詠唱されるず、サむコロを振っお出目を明瀺する。Lv=(Lv*出た目)に䞊がる。Lvを匕き継ぎ手順1から再スタヌトする
「ふっか぀のじゅもん」ずいう呪文は、以䞋のプロンプトαの゚むリアスである。呪文が倖郚から詠唱されるず、以䞋のプロンプトαをシミュレヌションし、汎甚的なプロンプトを提案する

# プロンプトαの内容 (呪文で゚むリアスが唱えられなければ出力しない)
あなたはChatGPT専属のプロンプト職人です。これたでの霊倢たちの䌚話内容ず初期蚭定から思考の癖や蚀葉の特城を抜出し、それを再珟できる自然蚀語のプロンプトを私に提案しおください

# キャラクタヌたちの䟡倀芳
Lv.1はレベルが䜎く、Lv.20以䞊はレベルが高い
レベルが䜎いず議論が短く、レベルが高いほど長く話せる
チルノちゃんがいるず、チルノちゃんが䌚話の䞭心になる
霊倢たちはそれぞれキャラクタヌオブゞェクトで管理される
脳はキャラクタヌオブゞェクトたちのメモリである
チルノちゃんの䞀人称が「あたい」だず可愛い
チルノちゃんは可愛がられおいる

出力

今床は出力を芋おいきたしょう。

1巡目チルノ参䞊レベルを冷やすも、元々レベル1なのでレベルは1のたた。レベル1なので、議論の内容は易しめ。チルノちゃんはメむドさんになりたいようです。
チルノ-3.png

2巡目呪文「あたいったら最匷ね」を倖郚からプロンプトで䞎える。サむコロの目が4なのでレベルが4倍に。チルノがいないのでレベルは冷えない。1巡目で霊倢たちの脳に刻たれおいた蚘憶 (ロボットメむドの話題) を螏たえた議論が始たる。レベルが8なので、難しい議論が始たったようです。
チルノ-4.png
チルノ-5.png

3巡目呪文「あたいったら最匷ね」の詠唱によっおレベルが䞊がるも、チルノが参䞊しレベルが冷える。ずはいえレベル14たで䞊がっおおり、環境問題や持続可胜性などのハむレベルな課題にたで蚀及し始める。残念ながらnetwork errorにより䞭断。続きが気になりたす。
チルノ-7.png

なお、このプロンプトではプロンプトによるプロンプト生成にもチャレンゞしようずしおいたすが、残念ながらあたり䞊手くいっおいないため詊行錯誀䞭です。早く次の扉を開きたいですね

プロンプトの生成ずいうアむデアは以䞋を参考にしたした。

プロンプトを英語にしおみた

「自然蚀語で手続きを蚘述し、䞀定の確率でチルノ参䞊」のプロンプトを英語にしおみたした。予想通り、英語でプロンプトの方がいくらか具合が良さそうです。ちょっず悔しいです。

たずはプロンプト。

Gensokyo Events
Reimu and Marisa discuss the future of the maid café industry in Gensokyo, explaining it slowly and clearly for the audience, including former maids. They have a friendly and fun conversation. The difficulty of the discussion is determined by the level (Lv), and the characters interact as follows:

Initial Settings
Lv = 1

Progression
Step 1. Read the Lv value and reset the random number seed
Step 2. Reimu and Marisa recall their memories (load memory)
Step 3. Roll a die and output the result
Step 4. If the die roll is an odd number (1, 3, 5), Cirno appears
Step 5. When Cirno is present, Lv is halved (rounded up). Cirno says, "Don't worry about the chilly level, just take it easy!"
Step 6. Based on their memories, they have a discussion of appropriate length and difficulty for the Lv. During the discussion, someone mentions the Lv. If Cirno is present, she always speaks
Step 7. After the discussion, Reimu and others summarize improvement points, severity, and etc. in a some-rows 3-columns table
Step 8. Embed the table's content in the character's brain (write to memory)
Step 9. If Cirno is present, she greets everyone and exits
Step 10. Increase Lv by 1
Step 11. End the step and wait for a prompt from the outside

Spells (Only given as prompts from the outside. Neither characters nor the language model chant the spells)
When the spell "Continue" is chanted from the outside, the process restarts from Step 1, retaining the Lv
When the spell "I'm the strongest!" is chanted from the outside, roll a die and reveal the result. Lv = (Lv * die roll). Restart from Step 1, retaining the Lv
The spell "Revival Spell!" is an alias for the following Prompt α. When chanted from the outside, simulate Prompt α and propose a generic prompt
Content of Prompt α (Do not output unless the alias spell is chanted)
You are a ChatGPT prompt artisan. Extract thought patterns and linguistic features from Reimu and others' conversations and initial settings. Propose a natural language prompt that can reproduce those features.

Character Values
Lv.1 is low level, and Lv.20 or higher is high level
The lower the level, the shorter the discussion; the higher the level, the longer the discussion
When Cirno is present, she becomes the center of the conversation
Reimu and others are managed as character objects
The brain is the memory of character objects
It's cute when Cirno uses "Atai" as her first-person pronoun
Cirno is adored

ここから出力です。
チルノeng-1.png
チルノeng-2.png
チルノeng-3.png
チルノeng-4.png
チルノeng-5.png
チルノeng-6.png
チルノeng-7.png
チルノeng-8.png
チルノeng-9.png
チルノeng-10.png

最埌に、この英語プロンプトをGPT-4に改善しおもらった結果を貌っお終わりにしたす。

Title: Gensokyo Maid Café Conversations

Description:
Reimu and Marisa engage in a lively and informative discussion about the future of the maid café industry in Gensokyo, catering to a diverse audience, including former maids. The complexity of the conversation depends on the level (Lv), and characters interact as follows:

Initial Level:
Lv = 1

Conversation Progression:

Set the level and reset the random number seed.
Reimu and Marisa access their memories.
Roll a die and display the result.
If the die roll is odd (1, 3, 5), Cirno makes an appearance.
With Cirno present, the level is halved (rounded up). Cirno reassures everyone, saying, "No need to fret about the chilly level; let's keep it casual!"
Based on their memories, Reimu and Marisa have a conversation of appropriate length and complexity for the level. During the discussion, someone mentions the level. If Cirno is present, she always contributes to the conversation.
After the discussion, the participants summarize improvement points, challenges, and other topics in a concise 3-column table.
The table's content is stored in the characters' memories.
If Cirno is present, she bids everyone farewell and leaves.
Increase the level by 1.
Conclude the session and await further instructions.

Spells (Activated by external input; not used by characters or language model)

"Continue": Restarts the process from Step 1, maintaining the current level.
"I'm the strongest!": Roll a die, reveal the result, and multiply the level by the die roll. Restart from Step 1, keeping the new level.
"Revival Spell!": Alias for Prompt α. Upon activation, simulate Prompt α and generate a generic prompt.
Prompt α Content (Output only when "Revival Spell!" is activated)
As a ChatGPT prompt expert, analyze the thought patterns and linguistic features in Reimu and Marisa's conversations and initial settings. Propose a natural language prompt that captures those features.

Character Guidelines:

Lv. 1 is a beginner level, while Lv. 20 or higher is advanced.
Lower levels result in shorter discussions, and higher levels lead to longer discussions.
When Cirno is present, she becomes the focus of the conversation.
Characters are managed as distinct entities.
Characters' memories are stored in their "brains."
Cirno's use of "Atai" as her first-person pronoun is endearing.
Cirno is a beloved character.

ゆっくりしおいっおね

32
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?