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?

LangChain v0.3 その17 ~番外編:偏差値を計算~

Last updated at Posted at 2025-03-10

1. きっかけ

前回はbigtoolsで遊びました。偏差値の計算をさせてみたけど、イマイチだったので自分でtoolsを作って試してみます。



2. バージョン情報

バージョン情報
Python==3.11.9
langchain==0.3.19
python-dotenv
langchain-openai==0.3.7
langgraph==0.3.2
langchain-core==0.3.40
langchain-community==0.3.18
langgraph-bigtool==0.0.2



3. おさらい

標準偏差と偏差値の算出方法について念のためにメモしておく。
正直、僕はよく忘れる・・・


母標準偏差の計算式

標準偏差
s = \sqrt{\frac{1}{N} \sum_{i=1}^{N} (x_i - \bar{x})^2}



偏差値の計算式

偏差値
T = 50 + 10 \times \frac{x - \bar{x}}{s}



4. 解きたい問題

タイトルなし.png

これを解かせてみるのが目的。この計算はエクセルで行いました。



5. 早速、プログラムを書いていく

5-1. 定番の部分

まとめて書いていく。

ライブラリインポート
import os
import json
import math
import types
from pprint import pprint
# 環境関連
from dotenv import load_dotenv
# モデル
from langchain_openai import AzureChatOpenAI
from langchain_openai import AzureOpenAIEmbeddings
# tools関連
from langchain_core.tools import tool
# graph関連
from typing import Annotated, Any, Dict, Union
from typing_extensions import TypedDict
from pydantic import BaseModel, Field
from langgraph.store.memory import InMemoryStore
環境関連
load_dotenv('.env')

END_POINT = os.getenv('END_POINT')
API_VERSION = os.getenv('API_VERSION')
API_KEY = os.getenv('API_KEY')
モデルの用意
model = AzureChatOpenAI(
    azure_deployment='gpt-4o-mini',
    azure_endpoint=END_POINT,
    api_version=API_VERSION,
    api_key=API_KEY
)

いつも通り、4o-miniをつかいます。

5-2. toolsの引数の型宣言

tools引数の型宣言
class CalculatorNumericInput(BaseModel):
    value_a: Union[int, float] = Field(..., description="value")

class Calculator2NumericInput(BaseModel):
    value_a: Union[int, float] = Field(..., description="first value")
    value_b: Union[int, float] = Field(..., description="second value")

class CalculatorListInput(BaseModel):
    input_list: list[float] = Field(..., description="list of values")

class CalculatorListNumericInput(BaseModel):
    input_list: list[float] = Field(..., description="list of values")
    exponent: float = Field(..., description="exponent")

4種類の型を宣言しました。

  • 一つの数字を処理するパターン
  • 二つの数字を処理するパターン
  • リスト形式に数値が入っているリストを処理するパターン
  • リスト形式と数字を合わせて処理するパターン

5-3. toolの作成

toolsの作成
@tool("addition-tool", args_schema=Calculator2NumericInput, return_direct=True)
def add_numerics(value_a: Union[int, float], value_b: Union[int, float]) -> Union[int, float]:
    """Add two values.

    Args:
        value_a: first value
        value_b: second value
    """
    return value_a + value_b

@tool("subtract-tool", args_schema=Calculator2NumericInput, return_direct=True)
def subtract_numerics(value_a: Union[int, float], value_b: Union[int, float]) -> Union[int, float]:
    """subtruct two values.

    Args:
        value_a: first value
        value_b: second value
    """
    return value_a - value_b

@tool("devision-tool", args_schema=Calculator2NumericInput, return_direct=True)
def devision_numerics(value_a: Union[int, float], value_b: Union[int, float]) -> Union[int, float]:
    """first value devide by second value.

    Args:
        value_a: first value;Numerator 
        value_b: seoond value;Denominator
    """
    return value_a / value_b

@tool("multiply-tool", args_schema=Calculator2NumericInput, return_direct=True)
def multiply(value_a: Union[int, float], value_b: Union[int, float]) -> Union[int, float]:
    """multiply two values.

    Args:
        value_a: first value;Multiplier
        value_b: second value;Multiplicand
    """
    return value_a * value_b

@tool("power-tool", args_schema=Calculator2NumericInput, return_direct=True)
def power(value_a: Union[int, float], value_b: Union[int, float]) -> Union[int, float]:
    """first value exponentiate by second value.

    Args:
        value_a: first value is base
        value_b: second value is exponent
    """
    return value_a ** value_b

@tool("square-root-tool", args_schema=CalculatorNumericInput, return_direct=True)
def square_root(value_a: Union[int, float]) -> Union[int, float]:
    """calclate square root.

    Args:
        value_a: value
    Retrun:
        The square root of value_a
    """
    return math.sqrt(value_a)

@tool("sum-values-in-list-tool", args_schema=CalculatorListInput, return_direct=True)
def sum_list_values(input_list: list[int, float]) -> Union[int, float]:
    """sum up the values in the list.

    Args:
        input_list: list of values
    Return:
        sumed up value
    """
    return sum(input_list)

@tool("power-values-in-list-tool", args_schema=CalculatorListNumericInput, return_direct=True)
def power_list_values(input_list: list[float], exponent: Union[int, float]) -> list[float]:
    """
    Raises each value in the list to the power of the exponent argument.

    Args:
    numbers (list[float]): The input list of numbers.
    exponent (float): The exponent to raise each number to.

    Returns:
    list[float]: A new list with each value raised to the specified power.
    """
    output = [x ** exponent for x in input_list]
    return output

テストなので、少し余分なtoolも作りました。DocStringsはChatGPTで。
二つの数値の処理:足し算、引き算、割り算、掛け算、べき乗
一つの数字の処理:平方根
リストの処理:全部を足し合わせる、全部べき乗する

まぁ、ちょっと恣意的ですが・・・

大事なのは型宣言したときの引数名とあわせて関数を作ること。
(たくさんミスった。💦)



5-4. バインドモデルを作る

モデルにバインド
tools = [
    add_numerics,
    subtract_numerics,
    devision_numerics,
    multiply,
    power,
    square_root,
    sum_list_values,
    power_list_values,
]

model_with_tools = model.bind_tools(tools)

これで、tool_callingが使えます。


5−4−1. toolsの確認

pprint(tools)
# [StructuredTool(name='addition-tool', description='Add two values.\n\n    Args:\n        value_a: first value\n        value_b: second value', args_schema=<class '__main__.Calculator2NumericInput'>, return_direct=True, func=<function add_numerics at 0x1180aab60>),
#  StructuredTool(name='subtract-tool', description='subtruct two values.\n\n    Args:\n        value_a: first value\n        value_b: second value', args_schema=<class '__main__.Calculator2NumericInput'>, return_direct=True, func=<function subtract_numerics at 0x1180abf60>),
#  StructuredTool(name='devision-tool', description='first value devide by second value.\n\n    Args:\n        value_a: first value;Numerator \n        value_b: seoond value;Denominator', args_schema=<class '__main__.Calculator2NumericInput'>, return_direct=True, func=<function devision_numerics at 0x1180aae80>),
#  StructuredTool(name='multiply-tool', description='multiply two values.\n\n    Args:\n        value_a: first value;Multiplier\n        value_b: second value;Multiplicand', args_schema=<class '__main__.Calculator2NumericInput'>, return_direct=True, func=<function multiply at 0x1180abec0>),
#  StructuredTool(name='power-tool', description='first value exponentiate by second value.\n\n    Args:\n        value_a: first value is base\n        value_b: second value is exponent', args_schema=<class '__main__.Calculator2NumericInput'>, return_direct=True, func=<function power at 0x1180abce0>),
#  StructuredTool(name='square-root-tool', description='calclate square root.\n\n    Args:\n        value_a: value\n    Retrun:\n        The square root of value_a', args_schema=<class '__main__.CalculatorNumericInput'>, return_direct=True, func=<function square_root at 0x1180abc40>),
#  StructuredTool(name='sum-values-in-list-tool', description='sum up the values in the list.\n\n    Args:\n        input_list: list of values\n    Return:\n        sumed up value', args_schema=<class '__main__.CalculatorListInput'>, return_direct=True, func=<function sum_list_values at 0x1180aba60>),
#  StructuredTool(name='power-values-in-list-tool', description='Raises each value in the list to the power of the exponent argument.\n\nArgs:\nnumbers (list[float]): The input list of numbers.\nexponent (float): The exponent to raise each number to.\n\nReturns:\nlist[float]: A new list with each value raised to the specified power.', args_schema=<class '__main__.CalculatorListNumericInput'>, return_direct=True, func=<function power_list_values at 0x1180ab920>)]

忘れ物がありませんように。


5-5. tool呼び出し用の辞書の作成

tools呼び出し用辞書
tools_by_name = {
    tool.name: tool
    for tool in tools
}
print(tools_by_name)

# {'addition-tool': StructuredTool(name='addition-tool', description='Add two values.\n\n    Args:\n        value_a: first value\n        value_b: second value', args_schema=<class '__main__.Calculator2NumericInput'>, return_direct=True, func=<function add_numerics at 0x1180aab60>),
#  'subtract-tool': StructuredTool(name='subtract-tool', description='subtruct two values.\n\n    Args:\n        value_a: first value\n        value_b: second value', args_schema=<class '__main__.Calculator2NumericInput'>, return_direct=True, func=<function subtract_numerics at 0x1180abf60>),
#  'devision-tool': StructuredTool(name='devision-tool', description='first value devide by second value.\n\n    Args:\n        value_a: first value;Numerator \n        value_b: seoond value;Denominator', args_schema=<class '__main__.Calculator2NumericInput'>, return_direct=True, func=<function devision_numerics at 0x1180aae80>),
#  'multiply-tool': StructuredTool(name='multiply-tool', description='multiply two values.\n\n    Args:\n        value_a: first value;Multiplier\n        value_b: second value;Multiplicand', args_schema=<class '__main__.Calculator2NumericInput'>, return_direct=True, func=<function multiply at 0x1180abec0>),
#  'power-tool': StructuredTool(name='power-tool', description='first value exponentiate by second value.\n\n    Args:\n        value_a: first value is base\n        value_b: second value is exponent', args_schema=<class '__main__.Calculator2NumericInput'>, return_direct=True, func=<function power at 0x1180abce0>),
#  'square-root-tool': StructuredTool(name='square-root-tool', description='calclate square root.\n\n    Args:\n        value_a: value\n    Retrun:\n        The square root of value_a', args_schema=<class '__main__.CalculatorNumericInput'>, return_direct=True, func=<function square_root at 0x1180abc40>),
#  'sum-values-in-list-tool': StructuredTool(name='sum-values-in-list-tool', description='sum up the values in the list.\n\n    Args:\n        input_list: list of values\n    Return:\n        sumed up value', args_schema=<class '__main__.CalculatorListInput'>, return_direct=True, func=<function sum_list_values at 0x1180aba60>),
#  'power-values-in-list-tool': StructuredTool(name='power-values-in-list-tool', description='Raises each value in the list to the power of the exponent argument.\n\nArgs:\nnumbers (list[float]): The input list of numbers.\nexponent (float): The exponent to raise each number to.\n\nReturns:\nlist[float]: A new list with each value raised to the specified power.', args_schema=<class '__main__.CalculatorListNumericInput'>, return_direct=True, func=<function power_list_values at 0x1180ab920>)}

こちらはtool名で関数を呼び出すための辞書。


5-6. toolsの実行用関数

