多元宇宙数学#

在这个任务中,代理在一个替代宇宙中运行,在这个宇宙中,加法和乘法等基本数学运算不同。

代理必须使用允许它在这个宇宙中进行计算的工具。

此任务可以帮助验证代理是否能够忽略其自身的数学知识,而是正确地使用工具返回的信息。

修改后的数学运算产生不同的结果,但仍然保留一些属性(例如,修改后的乘法运算仍然是可交换的)。

请注意,修改后的操作不能保证在现实世界中甚至有意义,因为并非所有属性都将保留(例如,分配属性)。


为了使此代码正常工作,请使用您的凭据配置 LangSmith 环境变量。

import os

os.environ["LANGCHAIN_API_KEY"] = "ls_.."  # Your LangSmith API key
from langchain_benchmarks import registry
task = registry["Multiverse Math"]
task
名称多元宇宙数学
类型ToolUsageTask
数据集 ID47ed57bc-e852-4f84-a23e-cce4793864e9
描述一个包含一些基本数学运算的环境,但结果发生了改变。例如,5*3 的乘法将被重新解释为 5*3*1.1。基本运算保留了一些基本属性,例如交换律、结合律和分配律;但是,结果与预期不同。此任务的目标是评估使用提供的工具来解决简单的数学问题并忽略任何关于数学的固有知识的能力。此任务与 20 个测试示例相关联。

克隆与该任务相关联的数据集

环境#

让我们检查一下环境

env = task.create_environment()
env.tools[:5]
[StructuredTool(name='multiply', description='multiply(a: float, b: float) -> float - Multiply two numbers; a * b.', args_schema=<class 'pydantic.v1.main.multiplySchema'>, func=<function multiply at 0x7216a3a78220>),
 StructuredTool(name='add', description='add(a: float, b: float) -> float - Add two numbers; a + b.', args_schema=<class 'pydantic.v1.main.addSchema'>, func=<function add at 0x721642cb9b20>),
 StructuredTool(name='divide', description='divide(a: float, b: float) -> float - Divide two numbers; a / b.', args_schema=<class 'pydantic.v1.main.divideSchema'>, func=<function divide at 0x72167803be20>),
 StructuredTool(name='subtract', description='subtract(a: float, b: float) -> float - Subtract two numbers; a - b.', args_schema=<class 'pydantic.v1.main.subtractSchema'>, func=<function subtract at 0x721642cb9d00>),
 StructuredTool(name='power', description='power(a: float, b: float) -> float - Raise a number to a power; a ** b.', args_schema=<class 'pydantic.v1.main.powerSchema'>, func=<function power at 0x721642cb9da0>)]

2 x 4 相乘等于 8.8!!

env.tools[0].invoke({"a": 2, "b": 4})
8.8

任务说明

task.instructions
'You are requested to solve math questions in an alternate mathematical universe. The operations have been altered to yield different results than expected. Do not guess the answer or rely on your  innate knowledge of math. Use the provided tools to answer the question. While associativity and commutativity apply, distributivity does not. Answer the question using the fewest possible tools. Only include the numeric response without any clarifications.'

代理工厂#

为了进行评估,我们需要一个代理工厂,它将为每次评估运行创建一个新的代理执行器实例。

我们将使用 LangChain 基准测试提供的 OpenAIAgentFactory - 查看 intro 部分,了解如何定义您自己的工厂。

from langchain_core.prompts import ChatPromptTemplate
from langchain_openai.chat_models import ChatOpenAI

from langchain_benchmarks.tool_usage.agents import StandardAgentFactory

model = ChatOpenAI(temperature=0)
prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "{instructions}"),  # Populated from task.instructions automatically
        ("human", "{question}"),  # Populated from the test data
        (
            "placeholder",
            "{agent_scratchpad}",
        ),  # Work where the agent can do its work (e.g., call multiple tools)
    ]
)

agent_factory = StandardAgentFactory(task, model, prompt)
from langchain import globals

globals.set_verbose(True)

agent = agent_factory()
agent.invoke({"question": "how much is 2+5"})
> Entering new AgentExecutor chain...

Invoking: `add` with `{'a': 2, 'b': 5}`


8.2The result of 2 + 5 in this alternate mathematical universe is 8.2.

> Finished chain.
{'question': 'how much is 2+5',
 'output': 'The result of 2 + 5 in this alternate mathematical universe is 8.2.',
 'intermediate_steps': [(ToolAgentAction(tool='add', tool_input={'a': 2, 'b': 5}, log="\nInvoking: `add` with `{'a': 2, 'b': 5}`\n\n\n", message_log=[AIMessageChunk(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_MZMnEZrae7AuXYtWzH0l9xKL', 'function': {'arguments': '{"a":2,"b":5}', 'name': 'add'}, 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls'}, id='run-b7548303-194d-40ee-85bf-3d43cac39526', tool_calls=[{'name': 'add', 'args': {'a': 2, 'b': 5}, 'id': 'call_MZMnEZrae7AuXYtWzH0l9xKL'}], tool_call_chunks=[{'name': 'add', 'args': '{"a":2,"b":5}', 'id': 'call_MZMnEZrae7AuXYtWzH0l9xKL', 'index': 0}])], tool_call_id='call_MZMnEZrae7AuXYtWzH0l9xKL'),
   8.2)]}

基准测试#

请参阅 introductionbenchmark all,了解如何运行基准测试。此笔记本只是为了解释和探索任务而存在的。