LoginSignup
1
1

More than 1 year has passed since last update.

プロンプティングを用いたChatGPTによる小説情報要約 (1) 自動Wiki作成

Last updated at Posted at 2023-03-08

TL;DR

  • 小説本文から人物や地名などのエンティティに関する情報を抽出し、小説中の設定情報に関するWikiページを作成する実験を行った。
  • 今回利用したプロンプトなどはGitHubレポジトリで公開しています。

目的

多くの小説には人物や舞台、世界観など、その小説独自の世界に関する設定が多く存在します。そうした設定をまとめておくことは小説を読む際の助けになるだけでなく、AIのべりすとのキャラクターブック機能など小説執筆支援ツールに対して与える情報としても活用できる可能性があります。

……とはいえ、小説をしらみつぶしに読んで網羅的に設定をまとめる作業、退屈ですよね?非公式wikiが作られるほどメジャーな作品ならともかく、個人で執筆しているシリーズ物の作品となると頼れるのは自分だけ。投入できる工数にも限りがあります。

こんな時こそChatGPT!!ということで、ChatGPTを使って自動的に作品世界の設定情報をまとめる方法をいろいろ試行錯誤していきたいと思います。

初回(次回があるかはわかりませんが……)の今回は、小説中のエンティティ(人物、組織、地名など)に関する情報をwikiの記事のような形式でまとめる試みをしてみようと思います。

今回実装したコードはプロンプト含めてGitHubレポジトリで公開しておりますので、実際に動かしてみたい方はそちらもご参照ください。(書きなぐりでドキュメントなどはまだ整備していませんが……。)

入出力

今回は以下のような入出力を考えました。

  • 入力
    • 小説本文のみ
  • 出力
    • 小説中に出現するエンティティそれぞれに対するwikiページ
      • 各wikiページには対応するエンティティに関する設定情報がまとめられている。
      • 各wikiページは、必要に応じて階層的に章立てされている。

パイプライン構成

今回実装したパイプラインは、以下のように複数のモジュールの組み合わせで構成されています。各モジュールについては以降の節で説明していきます。

全体としては

  • 小説中からエンティティごとの設定情報を抽出する
  • 得られた設定情報をエンティティごとにwikiページ形式でまとめる

という2段階の処理を行っています。

無題のプレゼンテーション (1).png

モジュールの実装について

プロンプティング (prompting)

モジュールそれぞれはChatGPTに対して自然言語のタスク指示文(instruction)を与えて推論させるプロンプティングによって実現しており、実装には便利なLangChainを利用しました。
また性能上必要であれば入出力事例を数個作成してプロンプトに含めるin-context learningを利用しています。(プロンプトも公開できるよう、これらの事例で利用する小説文は自作しています。)

言語について

ChatGPTの英語以外の言語に対する能力については以下の論文などで様々な分析が行われています。この論文でも指摘されていますし、著者も個人的に同様の印象を抱いているのですが、ChatGPTは日本語などの英語以外の言語の文章についてはReadingは割とできるがWritingは相対的に苦手なようです。

そこで今回の試みでは小説本文は日本語のまま翻訳せずにそのままChatGPTに入力する一方で、抜き出したエンティティごとの新情報やエンティティwiki本文など、ChatGPTに出力させる文章は英語を基本としました。

モジュール説明

それぞれのモジュールについて、入出力例を交えながら説明していきます。

なお、以下では青空文庫に収録されている『フランダースの犬』(*)を題材にしていきたいと思います。

(*)ド・ラ・ラメー マリー・ルイーズ『フランダースの犬』(菊池寛訳)興文社/文芸春秋社, 1929年

エンティティ抽出モジュール

エンティティ抽出モジュールは、与えられた小説本文から人物名や地名などの固有名詞を抽出します。
今回は訓練事例を1つだけ作ってin-context learningを行いました。