tools実行関数
def execute_tool(tools_by_name: Dict[str, Any], tool_call: Dict[str, Any]) -> str:
    """Execute a tool call and return the result"""
    tool_name = tool_call["name"]

    if tool_name not in tools_by_name:
        return f"Error: Tool {tool_name} not found"
    else:
        # print(tool_call)
        pass

    tool = tools_by_name[tool_name]
    # print(tool)
    try:
        result = tool.invoke(tool_call["args"])
        return str(result)
    except Exception as e:
        return f"Error executing {tool_name}: {str(e)}"

前回のコピペ


5−7. agent実行用関数

Agent実行関数
def agent_run(messages: list)-> list:
    while True:
        # モデルに入れて推論して、出力をmessagesに追加する
        response = model_with_tools.invoke(messages)
        tool_calls = response.tool_calls
        tool_calls_len = len(tool_calls)

        messages.append(
            {"role": "assistant", "content": response.content, "tool_calls": tool_calls}
        )
        
        # tool_callingされない時の処理はモデルの出力のmessagesを返す
        if not tool_calls:
            print(f"{response.content=}")
            return messages

        # tool_callingされたときにtoolsを実行してmessagesに追加し、返す
        for i, tool_call in enumerate(tool_calls):
            print(tool_call)
            tool_result = execute_tool(tools_by_name, tool_call)
            tools_result = {
                    "role": "tool",
                    "tool_call_id": tool_call["id"],
                    "content": tool_result,
            }
            messages.append(tools_result)

Agent関数を作ります。これも前回のコピペ。

6. 実行!

6-1. 標準偏差を計算させてみる

標準偏差
user_input = """以下の値の標準偏差を教えてください。
90
70
40
65"""

messages = [
    {"role": "system", "content": "計算手順を考えたうえで、一つずつ正確に計算して結論を導きます。"},
    {"role": "user", "content": user_input}
]
messages = agent_run(messages)
pprint(messages)

# 与えられた値 90, 70, 40, 65 の標準偏差は約 **17.81** です。(最終出力のみ)
全出力
{'name': 'sum-values-in-list-tool', 'args': {'input_list': [90, 70, 40, 65]}, 'id': 'call_eWomUg0SEoJVnpLcPZyLnnqo', 'type': 'tool_call'}
{'name': 'devision-tool', 'args': {'value_a': 265, 'value_b': 4}, 'id': 'call_eMKKtyZo799MdL8cp7iPUaXz', 'type': 'tool_call'}
{'name': 'subtract-tool', 'args': {'value_a': 90, 'value_b': 66.25}, 'id': 'call_yUVF2sFMvT29OdZUYcDJ6gMX', 'type': 'tool_call'}
{'name': 'subtract-tool', 'args': {'value_a': 70, 'value_b': 66.25}, 'id': 'call_74Pbw4PmEaNkxXrZr0FcYjve', 'type': 'tool_call'}
{'name': 'subtract-tool', 'args': {'value_a': 40, 'value_b': 66.25}, 'id': 'call_RxK7Xz9aH4ucV9iPlWZeN3L0', 'type': 'tool_call'}
{'name': 'subtract-tool', 'args': {'value_a': 65, 'value_b': 66.25}, 'id': 'call_QsYyZ4TQxzNwglYLesFZbi5F', 'type': 'tool_call'}
{'name': 'power-values-in-list-tool', 'args': {'input_list': [23.75, 3.75, -26.25, -1.25], 'exponent': 2}, 'id': 'call_Vk1Bl7UPakOeuRP1hoADyQVv', 'type': 'tool_call'}
{'name': 'sum-values-in-list-tool', 'args': {'input_list': [23.75, 3.75, -26.25, -1.25]}, 'id': 'call_5qFywS96sZC8pzd3tnfFGusn', 'type': 'tool_call'}
{'name': 'sum-values-in-list-tool', 'args': {'input_list': [564.0625, 14.0625, 689.0625, 1.5625]}, 'id': 'call_NwAtjJkGcDGwKZO4SprMQSiA', 'type': 'tool_call'}
{'name': 'devision-tool', 'args': {'value_a': 1268.75, 'value_b': 4}, 'id': 'call_iQZk0AskCmCtAK687hZr0C6u', 'type': 'tool_call'}
{'name': 'square-root-tool', 'args': {'value_a': 317.1875}, 'id': 'call_Ltut1V7rc2LFKPllvQfBxpj0', 'type': 'tool_call'}
response.content='与えられた値 90, 70, 40, 65 の標準偏差は約 **17.81** です。'
[{'content': '計算手順を考えたうえで、一つずつ正確に計算して結論を導きます。', 'role': 'system'},
 {'content': '以下の値の標準偏差を教えてください。\n90\n70\n40\n65', 'role': 'user'},
 {'content': '標準偏差を求める手順は以下の通りです。\n'
             '\n'
             '1. **平均を計算**: 値の合計をデータの個数で割ります。\n'
             '2. **各データ点の偏差を計算**: 各データ点から平均を引き、その値を2乗します。\n'
             '3. **偏差の合計を求める**: すべての偏差の2乗の合計を求めます。\n'
             '4. **分散を計算**: 偏差の合計をデータの個数で割ります(標本の場合はデータの個数 - 1で割ります)。\n'
             '5. **標準偏差を計算**: 分散の平方根を取ります。\n'
             '\n'
             'まず、与えられた値の平均を計算します。次に、各ステップを計算します。では、計算を始めましょう。',
  'role': 'assistant',
  'tool_calls': [{'args': {'input_list': [90, 70, 40, 65]},
                  'id': 'call_eWomUg0SEoJVnpLcPZyLnnqo',
                  'name': 'sum-values-in-list-tool',
                  'type': 'tool_call'}]},
 {'content': '265.0',
  'role': 'tool',
  'tool_call_id': 'call_eWomUg0SEoJVnpLcPZyLnnqo'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 265, 'value_b': 4},
                  'id': 'call_eMKKtyZo799MdL8cp7iPUaXz',
                  'name': 'devision-tool',
                  'type': 'tool_call'}]},
 {'content': '66.25',
  'role': 'tool',
  'tool_call_id': 'call_eMKKtyZo799MdL8cp7iPUaXz'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 90, 'value_b': 66.25},
                  'id': 'call_yUVF2sFMvT29OdZUYcDJ6gMX',
                  'name': 'subtract-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 70, 'value_b': 66.25},
                  'id': 'call_74Pbw4PmEaNkxXrZr0FcYjve',
                  'name': 'subtract-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 40, 'value_b': 66.25},
                  'id': 'call_RxK7Xz9aH4ucV9iPlWZeN3L0',
                  'name': 'subtract-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 65, 'value_b': 66.25},
                  'id': 'call_QsYyZ4TQxzNwglYLesFZbi5F',
                  'name': 'subtract-tool',
                  'type': 'tool_call'}]},
 {'content': '23.75',
  'role': 'tool',
  'tool_call_id': 'call_yUVF2sFMvT29OdZUYcDJ6gMX'},
 {'content': '3.75',
  'role': 'tool',
  'tool_call_id': 'call_74Pbw4PmEaNkxXrZr0FcYjve'},
 {'content': '-26.25',
  'role': 'tool',
  'tool_call_id': 'call_RxK7Xz9aH4ucV9iPlWZeN3L0'},
 {'content': '-1.25',
  'role': 'tool',
  'tool_call_id': 'call_QsYyZ4TQxzNwglYLesFZbi5F'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'exponent': 2,
                           'input_list': [23.75, 3.75, -26.25, -1.25]},
                  'id': 'call_Vk1Bl7UPakOeuRP1hoADyQVv',
                  'name': 'power-values-in-list-tool',
                  'type': 'tool_call'},
                 {'args': {'input_list': [23.75, 3.75, -26.25, -1.25]},
                  'id': 'call_5qFywS96sZC8pzd3tnfFGusn',
                  'name': 'sum-values-in-list-tool',
                  'type': 'tool_call'}]},
 {'content': '[564.0625, 14.0625, 689.0625, 1.5625]',
  'role': 'tool',
  'tool_call_id': 'call_Vk1Bl7UPakOeuRP1hoADyQVv'},
 {'content': '0.0',
  'role': 'tool',
  'tool_call_id': 'call_5qFywS96sZC8pzd3tnfFGusn'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'input_list': [564.0625, 14.0625, 689.0625, 1.5625]},
                  'id': 'call_NwAtjJkGcDGwKZO4SprMQSiA',
                  'name': 'sum-values-in-list-tool',
                  'type': 'tool_call'}]},
 {'content': '1268.75',
  'role': 'tool',
  'tool_call_id': 'call_NwAtjJkGcDGwKZO4SprMQSiA'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 1268.75, 'value_b': 4},
                  'id': 'call_iQZk0AskCmCtAK687hZr0C6u',
                  'name': 'devision-tool',
                  'type': 'tool_call'}]},
 {'content': '317.1875',
  'role': 'tool',
  'tool_call_id': 'call_iQZk0AskCmCtAK687hZr0C6u'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 317.1875},
                  'id': 'call_Ltut1V7rc2LFKPllvQfBxpj0',
                  'name': 'square-root-tool',
                  'type': 'tool_call'}]},
 {'content': '17.80975856096876',
  'role': 'tool',
  'tool_call_id': 'call_Ltut1V7rc2LFKPllvQfBxpj0'},
 {'content': '与えられた値 90, 70, 40, 65 の標準偏差は約 **17.81** です。',
  'role': 'assistant',
  'tool_calls': []}]

いいじゃん!
期待が持てます!!!



6-2. 偏差値の計算

いよいよです!

偏差値の計算1
user_input = """テストの結果が以下の通りになりました。全員の偏差値を計算してください。

# テスト結果
A氏 90点
B氏 70点
C氏 40点
D氏 65点
"""
messages = [
    {"role": "system", "content": "計算手順を考えたうえで、一つずつ正確に計算して結論を導きます。"},
    {"role": "user", "content": user_input}
]
messages = agent_run(messages)
pprint(messages)

# (最終出力の抜粋)
#  '各人の偏差値は次の通りです:\n'
#              '\n'
#              '- A氏の偏差値: \\( 56.67 \\)\n'
#              '- B氏の偏差値: \\( 51.05 \\)\n'
#              '- C氏の偏差値: \\( 42.63 \\)\n'
#              '- D氏の偏差値: \\( 49.65 \\)\n'
#              '\n'
#              'これがテスト結果に基づく偏差値です。',

A氏:56.67
B氏:51.05
C氏:42.63
D氏:49.65

うん、間違ってる!

