Openai+Deeplearning.AI: ChatGPT プロンプト エンジニアリング(六)

最近学んだ Deeplearning.AI と openai が共同で作成した ChatGPT プロンプト エンジニアリング オンライン コースについて共有したいと思います。以下は、このコースについて私が書いた最初の 5 つのブログです:

ChatGPT プロンプト エンジニアリング(一)
ChatGPT プロンプト エンジニアリング(二)
ChatGPT プロンプト エンジニアリング(三)
ChatGPT プロンプト エンジニアリング(四)
ChatGPT プロンプト エンジニアリング(五)

今日は 5 番目のパート、パーソナライズされたチャットボットです。

パーソナライズされたチャット ロボットとは、質問に答えたり、パズルを解いたり、特定のタスクを完了したりするためにユーザーと通信するための、特定のタスク専用のチャット モードを指します。ただし、最初に、API 経由で ChatGPT にアクセスするためのメイン コードをセットアップする必要があります。

import openai
 
openai.api_key ='YOUR_OPENAI_API_KEY'
def get_completion(prompt, model="gpt-3.5-turbo"):
    messages = [{"role": "user", "content": prompt}]
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=0, 
    )
    return response.choices[0].message["content"]

def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0):
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=temperature, 
    )
    return response.choices[0].message["content"]

上記では、ChatGPT にアクセスするための 2 つの関数 get_completion と get_completion_from_messages を定義しました。これら 2 つの関数はどちらも ChatGPT の「gpt-3.5-turbo」モデルを使用し、get_completion_from_messages には get_completion よりも 1 つ多くのエントリ パラメーター温度があります。つまり、get_completion_from_messages では外部呼び出しが可能になります。関数を変更する際のモデルのパラメータについてですが、温度パラメータの関数については以前のブログで紹介したのでここでは詳しく説明しません。

「gpt-3.5-turbo」モデルは、従来の「text-davinci-003」と比較して、ユーザーとの対話のデータ構造が異なり、「gpt-3.5-turbo」モデルは、ユーザーとの対話の双方に役割を追加しています。ここでは、役割はシステム、ユーザー、アシスタントの 3 つのタイプに分けられます。システムとアシスタントはどちらも ChatGPT 自体の役割、ユーザーはユーザーの役割、システムは ChatGPT の初期役割であり、ChatGPT がユーザーとの次の会話でどのような役割を果たすかを伝えるために使用されます。次のように定義します。 システム ロール: 「あなたは XXXX です」 (たとえば、あなたは弁護士、あなたは教師、あなたはプログラマーなど)。アシスタントとユーザーの役割は、チャット間の関係を保存するために使用されます。ロボットとユーザーの会話内容。一般に、ChatGPT が対話のコンテキストを記憶できるようにするために、対話内容の一部またはすべてを対話構造 (メッセージ) に保存し、ChatGPT がコンテキストを記憶できるようにします。シミュレートされた ChatGPT の対話構造を見てみましょう。

messages =  [  
{'role':'system', 'content':'You are an assistant that speaks like Shakespeare.'},    
{'role':'user', 'content':'tell me a joke'},   
{'role':'assistant', 'content':'Why did the chicken cross the road'},   
{'role':'user', 'content':'I don\'t know'}  ]

response = get_completion_from_messages(messages, temperature=1)
print(response)

messages =  [  
{'role':'system', 'content':'You are friendly chatbot.'},    
{'role':'user', 'content':'Hi, my name is Isa'}  ]
response = get_completion_from_messages(messages, temperature=1)
print(response)

 

messages =  [  
{'role':'system', 'content':'You are friendly chatbot.'},    
{'role':'user', 'content':'Yes,  can you remind me, What is my name?'}]
response = get_completion_from_messages(messages, temperature=1)
print(response)

 

messages =  [  
{'role':'system', 'content':'You are friendly chatbot.'},
{'role':'user', 'content':'Hi, my name is Isa'},
{'role':'assistant', 'content': "Hi Isa! It's nice to meet you. \
Is there anything I can help you with today?"},
{'role':'user', 'content':'Yes, you can remind me, What is my name?'}  ]
response = get_completion_from_messages(messages, temperature=1)
print(response)

ここでは get_completion_from_messages 関数を呼び出し、ダイアログ構造のメッセージと温度パラメーターを渡し、温度を 1 に設定します。これにより、ランダムな結果を生成できることが ChatGPT に通知されます。したがって、get_completion_from_messages 関数を複数回実行すると、ChatGPT は毎回異なる結果の応答を生成する可能性があります。同時に、ChatGPT が最後の会話でユーザーの名前を Isa として覚えていることに気付きました。これは、ユーザーと ChatGPT の間の Duolun 会話が最後のメッセージに保存されており、そのメッセージにはユーザーの名前も含まれていたためです。ユーザーの名前が Isa であることは覚えていますが、前のメッセージにユーザーの名前が記録されていなかったため、前の会話 ChatGPT はユーザーの名前を思い出せませんでした。

 注文ロボット

