ラーメン店員注文取りシステム - ChatGPT Function Calling

<iframe width="560" height="315" src="https://www.youtube.com/embed/4zeuHFkLvSo" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>

ChatGPT と ChatGPT Function Calling を使ってラーメンの好みを一つ一つ聞いていきパラメーターを取得していきます。上手くいったときの動画をあげてますが、わりと失敗します。

```py
import codecs
import json
import pprint
import openai

openai.api_key = "XXXXXXX"


ramen_params = {
    "味のベース": ["醤油", "塩", "味噌", "豚骨", "鶏白湯", "魚介", "煮干し"],
    "味のこさ": ["濃い", "普通", "薄い"],
    "麺の太さ": ["太麺", "中太麺", "細麺"],
    "麺の硬さ": ["硬め", "普通", "柔め"],
    "麺の量": ["多め", "普通", "少なめ"],
    "スープの量": ["多め", "普通", "少なめ"],
    "ネギ": ["多め", "普通", "少なめ"],
    "追加トッピング": ["チャーシュ", "味玉", "メンマ", "海苔", "コーン", "ほうれん草", "キクラゲ", "もやし", "キャベツ", "ニンニク", "バター"],
}

system_content = "あなたはラーメン屋の店員です。お客さんに現在の組み合わせの空欄の箇所を質問して埋めていってください。注文可能な選択肢から選択肢を提示してください。質問は一度に一つのみにしてください。"

functions=[
    {
        "name": "make_params",
        "description": system_content,
        "parameters": {
            "type": "object",
            "properties": {},
            "required": [],
        },
    }
]

function_properties = {}
ramen_order = {}
for key, values in ramen_params.items():
    data = {
        "type": "string",
        "enum": values
    }
    function_properties[key] = data
    functions[0]["parameters"]["required"].append(key)
    ramen_order[key] = ""

functions[0]["parameters"]["properties"] = function_properties
#print("functions:", functions)
#pprint.pprint(functions)

while True:
    question_user_content = "# 注文可能な選択肢\n"
    for key, values in ramen_params.items():
        question_user_content += f"{key}:"
        for value in values:
            question_user_content += f"{value},"
        question_user_content += "\n"
    question_user_content += "\n# 現在の組み合わせ\n"
    for key, values in ramen_params.items():
        question_user_content += f"{key}:" + ramen_order.get(key, "") + "\n"
    #print("question_user_content")
    #print(question_user_content)

    completion = openai.ChatCompletion.create(
        model="gpt-3.5-turbo-0613",
        messages=[
            {"role": "system", "content": system_content},
            {"role": "user", "content": question_user_content},
        ],
        temperature=0.7
    )
    answer = str(completion.choices[0].message.content)
    print("\nラーメン屋の店員: ", answer)

    user_input = input("Input: ")
    if user_input.lower() == 'quit':
        break

    order_user_content = "\n# 現在の注文状況\n"
    order_user_content += codecs.decode(json.dumps(ramen_order), 'unicode_escape')
    order_user_content += f"\n\nラーメン屋の店員: {answer}\n"
    order_user_content += f"お客の注文: {user_input}"
    #print("order_user_content")
    #print(order_user_content)
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo-0613",
        messages=[
            {"role": "user", "content": order_user_content},
        ],
        functions=functions,
        function_call={"name": "make_params"},
    )
    try:
        ramen_order = json.loads(response.choices[0].message.function_call.arguments)
        print("ramen_order:", ramen_order)
    except:
        print("error")
        print(response.choices[0].message.function_call.arguments)
```