全出力
{'name': 'sum-values-in-list-tool', 'args': {'input_list': [90, 70, 40, 65]}, 'id': 'call_yRgRifNvi4ABTtlqc9kPwEgI', 'type': 'tool_call'}
{'name': 'devision-tool', 'args': {'value_a': 265, 'value_b': 4}, 'id': 'call_pILxoDdhxe3bEw3girr9RrHW', 'type': 'tool_call'}
{'name': 'subtract-tool', 'args': {'value_a': 90, 'value_b': 66.25}, 'id': 'call_Ftdwv4zk93MBTgAViOgKo96l', 'type': 'tool_call'}
{'name': 'subtract-tool', 'args': {'value_a': 70, 'value_b': 66.25}, 'id': 'call_wsqmFHIMYHZ7zbeh9yZ7QqrE', 'type': 'tool_call'}
{'name': 'subtract-tool', 'args': {'value_a': 40, 'value_b': 66.25}, 'id': 'call_5DZ3b4zvWq1QPaqDy5nxMmxX', 'type': 'tool_call'}
{'name': 'subtract-tool', 'args': {'value_a': 65, 'value_b': 66.25}, 'id': 'call_mmkH9wRbLlvYmZfU6aAFzShC', 'type': 'tool_call'}
{'name': 'power-tool', 'args': {'value_a': 23.75, 'value_b': 2}, 'id': 'call_MgVjXOEipvGhZQz7SZ93iU6C', 'type': 'tool_call'}
{'name': 'power-tool', 'args': {'value_a': 3.75, 'value_b': 2}, 'id': 'call_tkrl6GuL98VSc3ctLkNlmFu2', 'type': 'tool_call'}
{'name': 'power-tool', 'args': {'value_a': -26.25, 'value_b': 2}, 'id': 'call_3WfHONuIcv90XJ8d8gUhJrW6', 'type': 'tool_call'}
{'name': 'power-tool', 'args': {'value_a': -1.25, 'value_b': 2}, 'id': 'call_WBbEePax5W8IvgwYtcL7ZB85', 'type': 'tool_call'}
{'name': 'sum-values-in-list-tool', 'args': {'input_list': [564.0625, 14.0625, 689.0625, 1.5625]}, 'id': 'call_VIJ0kRK8u9xqi1NB5MHKnkd0', 'type': 'tool_call'}
{'name': 'square-root-tool', 'args': {'value_a': 1268.75}, 'id': 'call_jnMgDp23HEKxrBmrDlrRhsIm', 'type': 'tool_call'}
{'name': 'devision-tool', 'args': {'value_a': 23.75, 'value_b': 35.61951712193752}, 'id': 'call_yNp8ZfHAfEuekrz3RBZNxQJV', 'type': 'tool_call'}
{'name': 'devision-tool', 'args': {'value_a': 3.75, 'value_b': 35.61951712193752}, 'id': 'call_cDGfycyrKv8fEy39dv4x3hGU', 'type': 'tool_call'}
{'name': 'devision-tool', 'args': {'value_a': -26.25, 'value_b': 35.61951712193752}, 'id': 'call_QuAx0thf5NpRc53mVeWFhlkM', 'type': 'tool_call'}
{'name': 'devision-tool', 'args': {'value_a': -1.25, 'value_b': 35.61951712193752}, 'id': 'call_hFQh6MABqIf2cZS3n5nx81ae', 'type': 'tool_call'}
{'name': 'multiply-tool', 'args': {'value_a': 0.6667692860264166, 'value_b': 10}, 'id': 'call_BUksvV0bgRki9ecArA8JfNMg', 'type': 'tool_call'}
{'name': 'multiply-tool', 'args': {'value_a': 0.10527936095153946, 'value_b': 10}, 'id': 'call_EBtY8wwipdSRGvzpZDkVPuuJ', 'type': 'tool_call'}
{'name': 'multiply-tool', 'args': {'value_a': -0.7369555266607762, 'value_b': 10}, 'id': 'call_G4UOm3stXjqPLYTnaJ0tPhS3', 'type': 'tool_call'}
{'name': 'multiply-tool', 'args': {'value_a': -0.035093120317179816, 'value_b': 10}, 'id': 'call_YWfqI9sGeJpDBOXmCSwbf7rF', 'type': 'tool_call'}
{'name': 'addition-tool', 'args': {'value_a': 6.667692860264166, 'value_b': 50}, 'id': 'call_9z9RGsbsQ8RRkSUkhq3F4ZOx', 'type': 'tool_call'}
{'name': 'addition-tool', 'args': {'value_a': 1.0527936095153947, 'value_b': 50}, 'id': 'call_kovFf4iPrY02MkbG3EWIjW0I', 'type': 'tool_call'}
{'name': 'addition-tool', 'args': {'value_a': -7.369555266607763, 'value_b': 50}, 'id': 'call_2e8qivuVMbRCR3D8D8WjYq5S', 'type': 'tool_call'}
{'name': 'addition-tool', 'args': {'value_a': -0.35093120317179816, 'value_b': 50}, 'id': 'call_92G4YAgM2ONURfa26RNALGn3', 'type': 'tool_call'}
response.content='各人の偏差値は次の通りです:\n\n- A氏の偏差値: \\( 56.67 \\)\n- B氏の偏差値: \\( 51.05 \\)\n- C氏の偏差値: \\( 42.63 \\)\n- D氏の偏差値: \\( 49.65 \\)\n\nこれがテスト結果に基づく偏差値です。'
[{'content': '計算手順を考えたうえで、一つずつ正確に計算して結論を導きます。', 'role': 'system'},
 {'content': 'テストの結果が以下の通りになりました。全員の偏差値を計算してください。\n'
             '\n'
             '# テスト結果\n'
             'A氏 90点\n'
             'B氏 70点\n'
             'C氏 40点\n'
             'D氏 65点\n',
  'role': 'user'},
 {'content': '偏差値を計算するためには、以下の手順を踏む必要があります。\n'
             '\n'
             '1. 平均点を計算する。\n'
             '2. 標準偏差を計算する。\n'
             '3. 各人の偏差値を求める。\n'
             '\n'
             '偏差値の計算式は次の通りです:\n'
             '\n'
             '\\[\n'
             '\\text{偏差値} = 50 + \\frac{(X - \\mu)}{\\sigma} \\times 10\n'
             '\\]\n'
             '\n'
             'ここで、\n'
             '- \\(X\\) = 個々の点数\n'
             '- \\(\\mu\\) = 平均点\n'
             '- \\(\\sigma\\) = 標準偏差\n'
             '\n'
             'それでは、まず平均点を計算します。得点は以下の通りです:\n'
             '\n'
             '- A氏: 90点\n'
             '- B氏: 70点\n'
             '- C氏: 40点\n'
             '- D氏: 65点\n'
             '\n'
             'これらの点数の合計を計算し、人数で割ります。',
  'role': 'assistant',
  'tool_calls': [{'args': {'input_list': [90, 70, 40, 65]},
                  'id': 'call_yRgRifNvi4ABTtlqc9kPwEgI',
                  'name': 'sum-values-in-list-tool',
                  'type': 'tool_call'}]},
 {'content': '265.0',
  'role': 'tool',
  'tool_call_id': 'call_yRgRifNvi4ABTtlqc9kPwEgI'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 265, 'value_b': 4},
                  'id': 'call_pILxoDdhxe3bEw3girr9RrHW',
                  'name': 'devision-tool',
                  'type': 'tool_call'}]},
 {'content': '66.25',
  'role': 'tool',
  'tool_call_id': 'call_pILxoDdhxe3bEw3girr9RrHW'},
 {'content': '平均点は \\( 66.25 \\) 点です。\n'
             '\n'
             '次に、標準偏差を計算します。標準偏差の計算式は次の通りです:\n'
             '\n'
             '\\[\n'
             '\\sigma = \\sqrt{\\frac{\\sum{(X_i - \\mu)^2}}{N}}\n'
             '\\]\n'
             '\n'
             'ここで、\\(X_i\\) は各人の得点、\\(\\mu\\) は平均点、\\(N\\) は人数です。\n'
             '\n'
             'それでは、各人の得点と平均点の差の二乗を計算します。\n'
             '\n'
             '- A氏の得点: \\( (90 - 66.25)^2 \\)\n'
             '- B氏の得点: \\( (70 - 66.25)^2 \\)\n'
             '- C氏の得点: \\( (40 - 66.25)^2 \\)\n'
             '- D氏の得点: \\( (65 - 66.25)^2 \\)\n'
             '\n'
             'これらを計算します。',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 90, 'value_b': 66.25},
                  'id': 'call_Ftdwv4zk93MBTgAViOgKo96l',
                  'name': 'subtract-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 70, 'value_b': 66.25},
                  'id': 'call_wsqmFHIMYHZ7zbeh9yZ7QqrE',
                  'name': 'subtract-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 40, 'value_b': 66.25},
                  'id': 'call_5DZ3b4zvWq1QPaqDy5nxMmxX',
                  'name': 'subtract-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 65, 'value_b': 66.25},
                  'id': 'call_mmkH9wRbLlvYmZfU6aAFzShC',
                  'name': 'subtract-tool',
                  'type': 'tool_call'}]},
 {'content': '23.75',
  'role': 'tool',
  'tool_call_id': 'call_Ftdwv4zk93MBTgAViOgKo96l'},
 {'content': '3.75',
  'role': 'tool',
  'tool_call_id': 'call_wsqmFHIMYHZ7zbeh9yZ7QqrE'},
 {'content': '-26.25',
  'role': 'tool',
  'tool_call_id': 'call_5DZ3b4zvWq1QPaqDy5nxMmxX'},
 {'content': '-1.25',
  'role': 'tool',
  'tool_call_id': 'call_mmkH9wRbLlvYmZfU6aAFzShC'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 23.75, 'value_b': 2},
                  'id': 'call_MgVjXOEipvGhZQz7SZ93iU6C',
                  'name': 'power-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 3.75, 'value_b': 2},
                  'id': 'call_tkrl6GuL98VSc3ctLkNlmFu2',
                  'name': 'power-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': -26.25, 'value_b': 2},
                  'id': 'call_3WfHONuIcv90XJ8d8gUhJrW6',
                  'name': 'power-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': -1.25, 'value_b': 2},
                  'id': 'call_WBbEePax5W8IvgwYtcL7ZB85',
                  'name': 'power-tool',
                  'type': 'tool_call'}]},
 {'content': '564.0625',
  'role': 'tool',
  'tool_call_id': 'call_MgVjXOEipvGhZQz7SZ93iU6C'},
 {'content': '14.0625',
  'role': 'tool',
  'tool_call_id': 'call_tkrl6GuL98VSc3ctLkNlmFu2'},
 {'content': '689.0625',
  'role': 'tool',
  'tool_call_id': 'call_3WfHONuIcv90XJ8d8gUhJrW6'},
 {'content': '1.5625',
  'role': 'tool',
  'tool_call_id': 'call_WBbEePax5W8IvgwYtcL7ZB85'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'input_list': [564.0625, 14.0625, 689.0625, 1.5625]},
                  'id': 'call_VIJ0kRK8u9xqi1NB5MHKnkd0',
                  'name': 'sum-values-in-list-tool',
                  'type': 'tool_call'}]},
 {'content': '1268.75',
  'role': 'tool',
  'tool_call_id': 'call_VIJ0kRK8u9xqi1NB5MHKnkd0'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 1268.75},
                  'id': 'call_jnMgDp23HEKxrBmrDlrRhsIm',
                  'name': 'square-root-tool',
                  'type': 'tool_call'}]},
 {'content': '35.61951712193752',
  'role': 'tool',
  'tool_call_id': 'call_jnMgDp23HEKxrBmrDlrRhsIm'},
 {'content': '標準偏差は \\( \\sigma \\approx 35.62 \\) です。\n'
             '\n'
             '次に、各人の偏差値を計算します。偏差値の計算は以下のように行います:\n'
             '\n'
             '- A氏の偏差値: \\( 50 + \\frac{(90 - 66.25)}{35.62} \\times 10 \\)\n'
             '- B氏の偏差値: \\( 50 + \\frac{(70 - 66.25)}{35.62} \\times 10 \\)\n'
             '- C氏の偏差値: \\( 50 + \\frac{(40 - 66.25)}{35.62} \\times 10 \\)\n'
             '- D氏の偏差値: \\( 50 + \\frac{(65 - 66.25)}{35.62} \\times 10 \\)\n'
             '\n'
             'これらを計算します。',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 23.75, 'value_b': 35.61951712193752},
                  'id': 'call_yNp8ZfHAfEuekrz3RBZNxQJV',
                  'name': 'devision-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 3.75, 'value_b': 35.61951712193752},
                  'id': 'call_cDGfycyrKv8fEy39dv4x3hGU',
                  'name': 'devision-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': -26.25, 'value_b': 35.61951712193752},
                  'id': 'call_QuAx0thf5NpRc53mVeWFhlkM',
                  'name': 'devision-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': -1.25, 'value_b': 35.61951712193752},
                  'id': 'call_hFQh6MABqIf2cZS3n5nx81ae',
                  'name': 'devision-tool',
                  'type': 'tool_call'}]},
 {'content': '0.6667692860264166',
  'role': 'tool',
  'tool_call_id': 'call_yNp8ZfHAfEuekrz3RBZNxQJV'},
 {'content': '0.10527936095153946',
  'role': 'tool',
  'tool_call_id': 'call_cDGfycyrKv8fEy39dv4x3hGU'},
 {'content': '-0.7369555266607762',
  'role': 'tool',
  'tool_call_id': 'call_QuAx0thf5NpRc53mVeWFhlkM'},
 {'content': '-0.035093120317179816',
  'role': 'tool',
  'tool_call_id': 'call_hFQh6MABqIf2cZS3n5nx81ae'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 0.6667692860264166, 'value_b': 10},
                  'id': 'call_BUksvV0bgRki9ecArA8JfNMg',
                  'name': 'multiply-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 0.10527936095153946, 'value_b': 10},
                  'id': 'call_EBtY8wwipdSRGvzpZDkVPuuJ',
                  'name': 'multiply-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': -0.7369555266607762, 'value_b': 10},
                  'id': 'call_G4UOm3stXjqPLYTnaJ0tPhS3',
                  'name': 'multiply-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': -0.035093120317179816, 'value_b': 10},
                  'id': 'call_YWfqI9sGeJpDBOXmCSwbf7rF',
                  'name': 'multiply-tool',
                  'type': 'tool_call'}]},
 {'content': '6.667692860264166',
  'role': 'tool',
  'tool_call_id': 'call_BUksvV0bgRki9ecArA8JfNMg'},
 {'content': '1.0527936095153947',
  'role': 'tool',
  'tool_call_id': 'call_EBtY8wwipdSRGvzpZDkVPuuJ'},
 {'content': '-7.369555266607763',
  'role': 'tool',
  'tool_call_id': 'call_G4UOm3stXjqPLYTnaJ0tPhS3'},
 {'content': '-0.35093120317179816',
  'role': 'tool',
  'tool_call_id': 'call_YWfqI9sGeJpDBOXmCSwbf7rF'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 6.667692860264166, 'value_b': 50},
                  'id': 'call_9z9RGsbsQ8RRkSUkhq3F4ZOx',
                  'name': 'addition-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 1.0527936095153947, 'value_b': 50},
                  'id': 'call_kovFf4iPrY02MkbG3EWIjW0I',
                  'name': 'addition-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': -7.369555266607763, 'value_b': 50},
                  'id': 'call_2e8qivuVMbRCR3D8D8WjYq5S',
                  'name': 'addition-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': -0.35093120317179816, 'value_b': 50},
                  'id': 'call_92G4YAgM2ONURfa26RNALGn3',
                  'name': 'addition-tool',
                  'type': 'tool_call'}]},
 {'content': '56.66769286026417',
  'role': 'tool',
  'tool_call_id': 'call_9z9RGsbsQ8RRkSUkhq3F4ZOx'},
 {'content': '51.0527936095154',
  'role': 'tool',
  'tool_call_id': 'call_kovFf4iPrY02MkbG3EWIjW0I'},
 {'content': '42.63044473339224',
  'role': 'tool',
  'tool_call_id': 'call_2e8qivuVMbRCR3D8D8WjYq5S'},
 {'content': '49.6490687968282',
  'role': 'tool',
  'tool_call_id': 'call_92G4YAgM2ONURfa26RNALGn3'},
 {'content': '各人の偏差値は次の通りです:\n'
             '\n'
             '- A氏の偏差値: \\( 56.67 \\)\n'
             '- B氏の偏差値: \\( 51.05 \\)\n'
             '- C氏の偏差値: \\( 42.63 \\)\n'
             '- D氏の偏差値: \\( 49.65 \\)\n'
             '\n'
             'これがテスト結果に基づく偏差値です。',
  'role': 'assistant',
  'tool_calls': []}]