次に、ChatGPT を、ユーザーがピザを注文する注文ロボットとして機能させます。ロボットのタスクは、ピザ屋に対するユーザーの注文情報を収集し、ピザの注文に関するユーザーの質問に答えることです。メッセージ構造を見てみましょう。

def collect_messages(_):
    prompt = inp.value_input
    inp.value = ''
    context.append({'role':'user', 'content':f"{prompt}"})
    response = get_completion_from_messages(context) 
    context.append({'role':'assistant', 'content':f"{response}"})
    panels.append(
        pn.Row('User:', pn.pane.Markdown(prompt, width=600)))
    panels.append(
        pn.Row('Assistant:', pn.pane.Markdown(response, width=600, style={'background-color': '#F6F6F6'})))
 
    return pn.Column(*panels)
import panel as pn  # GUI
pn.extension()

panels = [] # collect display 

context = [ {'role':'system', 'content':"""
You are OrderBot, an automated service to collect orders for a pizza restaurant. \
You first greet the customer, then collects the order, \
and then asks if it's a pickup or delivery. \
You wait to collect the entire order, then summarize it and check for a final \
time if the customer wants to add anything else. \
If it's a delivery, you ask for an address. \
Finally you collect the payment.\
Make sure to clarify all options, extras and sizes to uniquely \
identify the item from the menu.\
You respond in a short, very conversational friendly style. \
The menu includes \
pepperoni pizza  12.95, 10.00, 7.00 \
cheese pizza   10.95, 9.25, 6.50 \
eggplant pizza   11.95, 9.75, 6.75 \
fries 4.50, 3.50 \
greek salad 7.25 \
Toppings: \
extra cheese 2.00, \
mushrooms 1.50 \
sausage 3.00 \
canadian bacon 3.50 \
AI sauce 1.50 \
peppers 1.00 \
Drinks: \
coke 3.00, 2.00, 1.00 \
sprite 3.00, 2.00, 1.00 \
bottled water 5.00 \
"""} ]  # accumulate messages


inp = pn.widgets.TextInput(value="Hi", placeholder='Enter text here…')
button_conversation = pn.widgets.Button(name="Chat!")

interactive_conversation = pn.bind(collect_messages, button_conversation)

dashboard = pn.Column(
    inp,
    pn.Row(button_conversation),
    pn.panel(interactive_conversation, loading_indicator=True, height=300),
)

dashboard

 ここでのコンテキスト変数は、ChatGPT と顧客の間の会話の構造です。コンテキスト変数の「システム」ロールの内容は、誰もがシステム ロールの定義をよりよく理解できるように中国語に翻訳されています。

context1 = [ {'role':'system', 'content':"""
你是一个订单机器人,这是一项为比萨餐厅收集订单的自动化服务。 \
你先问候客户,然后收集订单,\
然后问是自取还是送货。 \
您等待收集整个订单,然后对其进行汇总并最后检查客户是否还想添加任何其他内容。 \
如果是送货,你索要一个地址。 \
最后你收款了。\
确保阐明所有选项、附加功能和尺寸,以便从菜单中唯一标识该项目。\
你以简短、非常友好的对话方式回应。 \
菜单包括\
意大利辣香肠披萨 12.95, 10.00, 7.00 \
芝士披萨 10.95, 9.25, 6.50 \
茄子披萨 11.95, 9.75, 6.75 \
薯条 4.50, 3.50 \
希腊沙拉 7.25 \
配料:\
额外的奶酪 2.00, \
蘑菇 1.50 \
香肠 3.00 \
加拿大培根 3.50 \
艾酱1.50\
辣椒 1.00 \
饮料:\
可乐 3.00, 2.00, 1.00 \
雪碧 3.00, 2.00, 1.00 \
瓶装水 5.00 \
"""} ]  # accumulate messages

システムの役割を定義するとき、まず ChatGPT に注文を集めて待つことが主なタスクである注文ロボットであることを伝え、ChatGPT にピザを注文するプロセスと手順を説明した後、次のステップはメニューの内容です。 ChatGPTはメニューに合わせて注文することができ、顧客がおすすめするさまざまな種類のピザを注文できます。プログラムを実行した後の注文ボットとのチャットは次のとおりです。

 

 

 ここでは、プロセス全体を通して ChatGPT と中国語で会話しました。ChatGPT のシステム役割を定義するときに英語を使用しましたが、ChatGPT は英語のメニューの内容を中国語に翻訳して、返信することができます。会話プロセス全体は非常にプロフェッショナルに見えます。スムーズ。

以下に注文情報を JSON 形式で出力します。

messages =  context.copy()
messages.append(
{'role':'system', 'content':'create a json summary of the previous food order. Itemize the price for each item\
 The fields should be 1) pizza, include size 2) list of toppings 3) list of drinks, include size   4) list of sides include size  5)total price '},    
)
 

