業界・業務から探す
導入目的・課題から探す
データ・AIについて学ぶ
News
Hakkyについて
ウェビナーコラム
◆トップ【AI・機械学習】
AI

執筆者:Handbook編集部

GPTモデルの機能呼び出しを革新するOpenAIのParallel Function Calling

はじめに

Function calling が更新され、1 つのメッセージで複数の関数を呼び出すように動作するようになりました。

以前までは「 関数の選択も GPT がやってくれる!function calling を使ってみた 」にあるように会話の内容に合わせて GPT 側が複数の関数の中から選択をしていました。

しかし、2023 年 11 月 6 日の OpenAI Dev Day2023 にて、gpt-4-1106-previewgpt-3.5-turbo-1106の 2 つのモデルで 1 つの入力から複数の関数を呼び出すことが可能になったと発表がありました。

ということで今回は、「 OpenAI Dev Day 2023 の発表まとめ 」の中から、Parallel Function Calling について紹介します。

Parallel function calling とは

複数の関数呼び出しを一緒に実行し、これらの関数呼び出し(function calling)と結果を並列(parallel)に解決できるようにするモデルの機能が Parallel function calling です。

OpenAI Dev Day2023 の発表以前もできないことはなかったのですが、当時は関数を 1 つづつ順番に呼び出しをこちら側で制御しなくてはならなかったり、エージェント的な関数(関数を呼び出す関数)に持っている関数渡し、ChatGPT にどの関数を実施すればいいかを判断してさせたりしなけらばなりませんでした。

今回のリリースにより、「窓を開けてエアコンを止めてくれ」という入力から、窓を開ける関数とエアコンを止める関数を 1 度に呼び出したり、1 度に 1 箇所の天気を取得できる関数を使って 3 箇所の天気を 1 度に取得したりするということが可能になりました。

Parallel function calling サンプルコード

from openai import OpenAI
import json

client = OpenAI(
    api_key = "************************"  # ここにapi key を入力
)

# Example dummy function hard coded to return the same weather
# In production, this could be your backend API or an external API
def get_current_weather(location, unit="fahrenheit"):
    """Get the current weather in a given location"""
    if "tokyo" in location.lower():
        return json.dumps({"location": "東京", "temperature": "10", "unit": "celsius"})
    elif "san francisco" in location.lower():
        return json.dumps({"location": "サンフランシスコ", "temperature": "72", "unit": "fahrenheit"})
    elif "paris" in location.lower():
        return json.dumps({"location": "パリ", "temperature": "22", "unit": "celsius"})
    else:
        return json.dumps({"location": location, "temperature": "unknown"})

def run_conversation():
    # Step 1: send the conversation and available functions to the model
    messages = [{"role": "user", "content": "東京とサンフランシスコとパリの気温は?"}]
    tools = [
        {
            "type": "function",
            "function": {
                "name": "get_current_weather",
                "description": "Get the current weather in a given location",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "location": {
                            "type": "string",
                            "description": "The city and state, e.g. San Francisco, CA",
                        },
                        "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
                    },
                    "required": ["location"],
                },
            },
        }
    ]
    response = client.chat.completions.create(
        model="gpt-3.5-turbo-1106",
        messages=messages,
        tools=tools,
        tool_choice="auto",  # auto is default, but we'll be explicit
    )
    response_message = response.choices[0].message
    tool_calls = response_message.tool_calls
    # Step 2: check if the model wanted to call a function
    if tool_calls:
        # Step 3: call the function
        # Note: the JSON response may not always be valid; be sure to handle errors
        available_functions = {
            "get_current_weather": get_current_weather,
        }  # only one function in this example, but you can have multiple
        messages.append(response_message)  # extend conversation with assistant's reply
        # Step 4: send the info for each function call and function response to the model
        for tool_call in tool_calls:
            function_name = tool_call.function.name
            function_to_call = available_functions[function_name]
            function_args = json.loads(tool_call.function.arguments)
            function_response = function_to_call(
                location=function_args.get("location"),
                unit=function_args.get("unit"),
            )
            messages.append(
                {
                    "tool_call_id": tool_call.id,
                    "role": "tool",
                    "name": function_name,
                    "content": function_response,
                }
            )  # extend conversation with function response
        second_response = client.chat.completions.create(
            model="gpt-3.5-turbo-1106",
            messages=messages,
        )  # get a new response from the model where it can see the function response
        return second_response

上記例では、1 度に 1 箇所の天気を取得できる関数を使って 3 箇所の天気を 1 度に取得しています。

結果としては

print(run_conversation())
# ChatCompletion(id='******************************::', choices=[Choice(finish_reason='stop', index=0, message=ChatCompletionMessage(content='東京の気温は10°C、サンフランシスコの気温は摂氏で約22°C、パリの気温は22°Cです。', role='assistant', function_call=None, tool_calls=None))], .............)

ときちんと複数の処理がなされた結果が出力されました。

model の部分をgpt-3.5-turbo-0613に変更して実行をすると 東京の気温は10℃です。となり、gpt-3.5-turbo-1106の出力の東京の気温は10°C、サンフランシスコの気温は摂氏で約22°C、パリの気温は22°Cです。と比べると出力が異なっていることがわかります。

参考

info
備考

ChatGPT を業務で活用したい場合、機密情報を入力することがセキュリティの観点から NG だったり、自社のシステムに組み込まないと効率が悪かったりする場合があります。Hakky では、ChatGPT を用いた企業独自のシステムを構築するご支援を行っています。 ソリューションサイト:https://www.about.st-hakky.com/chatgpt-solution

「どんなことが出来るのか」「いま考えているこのようなことは本当に実現可能か」など、ご検討段階でも構いませんので、ぜひお気軽にフォームよりお問い合わせくださいませ。

Hakkyへのお問い合わせ
2025年06月15日に最終更新
読み込み中...