計算履歴を確認してみた。

  1. 平均点を計算(OK)
  2. 各点の平均点との差を計算(OK)
  3. 2.の各値を2乗(OK)
  4. 3.の値の総和を計算(OK)
  5. 4.の値の平方根を計算(NG)

残念!w
そこは平方根をとる前にデータの数で割らないと・・・。


6-3. 実験(計算式を与えてみる)

偏差値の計算2(チート)
user_input = """テストの結果が以下の通りになりました。偏差値の計算方法は以下の通りです。これを理解した上で、標準偏差を計算した後に全員の偏差値を計算してください。

# 偏差値の計算方法
\text{偏差値} = 50 + 10 \times \frac{x - \mu}{\sigma}

# テスト結果
A氏 90点
B氏 70点
C氏 40点
D氏 65点
"""
messages = [
    {"role": "system", "content": "計算手順を考えたうえで、一つずつ正確に計算して結論を導きます。"},
    {"role": "user", "content": user_input}
]
messages = agent_run(messages)
pprint(messages)

# '各人の偏差値は以下の通りです:\n'
#              '\n'
#              '- A氏の偏差値: 約63.34\n'
#              '- B氏の偏差値: 約52.11\n'
#              '- C氏の偏差値: 約35.26\n'
#              '- D氏の偏差値: 約49.30\n'
#              '\n'
#              '全員の偏差値を計算し、結果を提供しました。必要があれば、さらに解説をいたします。'

おおお、正解!計算できよった!
しかし、計算式を与えるのはチートだよな。
ユーザーは計算式書いている間に計算できちゃう。