例:

 ネルロとパトラッシュ――この二人はさびしい身の上同志でした。 ふたりともこの世に頼るものなく取り残されたひとりぼっち同志ですから、その仲のいいことは言うまでもありません。いや、「仲がいい」くらいな言葉では言いあらわせません。兄弟でもこれほど愛し合っている者はまずないでしょう。ほんとにこれ以上の親しさはかんがえられないほどの間柄でした。しかも、ふたり、と言っても人間同志ではないのです。ネルロは、フランスとベルギーの境を流れるムーズ河の畔の田舎町アンデルスに生れた少年。パトラッシュは、フランダース産の大きな犬なのです。このふたりは、年数としかずから言ったら、いわゆるおなじ年ですが、一方はまだあどけない子供ですのに、一方はすでに老犬の部類に入っています。ふたりが友達になったそもそものはじまりは、お互いに同情し合ったのがもとで、日を経ふるにしたがって、その気持はますます深まり、今ではもう切っても切れない親しさにむすびついてしまいました。 村はずれの小さな小舎こや、それがふたりの家でした。

例えば上の小説本文からは以下のエンティティが抽出されます。

ネルロ, パトラッシュ, ベルギー, ムーズ河, アンデルス, フランダース産

「フランダース産」が少し違和感があったり「フランス」が抽出できていなかったりと完璧ではありませんが、ひとまず動いてはいそうです。

エンティティ情報抽出モジュール

エンティティ情報抽出モジュールは、前段で抽出されていたエンティティそれぞれに対して与えられた小説本文中から設定情報を抜き出します。

前述したように今回はChatGPTの出力は英語を基本としますが、エンティティ名の日本語表記は英語では保持不可能な情報なので括弧内にそのまま保持するようにしています。

例:

例えば上のエンティティ抽出モジュールと同じ小説本文に対しては、例えば「ネルロ」というエンティティに対し以下のような情報が抽出されます。
「さびしい身の上同志」という情報が抜け落ちるなどしていますが、おおむね記載されている設定情報が正しく要約されていることがわかります。

エンティティ: ネルロ
設定情報: Nelro (ネルロ) is a boy who was born in the countryside town of Anders on the banks of the Meuse River between France and Belgium. He has a close relationship with his dog, Patrash (パトラッシュ), whom he considers a dear friend.
設定情報(和訳): フランスとベルギーの間にあるムーズ川沿いの田舎町アンデルスで生まれた少年ネルロ。愛犬パトラッシュを大切な友人と思い、親交がある。

望ましくない出力例

なおエンティティが出現してはいるものの特に設定情報が記載されていない場合は N/A と出力するように指示しているのですが、ChatGPTである gpt-3.5-turbo を利用した場合は以下のようにN/Aと出力するに至った理由のような余分な出力も出されてしまうケースもありました。

A milk cart is mentioned in the text, but not enough relevant information is provided to include in the entity's Wikipedia article. Output: N/A.

編集セクション選択モジュール

これ以降のモジュールでは、抽出されたエンティティと設定情報文を基にエンティティのwikiページを更新していく処理を行います。

ここで上で見ていたネルロの設定情報例を改めて見ると、出身地や他のキャラクターとの関係など複数の観点の設定情報が混ざっていることがわかります。

設定情報(和訳): フランスとベルギーの間にあるムーズ川沿いの田舎町アンデルスで生まれた少年ネルロ。愛犬パトラッシュを大切な友人と思い、親交がある

後の利用の際の利便性を考えると、こうした情報は実際に人間が書くwikiページのように、セクションごとに分けて記載しても良さそうです。

編集セクション選択モジュールはこのような情報のセクション分割を行うモジュールです。
なおwikiページに現在どのようなセクションが存在するかがわからないと分割しようがないため、以下のように分割後の候補となるセクションリストも入力として与えます。

入力例

エンティティ名: ネルロ
セクションリスト:

  1. Life
  2. Relationships
  3. Occupation

