inspect-ai 0.3.82__py3-none-any.whl → 0.3.83__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- inspect_ai/__init__.py +2 -1
- inspect_ai/_display/textual/app.py +14 -3
- inspect_ai/_display/textual/display.py +4 -0
- inspect_ai/_display/textual/widgets/samples.py +9 -3
- inspect_ai/_display/textual/widgets/task_detail.py +3 -4
- inspect_ai/_display/textual/widgets/tasks.py +17 -1
- inspect_ai/_display/textual/widgets/vscode.py +44 -0
- inspect_ai/_eval/eval.py +36 -24
- inspect_ai/_eval/evalset.py +17 -18
- inspect_ai/_eval/loader.py +34 -11
- inspect_ai/_eval/run.py +8 -13
- inspect_ai/_eval/score.py +13 -3
- inspect_ai/_eval/task/generate.py +8 -9
- inspect_ai/_eval/task/log.py +2 -0
- inspect_ai/_eval/task/task.py +23 -9
- inspect_ai/_util/file.py +13 -0
- inspect_ai/_util/json.py +2 -1
- inspect_ai/_util/registry.py +1 -0
- inspect_ai/_util/vscode.py +37 -0
- inspect_ai/_view/www/App.css +6 -0
- inspect_ai/_view/www/dist/assets/index.css +304 -128
- inspect_ai/_view/www/dist/assets/index.js +47495 -27519
- inspect_ai/_view/www/log-schema.json +124 -31
- inspect_ai/_view/www/package.json +3 -0
- inspect_ai/_view/www/src/App.tsx +12 -0
- inspect_ai/_view/www/src/appearance/icons.ts +1 -0
- inspect_ai/_view/www/src/components/Card.tsx +6 -4
- inspect_ai/_view/www/src/components/LinkButton.module.css +16 -0
- inspect_ai/_view/www/src/components/LinkButton.tsx +33 -0
- inspect_ai/_view/www/src/components/LiveVirtualList.tsx +1 -1
- inspect_ai/_view/www/src/components/MarkdownDiv.tsx +113 -23
- inspect_ai/_view/www/src/components/Modal.module.css +38 -0
- inspect_ai/_view/www/src/components/Modal.tsx +77 -0
- inspect_ai/_view/www/src/plan/DetailStep.module.css +4 -0
- inspect_ai/_view/www/src/plan/DetailStep.tsx +6 -3
- inspect_ai/_view/www/src/plan/SolverDetailView.module.css +2 -1
- inspect_ai/_view/www/src/samples/InlineSampleDisplay.tsx +7 -0
- inspect_ai/_view/www/src/samples/SampleDialog.tsx +7 -0
- inspect_ai/_view/www/src/samples/SampleDisplay.tsx +11 -34
- inspect_ai/_view/www/src/samples/SampleSummaryView.module.css +6 -0
- inspect_ai/_view/www/src/samples/SampleSummaryView.tsx +2 -2
- inspect_ai/_view/www/src/samples/SamplesTools.tsx +12 -0
- inspect_ai/_view/www/src/samples/chat/MessageContent.tsx +2 -0
- inspect_ai/_view/www/src/samples/chat/MessageContents.tsx +2 -0
- inspect_ai/_view/www/src/samples/chat/messages.ts +3 -1
- inspect_ai/_view/www/src/samples/chat/tools/ToolCallView.tsx +1 -0
- inspect_ai/_view/www/src/samples/descriptor/samplesDescriptor.tsx +9 -3
- inspect_ai/_view/www/src/samples/descriptor/score/BooleanScoreDescriptor.module.css +3 -3
- inspect_ai/_view/www/src/samples/descriptor/score/BooleanScoreDescriptor.tsx +1 -1
- inspect_ai/_view/www/src/samples/descriptor/score/ObjectScoreDescriptor.module.css +4 -4
- inspect_ai/_view/www/src/samples/descriptor/score/ObjectScoreDescriptor.tsx +10 -11
- inspect_ai/_view/www/src/samples/list/SampleFooter.module.css +2 -1
- inspect_ai/_view/www/src/samples/list/SampleFooter.tsx +7 -1
- inspect_ai/_view/www/src/samples/list/SampleList.tsx +25 -8
- inspect_ai/_view/www/src/samples/list/SampleRow.tsx +1 -1
- inspect_ai/_view/www/src/samples/scores/SampleScores.tsx +11 -22
- inspect_ai/_view/www/src/samples/scores/SampleScoresGrid.module.css +38 -0
- inspect_ai/_view/www/src/samples/scores/SampleScoresGrid.tsx +118 -0
- inspect_ai/_view/www/src/samples/scores/{SampleScoreView.module.css → SampleScoresView.module.css} +10 -1
- inspect_ai/_view/www/src/samples/scores/SampleScoresView.tsx +78 -0
- inspect_ai/_view/www/src/samples/transcript/SampleLimitEventView.tsx +3 -3
- inspect_ai/_view/www/src/samples/transcript/ToolEventView.tsx +25 -4
- inspect_ai/_view/www/src/samples/transcript/event/EventPanel.tsx +29 -2
- inspect_ai/_view/www/src/samples/transcript/state/StateEventRenderers.tsx +0 -1
- inspect_ai/_view/www/src/state/hooks.ts +5 -3
- inspect_ai/_view/www/src/state/logPolling.ts +5 -1
- inspect_ai/_view/www/src/state/logSlice.ts +10 -0
- inspect_ai/_view/www/src/state/samplePolling.ts +4 -1
- inspect_ai/_view/www/src/state/sampleSlice.ts +13 -0
- inspect_ai/_view/www/src/types/log.d.ts +34 -26
- inspect_ai/_view/www/src/types/markdown-it-katex.d.ts +21 -0
- inspect_ai/_view/www/src/utils/json-worker.ts +79 -12
- inspect_ai/_view/www/src/workspace/WorkSpace.tsx +18 -16
- inspect_ai/_view/www/src/workspace/navbar/ResultsPanel.module.css +16 -0
- inspect_ai/_view/www/src/workspace/navbar/ResultsPanel.tsx +68 -71
- inspect_ai/_view/www/src/workspace/navbar/ScoreGrid.module.css +35 -0
- inspect_ai/_view/www/src/workspace/navbar/ScoreGrid.tsx +117 -0
- inspect_ai/_view/www/src/workspace/navbar/SecondaryBar.tsx +1 -1
- inspect_ai/_view/www/src/workspace/sidebar/Sidebar.module.css +3 -2
- inspect_ai/_view/www/src/workspace/tabs/SamplesTab.tsx +18 -0
- inspect_ai/_view/www/yarn.lock +94 -1
- inspect_ai/agent/__init__.py +36 -0
- inspect_ai/agent/_agent.py +268 -0
- inspect_ai/agent/_as_solver.py +72 -0
- inspect_ai/agent/_as_tool.py +122 -0
- inspect_ai/{solver → agent}/_bridge/bridge.py +23 -37
- inspect_ai/{solver → agent}/_bridge/patch.py +9 -8
- inspect_ai/agent/_filter.py +46 -0
- inspect_ai/agent/_handoff.py +93 -0
- inspect_ai/{solver/_human_agent → agent/_human}/agent.py +11 -12
- inspect_ai/{solver/_human_agent → agent/_human}/commands/__init__.py +2 -3
- inspect_ai/{solver/_human_agent → agent/_human}/commands/clock.py +3 -1
- inspect_ai/{solver/_human_agent → agent/_human}/commands/score.py +5 -5
- inspect_ai/{solver/_human_agent → agent/_human}/install.py +6 -3
- inspect_ai/{solver/_human_agent → agent/_human}/service.py +7 -3
- inspect_ai/{solver/_human_agent → agent/_human}/state.py +5 -5
- inspect_ai/agent/_react.py +241 -0
- inspect_ai/agent/_run.py +36 -0
- inspect_ai/agent/_types.py +81 -0
- inspect_ai/log/_log.py +11 -2
- inspect_ai/log/_transcript.py +13 -9
- inspect_ai/model/__init__.py +7 -1
- inspect_ai/model/_call_tools.py +256 -52
- inspect_ai/model/_chat_message.py +7 -4
- inspect_ai/model/_conversation.py +13 -62
- inspect_ai/model/_display.py +85 -0
- inspect_ai/model/_model.py +113 -14
- inspect_ai/model/_model_output.py +14 -9
- inspect_ai/model/_openai.py +16 -4
- inspect_ai/model/_openai_computer_use.py +162 -0
- inspect_ai/model/_openai_responses.py +319 -165
- inspect_ai/model/_providers/anthropic.py +20 -21
- inspect_ai/model/_providers/azureai.py +24 -13
- inspect_ai/model/_providers/bedrock.py +1 -7
- inspect_ai/model/_providers/cloudflare.py +3 -3
- inspect_ai/model/_providers/goodfire.py +2 -6
- inspect_ai/model/_providers/google.py +11 -10
- inspect_ai/model/_providers/groq.py +6 -3
- inspect_ai/model/_providers/hf.py +7 -3
- inspect_ai/model/_providers/mistral.py +7 -10
- inspect_ai/model/_providers/openai.py +47 -17
- inspect_ai/model/_providers/openai_o1.py +11 -4
- inspect_ai/model/_providers/openai_responses.py +12 -14
- inspect_ai/model/_providers/providers.py +2 -2
- inspect_ai/model/_providers/together.py +12 -2
- inspect_ai/model/_providers/util/chatapi.py +7 -2
- inspect_ai/model/_providers/util/hf_handler.py +4 -2
- inspect_ai/model/_providers/util/llama31.py +4 -2
- inspect_ai/model/_providers/vertex.py +11 -9
- inspect_ai/model/_providers/vllm.py +4 -4
- inspect_ai/scorer/__init__.py +2 -0
- inspect_ai/scorer/_metrics/__init__.py +2 -0
- inspect_ai/scorer/_metrics/grouped.py +84 -0
- inspect_ai/scorer/_score.py +26 -6
- inspect_ai/solver/__init__.py +2 -2
- inspect_ai/solver/_basic_agent.py +22 -9
- inspect_ai/solver/_bridge.py +31 -0
- inspect_ai/solver/_chain.py +20 -12
- inspect_ai/solver/_fork.py +5 -1
- inspect_ai/solver/_human_agent.py +52 -0
- inspect_ai/solver/_prompt.py +3 -1
- inspect_ai/solver/_run.py +59 -0
- inspect_ai/solver/_solver.py +14 -4
- inspect_ai/solver/_task_state.py +5 -3
- inspect_ai/tool/_tool_call.py +15 -8
- inspect_ai/tool/_tool_def.py +17 -12
- inspect_ai/tool/_tool_support_helpers.py +2 -2
- inspect_ai/tool/_tool_with.py +14 -11
- inspect_ai/tool/_tools/_bash_session.py +11 -2
- inspect_ai/tool/_tools/_computer/_common.py +18 -2
- inspect_ai/tool/_tools/_computer/_computer.py +18 -2
- inspect_ai/tool/_tools/_computer/_resources/tool/_constants.py +2 -0
- inspect_ai/tool/_tools/_computer/_resources/tool/_x11_client.py +17 -0
- inspect_ai/tool/_tools/_think.py +1 -1
- inspect_ai/tool/_tools/_web_browser/_web_browser.py +100 -61
- inspect_ai/util/__init__.py +2 -0
- inspect_ai/util/_anyio.py +27 -0
- inspect_ai/util/_sandbox/__init__.py +2 -1
- inspect_ai/util/_sandbox/context.py +32 -7
- inspect_ai/util/_sandbox/docker/cleanup.py +4 -0
- inspect_ai/util/_sandbox/docker/compose.py +2 -2
- inspect_ai/util/_sandbox/docker/docker.py +12 -1
- inspect_ai/util/_store_model.py +30 -7
- inspect_ai/util/_subprocess.py +13 -3
- {inspect_ai-0.3.82.dist-info → inspect_ai-0.3.83.dist-info}/METADATA +1 -1
- {inspect_ai-0.3.82.dist-info → inspect_ai-0.3.83.dist-info}/RECORD +179 -153
- inspect_ai/_view/www/src/samples/scores/SampleScoreView.tsx +0 -167
- /inspect_ai/{solver → agent}/_bridge/__init__.py +0 -0
- /inspect_ai/{solver/_human_agent → agent/_human}/__init__.py +0 -0
- /inspect_ai/{solver/_human_agent → agent/_human}/commands/command.py +0 -0
- /inspect_ai/{solver/_human_agent → agent/_human}/commands/instructions.py +0 -0
- /inspect_ai/{solver/_human_agent → agent/_human}/commands/note.py +0 -0
- /inspect_ai/{solver/_human_agent → agent/_human}/commands/status.py +0 -0
- /inspect_ai/{solver/_human_agent → agent/_human}/commands/submit.py +0 -0
- /inspect_ai/{solver/_human_agent → agent/_human}/panel.py +0 -0
- /inspect_ai/{solver/_human_agent → agent/_human}/view.py +0 -0
- {inspect_ai-0.3.82.dist-info → inspect_ai-0.3.83.dist-info}/WHEEL +0 -0
- {inspect_ai-0.3.82.dist-info → inspect_ai-0.3.83.dist-info}/entry_points.txt +0 -0
- {inspect_ai-0.3.82.dist-info → inspect_ai-0.3.83.dist-info}/licenses/LICENSE +0 -0
- {inspect_ai-0.3.82.dist-info → inspect_ai-0.3.83.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,72 @@
|
|
1
|
+
from __future__ import annotations
|
2
|
+
|
3
|
+
from typing import TYPE_CHECKING, Any
|
4
|
+
|
5
|
+
if TYPE_CHECKING:
|
6
|
+
from inspect_ai.solver._solver import Solver
|
7
|
+
|
8
|
+
from inspect_ai._util.registry import (
|
9
|
+
is_registry_object,
|
10
|
+
registry_unqualified_name,
|
11
|
+
)
|
12
|
+
from inspect_ai.tool._tool_info import parse_tool_info
|
13
|
+
|
14
|
+
from ._agent import Agent, AgentState
|
15
|
+
|
16
|
+
|
17
|
+
def as_solver(agent: Agent, **agent_kwargs: Any) -> Solver:
|
18
|
+
"""Convert an agent to a solver.
|
19
|
+
|
20
|
+
Note that agents used as solvers will only receive their first parameter
|
21
|
+
(`state`). Any other parameters must provide appropriate defaults
|
22
|
+
or be explicitly specified in `agent_kwargs`
|
23
|
+
|
24
|
+
Args:
|
25
|
+
agent: Agent to convert.
|
26
|
+
**agent_kwargs: Arguments to curry to Agent function (required
|
27
|
+
if the agent has parameters without default values).
|
28
|
+
|
29
|
+
Solver:
|
30
|
+
Solver from agent.
|
31
|
+
"""
|
32
|
+
from inspect_ai.solver._solver import Generate, solver
|
33
|
+
from inspect_ai.solver._task_state import TaskState
|
34
|
+
|
35
|
+
# agent must be registered (so we can get its name)
|
36
|
+
if not is_registry_object(agent):
|
37
|
+
raise RuntimeError(
|
38
|
+
"Agent passed to as_solver was not created by an @agent decorated function"
|
39
|
+
)
|
40
|
+
agent_name = registry_unqualified_name(agent)
|
41
|
+
|
42
|
+
# check to make sure we have all the parameters we need to run the agent
|
43
|
+
agent_info = parse_tool_info(agent)
|
44
|
+
for name, param in list(agent_info.parameters.properties.items())[1:]:
|
45
|
+
if param.default is None and name not in agent_kwargs:
|
46
|
+
raise ValueError(
|
47
|
+
f"To use the {agent_name} agent as a solver "
|
48
|
+
+ f"you must pass a value for the agent's required '{name}' "
|
49
|
+
+ "parameter to the as_solver() function."
|
50
|
+
)
|
51
|
+
|
52
|
+
@solver(name=agent_name)
|
53
|
+
def agent_to_solver() -> Solver:
|
54
|
+
async def solve(state: TaskState, generate: Generate) -> TaskState:
|
55
|
+
# run agent
|
56
|
+
agent_state = await agent(
|
57
|
+
AgentState(messages=state.messages), **agent_kwargs
|
58
|
+
)
|
59
|
+
|
60
|
+
# update messages
|
61
|
+
state.messages = agent_state.messages
|
62
|
+
|
63
|
+
# update output if its not empty
|
64
|
+
if agent_state.output:
|
65
|
+
state.output = agent_state.output
|
66
|
+
|
67
|
+
return state
|
68
|
+
|
69
|
+
# return solver
|
70
|
+
return solve
|
71
|
+
|
72
|
+
return agent_to_solver()
|
@@ -0,0 +1,122 @@
|
|
1
|
+
from typing import Any
|
2
|
+
|
3
|
+
from inspect_ai._util.registry import (
|
4
|
+
is_registry_object,
|
5
|
+
registry_info,
|
6
|
+
registry_unqualified_name,
|
7
|
+
)
|
8
|
+
from inspect_ai.model._chat_message import ChatMessageAssistant, ChatMessageUser
|
9
|
+
from inspect_ai.tool._tool import Tool, ToolResult, tool
|
10
|
+
from inspect_ai.tool._tool_def import ToolDef, validate_tool_parameters
|
11
|
+
from inspect_ai.tool._tool_info import ToolInfo, parse_tool_info
|
12
|
+
from inspect_ai.tool._tool_params import ToolParam
|
13
|
+
|
14
|
+
from ._agent import AGENT_DESCRIPTION, Agent, AgentState
|
15
|
+
|
16
|
+
|
17
|
+
@tool
|
18
|
+
def as_tool(agent: Agent, description: str | None = None, **agent_kwargs: Any) -> Tool:
|
19
|
+
"""Convert an agent to a tool.
|
20
|
+
|
21
|
+
By default the model will see all of the agent's arguments as
|
22
|
+
tool arguments (save for `state` which is converted to an `input`
|
23
|
+
arguments of type `str`). Provide optional `agent_kwargs` to mask
|
24
|
+
out agent parameters with default values (these parameters will
|
25
|
+
not be presented to the model as part of the tool interface)
|
26
|
+
|
27
|
+
Args:
|
28
|
+
agent: Agent to convert.
|
29
|
+
description: Tool description (defaults to agent description)
|
30
|
+
**agent_kwargs: Arguments to curry to Agent function (arguments
|
31
|
+
provided here will not be presented to the model as part
|
32
|
+
of the tool interface).
|
33
|
+
|
34
|
+
Returns:
|
35
|
+
Tool from agent.
|
36
|
+
"""
|
37
|
+
# agent must be registered (so we can get its name)
|
38
|
+
if not is_registry_object(agent):
|
39
|
+
raise RuntimeError(
|
40
|
+
"Agent passed to as_tool was not created by an @agent decorated function"
|
41
|
+
)
|
42
|
+
|
43
|
+
async def execute(input: str, *args: Any, **kwargs: Any) -> ToolResult:
|
44
|
+
# prepare state and call agent
|
45
|
+
state = AgentState(messages=[ChatMessageUser(content=input)])
|
46
|
+
state = await agent(state, *args, **(agent_kwargs | kwargs))
|
47
|
+
|
48
|
+
# find assistant message to read content from (prefer output)
|
49
|
+
if not state.output.empty:
|
50
|
+
return state.output.message.content
|
51
|
+
elif len(state.messages) > 0 and isinstance(
|
52
|
+
state.messages[-1], ChatMessageAssistant
|
53
|
+
):
|
54
|
+
return state.messages[-1].content
|
55
|
+
else:
|
56
|
+
return ""
|
57
|
+
|
58
|
+
# get tool_info
|
59
|
+
tool_info = agent_tool_info(agent, description, **agent_kwargs)
|
60
|
+
|
61
|
+
# add "input" param
|
62
|
+
tool_info.parameters.properties = {
|
63
|
+
"input": ToolParam(type="string", description="Input message.")
|
64
|
+
} | tool_info.parameters.properties
|
65
|
+
tool_info.parameters.required.append("input")
|
66
|
+
|
67
|
+
# create tool
|
68
|
+
tool_def = ToolDef(
|
69
|
+
execute,
|
70
|
+
name=tool_info.name,
|
71
|
+
description=tool_info.description,
|
72
|
+
parameters=tool_info.parameters,
|
73
|
+
)
|
74
|
+
return tool_def.as_tool()
|
75
|
+
|
76
|
+
|
77
|
+
def agent_tool_info(
|
78
|
+
agent: Agent, description: str | None, **agent_kwargs: Any
|
79
|
+
) -> ToolInfo:
|
80
|
+
# get tool_info and name
|
81
|
+
tool_info = parse_tool_info(agent)
|
82
|
+
tool_info.name = registry_unqualified_name(agent)
|
83
|
+
|
84
|
+
# remove "state" param
|
85
|
+
def remove_param(param: str) -> None:
|
86
|
+
if param in tool_info.parameters.properties:
|
87
|
+
del tool_info.parameters.properties[param]
|
88
|
+
if param in tool_info.parameters.required:
|
89
|
+
tool_info.parameters.required.remove(param)
|
90
|
+
|
91
|
+
remove_param("state")
|
92
|
+
|
93
|
+
# validate and remove curried params
|
94
|
+
for agent_param in agent_kwargs.keys():
|
95
|
+
if agent_param in tool_info.parameters.properties:
|
96
|
+
remove_param(agent_param)
|
97
|
+
else:
|
98
|
+
raise ValueError(
|
99
|
+
f"Agent {tool_info.name} does not have a '{agent_param}' parameter."
|
100
|
+
)
|
101
|
+
|
102
|
+
# resolve and validate description. the description in the call takes
|
103
|
+
# precedence, then any @agent(description="<foo>"), and finally any
|
104
|
+
# doc comment on the agent's execute function
|
105
|
+
reg_info = registry_info(agent)
|
106
|
+
tool_info.description = (
|
107
|
+
description
|
108
|
+
or reg_info.metadata.get(AGENT_DESCRIPTION, None)
|
109
|
+
or tool_info.description
|
110
|
+
)
|
111
|
+
if len(tool_info.description) == 0:
|
112
|
+
raise ValueError(
|
113
|
+
f"Description not provided for agent function '{tool_info.name}'. Provide a "
|
114
|
+
+ "description either via @agent(description='<description>'), the description "
|
115
|
+
+ "argument to as_tool() or handoff(), or via a doc comment on the agent's "
|
116
|
+
+ "execute function."
|
117
|
+
)
|
118
|
+
|
119
|
+
# validate parameter descriptions and types
|
120
|
+
validate_tool_parameters(tool_info.name, tool_info.parameters.properties)
|
121
|
+
|
122
|
+
return tool_info
|
@@ -5,17 +5,15 @@ from pydantic import BaseModel, Field, ValidationError
|
|
5
5
|
from pydantic_core import to_json
|
6
6
|
|
7
7
|
from inspect_ai._util._async import is_callable_coroutine
|
8
|
-
from inspect_ai.
|
8
|
+
from inspect_ai.agent._agent import Agent, AgentState, agent
|
9
|
+
from inspect_ai.model._model import get_model
|
10
|
+
from inspect_ai.model._model_output import ModelOutput
|
9
11
|
from inspect_ai.model._providers.providers import validate_openai_client
|
10
|
-
from inspect_ai.scorer._metric import Score
|
11
12
|
|
12
|
-
from .._solver import Generate, Solver, solver
|
13
|
-
from .._task_state import TaskState
|
14
13
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
"""Bridge an external agent into an Inspect Solver.
|
14
|
+
@agent
|
15
|
+
def bridge(agent: Callable[[dict[str, Any]], Awaitable[dict[str, Any]]]) -> Agent:
|
16
|
+
"""Bridge an external agent into an Inspect Agent.
|
19
17
|
|
20
18
|
See documentation at <https://inspect.aisi.org.uk/agent-bridge.html>
|
21
19
|
|
@@ -25,7 +23,7 @@ def bridge(agent: Callable[[dict[str, Any]], Awaitable[dict[str, Any]]]) -> Solv
|
|
25
23
|
Returns:
|
26
24
|
Standard Inspect solver.
|
27
25
|
"""
|
28
|
-
validate_openai_client("
|
26
|
+
validate_openai_client("Agent bridge()")
|
29
27
|
|
30
28
|
from openai.types.chat import ChatCompletionMessageParam
|
31
29
|
|
@@ -36,17 +34,15 @@ def bridge(agent: Callable[[dict[str, Any]], Awaitable[dict[str, Any]]]) -> Solv
|
|
36
34
|
|
37
35
|
from .patch import openai_request_to_inspect_model
|
38
36
|
|
39
|
-
class
|
40
|
-
|
41
|
-
|
37
|
+
class BridgeInput(BaseModel):
|
38
|
+
messages: list[ChatCompletionMessageParam]
|
39
|
+
|
40
|
+
# temporarily here for backward compatibility w/ previous bridge
|
42
41
|
input: list[ChatCompletionMessageParam]
|
43
|
-
metadata: dict[str, Any]
|
44
|
-
target: list[str]
|
45
42
|
|
46
43
|
class BridgeResult(BaseModel):
|
47
44
|
output: str
|
48
45
|
messages: list[ChatCompletionMessageParam] | None = Field(default=None)
|
49
|
-
scores: dict[str, Score] | None = Field(default=None)
|
50
46
|
|
51
47
|
result_schema = BridgeResult.model_json_schema()
|
52
48
|
result_validator = Draft7Validator(result_schema)
|
@@ -55,27 +51,15 @@ def bridge(agent: Callable[[dict[str, Any]], Awaitable[dict[str, Any]]]) -> Solv
|
|
55
51
|
if not is_callable_coroutine(agent):
|
56
52
|
raise TypeError(f"'{agent.__name__}' is not declared as an async callable.")
|
57
53
|
|
58
|
-
async def
|
59
|
-
#
|
60
|
-
|
61
|
-
|
62
|
-
if isinstance(state.input, str)
|
63
|
-
else state.input
|
64
|
-
)
|
65
|
-
|
66
|
-
# create sample (use standard gpt-4 message encoding -- i.e. no 'developer' messages)
|
67
|
-
sample = BridgeSample(
|
68
|
-
sample_id=str(state.sample_id),
|
69
|
-
epoch=state.epoch,
|
70
|
-
input=await openai_chat_messages(input, model="gpt-4"),
|
71
|
-
metadata=state.metadata,
|
72
|
-
target=list(state.target),
|
73
|
-
)
|
54
|
+
async def execute(state: AgentState) -> AgentState:
|
55
|
+
# create input (use standard gpt-4 message encoding -- i.e. no 'developer' messages)
|
56
|
+
messages = await openai_chat_messages(state.messages, model="gpt-4")
|
57
|
+
input = BridgeInput(messages=messages, input=messages)
|
74
58
|
|
75
59
|
# run target function
|
76
60
|
async with openai_request_to_inspect_model():
|
77
61
|
# call the function
|
78
|
-
result_dict = await agent(
|
62
|
+
result_dict = await agent(input.model_dump())
|
79
63
|
try:
|
80
64
|
result = BridgeResult.model_validate(result_dict)
|
81
65
|
except ValidationError:
|
@@ -89,12 +73,14 @@ def bridge(agent: Callable[[dict[str, Any]], Awaitable[dict[str, Any]]]) -> Solv
|
|
89
73
|
raise ValueError(message)
|
90
74
|
|
91
75
|
# update and return state
|
92
|
-
state.output
|
76
|
+
state.output = ModelOutput.from_content(
|
77
|
+
model=get_model().name, content=result.output
|
78
|
+
)
|
93
79
|
if result.messages is not None:
|
94
|
-
state.messages = chat_messages_from_openai(
|
95
|
-
|
96
|
-
|
80
|
+
state.messages = chat_messages_from_openai(
|
81
|
+
state.output.model, result.messages
|
82
|
+
)
|
97
83
|
|
98
84
|
return state
|
99
85
|
|
100
|
-
return
|
86
|
+
return execute
|
@@ -23,7 +23,6 @@ from inspect_ai.model._openai import (
|
|
23
23
|
openai_chat_choices,
|
24
24
|
openai_completion_usage,
|
25
25
|
)
|
26
|
-
from inspect_ai.solver._task_state import sample_state
|
27
26
|
from inspect_ai.tool._tool_choice import ToolChoice, ToolFunction
|
28
27
|
from inspect_ai.tool._tool_info import ToolInfo
|
29
28
|
from inspect_ai.tool._tool_params import ToolParams
|
@@ -98,10 +97,18 @@ def init_openai_request_patch() -> None:
|
|
98
97
|
async def inspect_model_request(
|
99
98
|
model_name: str, options: FinalRequestOptions
|
100
99
|
) -> ChatCompletion:
|
100
|
+
from inspect_ai.solver._task_state import sample_state
|
101
|
+
|
102
|
+
# resolve model
|
103
|
+
if model_name == "inspect":
|
104
|
+
model = get_model()
|
105
|
+
else:
|
106
|
+
model = get_model(model_name.removeprefix("inspect/"))
|
107
|
+
|
101
108
|
# convert openai messages to inspect messages
|
102
109
|
json_data = cast(dict[str, Any], options.json_data)
|
103
110
|
messages: list[ChatCompletionMessageParam] = json_data["messages"]
|
104
|
-
input = chat_messages_from_openai(messages)
|
111
|
+
input = chat_messages_from_openai(model.api.model_name, messages)
|
105
112
|
|
106
113
|
# convert openai tools to inspect tools
|
107
114
|
tools: list[ChatCompletionToolParam] = json_data.get("tools", [])
|
@@ -130,12 +137,6 @@ async def inspect_model_request(
|
|
130
137
|
case _:
|
131
138
|
inspect_tool_choice = ToolFunction(name=tool_choice["function"]["name"])
|
132
139
|
|
133
|
-
# resolve model
|
134
|
-
if model_name == "inspect":
|
135
|
-
model = get_model()
|
136
|
-
else:
|
137
|
-
model = get_model(model_name.removeprefix("inspect/"))
|
138
|
-
|
139
140
|
output = await model.generate(
|
140
141
|
input=input,
|
141
142
|
tools=inspect_tools,
|
@@ -0,0 +1,46 @@
|
|
1
|
+
from typing import Awaitable, Callable
|
2
|
+
|
3
|
+
from inspect_ai.model._chat_message import (
|
4
|
+
ChatMessage,
|
5
|
+
ChatMessageAssistant,
|
6
|
+
ChatMessageTool,
|
7
|
+
)
|
8
|
+
|
9
|
+
MessageFilter = Callable[[list[ChatMessage]], Awaitable[list[ChatMessage]]]
|
10
|
+
"""Filter messages sent to or received from agent handoffs."""
|
11
|
+
|
12
|
+
|
13
|
+
async def remove_tools(messages: list[ChatMessage]) -> list[ChatMessage]:
|
14
|
+
"""Remove tool calls from messages.
|
15
|
+
|
16
|
+
Removes all instances of `ChatMessageTool` as well as the `tool_calls`
|
17
|
+
field from `ChatMessageAssistant`.
|
18
|
+
|
19
|
+
Args:
|
20
|
+
messages: Messages to remove tool calls from.
|
21
|
+
|
22
|
+
Returns:
|
23
|
+
Messages without tool calls.
|
24
|
+
"""
|
25
|
+
filtered: list[ChatMessage] = []
|
26
|
+
for message in messages:
|
27
|
+
if isinstance(message, ChatMessageTool):
|
28
|
+
continue
|
29
|
+
if isinstance(message, ChatMessageAssistant):
|
30
|
+
message = message.model_copy(update=dict(tool_calls=None))
|
31
|
+
filtered.append(message)
|
32
|
+
|
33
|
+
return filtered
|
34
|
+
|
35
|
+
|
36
|
+
async def last_message(messages: list[ChatMessage]) -> list[ChatMessage]:
|
37
|
+
"""Remove all but the last message.
|
38
|
+
|
39
|
+
Args:
|
40
|
+
messages: Target messages.
|
41
|
+
|
42
|
+
Returns:
|
43
|
+
List containing only the last message from the input list.
|
44
|
+
|
45
|
+
"""
|
46
|
+
return messages[-1:]
|
@@ -0,0 +1,93 @@
|
|
1
|
+
from typing import Any
|
2
|
+
|
3
|
+
from inspect_ai._util.registry import (
|
4
|
+
RegistryInfo,
|
5
|
+
is_registry_object,
|
6
|
+
registry_unqualified_name,
|
7
|
+
set_registry_info,
|
8
|
+
)
|
9
|
+
from inspect_ai.tool._tool import Tool, ToolResult
|
10
|
+
from inspect_ai.tool._tool_description import ToolDescription, set_tool_description
|
11
|
+
|
12
|
+
from ._agent import Agent
|
13
|
+
from ._as_tool import agent_tool_info
|
14
|
+
from ._filter import MessageFilter
|
15
|
+
|
16
|
+
|
17
|
+
def handoff(
|
18
|
+
agent: Agent,
|
19
|
+
description: str | None = None,
|
20
|
+
input_filter: MessageFilter | None = None,
|
21
|
+
output_filter: MessageFilter | None = None,
|
22
|
+
tool_name: str | None = None,
|
23
|
+
**agent_kwargs: Any,
|
24
|
+
) -> Tool:
|
25
|
+
"""Create a tool that enables models to handoff to agents.
|
26
|
+
|
27
|
+
Args:
|
28
|
+
agent: Agent to hand off to.
|
29
|
+
description: Handoff tool description (defaults to agent description)
|
30
|
+
input_filter: Filter to modify the message history before calling the tool.
|
31
|
+
Use the built-in `remove_tools` filter to remove all tool calls
|
32
|
+
or alternatively specify a custom `MessageFilter` function.
|
33
|
+
output_filter: Filter to modify the message history after calling the tool.
|
34
|
+
Use the built-in `last_message` filter to return only the last message
|
35
|
+
or alternatively specify a custom `MessageFilter` function.
|
36
|
+
tool_name: Alternate tool name (defaults to `transfer_to_{agent_name}`)
|
37
|
+
**agent_kwargs: Arguments to curry to `Agent` function (arguments provided here
|
38
|
+
will not be presented to the model as part of the tool interface).
|
39
|
+
|
40
|
+
Returns:
|
41
|
+
Tool for handing off to the agent (must be called using `execute_tools()` to be
|
42
|
+
properly handled)
|
43
|
+
"""
|
44
|
+
# agent must be registered (so we can get its name)
|
45
|
+
if not is_registry_object(agent):
|
46
|
+
raise RuntimeError(
|
47
|
+
"Agent passed to as_tool was not created by an @agent decorated function"
|
48
|
+
)
|
49
|
+
|
50
|
+
# get tool_info
|
51
|
+
tool_info = agent_tool_info(agent, description, **agent_kwargs)
|
52
|
+
|
53
|
+
# AgentTool calls will be intercepted by execute_tools
|
54
|
+
agent_tool = AgentTool(agent, input_filter, output_filter, **agent_kwargs)
|
55
|
+
tool_name = tool_name or f"transfer_to_{tool_info.name}"
|
56
|
+
set_registry_info(agent_tool, RegistryInfo(type="tool", name=tool_name))
|
57
|
+
set_tool_description(
|
58
|
+
agent_tool,
|
59
|
+
ToolDescription(
|
60
|
+
name=tool_name,
|
61
|
+
description=tool_info.description,
|
62
|
+
parameters=tool_info.parameters,
|
63
|
+
),
|
64
|
+
)
|
65
|
+
return agent_tool
|
66
|
+
|
67
|
+
|
68
|
+
class AgentTool(Tool):
|
69
|
+
def __init__(
|
70
|
+
self,
|
71
|
+
agent: Agent,
|
72
|
+
input_filter: MessageFilter | None = None,
|
73
|
+
output_filter: MessageFilter | None = None,
|
74
|
+
**kwargs: Any,
|
75
|
+
):
|
76
|
+
self.agent = agent
|
77
|
+
self.input_filter = input_filter
|
78
|
+
self.output_filter = output_filter
|
79
|
+
self.kwargs = kwargs
|
80
|
+
|
81
|
+
@property
|
82
|
+
def __name__(self) -> str:
|
83
|
+
return registry_unqualified_name(self.agent)
|
84
|
+
|
85
|
+
async def __call__(self) -> ToolResult:
|
86
|
+
raise RuntimeError("AgentTool should not be called directly")
|
87
|
+
|
88
|
+
|
89
|
+
def has_handoff(tools: list[Tool] | None) -> bool:
|
90
|
+
if tools:
|
91
|
+
return any([isinstance(tool, AgentTool) for tool in tools])
|
92
|
+
else:
|
93
|
+
return False
|
@@ -5,8 +5,7 @@ import anyio
|
|
5
5
|
from inspect_ai.util import display_type, input_panel, sandbox
|
6
6
|
from inspect_ai.util._sandbox.events import SandboxEnvironmentProxy
|
7
7
|
|
8
|
-
from ..
|
9
|
-
from .._task_state import TaskState
|
8
|
+
from .._agent import Agent, AgentState, agent
|
10
9
|
from .commands import human_agent_commands
|
11
10
|
from .install import install_human_agent
|
12
11
|
from .panel import HumanAgentPanel
|
@@ -14,15 +13,15 @@ from .service import run_human_agent_service
|
|
14
13
|
from .view import ConsoleView, HumanAgentView
|
15
14
|
|
16
15
|
|
17
|
-
@
|
18
|
-
def
|
16
|
+
@agent
|
17
|
+
def human_cli(
|
19
18
|
answer: bool | str = True,
|
20
19
|
intermediate_scoring: bool = False,
|
21
20
|
record_session: bool = True,
|
22
|
-
) ->
|
23
|
-
"""Human
|
21
|
+
) -> Agent:
|
22
|
+
"""Human CLI agent for tasks that run in a sandbox.
|
24
23
|
|
25
|
-
The Human agent
|
24
|
+
The Human CLI agent installs agent task tools in the default
|
26
25
|
sandbox and presents the user with both task instructions and
|
27
26
|
documentation for the various tools (e.g. `task submit`,
|
28
27
|
`task start`, `task stop` `task instructions`, etc.). A human agent panel
|
@@ -40,12 +39,12 @@ def human_agent(
|
|
40
39
|
record_session: Record all user commands and outputs in the sandbox bash session.
|
41
40
|
|
42
41
|
Returns:
|
43
|
-
|
42
|
+
Agent: Human CLI agent.
|
44
43
|
"""
|
45
44
|
# we can only run one human agent interaction at a time (use lock to enforce)
|
46
45
|
agent_lock = anyio.Lock()
|
47
46
|
|
48
|
-
async def
|
47
|
+
async def execute(state: AgentState) -> AgentState:
|
49
48
|
async with agent_lock:
|
50
49
|
# ensure that we have a sandbox to work with
|
51
50
|
try:
|
@@ -58,7 +57,7 @@ def human_agent(
|
|
58
57
|
)
|
59
58
|
|
60
59
|
# helper function to run the agent (called for fullscreen vs. fallback below)
|
61
|
-
async def run_human_agent(view: HumanAgentView) ->
|
60
|
+
async def run_human_agent(view: HumanAgentView) -> AgentState:
|
62
61
|
sandbox_proxy = cast(SandboxEnvironmentProxy, sandbox())
|
63
62
|
with sandbox_proxy.no_events():
|
64
63
|
# create agent commands
|
@@ -67,7 +66,7 @@ def human_agent(
|
|
67
66
|
)
|
68
67
|
|
69
68
|
# install agent tools
|
70
|
-
await install_human_agent(
|
69
|
+
await install_human_agent(commands, record_session)
|
71
70
|
|
72
71
|
# hookup the view ui
|
73
72
|
view.connect(connection)
|
@@ -82,4 +81,4 @@ def human_agent(
|
|
82
81
|
else:
|
83
82
|
return await run_human_agent(ConsoleView())
|
84
83
|
|
85
|
-
return
|
84
|
+
return execute
|
@@ -1,5 +1,4 @@
|
|
1
|
-
from
|
2
|
-
|
1
|
+
from ..._agent import AgentState
|
3
2
|
from .clock import StartCommand, StopCommand
|
4
3
|
from .command import HumanAgentCommand
|
5
4
|
from .instructions import InstructionsCommand
|
@@ -10,7 +9,7 @@ from .submit import QuitCommand, SubmitCommand, ValidateCommand
|
|
10
9
|
|
11
10
|
|
12
11
|
def human_agent_commands(
|
13
|
-
state:
|
12
|
+
state: AgentState,
|
14
13
|
answer: bool | str,
|
15
14
|
intermediate_scoring: bool,
|
16
15
|
record_session: bool,
|
@@ -62,7 +62,9 @@ class StopCommand(HumanAgentCommand):
|
|
62
62
|
return stop
|
63
63
|
|
64
64
|
|
65
|
-
def clock_action_event(
|
65
|
+
def clock_action_event(
|
66
|
+
action: Literal["start", "stop"], state: HumanAgentState
|
67
|
+
) -> None:
|
66
68
|
from inspect_ai.log._transcript import transcript
|
67
69
|
|
68
70
|
transcript().info(
|
@@ -8,13 +8,13 @@ from inspect_ai._util.ansi import render_text
|
|
8
8
|
from inspect_ai.model._model_output import ModelOutput
|
9
9
|
from inspect_ai.scorer._score import score
|
10
10
|
|
11
|
-
from ...
|
11
|
+
from ..._agent import AgentState
|
12
12
|
from ..state import HumanAgentState, IntermediateScoring
|
13
13
|
from .command import HumanAgentCommand, call_human_agent
|
14
14
|
|
15
15
|
|
16
16
|
class ScoreCommand(HumanAgentCommand):
|
17
|
-
def __init__(self, state:
|
17
|
+
def __init__(self, state: AgentState):
|
18
18
|
self._state = state
|
19
19
|
|
20
20
|
@property
|
@@ -52,9 +52,9 @@ class ScoreCommand(HumanAgentCommand):
|
|
52
52
|
async def score_task(answer: str | None) -> str:
|
53
53
|
# make a copy of TaskState, add the answer, then score
|
54
54
|
if answer:
|
55
|
-
|
56
|
-
|
57
|
-
result = await score(
|
55
|
+
agent_state = deepcopy(self._state)
|
56
|
+
agent_state.output = ModelOutput.from_content("human_agent", answer)
|
57
|
+
result = await score(agent_state)
|
58
58
|
else:
|
59
59
|
result = await score(self._state)
|
60
60
|
|
@@ -3,7 +3,6 @@ from textwrap import dedent
|
|
3
3
|
|
4
4
|
from inspect_ai.util import sandbox
|
5
5
|
|
6
|
-
from .._task_state import TaskState
|
7
6
|
from .commands.command import HumanAgentCommand
|
8
7
|
|
9
8
|
INSTALL_DIR = "human_agent_install"
|
@@ -18,7 +17,7 @@ RECORD_SESSION_DIR = "/var/tmp/user-sessions"
|
|
18
17
|
|
19
18
|
|
20
19
|
async def install_human_agent(
|
21
|
-
|
20
|
+
commands: list[HumanAgentCommand], record_session: bool
|
22
21
|
) -> None:
|
23
22
|
# see if we have already installed
|
24
23
|
if not (await sandbox().exec(["mkdir", HUMAN_AGENT_DIR])).success:
|
@@ -183,8 +182,12 @@ def human_agent_bashrc(commands: list[HumanAgentCommand], record_session: bool)
|
|
183
182
|
fi
|
184
183
|
""").lstrip()
|
185
184
|
|
185
|
+
CLOCK = dedent("""
|
186
|
+
task start
|
187
|
+
""").lstrip()
|
188
|
+
|
186
189
|
# return .bashrc
|
187
|
-
return "\n".join([TERMINAL_CHECK, COMMANDS, RECORDING, INSTRUCTIONS])
|
190
|
+
return "\n".join([TERMINAL_CHECK, COMMANDS, RECORDING, INSTRUCTIONS, CLOCK])
|
188
191
|
|
189
192
|
|
190
193
|
def human_agent_install_sh() -> str:
|
@@ -1,20 +1,24 @@
|
|
1
|
+
from inspect_ai.agent._human.commands.clock import clock_action_event
|
1
2
|
from inspect_ai.model import ModelOutput
|
2
3
|
from inspect_ai.util._sandbox import sandbox
|
3
4
|
from inspect_ai.util._sandbox.service import sandbox_service
|
4
5
|
|
5
|
-
from ..
|
6
|
+
from .._agent import AgentState
|
6
7
|
from .commands.command import HumanAgentCommand
|
7
8
|
from .state import HumanAgentState
|
8
9
|
from .view import HumanAgentView
|
9
10
|
|
10
11
|
|
11
12
|
async def run_human_agent_service(
|
12
|
-
state:
|
13
|
-
) ->
|
13
|
+
state: AgentState, commands: list[HumanAgentCommand], view: HumanAgentView | None
|
14
|
+
) -> AgentState:
|
14
15
|
# initialise agent state
|
15
16
|
instructions = "\n\n".join([message.text for message in state.messages]).strip()
|
16
17
|
agent_state = HumanAgentState(instructions=instructions)
|
17
18
|
|
19
|
+
# record that clock is stopped
|
20
|
+
clock_action_event("stop", agent_state)
|
21
|
+
|
18
22
|
# extract service methods from commands
|
19
23
|
methods = {
|
20
24
|
command.name: command.service(agent_state)
|