全出力
{'name': 'sum-values-in-list-tool', 'args': {'input_list': [90, 70, 40, 65]}, 'id': 'call_g8rN8yzibvpCpxm46ZPpEKXl', 'type': 'tool_call'}
{'name': 'devision-tool', 'args': {'value_a': 265, 'value_b': 4}, 'id': 'call_Ftdwv4zk93MBTgAViOgKo96l', 'type': 'tool_call'}
{'name': 'power-tool', 'args': {'value_a': 90, 'value_b': 2}, 'id': 'call_QCjSeHoxxDJElvik2oiEV8WM', 'type': 'tool_call'}
{'name': 'power-tool', 'args': {'value_a': 70, 'value_b': 2}, 'id': 'call_BWjWX5xarWWuKOiPQmNG82Ej', 'type': 'tool_call'}
{'name': 'power-tool', 'args': {'value_a': 40, 'value_b': 2}, 'id': 'call_uu1Ia5MfNFbh2w5iGb5ZQxrY', 'type': 'tool_call'}
{'name': 'power-tool', 'args': {'value_a': 65, 'value_b': 2}, 'id': 'call_DGBIbHl9KDLlTMxDhgvB6Fdf', 'type': 'tool_call'}
{'name': 'subtract-tool', 'args': {'value_a': 90, 'value_b': 66.25}, 'id': 'call_UbY1E30kQDXWmc48ovQ3fjXK', 'type': 'tool_call'}
{'name': 'subtract-tool', 'args': {'value_a': 70, 'value_b': 66.25}, 'id': 'call_XlFnveK6nLAc3JIHvpsFwAXs', 'type': 'tool_call'}
{'name': 'subtract-tool', 'args': {'value_a': 40, 'value_b': 66.25}, 'id': 'call_1mRkTrvMUGCXl4UgGPQRkgow', 'type': 'tool_call'}
{'name': 'subtract-tool', 'args': {'value_a': 65, 'value_b': 66.25}, 'id': 'call_dEuETmJxVp1m6nO6x5AFM9Ys', 'type': 'tool_call'}
{'name': 'power-tool', 'args': {'value_a': 23.75, 'value_b': 2}, 'id': 'call_QjA2uGmw1G2OIXocsyzXSRSh', 'type': 'tool_call'}
{'name': 'power-tool', 'args': {'value_a': 3.75, 'value_b': 2}, 'id': 'call_uu1aFvVZTF9tMq9hY1QZRwXO', 'type': 'tool_call'}
{'name': 'power-tool', 'args': {'value_a': -26.25, 'value_b': 2}, 'id': 'call_K04FG2lyvDspWy2tDOXv6FCh', 'type': 'tool_call'}
{'name': 'power-tool', 'args': {'value_a': -1.25, 'value_b': 2}, 'id': 'call_uJTyRT48W5OyYoBEbB0IZKcy', 'type': 'tool_call'}
{'name': 'sum-values-in-list-tool', 'args': {'input_list': [564.0625, 14.0625, 689.0625, 1.5625]}, 'id': 'call_j1c3kCcm0hO7DVDsZCxJuyDF', 'type': 'tool_call'}
{'name': 'devision-tool', 'args': {'value_a': 1268.75, 'value_b': 4}, 'id': 'call_m9qRrACQ9NbAWbXMYp0PqktG', 'type': 'tool_call'}
{'name': 'square-root-tool', 'args': {'value_a': 317.1875}, 'id': 'call_kFmFZ0IC30Y0kWixeOQipLrP', 'type': 'tool_call'}
{'name': 'devision-tool', 'args': {'value_a': 23.75, 'value_b': 17.81}, 'id': 'call_SA68B7dCoOnzPpTDOo6Z3R0P', 'type': 'tool_call'}
{'name': 'devision-tool', 'args': {'value_a': 3.75, 'value_b': 17.81}, 'id': 'call_Jg9wPH5RJoVrYlPzeV5cmS33', 'type': 'tool_call'}
{'name': 'devision-tool', 'args': {'value_a': -26.25, 'value_b': 17.81}, 'id': 'call_BHjvXJIxXDsD4E5d5Fd9kRTP', 'type': 'tool_call'}
{'name': 'devision-tool', 'args': {'value_a': -1.25, 'value_b': 17.81}, 'id': 'call_a3iiIMz4PKQWVjpMMFwu5Kqm', 'type': 'tool_call'}
{'name': 'power-values-in-list-tool', 'args': {'input_list': [1.3335204941044358, 0.21055586749017408, -1.4738910724312184, -0.07018528916339135], 'exponent': 10}, 'id': 'call_EFOgXwkm9yQvcBtqzLQzyhv8', 'type': 'tool_call'}
{'name': 'multiply-tool', 'args': {'value_a': 10, 'value_b': 1.3335204941044358}, 'id': 'call_iFkX0qFYiwuDnbhSxNmgUIrC', 'type': 'tool_call'}
{'name': 'multiply-tool', 'args': {'value_a': 10, 'value_b': 0.21055586749017408}, 'id': 'call_v3rSSa2Q55l9xe6gE5BWMzog', 'type': 'tool_call'}
{'name': 'multiply-tool', 'args': {'value_a': 10, 'value_b': -1.4738910724312184}, 'id': 'call_35XdDzWi0TgOIgKmCW5y6qVe', 'type': 'tool_call'}
{'name': 'multiply-tool', 'args': {'value_a': 10, 'value_b': -0.07018528916339135}, 'id': 'call_QZ4zErW40EySDlrjfppkxciQ', 'type': 'tool_call'}
{'name': 'addition-tool', 'args': {'value_a': 50, 'value_b': 13.335204941044358}, 'id': 'call_Ftdwv4zk93MBTgAViOgKo96l', 'type': 'tool_call'}
{'name': 'addition-tool', 'args': {'value_a': 50, 'value_b': 2.105558674901741}, 'id': 'call_wsqmFHIMYHZ7zbeh9yZ7QqrE', 'type': 'tool_call'}
{'name': 'addition-tool', 'args': {'value_a': 50, 'value_b': -14.738910724312184}, 'id': 'call_5DZ3b4zvWq1QPaqDy5nxMmxX', 'type': 'tool_call'}
{'name': 'addition-tool', 'args': {'value_a': 50, 'value_b': -0.7018528916339135}, 'id': 'call_mmkH9wRbLlvYmZfU6aAFzShC', 'type': 'tool_call'}
response.content='各人の偏差値は以下の通りです:\n\n- A氏の偏差値: 約63.34\n- B氏の偏差値: 約52.11\n- C氏の偏差値: 約35.26\n- D氏の偏差値: 約49.30\n\n全員の偏差値を計算し、結果を提供しました。必要があれば、さらに解説をいたします。'
[{'content': '計算手順を考えたうえで、一つずつ正確に計算して結論を導きます。', 'role': 'system'},
 {'content': 'テストの結果が以下の通りになりました。偏差値の計算方法は以下の通りです。これを理解した上で、標準偏差を計算した後に全員の偏差値を計算してください。\n'
             '\n'
             '# 偏差値の計算方法\n'
             '\text{偏差値} = 50 + 10 \times \x0c'
             'rac{x - \\mu}{\\sigma}\n'
             '\n'
             '# テスト結果\n'
             'A氏 90点\n'
             'B氏 70点\n'
             'C氏 40点\n'
             'D氏 65点\n',
  'role': 'user'},
 {'content': '偏差値を計算する前に、まずは標準偏差(σ)を計算する必要があります。標準偏差は以下の手順で計算します:\n'
             '\n'
             '1. 各データ点の平均(μ)を計算する。\n'
             '2. 各データ点と平均の差を二乗する。\n'
             '3. それらの二乗の平均を計算する(分散)。\n'
             '4. 分散の平方根を取る(これが標準偏差σです)。\n'
             '\n'
             'まずは平均を計算します。\n'
             '\n'
             'テスト結果:\n'
             '- A氏: 90点\n'
             '- B氏: 70点\n'
             '- C氏: 40点\n'
             '- D氏: 65点\n'
             '\n'
             'それぞれの得点を使って計算を行います。まず、平均を求めます。計算を始めます。',
  'role': 'assistant',
  'tool_calls': [{'args': {'input_list': [90, 70, 40, 65]},
                  'id': 'call_g8rN8yzibvpCpxm46ZPpEKXl',
                  'name': 'sum-values-in-list-tool',
                  'type': 'tool_call'}]},
 {'content': '265.0',
  'role': 'tool',
  'tool_call_id': 'call_g8rN8yzibvpCpxm46ZPpEKXl'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 265, 'value_b': 4},
                  'id': 'call_Ftdwv4zk93MBTgAViOgKo96l',
                  'name': 'devision-tool',
                  'type': 'tool_call'}]},
 {'content': '66.25',
  'role': 'tool',
  'tool_call_id': 'call_Ftdwv4zk93MBTgAViOgKo96l'},
 {'content': '平均(μ)は66.25点です。\n'
             '\n'
             '次に、各データ点と平均の差を二乗します。\n'
             '\n'
             '- A氏: \\((90 - 66.25)^2\\)\n'
             '- B氏: \\((70 - 66.25)^2\\)\n'
             '- C氏: \\((40 - 66.25)^2\\)\n'
             '- D氏: \\((65 - 66.25)^2\\)\n'
             '\n'
             'この計算を行います。',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 90, 'value_b': 2},
                  'id': 'call_QCjSeHoxxDJElvik2oiEV8WM',
                  'name': 'power-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 70, 'value_b': 2},
                  'id': 'call_BWjWX5xarWWuKOiPQmNG82Ej',
                  'name': 'power-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 40, 'value_b': 2},
                  'id': 'call_uu1Ia5MfNFbh2w5iGb5ZQxrY',
                  'name': 'power-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 65, 'value_b': 2},
                  'id': 'call_DGBIbHl9KDLlTMxDhgvB6Fdf',
                  'name': 'power-tool',
                  'type': 'tool_call'}]},
 {'content': '8100',
  'role': 'tool',
  'tool_call_id': 'call_QCjSeHoxxDJElvik2oiEV8WM'},
 {'content': '4900',
  'role': 'tool',
  'tool_call_id': 'call_BWjWX5xarWWuKOiPQmNG82Ej'},
 {'content': '1600',
  'role': 'tool',
  'tool_call_id': 'call_uu1Ia5MfNFbh2w5iGb5ZQxrY'},
 {'content': '4225',
  'role': 'tool',
  'tool_call_id': 'call_DGBIbHl9KDLlTMxDhgvB6Fdf'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 90, 'value_b': 66.25},
                  'id': 'call_UbY1E30kQDXWmc48ovQ3fjXK',
                  'name': 'subtract-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 70, 'value_b': 66.25},
                  'id': 'call_XlFnveK6nLAc3JIHvpsFwAXs',
                  'name': 'subtract-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 40, 'value_b': 66.25},
                  'id': 'call_1mRkTrvMUGCXl4UgGPQRkgow',
                  'name': 'subtract-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 65, 'value_b': 66.25},
                  'id': 'call_dEuETmJxVp1m6nO6x5AFM9Ys',
                  'name': 'subtract-tool',
                  'type': 'tool_call'}]},
 {'content': '23.75',
  'role': 'tool',
  'tool_call_id': 'call_UbY1E30kQDXWmc48ovQ3fjXK'},
 {'content': '3.75',
  'role': 'tool',
  'tool_call_id': 'call_XlFnveK6nLAc3JIHvpsFwAXs'},
 {'content': '-26.25',
  'role': 'tool',
  'tool_call_id': 'call_1mRkTrvMUGCXl4UgGPQRkgow'},
 {'content': '-1.25',
  'role': 'tool',
  'tool_call_id': 'call_dEuETmJxVp1m6nO6x5AFM9Ys'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 23.75, 'value_b': 2},
                  'id': 'call_QjA2uGmw1G2OIXocsyzXSRSh',
                  'name': 'power-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 3.75, 'value_b': 2},
                  'id': 'call_uu1aFvVZTF9tMq9hY1QZRwXO',
                  'name': 'power-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': -26.25, 'value_b': 2},
                  'id': 'call_K04FG2lyvDspWy2tDOXv6FCh',
                  'name': 'power-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': -1.25, 'value_b': 2},
                  'id': 'call_uJTyRT48W5OyYoBEbB0IZKcy',
                  'name': 'power-tool',
                  'type': 'tool_call'}]},
 {'content': '564.0625',
  'role': 'tool',
  'tool_call_id': 'call_QjA2uGmw1G2OIXocsyzXSRSh'},
 {'content': '14.0625',
  'role': 'tool',
  'tool_call_id': 'call_uu1aFvVZTF9tMq9hY1QZRwXO'},
 {'content': '689.0625',
  'role': 'tool',
  'tool_call_id': 'call_K04FG2lyvDspWy2tDOXv6FCh'},
 {'content': '1.5625',
  'role': 'tool',
  'tool_call_id': 'call_uJTyRT48W5OyYoBEbB0IZKcy'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'input_list': [564.0625, 14.0625, 689.0625, 1.5625]},
                  'id': 'call_j1c3kCcm0hO7DVDsZCxJuyDF',
                  'name': 'sum-values-in-list-tool',
                  'type': 'tool_call'}]},
 {'content': '1268.75',
  'role': 'tool',
  'tool_call_id': 'call_j1c3kCcm0hO7DVDsZCxJuyDF'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 1268.75, 'value_b': 4},
                  'id': 'call_m9qRrACQ9NbAWbXMYp0PqktG',
                  'name': 'devision-tool',
                  'type': 'tool_call'}]},
 {'content': '317.1875',
  'role': 'tool',
  'tool_call_id': 'call_m9qRrACQ9NbAWbXMYp0PqktG'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 317.1875},
                  'id': 'call_kFmFZ0IC30Y0kWixeOQipLrP',
                  'name': 'square-root-tool',
                  'type': 'tool_call'}]},
 {'content': '17.80975856096876',
  'role': 'tool',
  'tool_call_id': 'call_kFmFZ0IC30Y0kWixeOQipLrP'},
 {'content': '標準偏差(σ)は約17.81です。\n'
             '\n'
             '次に、全員の偏差値を計算します。偏差値の計算式は以下の通りです:\n'
             '\\[\n'
             '\\text{偏差値} = 50 + 10 \\times \\frac{x - \\mu}{\\sigma}\n'
             '\\]\n'
             '各人の得点を用いて偏差値を計算します。\n'
             '\n'
             '- A氏の偏差値\n'
             '- B氏の偏差値\n'
             '- C氏の偏差値\n'
             '- D氏の偏差値\n'
             '\n'
             '計算を行います。',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 23.75, 'value_b': 17.81},
                  'id': 'call_SA68B7dCoOnzPpTDOo6Z3R0P',
                  'name': 'devision-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 3.75, 'value_b': 17.81},
                  'id': 'call_Jg9wPH5RJoVrYlPzeV5cmS33',
                  'name': 'devision-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': -26.25, 'value_b': 17.81},
                  'id': 'call_BHjvXJIxXDsD4E5d5Fd9kRTP',
                  'name': 'devision-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': -1.25, 'value_b': 17.81},
                  'id': 'call_a3iiIMz4PKQWVjpMMFwu5Kqm',
                  'name': 'devision-tool',
                  'type': 'tool_call'}]},
 {'content': '1.3335204941044358',
  'role': 'tool',
  'tool_call_id': 'call_SA68B7dCoOnzPpTDOo6Z3R0P'},
 {'content': '0.21055586749017408',
  'role': 'tool',
  'tool_call_id': 'call_Jg9wPH5RJoVrYlPzeV5cmS33'},
 {'content': '-1.4738910724312184',
  'role': 'tool',
  'tool_call_id': 'call_BHjvXJIxXDsD4E5d5Fd9kRTP'},
 {'content': '-0.07018528916339135',
  'role': 'tool',
  'tool_call_id': 'call_a3iiIMz4PKQWVjpMMFwu5Kqm'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'exponent': 10,
                           'input_list': [1.3335204941044358,
                                          0.21055586749017408,
                                          -1.4738910724312184,
                                          -0.07018528916339135]},
                  'id': 'call_EFOgXwkm9yQvcBtqzLQzyhv8',
                  'name': 'power-values-in-list-tool',
                  'type': 'tool_call'}]},
 {'content': '[17.782669008622005, 1.712669180428555e-07, 48.37866531961816, '
             '2.900420295734989e-12]',
  'role': 'tool',
  'tool_call_id': 'call_EFOgXwkm9yQvcBtqzLQzyhv8'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 10, 'value_b': 1.3335204941044358},
                  'id': 'call_iFkX0qFYiwuDnbhSxNmgUIrC',
                  'name': 'multiply-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 10, 'value_b': 0.21055586749017408},
                  'id': 'call_v3rSSa2Q55l9xe6gE5BWMzog',
                  'name': 'multiply-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 10, 'value_b': -1.4738910724312184},
                  'id': 'call_35XdDzWi0TgOIgKmCW5y6qVe',
                  'name': 'multiply-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 10, 'value_b': -0.07018528916339135},
                  'id': 'call_QZ4zErW40EySDlrjfppkxciQ',
                  'name': 'multiply-tool',
                  'type': 'tool_call'}]},
 {'content': '13.335204941044358',
  'role': 'tool',
  'tool_call_id': 'call_iFkX0qFYiwuDnbhSxNmgUIrC'},
 {'content': '2.105558674901741',
  'role': 'tool',
  'tool_call_id': 'call_v3rSSa2Q55l9xe6gE5BWMzog'},
 {'content': '-14.738910724312184',
  'role': 'tool',
  'tool_call_id': 'call_35XdDzWi0TgOIgKmCW5y6qVe'},
 {'content': '-0.7018528916339135',
  'role': 'tool',
  'tool_call_id': 'call_QZ4zErW40EySDlrjfppkxciQ'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 50, 'value_b': 13.335204941044358},
                  'id': 'call_Ftdwv4zk93MBTgAViOgKo96l',
                  'name': 'addition-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 50, 'value_b': 2.105558674901741},
                  'id': 'call_wsqmFHIMYHZ7zbeh9yZ7QqrE',
                  'name': 'addition-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 50, 'value_b': -14.738910724312184},
                  'id': 'call_5DZ3b4zvWq1QPaqDy5nxMmxX',
                  'name': 'addition-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 50, 'value_b': -0.7018528916339135},
                  'id': 'call_mmkH9wRbLlvYmZfU6aAFzShC',
                  'name': 'addition-tool',
                  'type': 'tool_call'}]},
 {'content': '63.33520494104436',
  'role': 'tool',
  'tool_call_id': 'call_Ftdwv4zk93MBTgAViOgKo96l'},
 {'content': '52.105558674901744',
  'role': 'tool',
  'tool_call_id': 'call_wsqmFHIMYHZ7zbeh9yZ7QqrE'},
 {'content': '35.261089275687816',
  'role': 'tool',
  'tool_call_id': 'call_5DZ3b4zvWq1QPaqDy5nxMmxX'},
 {'content': '49.29814710836609',
  'role': 'tool',
  'tool_call_id': 'call_mmkH9wRbLlvYmZfU6aAFzShC'},
 {'content': '各人の偏差値は以下の通りです:\n'
             '\n'
             '- A氏の偏差値: 約63.34\n'
             '- B氏の偏差値: 約52.11\n'
             '- C氏の偏差値: 約35.26\n'
             '- D氏の偏差値: 約49.30\n'
             '\n'
             '全員の偏差値を計算し、結果を提供しました。必要があれば、さらに解説をいたします。',
  'role': 'assistant',
  'tool_calls': []}]

