0
0

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 Function Calling

Last updated at Posted at 2023-07-08

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

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?