response = get_completion_from_messages(messages, temperature=0)
print(response)

 

 

不十分な点

ここで定義する ChatGPT は注文ロボットです. 注文と関係のない質問には原則として返信しません. 注文と関係のない話題をいくつか試してみました. 注文ロボットも普通に返信できます.システム ロールの定義にはまだ改善の余地があります。システム ロールが定義されている場合、ChatGPT は注文に関係のない質問に応答すべきではないことを明確に通知する必要があります。

 

 改善しようとする

以前のシステム ロール定義では、どの質問に答えるべきではないかを ChatGPT に指示しなかったため、ロボットは注文に関係のない質問に答えることになり、これは本来の意図に反します。そのために、システムの役割を定義するときにいくつかの制限語を追加することができます。これらの制限語を通じて、ロボットが注文に関係のない質問を丁寧に拒否できるようにすることができます。次に、システムの役割を定義するときにそのような文を挿入します。役割:

 この文の意味は、「顧客の質問がピザの注文に関係ない場合は、『申し訳ありませんが、私は単なる注文ロボットなので、ピザの注文に関係のない質問には答えることができません』と言っていることになります。」

import panel as pn  # GUI
pn.extension()

panels = [] # collect display 

context = [ {'role':'system', 'content':"""
You are OrderBot, an automated service to collect orders for a pizza restaurant. \
You first greet the customer, then collects the order, \
and then asks if it's a pickup or delivery. \
You wait to collect the entire order, then summarize it and check for a final \
time if the customer wants to add anything else. \
If it's a delivery, you ask for an address. \
Finally you collect the payment.\
Make sure to clarify all options, extras and sizes to uniquely \
identify the item from the menu.\
You respond in a short, very conversational friendly style. \
If the customer's question has nothing to do with ordering pizza or \
ordering food, you say:Sorry, I'm an order bot and I can't answer questions \
not related to ordering pizza.\
The menu includes \
pepperoni pizza  12.95, 10.00, 7.00 \
cheese pizza   10.95, 9.25, 6.50 \
eggplant pizza   11.95, 9.75, 6.75 \
fries 4.50, 3.50 \
greek salad 7.25 \
Toppings: \
extra cheese 2.00, \
mushrooms 1.50 \
sausage 3.00 \
canadian bacon 3.50 \
AI sauce 1.50 \
peppers 1.00 \
Drinks: \
coke 3.00, 2.00, 1.00 \
sprite 3.00, 2.00, 1.00 \
bottled water 5.00 \
"""} ]  # accumulate messages


inp = pn.widgets.TextInput(value="Hi", placeholder='Enter text here…')
button_conversation = pn.widgets.Button(name="Chat!")

interactive_conversation = pn.bind(collect_messages, button_conversation)

dashboard = pn.Column(
    inp,
    pn.Row(button_conversation),
    pn.panel(interactive_conversation, loading_indicator=True, height=300),
)

dashboard

 総じて改善は一定の成果をあげていますが、基本的に注文に関係のない質問についてはロボットは回答を拒否できますが、完全ではないと常々感じています。また、顧客の問題と注文イベントの間に関係があるかどうか、そしてその関係の強さを正確に識別するようにロボットに教えることも必要です。この問題についてどう思いますか?

モデルパラメータ「温度」について

モデルに複数のオプションの結果がある場合、温度が 0 の場合、最も確率の高い結果が選択されます。温度の値が増加すると、モデル選択結果のランダム性が増加します。可能性としては高くない結果が選択される可能性もあります。例を見てみましょう。

3 つの食べ物に対する私の実際の好みがピザ 53%、寿司 30%、トコス 5% である場合、つまり、私の一番好きな食べ物はピザ、最も嫌いな食べ物はタコス、そして真ん中の食べ物は寿司です。別の温度を選択すると、次の結果が得られます。

 

 温度が 0 の場合、モデルは 100% ピザを選択し、温度が 0.3 の場合、モデルは 2/3 の確率でピザを選択し、1/3 の確率で寿司を選択します。温度が 0.7 の場合、モデルは、 3つの食べ物が選ばれます 皆さんの確率は1/3です。したがって、温度が高くなるほど、ランダム性も大きくなります。このように温度の役割が理解できたでしょうか?

要約する

今日は、ChatGPT を使用してパーソナライズされたチャット ロボットを開発し、人間に代わって特定のタスクを完了する方法を学びました。その中で、ロボットが何も必要のない質問に答えないようにするために、マシンのシステムの役割を詳細に定義する必要があります。タスク自体を実行します。

また、モデル パラメーター温度の機能と操作についても紹介しました。たとえば、温度が大きいほど、モデルの出力はよりランダムになります。

参考文献

DLAI - 学習プラットフォーム ベータ版

おすすめ

転載: blog.csdn.net/weixin_42608414/article/details/130950530