設定情報: Nelro (ネルロ) is a boy who was born in the countryside town of Anders on the banks of the Meuse River between France and Belgium. He has a close relationship with his dog, Patrash (パトラッシュ), whom he considers a dear friend.

この入力に対する出力は例えば以下のようになります。期待通り、生い立ちに関する情報と他のキャラクターとの関係に関する情報とが適切なセクションに分割されて出力されていることがわかります。

出力例

  1. Life
    Nelro (ネルロ) was born in the countryside town of Anders on the banks of the Meuse River between France and Belgium.

  2. Relationships
    Nelro has a close relationship with his dog, Patrash (パトラッシュ), whom he considers a dear friend.

なお、この例では示されていませんが、適切なセクションがリストに含まれない場合は新しくセクションを作成してタイトルを提案してくれる機能も含まれています。

またサブセクションなど、複数の階層にわたってセクション分けがなされているwikiページに対しては、この処理を再帰的に繰り返すことによって子セクションを持たない末端まで情報を分割していくことができます。

セクション更新モジュール

続いてセクション更新モジュールでは、各セクションごとに与えられた新情報とそのセクションの既存の文章が1つの文章にまとめられます。

例えば新情報が

Nelro has a close relationship with his dog, Patrash (パトラッシュ), whom he considers a dear friend.

既存のセクション本文が

Neruro (ネルロ) comforted Patrash (パトラッシュ), a sick dog, along with Jehan (ジェハン) the old man who took care of both of them. Nelro has a close relationship with Patrash.

だった場合、これらを一つにまとめて以下のような文章を作成してくれます。

Neruro (ネルロ) has a close relationship with his dog, Patrash (パトラッシュ), whom he considers a dear friend. He comforted Patrash when the dog fell ill, along with Jehan (ジェハン), the old man who took care of both of them.

この例では、意図的に新情報とセクション本文に重複して記載させていた "Nelro has a close relationship with Patrash." という情報が出力では1つにまとめられています。(ただし、常にこうして理想的な挙動をするわけではありません。)

セクション分割モジュール

以上の流れで新情報を抽出しセクション本文を更新していくと、次第に情報が増えてきてセクションが長くなってきてしまいます。
セクションが長すぎると単にChatGPTのトークン制限に引っ掛かりやすくなるなどのAPI利用面での不都合があるだけでなく、セクションを更新する際に情報の欠損が起こりやすくなるなど性能面での課題も出てきます。

そこで今回はある程度セクションが長くなったタイミングで、その内容を複数のサブセクションに分割する処理も実装しました。
機能的には編集セクション選択モジュールで新情報を分割する場合と同様です。しかし編集セクション選択モジュールのように子セクションのリストを与えるだけだと、以下のように親セクションと同じタイトルのサブセクションが生まれてしまうなど整合性に問題が出るケースがありました。

1 Life
1.2 Early Life
1.2.1 Life

そこでこのモジュールに対しては対象のセクション自身とその親セクションたちのタイトル情報も含めて入力することで、上で述べた問題を軽減しています。

入出力例自体は編集セクション選択モジュールと同様なので、こちらでは割愛します。

課題

以上で説明したシステムによって以下の付録で紹介するようにwikiページらしい文章を生成することはできましたが、実用していくには以下のようにまだまだ課題が複数あります。

  • wikiページ中の情報がそれぞれ小説中のどこから引用されたものなのかが追跡されていない。
  • 分割された小説本文のchunk内で完結する情報しか扱えていない。
    • 「私」のような一人称、代名詞、「その国」などcoreference resolutionを行わなければエンティティ名が特定できない場合に設定情報を取り出せない。
  • 現在は同じ名前で場面により異なるエンティティが参照されている場合に対応できない。 (entity linking)
  • 場面によって設定が変わる場合など、文脈が無ければ矛盾する設定が存在するケースを特に考慮していない。
  • wikiページの構造が大域的に最適であることが保証されていない。
    • セクションの内容的な分け方や順番など。