6−4. 実験(計算式を考えさせてから計算させる)

じゃ、計算式を求めさせてから回答させればよくね?
やってみる。

計算方法の明確化
user_input = """標準偏差と偏差値の計算方法を教えてください。"""
messages2 = [{"role": "user", "content": user_input}]
messages2 = agent_run(messages2)
pprint(messages2)

# '標準偏差と偏差値の計算方法について説明します。\n'
#              '\n'
#              '### 標準偏差の計算方法\n'
#              '1. **データの平均を求める**:\n'
#              '   \\[\n'
#              '   \\text{平均} (\\mu) = \\frac{\\sum_{i=1}^{n} x_i}{n}\n'
#              '   \\]\n'
#              '   ここで、\\(x_i\\)はデータ点、\\(n\\)はデータの個数です。\n'
#              '\n'
#              '2. **各データ点の偏差を求める**:\n'
#              '   \\[\n'
#              '   \\text{偏差} = x_i - \\mu\n'
#              '   \\]\n'
#              '\n'
#              '3. **偏差の二乗を求める**:\n'
#              '   \\[\n'
#              '   \\text{偏差の二乗} = (x_i - \\mu)^2\n'
#              '   \\]\n'
#              '\n'
#              '4. **偏差の二乗の平均(分散)を求める**:\n'
#              '   \\[\n'
#              '   \\text{分散} (\\sigma^2) = \\frac{\\sum_{i=1}^{n} (x_i - '
#              '\\mu)^2}{n}\n'
#              '   \\]\n'
#              '\n'
#              '5. **標準偏差を求める**:\n'
#              '   \\[\n'
#              '   \\text{標準偏差} (\\sigma) = \\sqrt{\\sigma^2}\n'
#              '   \\]\n'
#              '\n'
#              '### 偏差値の計算方法\n'
#              '1. **データ点の標準偏差と平均を求める**(上記の手順に従い)。\n'
#              '   \n'
#              '2. **偏差値を求める**:\n'
#              '   偏差値は次の式で計算されます。\n'
#              '   \\[\n'
#              '   \\text{偏差値} = 50 + 10 \\times \\frac{(x - \\mu)}{\\sigma}\n'
#              '   \\]\n'
#              '   ここで、\\(x\\)は特定のデータ点、\\(\\mu\\)は平均、\\(\\sigma\\)は標準偏差です。\n'
#              '\n'
#              'この方法に従って計算すると、標準偏差と偏差値を求めることができます。もし具体的なデータを使って計算したい場合は、そのデータを教えていただければ、実際に計算します。',
 

ちょっと怪しいぞ。でもここで手を加えたらチートから脱出できない。

1

\text{平均} (\mu) = \frac{\sum_{i=1}^{n} x_i}{n}

2

\text{偏差} = x_i - \mu

3

\text{偏差の二乗} = (x_i - \mu)^2

4

\text{分散} (\sigma^2) = \frac{\sum_{i=1}^{n} (x_i - \mu)^2}{n}

5

\text{標準偏差} (\sigma) = \sqrt{\sigma^2}\

6

\text{偏差値} = 50 + 10 \times \frac{(x - \mu)}{\sigma}

計算手順を示すなら、分散はSにして、Sのルートが標準偏差ととするか、
標準偏差を求める式を4で示せばよかったかもね。

でも、チートは禁止だ。このままいく。



いざ計算
user_input = """テストの結果が以下の通りになりました。標準偏差と全員の偏差値を計算してください。

# テスト結果
A氏 90点
B氏 70点
C氏 40点
D氏 65点
"""
messages2.append({"role": "user", "content": user_input})
messages2 = agent_run(messages2)
pprint(messages2)

# '### 結果まとめ\n'
# '- **標準偏差**: 約 8.14\n'
# '- **偏差値**:\n'
# '  - A氏: 約 63.64\n'
# '  - B氏: 約 53.54\n'
# '  - C氏: 約 31.82\n'
# '  -  D氏: 約 49.39\n'

あ、ダメじゃん・・・

