打字机:单一工具#
在此任务中,代理可以使用名为“type_letter”的单一工具。此工具接受一个名为“letter”的参数,该参数应为一个字符。
代理必须重复用户的输入字符串,在一张虚拟纸上逐个打印字符。
根据代理使用“type_letter”工具打印正确字符串的能力对其进行评估。
from langchain_benchmarks import registry
task = registry["Tool Usage - Typewriter (1 tool)"]
task
名称 | 工具使用 - 打字机(1 个工具) |
类型 | ToolUsageTask |
数据集 ID | 59577193-8938-4ccf-92a7-e8a96bcf4f86 |
描述 | 环境包含一个单一工具和一张虚拟纸。该工具接受一个字母作为输入,并在虚拟纸上打印该字母。如果成功,该工具将返回输出“OK”。要确定纸上写了什么,需要读取环境状态。数据集包含难度各异的示例。难度由字符串的长度决定。 |
环境#
环境包含一个单一工具和一张虚拟纸。
该工具接受一个字母作为输入,并在虚拟纸上打印该字母。如果成功,该工具将返回输出“OK”。
要确定纸上写了什么,需要读取环境状态。
env = task.create_environment()
env.tools
[StructuredTool(name='type_letter', description='type_letter(letter: str) -> str - Print the given letter on the paper.', args_schema=<class 'pydantic.v1.main.type_letterSchema'>, func=<function create_typer.<locals>.type_letter at 0x73a65909ee80>)]
tool = env.tools[0]
tool.invoke({"letter": "a"})
'OK'
tool.invoke({"letter": "b"})
'OK'
env.read_state()
'ab'
探索任务#
为了评估,我们需要一个代理工厂,该工厂将为每次评估运行创建一个新的代理执行器实例。
我们将使用 StandardAgentFactory
- 查看 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": "abc"})
> Entering new AgentExecutor chain...
Invoking: `type_letter` with `{'letter': 'a'}`
OK
Invoking: `type_letter` with `{'letter': 'b'}`
OK
Invoking: `type_letter` with `{'letter': 'c'}`
OKabc
> Finished chain.
{'question': 'abc',
'output': 'abc',
'intermediate_steps': [(ToolAgentAction(tool='type_letter', tool_input={'letter': 'a'}, log="\nInvoking: `type_letter` with `{'letter': 'a'}`\n\n\n", message_log=[AIMessageChunk(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_f4exPQMfz4VWxFJw4LhyMc80', 'function': {'arguments': '{"letter": "a"}', 'name': 'type_letter'}, 'type': 'function'}, {'index': 1, 'id': 'call_DHOJfLJEKuOKdzBa8ZLRYJZq', 'function': {'arguments': '{"letter": "b"}', 'name': 'type_letter'}, 'type': 'function'}, {'index': 2, 'id': 'call_EziJvB6jtUEg3CmXSsQ7OWBj', 'function': {'arguments': '{"letter": "c"}', 'name': 'type_letter'}, 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls'}, id='run-7d6be045-b9e2-4f24-991c-8e34ccd53b98', tool_calls=[{'name': 'type_letter', 'args': {'letter': 'a'}, 'id': 'call_f4exPQMfz4VWxFJw4LhyMc80'}, {'name': 'type_letter', 'args': {'letter': 'b'}, 'id': 'call_DHOJfLJEKuOKdzBa8ZLRYJZq'}, {'name': 'type_letter', 'args': {'letter': 'c'}, 'id': 'call_EziJvB6jtUEg3CmXSsQ7OWBj'}], tool_call_chunks=[{'name': 'type_letter', 'args': '{"letter": "a"}', 'id': 'call_f4exPQMfz4VWxFJw4LhyMc80', 'index': 0}, {'name': 'type_letter', 'args': '{"letter": "b"}', 'id': 'call_DHOJfLJEKuOKdzBa8ZLRYJZq', 'index': 1}, {'name': 'type_letter', 'args': '{"letter": "c"}', 'id': 'call_EziJvB6jtUEg3CmXSsQ7OWBj', 'index': 2}])], tool_call_id='call_f4exPQMfz4VWxFJw4LhyMc80'),
'OK'),
(ToolAgentAction(tool='type_letter', tool_input={'letter': 'b'}, log="\nInvoking: `type_letter` with `{'letter': 'b'}`\n\n\n", message_log=[AIMessageChunk(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_f4exPQMfz4VWxFJw4LhyMc80', 'function': {'arguments': '{"letter": "a"}', 'name': 'type_letter'}, 'type': 'function'}, {'index': 1, 'id': 'call_DHOJfLJEKuOKdzBa8ZLRYJZq', 'function': {'arguments': '{"letter": "b"}', 'name': 'type_letter'}, 'type': 'function'}, {'index': 2, 'id': 'call_EziJvB6jtUEg3CmXSsQ7OWBj', 'function': {'arguments': '{"letter": "c"}', 'name': 'type_letter'}, 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls'}, id='run-7d6be045-b9e2-4f24-991c-8e34ccd53b98', tool_calls=[{'name': 'type_letter', 'args': {'letter': 'a'}, 'id': 'call_f4exPQMfz4VWxFJw4LhyMc80'}, {'name': 'type_letter', 'args': {'letter': 'b'}, 'id': 'call_DHOJfLJEKuOKdzBa8ZLRYJZq'}, {'name': 'type_letter', 'args': {'letter': 'c'}, 'id': 'call_EziJvB6jtUEg3CmXSsQ7OWBj'}], tool_call_chunks=[{'name': 'type_letter', 'args': '{"letter": "a"}', 'id': 'call_f4exPQMfz4VWxFJw4LhyMc80', 'index': 0}, {'name': 'type_letter', 'args': '{"letter": "b"}', 'id': 'call_DHOJfLJEKuOKdzBa8ZLRYJZq', 'index': 1}, {'name': 'type_letter', 'args': '{"letter": "c"}', 'id': 'call_EziJvB6jtUEg3CmXSsQ7OWBj', 'index': 2}])], tool_call_id='call_DHOJfLJEKuOKdzBa8ZLRYJZq'),
'OK'),
(ToolAgentAction(tool='type_letter', tool_input={'letter': 'c'}, log="\nInvoking: `type_letter` with `{'letter': 'c'}`\n\n\n", message_log=[AIMessageChunk(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_f4exPQMfz4VWxFJw4LhyMc80', 'function': {'arguments': '{"letter": "a"}', 'name': 'type_letter'}, 'type': 'function'}, {'index': 1, 'id': 'call_DHOJfLJEKuOKdzBa8ZLRYJZq', 'function': {'arguments': '{"letter": "b"}', 'name': 'type_letter'}, 'type': 'function'}, {'index': 2, 'id': 'call_EziJvB6jtUEg3CmXSsQ7OWBj', 'function': {'arguments': '{"letter": "c"}', 'name': 'type_letter'}, 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls'}, id='run-7d6be045-b9e2-4f24-991c-8e34ccd53b98', tool_calls=[{'name': 'type_letter', 'args': {'letter': 'a'}, 'id': 'call_f4exPQMfz4VWxFJw4LhyMc80'}, {'name': 'type_letter', 'args': {'letter': 'b'}, 'id': 'call_DHOJfLJEKuOKdzBa8ZLRYJZq'}, {'name': 'type_letter', 'args': {'letter': 'c'}, 'id': 'call_EziJvB6jtUEg3CmXSsQ7OWBj'}], tool_call_chunks=[{'name': 'type_letter', 'args': '{"letter": "a"}', 'id': 'call_f4exPQMfz4VWxFJw4LhyMc80', 'index': 0}, {'name': 'type_letter', 'args': '{"letter": "b"}', 'id': 'call_DHOJfLJEKuOKdzBa8ZLRYJZq', 'index': 1}, {'name': 'type_letter', 'args': '{"letter": "c"}', 'id': 'call_EziJvB6jtUEg3CmXSsQ7OWBj', 'index': 2}])], tool_call_id='call_EziJvB6jtUEg3CmXSsQ7OWBj'),
'OK')],
'state': 'abc'}
基准测试#
有关如何运行基准测试的信息,请参见 introduction
和 benchmark all
。此笔记本只是为了解释和探索任务。