今後はこうした課題や性能面での改善、そして作成したwikiを利用した実応用を考えていきたいと思っています。

(付録)実際の作成例

以下に、「パトラッシュ」について実際に今回実装したシステムで作成されたwikiページを記載します。
大外れなことはあまり書かれていない一方で、余計な感想的な情報が含まれていたり、セクション構造が最適ではなかったりとまだまだ改善の余地がある状態です。

1. Relationships and Character Traits

1.1. Romantic Relationships

Recently, it has come to light that Patrashu, also known as Petrus, is in love and is widely known for his romantic relationships. The new information reveals that Patrashu goes by Patrash in Japanese and is deeply in love with a boy named Nerro, known as ネルロ in Japanese. Additionally, Nerro confided in Patrick that he believes Rubens, a painter, must have loved dogs to paint them so beautifully. Furthermore, it has been revealed that Patrashu also has a close relationship with Neruro. In a separate account, Patrick (パトラッシュ) is mentioned in passing in a larger story about the death of a boy and his dog in a cathedral in Antwerp during Christmas.

1.2. Characteristics and Abilities

Nerro owns a dog named Patrash (パトラッシュ). Despite his age, Patrash still loves to go on long walks and play fetch, and with his thick fur and size, he is well-suited for colder climates. Additionally, Patrash closely observes Nerro's artistic talents, showing an appreciation for the arts. Patrashu (パトラッシュ) is known for its distinct characteristics and abilities, which include being often cold and hungry, refusing to eat even when offered treats. This unique characteristic may require additional attention and care from his owner. Patrashu is an old dog with certain characteristics and abilities.

1.3. Background and Rescue

Patrashu (パトラッシュ) is a big and strong dog who used to be mistreated by his former owner before he was found collapsed in the dust and taken in by an elderly man named Jehan and a young child named Neruro. Patrashu has been by Neruro's side since childhood. Recently, Nerro and Patrash left their home, which they paid the last of their money for due to a recent death.

1.4. In Popular Culture

Patrashu (パトラッシュ) is a character in the story. He is not only a character in a novel but has also been featured in popular culture.

1.5. Friendship and Bonds

Patrick (パトラッシュ) formed a deep bond with Nelro (ネルロ) despite being from different species, and the two share a close friendship that grew over time. Patrash is known for his attentive nature and listens when the protagonist, Nelro, speaks about his future dreams. Interestingly, Patrash is held by a boy named Neruro before sleeping. Recently, Patrashu (パトラッシュ) has also developed a bond with Nerro, a boy who makes charcoal drawings in a dilapidated room. It is worth noting that their relationships are not specified to be romantic. Additionally, it has been revealed that Patorasshu has a strong bond with Neruro.

1.6. Work and Activities

Unfortunately, Patorasshu's work and activities are currently unknown, but it was previously stated that he was determined to pull his owner's milk wagon every morning.

1.7. Concern and Loyalty

Patrashu, commonly referred to as Patrash (パトラッシュ), is a loyal dog known for his unwavering loyalty. He remains deeply concerned and highly loyal towards Nerlo, whom he follows devotedly. Patrick's reaction to the closed door shows his concern and loyalty. Patorasshu seems to have a strong concern and loyalty towards Neruro. It has recently come to light that Patrashu is also concerned about Nerlo's secret. Additionally, Patrashu has an unyielding belief in not resting until his death.

1.8. Personal Habits and Quirks

New Section - [Insert heading here]
Please add the new information that needs to be included in this section.

Patrick, the subject of this section, is known for his quirk of becoming angry and refusing to leave when faced with a closed door. This behavior has been observed by those who know him well, and is something that sets him apart from others in his social circle. Despite this seemingly unusual aspect of his personality, Patrick is a respected and valued member of his community.

1.9. Miscellaneous