全出力
{'name': 'sum-values-in-list-tool', 'args': {'input_list': [90, 70, 40, 65]}, 'id': 'call_DxerISmpr4I6KgFpCTufRC8Q', 'type': 'tool_call'}
{'name': 'power-values-in-list-tool', 'args': {'input_list': [90, 70, 40, 65], 'exponent': 2}, 'id': 'call_9jYpch6RDh27iJljdglV1Wqn', 'type': 'tool_call'}
{'name': 'devision-tool', 'args': {'value_a': 265, 'value_b': 4}, 'id': 'call_uby5OdRhAuZOJZ0kgpxWFIHp', 'type': 'tool_call'}
{'name': 'sum-values-in-list-tool', 'args': {'input_list': [8100, 4900, 1600, 4225]}, 'id': 'call_zq1cCl3st97aEQmJFTAQtPvx', 'type': 'tool_call'}
{'name': 'square-root-tool', 'args': {'value_a': 66.25}, 'id': 'call_LHvz6j4C7UdhVaQYv8BLbRCx', 'type': 'tool_call'}
{'name': 'devision-tool', 'args': {'value_a': 18825, 'value_b': 4}, 'id': 'call_1BK6qivRoj65mxNiiompnbDO', 'type': 'tool_call'}
{'name': 'power-tool', 'args': {'value_a': 8.139410298049853, 'value_b': -1}, 'id': 'call_HvW5bzrIBD46vJFaiQVAC51d', 'type': 'tool_call'}
{'name': 'addition-tool', 'args': {'value_a': 4706.25, 'value_b': -90}, 'id': 'call_XJ1ws4HVnpqIlY4NOGaYlYSA', 'type': 'tool_call'}
{'name': 'addition-tool', 'args': {'value_a': 4706.25, 'value_b': -70}, 'id': 'call_QBuoprlCqqrZl50WfZOGoIUz', 'type': 'tool_call'}
{'name': 'addition-tool', 'args': {'value_a': 4706.25, 'value_b': -40}, 'id': 'call_i11rrEzwiIOpu83BK1kcc7Ug', 'type': 'tool_call'}
{'name': 'addition-tool', 'args': {'value_a': 4706.25, 'value_b': -65}, 'id': 'call_s7q5T6e6PAAlWtFJNpZ1GoTI', 'type': 'tool_call'}
response.content='テスト結果に基づいて、以下のように標準偏差と偏差値を計算しました。\n\n### 標準偏差\n1. **平均**:\n   \\[\n   \\mu = 66.25\n   \\]\n2. **偏差の二乗の平均 (分散)**:\n   \\[\n   \\sigma^2 = 66.25 \\quad (\\text{計算済み})\n   \\]\n3. **標準偏差**:\n   \\[\n   \\sigma \\approx 8.14\n   \\]\n\n### 偏差値\n偏差値は以下の式によって計算されます。\n\\[\n\\text{偏差値} = 50 + 10 \\times \\frac{(x - \\mu)}{\\sigma}\n\\]\n\n各人の偏差値を計算しました:\n\n- A氏 (90点):\n  \\[\n  \\text{偏差値} \\approx 50 + 10 \\times \\frac{(90 - 66.25)}{8.14} \\approx 63.64\n  \\]\n  \n- B氏 (70点):\n  \\[\n  \\text{偏差値} \\approx 50 + 10 \\times \\frac{(70 - 66.25)}{8.14} \\approx 53.54\n  \\]\n  \n- C氏 (40点):\n  \\[\n  \\text{偏差値} \\approx 50 + 10 \\times \\frac{(40 - 66.25)}{8.14} \\approx 31.82\n  \\]\n  \n- D氏 (65点):\n  \\[\n  \\text{偏差値} \\approx 50 + 10 \\times \\frac{(65 - 66.25)}{8.14} \\approx 49.39\n  \\]\n\n### 結果まとめ\n- **標準偏差**: 約 8.14\n- **偏差値**:\n  - A氏: 約 63.64\n  - B氏: 約 53.54\n  - C氏: 約 31.82\n  - D氏: 約 49.39\n\n以上が計算結果です。何か他に知りたいことがあれば教えてください!'
[{'content': '標準偏差と偏差値の計算方法を教えてください。', 'role': 'user'},
 {'content': '標準偏差と偏差値の計算方法について説明します。\n'
             '\n'
             '### 標準偏差の計算方法\n'
             '1. **データの平均を求める**:\n'
             '   \\[\n'
             '   \\text{平均} (\\mu) = \\frac{\\sum_{i=1}^{n} x_i}{n}\n'
             '   \\]\n'
             '   ここで、\\(x_i\\)はデータ点、\\(n\\)はデータの個数です。\n'
             '\n'
             '2. **各データ点の偏差を求める**:\n'
             '   \\[\n'
             '   \\text{偏差} = x_i - \\mu\n'
             '   \\]\n'
             '\n'
             '3. **偏差の二乗を求める**:\n'
             '   \\[\n'
             '   \\text{偏差の二乗} = (x_i - \\mu)^2\n'
             '   \\]\n'
             '\n'
             '4. **偏差の二乗の平均(分散)を求める**:\n'
             '   \\[\n'
             '   \\text{分散} (\\sigma^2) = \\frac{\\sum_{i=1}^{n} (x_i - '
             '\\mu)^2}{n}\n'
             '   \\]\n'
             '\n'
             '5. **標準偏差を求める**:\n'
             '   \\[\n'
             '   \\text{標準偏差} (\\sigma) = \\sqrt{\\sigma^2}\n'
             '   \\]\n'
             '\n'
             '### 偏差値の計算方法\n'
             '1. **データ点の標準偏差と平均を求める**(上記の手順に従い)。\n'
             '   \n'
             '2. **偏差値を求める**:\n'
             '   偏差値は次の式で計算されます。\n'
             '   \\[\n'
             '   \\text{偏差値} = 50 + 10 \\times \\frac{(x - \\mu)}{\\sigma}\n'
             '   \\]\n'
             '   ここで、\\(x\\)は特定のデータ点、\\(\\mu\\)は平均、\\(\\sigma\\)は標準偏差です。\n'
             '\n'
             'この方法に従って計算すると、標準偏差と偏差値を求めることができます。もし具体的なデータを使って計算したい場合は、そのデータを教えていただければ、実際に計算します。',
  'role': 'assistant',
  'tool_calls': []},
 {'content': 'テストの結果が以下の通りになりました。標準偏差と全員の偏差値を計算してください。\n'
             '\n'
             '# テスト結果\n'
             'A氏 90点\n'
             'B氏 70点\n'
             'C氏 40点\n'
             'D氏 65点\n',
  'role': 'user'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'input_list': [90, 70, 40, 65]},
                  'id': 'call_DxerISmpr4I6KgFpCTufRC8Q',
                  'name': 'sum-values-in-list-tool',
                  'type': 'tool_call'},
                 {'args': {'exponent': 2, 'input_list': [90, 70, 40, 65]},
                  'id': 'call_9jYpch6RDh27iJljdglV1Wqn',
                  'name': 'power-values-in-list-tool',
                  'type': 'tool_call'}]},
 {'content': '265.0',
  'role': 'tool',
  'tool_call_id': 'call_DxerISmpr4I6KgFpCTufRC8Q'},
 {'content': '[8100.0, 4900.0, 1600.0, 4225.0]',
  'role': 'tool',
  'tool_call_id': 'call_9jYpch6RDh27iJljdglV1Wqn'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 265, 'value_b': 4},
                  'id': 'call_uby5OdRhAuZOJZ0kgpxWFIHp',
                  'name': 'devision-tool',
                  'type': 'tool_call'},
                 {'args': {'input_list': [8100, 4900, 1600, 4225]},
                  'id': 'call_zq1cCl3st97aEQmJFTAQtPvx',
                  'name': 'sum-values-in-list-tool',
                  'type': 'tool_call'}]},
 {'content': '66.25',
  'role': 'tool',
  'tool_call_id': 'call_uby5OdRhAuZOJZ0kgpxWFIHp'},
 {'content': '18825.0',
  'role': 'tool',
  'tool_call_id': 'call_zq1cCl3st97aEQmJFTAQtPvx'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 66.25},
                  'id': 'call_LHvz6j4C7UdhVaQYv8BLbRCx',
                  'name': 'square-root-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 18825, 'value_b': 4},
                  'id': 'call_1BK6qivRoj65mxNiiompnbDO',
                  'name': 'devision-tool',
                  'type': 'tool_call'}]},
 {'content': '8.139410298049853',
  'role': 'tool',
  'tool_call_id': 'call_LHvz6j4C7UdhVaQYv8BLbRCx'},
 {'content': '4706.25',
  'role': 'tool',
  'tool_call_id': 'call_1BK6qivRoj65mxNiiompnbDO'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 8.139410298049853, 'value_b': -1},
                  'id': 'call_HvW5bzrIBD46vJFaiQVAC51d',
                  'name': 'power-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 4706.25, 'value_b': -90},
                  'id': 'call_XJ1ws4HVnpqIlY4NOGaYlYSA',
                  'name': 'addition-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 4706.25, 'value_b': -70},
                  'id': 'call_QBuoprlCqqrZl50WfZOGoIUz',
                  'name': 'addition-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 4706.25, 'value_b': -40},
                  'id': 'call_i11rrEzwiIOpu83BK1kcc7Ug',
                  'name': 'addition-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 4706.25, 'value_b': -65},
                  'id': 'call_s7q5T6e6PAAlWtFJNpZ1GoTI',
                  'name': 'addition-tool',
                  'type': 'tool_call'}]},
 {'content': '0.12285902336679023',
  'role': 'tool',
  'tool_call_id': 'call_HvW5bzrIBD46vJFaiQVAC51d'},
 {'content': '4616.25',
  'role': 'tool',
  'tool_call_id': 'call_XJ1ws4HVnpqIlY4NOGaYlYSA'},
 {'content': '4636.25',
  'role': 'tool',
  'tool_call_id': 'call_QBuoprlCqqrZl50WfZOGoIUz'},
 {'content': '4666.25',
  'role': 'tool',
  'tool_call_id': 'call_i11rrEzwiIOpu83BK1kcc7Ug'},
 {'content': '4641.25',
  'role': 'tool',
  'tool_call_id': 'call_s7q5T6e6PAAlWtFJNpZ1GoTI'},
 {'content': 'テスト結果に基づいて、以下のように標準偏差と偏差値を計算しました。\n'
             '\n'
             '### 標準偏差\n'
             '1. **平均**:\n'
             '   \\[\n'
             '   \\mu = 66.25\n'
             '   \\]\n'
             '2. **偏差の二乗の平均 (分散)**:\n'
             '   \\[\n'
             '   \\sigma^2 = 66.25 \\quad (\\text{計算済み})\n'
             '   \\]\n'
             '3. **標準偏差**:\n'
             '   \\[\n'
             '   \\sigma \\approx 8.14\n'
             '   \\]\n'
             '\n'
             '### 偏差値\n'
             '偏差値は以下の式によって計算されます。\n'
             '\\[\n'
             '\\text{偏差値} = 50 + 10 \\times \\frac{(x - \\mu)}{\\sigma}\n'
             '\\]\n'
             '\n'
             '各人の偏差値を計算しました:\n'
             '\n'
             '- A氏 (90点):\n'
             '  \\[\n'
             '  \\text{偏差値} \\approx 50 + 10 \\times \\frac{(90 - '
             '66.25)}{8.14} \\approx 63.64\n'
             '  \\]\n'
             '  \n'
             '- B氏 (70点):\n'
             '  \\[\n'
             '  \\text{偏差値} \\approx 50 + 10 \\times \\frac{(70 - '
             '66.25)}{8.14} \\approx 53.54\n'
             '  \\]\n'
             '  \n'
             '- C氏 (40点):\n'
             '  \\[\n'
             '  \\text{偏差値} \\approx 50 + 10 \\times \\frac{(40 - '
             '66.25)}{8.14} \\approx 31.82\n'
             '  \\]\n'
             '  \n'
             '- D氏 (65点):\n'
             '  \\[\n'
             '  \\text{偏差値} \\approx 50 + 10 \\times \\frac{(65 - '
             '66.25)}{8.14} \\approx 49.39\n'
             '  \\]\n'
             '\n'
             '### 結果まとめ\n'
             '- **標準偏差**: 約 8.14\n'
             '- **偏差値**:\n'
             '  - A氏: 約 63.64\n'
             '  - B氏: 約 53.54\n'
             '  - C氏: 約 31.82\n'
             '  - D氏: 約 49.39\n'
             '\n'
             '以上が計算結果です。何か他に知りたいことがあれば教えてください!',
  'role': 'assistant',
  'tool_calls': []}]

7. ところで・・・

きっと気づいた人がいますよね。
GPT-4o-miniやん。GPT-4oはどうやねんと・・・

では、GPT-4oの出力をどうぞ。

