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?

仕事でAzure OpenAIのGpt-4oモデルに単純計算をさせる場面があって、単純計算にも関わらず正しく解答させられなくて困った時がありました↓。
スクリーンショット 2025-06-28 22.23.49.png
上記の画像のように、いくつかの数を足し合わせているだけなのですが、正解は228のところを218と回答してしまっています。
ちなみに実装は以下のようになっています。

from dotenv import load_dotenv
from langchain_openai import AzureChatOpenAI
from azure.identity import DefaultAzureCredential

credential = DefaultAzureCredential()

llm = AzureChatOpenAI(
    azure_deployment="gpt-4o",  # or your deployment
    api_version="2024-10-01-preview",  # or your api version
    temperature=0,
    max_tokens=None,
    timeout=None,
    max_retries=2
)

messages = [
    (
        "system",
        "あなたは優秀な計算機です。",
    ),
    ("human", "14 + 14 + 16 + 26 + 15 + 19 + 18 + 24 + 18 + 18 + 23 + 23 の答えは?"),
]
ai_msg = llm.invoke(messages)

ai_msg.content

プロンプトの部分を色々変えてみたりしたのですが、それでも正しく回答は得られませんでした。
ところがChatGPTで同じくgpt-4oモデルを使うとどうなるか検証してみたところ、以下のようにバッチリ正しく回答ができました。
スクリーンショット 2025-06-28 23.43.10.png
(ハイパーパラメータやカットオフのタイミングは異なるかもしれないですが)同じgpt-4oモデルでもAPI呼び出しとwebサービスのChatGPTで差が出るのは不思議に思い色々考えてみました。

結論から言って、おそらくChatGPTの方のgpt-4oはエージェントとして計算ツールを使って動いているのではないかなと考えてます。
実際に、単純な足し算を行う関数をツールとして利用するgpt-4oのエージェントを実装してみたところ、以下のように正しく回答ができました。
スクリーンショット 2025-06-29 0.15.22.png
ちなみに実装は以下のように行ってます。

from dotenv import load_dotenv
from typing import List
from langchain_openai import AzureChatOpenAI
from langchain.agents import create_openai_functions_agent, AgentExecutor
from langchain.tools import StructuredTool
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
from azure.identity import DefaultAzureCredential

credential = DefaultAzureCredential()

llm = AzureChatOpenAI(
    azure_deployment="gpt-4o",  # or your deployment
    api_version="2024-10-01-preview",  # or your api version
    temperature=0,
    max_tokens=None,
    timeout=None,
    max_retries=2
)

def sum_numbers(numbers: List[float]) -> float:
    """
    受け取った数値リストの合計を返します。
    """
    return float(sum(numbers))

sum_tool = StructuredTool.from_function(
    func=sum_numbers,
    name="sum_numbers",
    description=(
        "数値リストの合計を計算します。"
        "引数 numbers は float 型の数値の配列です。"
    ),
)

tools=[sum_tool]
custom_prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "あなたは優秀な計算機です。"),
        ("user", "{input}"),                        # ← invoke 時に渡す `input`
        MessagesPlaceholder("agent_scratchpad"),    # ← 中間思考用
    ]
)
agent = create_openai_functions_agent(llm, tools, custom_prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

question = "14 + 14 + 16 + 26 + 15 + 19 + 18 + 24 + 18 + 18 + 23 + 23 の答えは?"
result = agent_executor.invoke({"input": question})
print(result["output"])   # => 228

Gpt-4oはそれ単体で相当賢いとは思うのですが、ChatGPTの方の工夫でさらに賢く見せることができてるんだろうなと思いました。
しかしこれからモデルがどんどん新しくなっていったら、そんな工夫も要らなくなるのだろうか...?

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?