A new piece of information has been added to this section. According to the latest update, Patrashu, the protagonist of the story, is buried in a shared grave near the end of the story. This information sheds light on the final fate of the main character and provides important context to the events of the story.

2. Breed and Background

Patrick, also known as Patrash (パトラッシュ), is a dog belonging to a specific breed and having a certain background that plays a crucial role in the novel. However, the details of Patrashu's breed and background are still unknown. In the past, Patrashu was a slave, confirming his status in the story. Additionally, the novel introduces the character Petrus. It is worth noting that Patrick (パトラッシュ) spends time with Nello in their small hut, portraying a deep emotional bond between them. Furthermore, it is revealed that Nello, who Patrick spends time with, is a talented drawer who has never had any formal training in drawing and only uses black and white in his artwork.

3. Life and Struggles

3.1. Later Life

Patrashu (パトラッシュ) is an old dog who is experiencing an increasing pain in his body. However, he was able to recover from an earlier ailment and live the remainder of his life contentedly. Unfortunately, his later life was challenging, although the specifics of this period are unknown. Currently, he works for an elderly man with a green cart and a shiny brass can, delivering milk. Information about his role in Nerlo's life and the circumstances surrounding his friend's disappearance inside an ancient stone building in Antwerp remain unclear.

3.2. Support System

Despite enduring beatings and starvation without receiving any compensation for his services, Patrashu persevered and continues to go to work every morning. His life took a turn for the better when an old man and some children came to his rescue, helping him overcome his illnesses. It is unclear what kind of support Patrashu provides to Nerlo (ネルロ), and information about his support system is also unknown. Jehan and Neruro had provided support during his difficult times.

3.3. Early Life

Little is known about Patrashu's (パトラッシュ) early life or background. However, his later years were marked by hardship and suffering, having endured abuse, scars and neck injuries. In addition, Patrick and Nelro faced a financial struggle.

3.4. Struggles with Owner

Patrashu's life has been marked by various struggles. Unfortunately, the specific details of his challenges with his owner's beliefs are unknown. Additionally, new information has emerged suggesting that Patrashu may have also experienced some struggles with Nerlo; however, the nature of these struggles remains unclear. Despite these difficulties, Patrashu (パトラッシュ) remained resilient and eventually became a strong laborer, becoming the sole source of income for his owner after being abandoned in a ditch by a merchant who was transporting him.

3.5. Other

The subject of this Wikipedia page, also known as Patrashu, has recently been spotted pulling a brass can on a green milk-cart. This new information could be indicative of their occupation or mode of transportation. However, it has been revealed that the financial struggle is related to their inability to afford the entrance fee to see two famous paintings covered by cloth in a church. In addition, Patrashu also struggles with food and how it affects their daily life. Further details about this development are currently unknown. There is no additional information to add to the "Other" section.

3.6. Current Status

Patrasshu is a renowned author known for his award-winning work in the field of fiction. Recently, it has been reported that his house door is always closed now. However, information about Patorasshu's current status is unknown, which has sparked even more curiosity among his readers and fans, leading to further speculation about what could be going on behind the closed doors.

4. Miscellaneous Information

Recently, new information has come to light about this subject. According to reports, Patrick (パトラッシュ) found a sum of money and kindly brought it to the protagonist's house. This incident has shed some light on the possible motives behind Patrick's actions, and may help to better understand his character. Additionally, Patrick is the focus of efforts to make him feel more comfortable during Christmas festivities, indicating the community's appreciation for his actions and empathy towards his needs. Additional miscellaneous information can be added about Patrashu (パトラッシュ), but currently, it remains unknown.

5. Other Mentions

According to recent findings, it has been revealed that Neluro (ネルロ) is buried in the same shared grave as Patrashu. This new information sheds light on the possible connection between the two individuals and could provide a clearer understanding of their shared history. Further research may be necessary to fully uncover any implications or significance of this discovery.

1
1
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
1
1