GPT-4o出力
{'name': 'sum-values-in-list-tool', 'args': {'input_list': [90, 70, 40, 65]}, 'id': 'call_PRJ2DIfVjiCH0eHw8absReya', 'type': 'tool_call'}
{'name': 'power-values-in-list-tool', 'args': {'input_list': [23.75, 3.75, -26.25, -1.25], 'exponent': 2}, 'id': 'call_dQJB8nXFYH19bHo1ZXLxxLhG', 'type': 'tool_call'}
{'name': 'sum-values-in-list-tool', 'args': {'input_list': [564.0625, 14.0625, 689.0625, 1.5625]}, 'id': 'call_1QQkidgimKHdrVX3YiVGz0jC', 'type': 'tool_call'}
{'name': 'square-root-tool', 'args': {'value_a': 317.1875}, 'id': 'call_qxXmXH9DCIvmrA2mgidVQ5Pd', 'type': 'tool_call'}
{'name': 'subtract-tool', 'args': {'value_a': 90, 'value_b': 66.25}, 'id': 'call_LufrVC023zti39XT32Mf4QUH', 'type': 'tool_call'}
{'name': 'subtract-tool', 'args': {'value_a': 70, 'value_b': 66.25}, 'id': 'call_d6MmILFYpjbTBS6k2C3mF8us', 'type': 'tool_call'}
{'name': 'subtract-tool', 'args': {'value_a': 40, 'value_b': 66.25}, 'id': 'call_HXc8Xqv5Vp2QE6q0enFbbKhw', 'type': 'tool_call'}
{'name': 'subtract-tool', 'args': {'value_a': 65, 'value_b': 66.25}, 'id': 'call_ViXpu62cowcdVgYm5FYgah7Y', 'type': 'tool_call'}
{'name': 'devision-tool', 'args': {'value_a': 23.75, 'value_b': 17.81}, 'id': 'call_JVSvrUCocpa6KaRlNESOzLCr', 'type': 'tool_call'}
{'name': 'devision-tool', 'args': {'value_a': 3.75, 'value_b': 17.81}, 'id': 'call_hubiQlHFRAUShIaGIR0xjctU', 'type': 'tool_call'}
{'name': 'devision-tool', 'args': {'value_a': -26.25, 'value_b': 17.81}, 'id': 'call_GkqpxBOLx1Uz3RuIkQxTmCz2', 'type': 'tool_call'}
{'name': 'devision-tool', 'args': {'value_a': -1.25, 'value_b': 17.81}, 'id': 'call_hRKXplhy2Q6osyUqDnuRdUHN', 'type': 'tool_call'}
{'name': 'multiply-tool', 'args': {'value_a': 1.3335204941044358, 'value_b': 10}, 'id': 'call_qswwjIjUQiwgvSsNFL12QwuE', 'type': 'tool_call'}
{'name': 'multiply-tool', 'args': {'value_a': 0.21055586749017408, 'value_b': 10}, 'id': 'call_jIJvrBbRZMFuf2G5ESIRWJnV', 'type': 'tool_call'}
{'name': 'multiply-tool', 'args': {'value_a': -1.4738910724312184, 'value_b': 10}, 'id': 'call_9uaOhKZyadNe0xDYOqQnQY1t', 'type': 'tool_call'}
{'name': 'multiply-tool', 'args': {'value_a': -0.07018528916339135, 'value_b': 10}, 'id': 'call_nIWHsMPKf91YuXPLy1HuiZHU', 'type': 'tool_call'}
{'name': 'addition-tool', 'args': {'value_a': 13.335204941044358, 'value_b': 50}, 'id': 'call_GoXGi0tQkM7k5Za1HYwaeHsN', 'type': 'tool_call'}
{'name': 'addition-tool', 'args': {'value_a': 2.105558674901741, 'value_b': 50}, 'id': 'call_tIIPQ7KMsorCJ2g2KA9thWrG', 'type': 'tool_call'}
{'name': 'addition-tool', 'args': {'value_a': -14.738910724312184, 'value_b': 50}, 'id': 'call_ElUDamjw9CXmVbzwD8MJ5ruz', 'type': 'tool_call'}
{'name': 'addition-tool', 'args': {'value_a': -0.7018528916339135, 'value_b': 50}, 'id': 'call_dMkpUa1NeGqJKaAyNKiwVp3f', 'type': 'tool_call'}
response.content='各人の偏差値は次の通りです:\n\n1. A氏: 63.34\n2. B氏: 52.11\n3. C氏: 35.26\n4. D氏: 49.30\n\nとなります。'
[{'content': '計算手順を考えたうえで、一つずつ正確に計算して結論を導きます。', 'role': 'system'},
 {'content': 'テストの結果が以下の通りになりました。標準偏差を計算した後に全員の偏差値を計算してください。\n'
             '\n'
             '# テスト結果\n'
             'A氏 90点\n'
             'B氏 70点\n'
             'C氏 40点\n'
             'D氏 65点\n',
  'role': 'user'},
 {'content': 'まず、標準偏差を計算する必要があります。そのために最初に平均点を計算し、その後に各点数についての分散を計算します。最終的に標準偏差を求めるために分散の平方根を取ります。\n'
             '\n'
             '次に偏差値を計算します。偏差値は次の公式を使って計算されます:\n'
             '\\[ \\text{偏差値} = \\frac{(x - 平均)}{\\text{標準偏差}} \\times 10 + 50 '
             '\\]\n'
             '\n'
             'ステップとしては次の通りです:\n'
             '\n'
             '1. 平均点を計算する。\n'
             '2. 各点数についての分散を計算する。\n'
             '3. 分散の平均を取る。\n'
             '4. 標準偏差を計算する(分散の平方根を取る)。\n'
             '5. 各点数について偏差値を計算する。\n'
             '\n'
             'これらのステップを次々に進めます。まずは、各点数のリストから平均点を計算します。次に進みます。',
  'role': 'assistant',
  'tool_calls': [{'args': {'input_list': [90, 70, 40, 65]},
                  'id': 'call_PRJ2DIfVjiCH0eHw8absReya',
                  'name': 'sum-values-in-list-tool',
                  'type': 'tool_call'}]},
 {'content': '265.0',
  'role': 'tool',
  'tool_call_id': 'call_PRJ2DIfVjiCH0eHw8absReya'},
 {'content': '合計点は265点です。次に、平均点を計算します。\n'
             '\n'
             '平均点 = 合計点 / 人数\n'
             '         = 265 / 4 \n'
             '         = 66.25\n'
             '\n'
             '次に、標準偏差を計算するために各点数についての分散を計算します。具体的には、各点数から平均点を引き、その結果を二乗します。\n'
             '\n'
             'リストから平均点を引いた結果:\n'
             'A氏: (90 - 66.25) = 23.75\n'
             'B氏: (70 - 66.25) = 3.75\n'
             'C氏: (40 - 66.25) = -26.25\n'
             'D氏: (65 - 66.25) = -1.25\n'
             '\n'
             'これらの値を二乗します。',
  'role': 'assistant',
  'tool_calls': [{'args': {'exponent': 2,
                           'input_list': [23.75, 3.75, -26.25, -1.25]},
                  'id': 'call_dQJB8nXFYH19bHo1ZXLxxLhG',
                  'name': 'power-values-in-list-tool',
                  'type': 'tool_call'}]},
 {'content': '[564.0625, 14.0625, 689.0625, 1.5625]',
  'role': 'tool',
  'tool_call_id': 'call_dQJB8nXFYH19bHo1ZXLxxLhG'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'input_list': [564.0625, 14.0625, 689.0625, 1.5625]},
                  'id': 'call_1QQkidgimKHdrVX3YiVGz0jC',
                  'name': 'sum-values-in-list-tool',
                  'type': 'tool_call'}]},
 {'content': '1268.75',
  'role': 'tool',
  'tool_call_id': 'call_1QQkidgimKHdrVX3YiVGz0jC'},
 {'content': '次に、各点数の分散を二乗した結果の平均を取ります。これは分散の合計を点数の人数で割った値です。\n'
             '\n'
             '分散の平均 = 分散の合計 / 人数\n'
             '           = 1268.75 / 4\n'
             '           = 317.1875\n'
             '\n'
             '次に、標準偏差を計算するために分散の平方根を取ります。',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 317.1875},
                  'id': 'call_qxXmXH9DCIvmrA2mgidVQ5Pd',
                  'name': 'square-root-tool',
                  'type': 'tool_call'}]},
 {'content': '17.80975856096876',
  'role': 'tool',
  'tool_call_id': 'call_qxXmXH9DCIvmrA2mgidVQ5Pd'},
 {'content': '標準偏差は 17.81 です(小数点以下2桁で表記)。\n'
             '\n'
             '次に、各点数の偏差値を計算します。\n'
             '\n'
             '偏差値の公式:\n'
             '\\[ \\text{偏差値} = \\frac{(x - 平均)}{\\text{標準偏差}} \\times 10 + 50 '
             '\\]\n'
             '\n'
             '以下の4人について偏差値を計算します:\n'
             '1. A氏: 90点\n'
             '2. B氏: 70点\n'
             '3. C氏: 40点\n'
             '4. D氏: 65点\n'
             '\n'
             '順次計算を行います。',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 90, 'value_b': 66.25},
                  'id': 'call_LufrVC023zti39XT32Mf4QUH',
                  'name': 'subtract-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 70, 'value_b': 66.25},
                  'id': 'call_d6MmILFYpjbTBS6k2C3mF8us',
                  'name': 'subtract-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 40, 'value_b': 66.25},
                  'id': 'call_HXc8Xqv5Vp2QE6q0enFbbKhw',
                  'name': 'subtract-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 65, 'value_b': 66.25},
                  'id': 'call_ViXpu62cowcdVgYm5FYgah7Y',
                  'name': 'subtract-tool',
                  'type': 'tool_call'}]},
 {'content': '23.75',
  'role': 'tool',
  'tool_call_id': 'call_LufrVC023zti39XT32Mf4QUH'},
 {'content': '3.75',
  'role': 'tool',
  'tool_call_id': 'call_d6MmILFYpjbTBS6k2C3mF8us'},
 {'content': '-26.25',
  'role': 'tool',
  'tool_call_id': 'call_HXc8Xqv5Vp2QE6q0enFbbKhw'},
 {'content': '-1.25',
  'role': 'tool',
  'tool_call_id': 'call_ViXpu62cowcdVgYm5FYgah7Y'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 23.75, 'value_b': 17.81},
                  'id': 'call_JVSvrUCocpa6KaRlNESOzLCr',
                  'name': 'devision-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 3.75, 'value_b': 17.81},
                  'id': 'call_hubiQlHFRAUShIaGIR0xjctU',
                  'name': 'devision-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': -26.25, 'value_b': 17.81},
                  'id': 'call_GkqpxBOLx1Uz3RuIkQxTmCz2',
                  'name': 'devision-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': -1.25, 'value_b': 17.81},
                  'id': 'call_hRKXplhy2Q6osyUqDnuRdUHN',
                  'name': 'devision-tool',
                  'type': 'tool_call'}]},
 {'content': '1.3335204941044358',
  'role': 'tool',
  'tool_call_id': 'call_JVSvrUCocpa6KaRlNESOzLCr'},
 {'content': '0.21055586749017408',
  'role': 'tool',
  'tool_call_id': 'call_hubiQlHFRAUShIaGIR0xjctU'},
 {'content': '-1.4738910724312184',
  'role': 'tool',
  'tool_call_id': 'call_GkqpxBOLx1Uz3RuIkQxTmCz2'},
 {'content': '-0.07018528916339135',
  'role': 'tool',
  'tool_call_id': 'call_hRKXplhy2Q6osyUqDnuRdUHN'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 1.3335204941044358, 'value_b': 10},
                  'id': 'call_qswwjIjUQiwgvSsNFL12QwuE',
                  'name': 'multiply-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 0.21055586749017408, 'value_b': 10},
                  'id': 'call_jIJvrBbRZMFuf2G5ESIRWJnV',
                  'name': 'multiply-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': -1.4738910724312184, 'value_b': 10},
                  'id': 'call_9uaOhKZyadNe0xDYOqQnQY1t',
                  'name': 'multiply-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': -0.07018528916339135, 'value_b': 10},
                  'id': 'call_nIWHsMPKf91YuXPLy1HuiZHU',
                  'name': 'multiply-tool',
                  'type': 'tool_call'}]},
 {'content': '13.335204941044358',
  'role': 'tool',
  'tool_call_id': 'call_qswwjIjUQiwgvSsNFL12QwuE'},
 {'content': '2.105558674901741',
  'role': 'tool',
  'tool_call_id': 'call_jIJvrBbRZMFuf2G5ESIRWJnV'},
 {'content': '-14.738910724312184',
  'role': 'tool',
  'tool_call_id': 'call_9uaOhKZyadNe0xDYOqQnQY1t'},
 {'content': '-0.7018528916339135',
  'role': 'tool',
  'tool_call_id': 'call_nIWHsMPKf91YuXPLy1HuiZHU'},
 {'content': '',
  'role': 'assistant',
  'tool_calls': [{'args': {'value_a': 13.335204941044358, 'value_b': 50},
                  'id': 'call_GoXGi0tQkM7k5Za1HYwaeHsN',
                  'name': 'addition-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': 2.105558674901741, 'value_b': 50},
                  'id': 'call_tIIPQ7KMsorCJ2g2KA9thWrG',
                  'name': 'addition-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': -14.738910724312184, 'value_b': 50},
                  'id': 'call_ElUDamjw9CXmVbzwD8MJ5ruz',
                  'name': 'addition-tool',
                  'type': 'tool_call'},
                 {'args': {'value_a': -0.7018528916339135, 'value_b': 50},
                  'id': 'call_dMkpUa1NeGqJKaAyNKiwVp3f',
                  'name': 'addition-tool',
                  'type': 'tool_call'}]},
 {'content': '63.33520494104436',
  'role': 'tool',
  'tool_call_id': 'call_GoXGi0tQkM7k5Za1HYwaeHsN'},
 {'content': '52.105558674901744',
  'role': 'tool',
  'tool_call_id': 'call_tIIPQ7KMsorCJ2g2KA9thWrG'},
 {'content': '35.261089275687816',
  'role': 'tool',
  'tool_call_id': 'call_ElUDamjw9CXmVbzwD8MJ5ruz'},
 {'content': '49.29814710836609',
  'role': 'tool',
  'tool_call_id': 'call_dMkpUa1NeGqJKaAyNKiwVp3f'},
 {'content': '各人の偏差値は次の通りです:\n'
             '\n'
             '1. A氏: 63.34\n'
             '2. B氏: 52.11\n'
             '3. C氏: 35.26\n'
             '4. D氏: 49.30\n'
             '\n'
             'となります。',
  'role': 'assistant',
  'tool_calls': []}]

いやもうね、さすがです。API料金をケチってはいけない所ですね。💦



8. 終わりに

オリジナルで計算ツールを作って偏差値の計算をさせてみました。

GPT-4o-miniを用いた場合、計算式を与えれば、toolsを使って正確に偏差値を計算できました。しk、計算式を与えなかった場合は標準偏差までは計算できましたが、偏差値の計算はミスをしました。
なかなか複雑な計算をさせるのは難しいです。
いGPT-4oでは計算式を与えなくてもtoolsを活用してきちんと回答できました。
ぜひ、おためしください。

ではまた

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?