0
2

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 1 year has passed since last update.

ChatGPTの出力フォーマットを安定させる方法について

Posted at

先日ChatGPTを使ってYoutubeの台本を自動で生成して、動画を自動生成するアプリケーションを作成しました。
https://qiita.com/yunoko_gpt/items/d6fc200126627a673547

そのときに、ChatGPTによる出力のフォーマットが安定せず、様々な出力パターンに対応するための自然言語処理の実装を行う必要がありました。

しかし、少しプロンプトに工夫をすると、以前と比べてほとんどブレのない出力をしてくれるようになりました。

今回はその手法についてTips的にまとめました。

まずは今まで使用していたYoutubeの台本の構成をJson形式で出力してくれるプロンプトを紹介します。

I want a Youtube script to explain for the keywords. What kind of chapter titles should I use?
The output should be a markdown code snippet formatted in the following schema in Japanese:

\`\`\`json
[
  {
   chapter: string, // chapter number
   title: string // title of chapter
  },
  {
   chapter: string, // chapter number
   title: string // title of chapter
  }
]
\`\`\`

NOTES:
* Must not contain anything other than JSON.
* Response must be Japanese

スマホバッテリーの仕組みについて

このプロンプトのポイントは以下です。

  • 命令は英語で記述する。
  • Json以外の出力は出さない様に指定する。
  • 出力してほしいJsonのフォーマットをきちんと指定する。

このプロンプトを使用すると、結構な頻度で指定したルールを破ってきます。
例をあげると

  • Jsonのデータ以外の出力を出してくる
    • 「以下はJson出力です。」などの文章が先頭についてきたり、文末にJson形式出力の内容について説明する文章を追加してきたりする。
  • Jsonの最初の書き出し方がばらばらになる。
    • 以下のように様々な形式で出力してきます。
    ```json
    
    \`\`\`json
    
    \\`\\`\\`json
    
  • ときにはJson形式を無視してくる
    • 箇条書きで出力してくるときもありました。

これらのバラバラな出力を安定させるプロンプトは以下です。

I want a Youtube script to explain for the keywords. What kind of chapter titles should I use?
The output should be a markdown code snippet formatted in the following schema in Japanese:

\`\`\`json
[
  {
   chapter: string, // chapter number
   title: string // title of chapter
  },
  {
   chapter: string, // chapter number
   title: string // title of chapter
  }
]
\`\`\`

NOTES:
* Must not contain anything other than JSON.
* Response must be Japanese

スマホバッテリーの仕組みについて

\`\`\`json
[
  {
   chapter: 

追加したのは最後の一箇所だけです。

\`\`\`json
[
  {
   chapter: 

この部分は指定したフォーマットの書き出しの部分です。
この書き出しの一部を最後に示してあげることで、以下のような出力が安定して返ってくるようになりました。

1,
title: "スマホバッテリーの仕組みとは"
},
{
chapter: 2,
title: "リチウムイオン電池の仕組みとは"
},
{
chapter: 3,
title: "スマホバッテリーの寿命を延ばす方法"
},
{
chapter: 4,
title: "スマホバッテリーの充電に関する注意点"
}
]

出力された文字列と、書き出しの一部の文字列を結合させれば、きちんとしたJsonフォーマットでデータを安定して扱うことができます。

この様に出力の書き出しを少し書いてあげることで、その書き出しに沿った出力が予め設定されたフォーマットどおりに出力されるようになります。

この方法は他にも出力を安定させたいときに応用できると思い、記事にしました。

ChatGPTの出力をプログラムに組み込むときに助けになればと思います。

0
2
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?