プロンプトの反復的な改善

OpenAI API で学ぶプロンプトエンジニアリング

Fouad Trad

Machine Learning Engineer

プロンプトの反復的な改善

  • 最初から完璧なプロンプトはない
  • プロンプトエンジニアリングは反復的なプロセス:
    • プロンプトを作成する
    • モデルに入力する
    • 出力を観察・分析する
    • プロンプトを改善して繰り返す

4つの矢印が循環するサイクルを表す青い円の画像。

OpenAI API で学ぶプロンプトエンジニアリング

プロンプトの改善

初期プロンプト
prompt = "Generate an Excel sheet containing
five student names and their grades"

print(get_response(prompt))
I'm sorry, but as a text-based AI, I am 
unable to directly provide an Excel sheet. 
However, I can help you generate a sample
representation of the data you requested.
改善後のプロンプト
prompt = "Generate a table that I can copy 
to Excel, containing five student names and 
their grades"
print(get_response(prompt))
| Student Name | Grade |
|--------------|-------|
| Student 1    |  90   |
| Student 2    |  85   |
| Student 3    |  95   |
| Student 4    |  88   |
| Student 5    |  92   |
OpenAI API で学ぶプロンプトエンジニアリング

例:Python関数の分析

code = '''
def calculate_rectangle_area(length, width):
    area = length * width
    return area
'''

長方形と、幅と長さから面積を計算する公式(面積 = 長さ×幅)を示す画像。

OpenAI API で学ぶプロンプトエンジニアリング

例:初期プロンプト

prompt = f"""
  Analyze the code delimited by triple backticks with one sentence
  ```{code}```.
"""
print(get_response(prompt))
The code calculates the area of a rectangle based on its length and width.
OpenAI API で学ぶプロンプトエンジニアリング

例:プロンプトの改善

プログラミング言語を取得するようプロンプトを修正

prompt = f"""
  Analyze the code delimited by triple backticks and provide its programming 
  language with one sentence 
  ```{code}```.
"""
print(get_response(prompt))
The provided code is a function written in Python that calculates the area of a 
rectangle based on its length and width.
OpenAI API で学ぶプロンプトエンジニアリング

例:プロンプトの改善

構造化された出力を得るようプロンプトを修正

prompt = f"""
  For the function delimited by triple backticks, provide in a structured format
  the following:
  - description: one sentence short description
  - language: the programming language used
  - input: the inputs to the function
  - output: the output returned by the function
  ```{code}```.
"""
print(get_response(prompt))
OpenAI API で学ぶプロンプトエンジニアリング

例:プロンプトの改善

description: This function calculates the area of a rectangle.

language: Python

input:
 - length: The length of the rectangle.
 - width: The width of the rectangle.

output:
 - area: The calculated area of the rectangle, which is the product of the length 
 and width.
OpenAI API で学ぶプロンプトエンジニアリング

Few-shotプロンプトの改善

  • 天気の説明の分類

初期プロンプト

prompt = """
Clear skies and a gentle breeze. -> Sunny
Heavy rain and thunderstorms expected. -> Rainy
Fresh snowfall with freezing temperatures. -> 
"""
print(get_response(prompt))
Snowy
OpenAI API で学ぶプロンプトエンジニアリング

Few-shotプロンプトの改善

  • 天気の説明の分類

初期プロンプト

prompt = """
Clear skies and a gentle breeze. -> Sunny
Heavy rain and thunderstorms expected. -> Rainy
The wind of change brought a refreshing breeze to the company's operations. -> 
"""
print(get_response(prompt))
Windy
OpenAI API で学ぶプロンプトエンジニアリング

Few-shotプロンプトの改善

改善後のプロンプト

prompt = """
Clear skies and a gentle breeze. -> Sunny
Heavy rain and thunderstorms expected. -> Rainy
The political climate in the country was stormy -> Unknown
The wind of change brought a refreshing breeze to the company's operations. -> 
"""
print(get_response(prompt))
Unknown
OpenAI API で学ぶプロンプトエンジニアリング

各プロンプトタイプにおける改善

  • Few-shotプロンプト:例を改善する
  • マルチステッププロンプト:ガイドのステップを改善する
  • 思考の連鎖・自己一貫性プロンプト:問題の説明を改善する

3行のトグルスイッチを手で調整し、改善を表している画像。

OpenAI API で学ぶプロンプトエンジニアリング

練習しましょう!

OpenAI API で学ぶプロンプトエンジニアリング

Preparing Video For Download...