graphcore-apps 0.5.0__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.
- agents/__init__.py +0 -0
- agents/apps.py +15 -0
- agents/authorization.py +62 -0
- agents/conf.py +78 -0
- agents/delegation.py +218 -0
- agents/entities/__init__.py +0 -0
- agents/entities/agent.py +16 -0
- agents/entities/conversation.py +23 -0
- agents/entities/instruction.py +7 -0
- agents/entities/message.py +30 -0
- agents/entities/profile.py +14 -0
- agents/entities/skill.py +8 -0
- agents/migrations/0001_initial.py +88 -0
- agents/migrations/0002_message_context_versions.py +16 -0
- agents/migrations/0003_compaction_threshold.py +21 -0
- agents/migrations/0004_agent_debug_context.py +16 -0
- agents/migrations/0005_conversation_last_active_at.py +34 -0
- agents/migrations/0006_tasks.py +21 -0
- agents/migrations/0007_agent_start_address.py +16 -0
- agents/migrations/__init__.py +0 -0
- agents/models.py +64 -0
- agents/persistence.py +18 -0
- agents/providers/__init__.py +0 -0
- agents/providers/anthropic.py +142 -0
- agents/providers/fake.py +51 -0
- agents/providers/google.py +186 -0
- agents/providers/openai.py +177 -0
- agents/providers/port.py +101 -0
- agents/providers/registry.py +40 -0
- agents/routes.py +5 -0
- agents/runtime/__init__.py +0 -0
- agents/runtime/compaction.py +29 -0
- agents/runtime/context.py +78 -0
- agents/runtime/dispatch.py +76 -0
- agents/runtime/effective_agent.py +70 -0
- agents/runtime/loop.py +454 -0
- agents/runtime/prompt.py +183 -0
- agents/runtime/resume.py +30 -0
- agents/runtime/tools.py +35 -0
- agents/specs/__init__.py +3 -0
- agents/specs/dsl.py +310 -0
- agents/specs/test_r01_agents.py +74 -0
- agents/specs/test_r02_conversations.py +146 -0
- agents/specs/test_r03_acts_as_the_sender.py +60 -0
- agents/specs/test_r04_context.py +63 -0
- agents/specs/test_r05_personal_configuration.py +86 -0
- agents/specs/test_r06_delegation.py +70 -0
- agents/specs/test_r07_sharing.py +103 -0
- agents/specs/test_r08_embedding.py +40 -0
- agents/specs/test_r09_finding.py +41 -0
- agents/specs/test_r10_clients.py +41 -0
- agents/strategies/__init__.py +0 -0
- agents/strategies/_schema.py +16 -0
- agents/strategies/agents.py +93 -0
- agents/strategies/context.py +45 -0
- agents/strategies/conversations.py +65 -0
- agents/strategies/instructions.py +62 -0
- agents/strategies/messages.py +56 -0
- agents/strategies/models.py +27 -0
- agents/strategies/profiles.py +43 -0
- agents/strategies/skills.py +47 -0
- agents/strategies/turn.py +47 -0
- agents/test_settings.py +6 -0
- agents/tests/__init__.py +0 -0
- agents/tests/support.py +94 -0
- agents/tests/test_agent_views.py +181 -0
- agents/tests/test_agents.py +166 -0
- agents/tests/test_anthropic_adapter.py +221 -0
- agents/tests/test_context.py +231 -0
- agents/tests/test_control.py +162 -0
- agents/tests/test_conversations.py +158 -0
- agents/tests/test_delegation.py +392 -0
- agents/tests/test_google_adapter.py +208 -0
- agents/tests/test_inspect.py +59 -0
- agents/tests/test_live_providers.py +69 -0
- agents/tests/test_openai_adapter.py +259 -0
- agents/tests/test_profiles.py +92 -0
- agents/tests/test_resumption.py +150 -0
- agents/tests/test_turns.py +230 -0
- agents/validation.py +48 -0
- agents_agent_frontend/__init__.py +3 -0
- agents_agent_frontend/agent_views.py +286 -0
- audio/__init__.py +0 -0
- audio/apps.py +14 -0
- audio/authorization.py +13 -0
- audio/conf.py +27 -0
- audio/entities/__init__.py +11 -0
- audio/entities/recording.py +10 -0
- audio/migrations/0001_initial.py +26 -0
- audio/migrations/__init__.py +0 -0
- audio/models.py +9 -0
- audio/persistence.py +6 -0
- audio/providers/__init__.py +0 -0
- audio/providers/fake.py +35 -0
- audio/providers/openai.py +50 -0
- audio/providers/port.py +31 -0
- audio/providers/registry.py +31 -0
- audio/specs/__init__.py +3 -0
- audio/specs/dsl.py +120 -0
- audio/specs/test_clients.py +20 -0
- audio/specs/test_dictation.py +87 -0
- audio/specs/test_privacy.py +45 -0
- audio/specs/test_vendor.py +59 -0
- audio/strategies/__init__.py +11 -0
- audio/strategies/recordings.py +88 -0
- audio/test_settings.py +6 -0
- audio/tests/__init__.py +0 -0
- audio/tests/test_openai_adapter.py +65 -0
- audio/tests/test_recordings.py +139 -0
- audio_agent_frontend/__init__.py +3 -0
- audio_agent_frontend/agent_views.py +11 -0
- graphcore_apps-0.5.0.dist-info/METADATA +14 -0
- graphcore_apps-0.5.0.dist-info/RECORD +118 -0
- graphcore_apps-0.5.0.dist-info/WHEEL +5 -0
- graphcore_apps-0.5.0.dist-info/entry_points.txt +7 -0
- graphcore_apps-0.5.0.dist-info/licenses/LICENSE +202 -0
- graphcore_apps-0.5.0.dist-info/licenses/NOTICE +2 -0
- graphcore_apps-0.5.0.dist-info/top_level.txt +4 -0
agents/runtime/tools.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"""The tools the runtime offers beside the agent frontend's."""
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from typing import Awaitable, Callable
|
|
4
|
+
|
|
5
|
+
from agents.providers.port import ToolDef
|
|
6
|
+
from agents.runtime.effective_agent import EffectiveAgent
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ToolFailure(Exception):
|
|
10
|
+
"""A runtime tool's refusal, returned to the model as the call's error."""
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass(frozen=True)
|
|
14
|
+
class RuntimeTool:
|
|
15
|
+
definition: ToolDef
|
|
16
|
+
run: Callable[[EffectiveAgent, dict, str], Awaitable[str]]
|
|
17
|
+
"""Called with the turn's agent, the call's params and the call's id."""
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
async def _load_skill(agent: EffectiveAgent, params: dict, _call_id: str) -> str:
|
|
21
|
+
name = params.get('name')
|
|
22
|
+
for skill in agent.skills:
|
|
23
|
+
if skill.name == name:
|
|
24
|
+
return skill.text
|
|
25
|
+
known = ', '.join(skill.name for skill in agent.skills) or 'none'
|
|
26
|
+
raise ToolFailure(f"{name!r} is not one of this agent's skills: {known}")
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
LOAD_SKILL = RuntimeTool(
|
|
30
|
+
ToolDef(
|
|
31
|
+
'load_skill', "Return the full text of one of the skills listed in your instructions.",
|
|
32
|
+
{"type": "object", "properties": {"name": {"type": "string"}}, "required": ["name"]},
|
|
33
|
+
),
|
|
34
|
+
_load_skill,
|
|
35
|
+
)
|
agents/specs/__init__.py
ADDED
agents/specs/dsl.py
ADDED
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
"""The agents app's vocabulary: people make agents, talk to them in
|
|
2
|
+
conversations, tune and share them, and read what the agent did. The only
|
|
3
|
+
module that names the app's strategies, types and the model seam."""
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import asyncio
|
|
7
|
+
import functools
|
|
8
|
+
import json
|
|
9
|
+
from typing import Iterable, Optional
|
|
10
|
+
|
|
11
|
+
from graphcore.public.live_testing import Actor, Dsl, HttpError, eventually, strategy
|
|
12
|
+
|
|
13
|
+
from agents.providers.port import Done, ModelRequest, ProviderError, TextDelta, ToolCall, Usage
|
|
14
|
+
|
|
15
|
+
AGENT = 'agents.Agent'
|
|
16
|
+
CONVERSATION = 'agents.Conversation'
|
|
17
|
+
MESSAGE = 'agents.Message'
|
|
18
|
+
PROFILE = 'agents.AgentProfile'
|
|
19
|
+
INSTRUCTION = 'agents.Instruction'
|
|
20
|
+
DEFAULT_MODEL = 'agents.conf.default_model'
|
|
21
|
+
CATALOG = 'agents.conf.models'
|
|
22
|
+
MODEL_REPLY = 'agents.providers.fake.scripted_reply'
|
|
23
|
+
SCRIPTED_MODEL = 'fake/scripted'
|
|
24
|
+
OTHER_MODEL = 'fake/other'
|
|
25
|
+
TERMINAL = ('succeeded', 'failed')
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
# What the model replies, one list of events per call.
|
|
29
|
+
|
|
30
|
+
def says(*parts: str, pause: float = 0, input_tokens: int = 10) -> list:
|
|
31
|
+
"""Text, in parts with `pause` seconds between them when given."""
|
|
32
|
+
events: list = []
|
|
33
|
+
for index, part in enumerate(parts):
|
|
34
|
+
if index and pause:
|
|
35
|
+
events.append(functools.partial(asyncio.sleep, pause))
|
|
36
|
+
events.append(TextDelta(part))
|
|
37
|
+
return [*events, Done('end', Usage(input_tokens, 5))]
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _tools(*tool_calls: ToolCall, text: str = '') -> list:
|
|
41
|
+
return [*([TextDelta(text)] if text else []), *tool_calls, Done('tool_calls', Usage(10, 5))]
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def opens(at: str, text: str = '') -> list:
|
|
45
|
+
return _tools(ToolCall('open-1', 'open', {'at': at}), text=text)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def runs(strategy_name: str, target: Optional[str] = None, **params) -> list:
|
|
49
|
+
"""A call that runs `strategy_name` by its own name at `target`, or at `/` with none."""
|
|
50
|
+
return _tools(ToolCall('call-1', 'call', {'at': target or '/', 'tool': strategy_name, 'params': params}))
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def calls(at: str, tool: str, **params) -> list:
|
|
54
|
+
return _tools(ToolCall('call-1', 'call', {'at': at, 'tool': tool, 'params': params}))
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def delegates(agent: str, goal: str, background: bool = False) -> list:
|
|
58
|
+
return _tools(ToolCall('delegate-1', 'delegate', {'agent': agent, 'goal': goal, 'background': background}))
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def loads_skill(name: str) -> list:
|
|
62
|
+
return _tools(ToolCall('skill-1', 'load_skill', {'name': name}))
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def fails(code: str, message: str, retryable: bool = False) -> list:
|
|
66
|
+
return [ProviderError(code, message, retryable=retryable)]
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class Model:
|
|
70
|
+
"""The scripted model: what each call was sent, and holding calls back."""
|
|
71
|
+
|
|
72
|
+
def __init__(self, patch):
|
|
73
|
+
self._patch = patch
|
|
74
|
+
|
|
75
|
+
@property
|
|
76
|
+
def requests(self) -> list[ModelRequest]:
|
|
77
|
+
return [call.args[0] for call in self._patch.call_args_list]
|
|
78
|
+
|
|
79
|
+
@property
|
|
80
|
+
def call_count(self) -> int:
|
|
81
|
+
return self._patch.call_count
|
|
82
|
+
|
|
83
|
+
async def is_asked(self, times: int = 1) -> None:
|
|
84
|
+
await self._patch.wait_for_calls(times)
|
|
85
|
+
|
|
86
|
+
def answers(self, count: int = 1) -> None:
|
|
87
|
+
self._patch.release(count)
|
|
88
|
+
|
|
89
|
+
def saw(self, text: str, call: int = -1) -> bool:
|
|
90
|
+
"""Whether call `call` showed the model `text` anywhere in its prompt."""
|
|
91
|
+
return text in shown(self.requests[call])
|
|
92
|
+
|
|
93
|
+
def prefix(self, call: int = -1) -> tuple[str, ...]:
|
|
94
|
+
return self.requests[call].prefix
|
|
95
|
+
|
|
96
|
+
def tools(self, call: int = -1) -> list[str]:
|
|
97
|
+
return [tool.name for tool in self.requests[call].tools]
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def shown(request: ModelRequest) -> str:
|
|
101
|
+
parts = [*request.prefix]
|
|
102
|
+
for turn in request.turns:
|
|
103
|
+
parts.append(turn.text)
|
|
104
|
+
parts.extend(result.content for result in turn.results)
|
|
105
|
+
return '\n'.join(parts)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
class Person(Actor):
|
|
109
|
+
async def _run(self, strategy_name: str, target: Optional[str] = None, /, **params) -> dict:
|
|
110
|
+
return await self.execute_one(strategy(strategy_name, target, **params))
|
|
111
|
+
|
|
112
|
+
async def _made(self, strategy_name: str, key: str, target: Optional[str] = None, /, **params) -> str:
|
|
113
|
+
outcome = await self._run(strategy_name, target, **params)
|
|
114
|
+
assert outcome['status'] == 'succeeded', outcome
|
|
115
|
+
made = outcome['ret_val'][key]
|
|
116
|
+
self.dsl.cleanup(lambda: self.execute_one(strategy('DeleteEntity', made)))
|
|
117
|
+
return made
|
|
118
|
+
|
|
119
|
+
# Agents and their pieces
|
|
120
|
+
|
|
121
|
+
async def creates_an_agent(self, name: str, in_: Optional[str] = None, **fields) -> str:
|
|
122
|
+
return await self._made('CreateAgent', 'agent_id', in_, name=name, **fields)
|
|
123
|
+
|
|
124
|
+
async def tries_to_create_an_agent(self, name: str, **fields) -> dict:
|
|
125
|
+
return await self._run('CreateAgent', name=name, **fields)
|
|
126
|
+
|
|
127
|
+
async def writes_an_instruction(self, text: str, name: str = 'Brief', on: Optional[str] = None,
|
|
128
|
+
attach: bool = False) -> str:
|
|
129
|
+
return await self._made('CreateInstruction', 'instruction_id', on, name=name, text=text, attach=attach)
|
|
130
|
+
|
|
131
|
+
async def writes_a_skill(self, name: str, description: str, text: str, on: Optional[str] = None,
|
|
132
|
+
attach: bool = False) -> str:
|
|
133
|
+
return await self._made('CreateSkill', 'skill_id', on, name=name, description=description, text=text,
|
|
134
|
+
attach=attach)
|
|
135
|
+
|
|
136
|
+
async def edits_the_instruction(self, instruction: str, text: str) -> dict:
|
|
137
|
+
return await self._run('UpdateInstruction', instruction, text=text)
|
|
138
|
+
|
|
139
|
+
async def updates_the_agent(self, agent: str, **fields) -> dict:
|
|
140
|
+
return await self._run('UpdateAgent', agent, **fields)
|
|
141
|
+
|
|
142
|
+
async def customizes(self, agent: str, **fields) -> dict:
|
|
143
|
+
outcome = await self._run('CustomizeAgent', agent, **fields)
|
|
144
|
+
if outcome['status'] == 'succeeded':
|
|
145
|
+
profile = outcome['ret_val']['profile_id']
|
|
146
|
+
self.dsl.cleanup(lambda: self.execute_one(strategy('DeleteEntity', profile)))
|
|
147
|
+
return outcome
|
|
148
|
+
|
|
149
|
+
async def deletes(self, entity: str) -> dict:
|
|
150
|
+
return await self._run('DeleteEntity', entity)
|
|
151
|
+
|
|
152
|
+
async def models(self) -> dict:
|
|
153
|
+
outcome = await self._run('ListModels')
|
|
154
|
+
assert outcome['status'] == 'succeeded', outcome
|
|
155
|
+
return outcome['ret_val']
|
|
156
|
+
|
|
157
|
+
# Conversations
|
|
158
|
+
|
|
159
|
+
async def starts_a_conversation(self, agent: str, **fields) -> str:
|
|
160
|
+
return await self._made('CreateConversation', 'conversation_id', agent, **fields)
|
|
161
|
+
|
|
162
|
+
async def tries_to_start_a_conversation(self, agent: str, **fields) -> dict:
|
|
163
|
+
return await self._run('CreateConversation', agent, **fields)
|
|
164
|
+
|
|
165
|
+
async def sends(self, conversation: str, content: str, **fields) -> dict:
|
|
166
|
+
return await self._run('SendMessage', conversation, content=content, **fields)
|
|
167
|
+
|
|
168
|
+
async def asks(self, conversation: str, content: str, **fields) -> str:
|
|
169
|
+
"""Sends `content`, waits for the turns it started to end, and returns the last reply."""
|
|
170
|
+
before = len(await self.turns_on(conversation))
|
|
171
|
+
outcome = await self.sends(conversation, content, **fields)
|
|
172
|
+
assert outcome['status'] == 'succeeded', outcome
|
|
173
|
+
await self.waits_for_the_turns(conversation, at_least=before + 1)
|
|
174
|
+
return await self.last_reply(conversation)
|
|
175
|
+
|
|
176
|
+
async def stops(self, conversation: str) -> dict:
|
|
177
|
+
return await self._run('UpdateConversation', conversation, stop=True)
|
|
178
|
+
|
|
179
|
+
async def picks_the_model(self, conversation: str, model: Optional[str]) -> dict:
|
|
180
|
+
return await self._run('UpdateConversation', conversation, model=model)
|
|
181
|
+
|
|
182
|
+
async def turns_on(self, conversation: str) -> list[dict]:
|
|
183
|
+
return [r for r in await self.requests_for(conversation) if r['strategy_name'] == 'RunTurn']
|
|
184
|
+
|
|
185
|
+
async def waits_for_the_turns(self, conversation: str, at_least: int = 1) -> list[dict]:
|
|
186
|
+
"""Every turn started on the conversation, once all have ended and it is idle."""
|
|
187
|
+
async def ended():
|
|
188
|
+
turns = await self.turns_on(conversation)
|
|
189
|
+
done = len(turns) >= at_least and all(t['status'] in TERMINAL for t in turns)
|
|
190
|
+
return done and (await self.get(conversation))['status'] == 'idle' and turns
|
|
191
|
+
return await eventually(ended, timeout=60, message=f'the turns on {conversation} never ended')
|
|
192
|
+
|
|
193
|
+
async def status_of(self, conversation: str) -> str:
|
|
194
|
+
return (await self.get(conversation))['status']
|
|
195
|
+
|
|
196
|
+
async def messages_in(self, conversation: str) -> list[dict]:
|
|
197
|
+
return sorted(await self.find(MESSAGE, parent_id=conversation), key=lambda m: m['sibling_position'])
|
|
198
|
+
|
|
199
|
+
async def transcript(self, conversation: str, roles: Iterable[str] = ('user', 'assistant', 'system')) -> list:
|
|
200
|
+
return [(m['role'], m['content']) for m in await self.messages_in(conversation) if m['role'] in roles]
|
|
201
|
+
|
|
202
|
+
async def last_reply(self, conversation: str) -> str:
|
|
203
|
+
replies = [m for m in await self.messages_in(conversation) if m['role'] == 'assistant' and m['content']]
|
|
204
|
+
return replies[-1]['content'] if replies else ''
|
|
205
|
+
|
|
206
|
+
async def tool_calls_in(self, conversation: str) -> list[dict]:
|
|
207
|
+
return [call for m in await self.messages_in(conversation) for call in m['tool_calls']]
|
|
208
|
+
|
|
209
|
+
async def sub_conversations_of(self, conversation: str) -> list[dict]:
|
|
210
|
+
return await self.find(CONVERSATION, parent_id=conversation)
|
|
211
|
+
|
|
212
|
+
# Sharing and reading
|
|
213
|
+
|
|
214
|
+
async def shares(self, entity: str, with_: 'Person', role: str = 'viewer') -> dict:
|
|
215
|
+
return await self._run('CreateGrant', entity, subject_id=await with_.entity_id(), role_name=role)
|
|
216
|
+
|
|
217
|
+
async def can_read(self, entity: str) -> bool:
|
|
218
|
+
try:
|
|
219
|
+
await self.get(entity)
|
|
220
|
+
return True
|
|
221
|
+
except HttpError:
|
|
222
|
+
return False
|
|
223
|
+
|
|
224
|
+
async def writes_a_message_directly(self, conversation: str, content: str) -> dict:
|
|
225
|
+
return await self._run('CreateEntity', conversation, entity_type_id=MESSAGE,
|
|
226
|
+
initial_attributes={'role': 'user', 'content': content})
|
|
227
|
+
|
|
228
|
+
# Finding
|
|
229
|
+
|
|
230
|
+
async def my_agents(self) -> list[str]:
|
|
231
|
+
return [a['name'] for a in await self.find(AGENT, owner=await self.entity_id())]
|
|
232
|
+
|
|
233
|
+
async def agents_i_can_use(self) -> list[str]:
|
|
234
|
+
return [a['name'] for a in await self.find(AGENT)]
|
|
235
|
+
|
|
236
|
+
async def shared_with_me(self) -> list[str]:
|
|
237
|
+
page = await self.call('POST', 'list_entities/', {
|
|
238
|
+
'type_ids': [AGENT, CONVERSATION, INSTRUCTION, 'agents.Skill'], 'granted_to_me': True, 'limit': 100,
|
|
239
|
+
})
|
|
240
|
+
return [e.get('name') or e.get('title') for e in page['entities']]
|
|
241
|
+
|
|
242
|
+
async def _conversations(self, *predicates: dict) -> list[str]:
|
|
243
|
+
mine = {'equal': {'field': 'owner', 'value': await self.entity_id()}}
|
|
244
|
+
page = await self.call('POST', 'list_entities/', {
|
|
245
|
+
'type_ids': [CONVERSATION], 'limit': 100, 'where': {'all_of': [mine, *predicates]},
|
|
246
|
+
})
|
|
247
|
+
return [c['entity_id'] for c in page['entities']]
|
|
248
|
+
|
|
249
|
+
async def my_conversations_with(self, agent: str) -> list[str]:
|
|
250
|
+
return await self._conversations({'equal': {'field': 'agent_id', 'value': agent}},
|
|
251
|
+
{'equal': {'field': 'parent_id', 'value': None}})
|
|
252
|
+
|
|
253
|
+
async def my_conversations_about(self, entity: str) -> list[str]:
|
|
254
|
+
return await self._conversations({'contains_value': {'field': 'context_ids', 'value': entity}})
|
|
255
|
+
|
|
256
|
+
async def my_running_conversations(self) -> list[str]:
|
|
257
|
+
return await self._conversations({'equal': {'field': 'status', 'value': 'running'}})
|
|
258
|
+
|
|
259
|
+
async def my_profiles_on(self, agent: str) -> list[dict]:
|
|
260
|
+
return await self.find(PROFILE, owner=await self.entity_id(), agent_id=agent)
|
|
261
|
+
|
|
262
|
+
# Over MCP, as an external agent
|
|
263
|
+
|
|
264
|
+
async def opens_over_mcp(self, at: str) -> str:
|
|
265
|
+
return (await self.mcp_tool('open', at=at))['content'][0]['text']
|
|
266
|
+
|
|
267
|
+
async def calls_over_mcp(self, at: str, tool: str, **params) -> dict:
|
|
268
|
+
result = await self.mcp_tool('call', at=at, tool=tool, params=params)
|
|
269
|
+
return {'error': bool(result.get('isError')), 'text': result['content'][0]['text']}
|
|
270
|
+
|
|
271
|
+
# Watching live
|
|
272
|
+
|
|
273
|
+
@staticmethod
|
|
274
|
+
def message_states(connection, conversation: str, role: str = 'assistant') -> list[dict]:
|
|
275
|
+
"""Every broadcast state of a message in `conversation` with `role`, in arrival order."""
|
|
276
|
+
return [state for m in connection.messages if m['type'] == 'entity_update'
|
|
277
|
+
for state in m.get('entities', {}).values()
|
|
278
|
+
if state.get('parent_id') == conversation and state.get('role') == role]
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
class AgentsDsl(Dsl):
|
|
282
|
+
actor_class = Person
|
|
283
|
+
|
|
284
|
+
def as_(self, name: str) -> Person:
|
|
285
|
+
return super().as_(name)
|
|
286
|
+
|
|
287
|
+
def the_deployment_offers_the_scripted_models(self) -> None:
|
|
288
|
+
"""`fake/scripted`, the default, and `fake/other`, for the rest of the scenario."""
|
|
289
|
+
if not getattr(self, '_scripted', False):
|
|
290
|
+
self._scripted = True
|
|
291
|
+
self.patch(DEFAULT_MODEL, return_value=SCRIPTED_MODEL).start()
|
|
292
|
+
self.patch(CATALOG, return_value=[
|
|
293
|
+
{'id': model, 'context_size': 200_000, 'max_output_tokens': 8192}
|
|
294
|
+
for model in (SCRIPTED_MODEL, OTHER_MODEL)
|
|
295
|
+
]).start()
|
|
296
|
+
|
|
297
|
+
def the_model_replies(self, *replies: list, hold: bool = False) -> Model:
|
|
298
|
+
"""Every turn runs on the scripted model, which gives these replies in
|
|
299
|
+
order, one per call, for the rest of the scenario. With `hold`, each
|
|
300
|
+
call waits for `answers()`."""
|
|
301
|
+
self.the_deployment_offers_the_scripted_models()
|
|
302
|
+
return Model(self.patch(MODEL_REPLY, side_effect=list(replies), hold=hold).start())
|
|
303
|
+
|
|
304
|
+
@staticmethod
|
|
305
|
+
async def together(*steps):
|
|
306
|
+
return await asyncio.gather(*steps)
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
def result_of(call: dict) -> dict:
|
|
310
|
+
return json.loads(call['result'])
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"""R1 — Agents: a person defines an agent, and every conversation with it
|
|
2
|
+
uses the definition as it is at its next turn."""
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from graphcore.public.live_testing import scenario
|
|
6
|
+
|
|
7
|
+
from agents.specs.dsl import OTHER_MODEL, SCRIPTED_MODEL, AgentsDsl, loads_skill, says
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@scenario('R1', "An agent's instructions and its skill index are in the prompt of its conversations")
|
|
11
|
+
async def _(a: AgentsDsl):
|
|
12
|
+
ana = a.as_('Ana')
|
|
13
|
+
troy = await ana.creates_an_agent('Troy')
|
|
14
|
+
await ana.writes_an_instruction('Answer in one sentence.', on=troy, attach=True)
|
|
15
|
+
await ana.writes_a_skill('research', 'How to research a venue', 'Search first, then call.', on=troy, attach=True)
|
|
16
|
+
conversation = await ana.starts_a_conversation(troy)
|
|
17
|
+
model = a.the_model_replies(says('Charleston has three.'))
|
|
18
|
+
|
|
19
|
+
reply = await ana.asks(conversation, 'Find venues near Charleston')
|
|
20
|
+
|
|
21
|
+
assert reply == 'Charleston has three.'
|
|
22
|
+
assert 'Answer in one sentence.' in model.prefix()
|
|
23
|
+
assert model.saw('- research: How to research a venue')
|
|
24
|
+
assert not model.saw('Search first, then call.')
|
|
25
|
+
assert {'open', 'find', 'call', 'load_skill', 'delegate'} <= set(model.tools())
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@scenario('R1', 'The agent loads a skill\'s text only when it needs it')
|
|
29
|
+
async def _(a: AgentsDsl):
|
|
30
|
+
ana = a.as_('Ana')
|
|
31
|
+
troy = await ana.creates_an_agent('Troy')
|
|
32
|
+
await ana.writes_a_skill('research', 'How to research a venue', 'Search first, then call.', on=troy, attach=True)
|
|
33
|
+
conversation = await ana.starts_a_conversation(troy)
|
|
34
|
+
model = a.the_model_replies(loads_skill('research'), says('Following the research skill.'))
|
|
35
|
+
|
|
36
|
+
await ana.asks(conversation, 'Research venues')
|
|
37
|
+
|
|
38
|
+
assert not model.saw('Search first, then call.', call=0)
|
|
39
|
+
assert model.saw('Search first, then call.', call=1)
|
|
40
|
+
[call] = await ana.tool_calls_in(conversation)
|
|
41
|
+
assert (call['tool'], call['state']) == ('load_skill', 'completed'), call
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@scenario('R1', 'Editing an agent changes the next turn of every conversation with it')
|
|
45
|
+
async def _(a: AgentsDsl):
|
|
46
|
+
ana = a.as_('Ana')
|
|
47
|
+
troy = await ana.creates_an_agent('Troy')
|
|
48
|
+
brief = await ana.writes_an_instruction('Answer in English.', on=troy, attach=True)
|
|
49
|
+
first, second = await ana.starts_a_conversation(troy), await ana.starts_a_conversation(troy)
|
|
50
|
+
model = a.the_model_replies(says('Hello.'), says('Hola.'), says('Hola otra vez.'))
|
|
51
|
+
await ana.asks(first, 'Hi')
|
|
52
|
+
|
|
53
|
+
assert (await ana.edits_the_instruction(brief, 'Answer in Spanish.'))['status'] == 'succeeded'
|
|
54
|
+
await ana.asks(second, 'Hi')
|
|
55
|
+
await ana.asks(first, 'Hi again')
|
|
56
|
+
|
|
57
|
+
assert 'Answer in English.' in model.prefix(call=0)
|
|
58
|
+
assert 'Answer in Spanish.' in model.prefix(call=1) and 'Answer in Spanish.' in model.prefix(call=2)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@scenario('R1', "An agent runs on the model it names, and only a model the deployment offers")
|
|
62
|
+
async def _(a: AgentsDsl):
|
|
63
|
+
ana = a.as_('Ana')
|
|
64
|
+
model = a.the_model_replies(says('From the other model.'))
|
|
65
|
+
|
|
66
|
+
catalog = await ana.models()
|
|
67
|
+
refused = await ana.tries_to_create_an_agent('Troy', model='nowhere/model')
|
|
68
|
+
troy = await ana.creates_an_agent('Troy', model=OTHER_MODEL)
|
|
69
|
+
await ana.asks(await ana.starts_a_conversation(troy), 'Which model?')
|
|
70
|
+
|
|
71
|
+
assert catalog['default'] == SCRIPTED_MODEL
|
|
72
|
+
assert [(m['id'], m['available']) for m in catalog['models']] == [(SCRIPTED_MODEL, True), (OTHER_MODEL, True)]
|
|
73
|
+
assert refused['status'] == 'failed' and 'nowhere/model' in refused['error'], refused
|
|
74
|
+
assert model.requests[0].model == OTHER_MODEL.split('/')[1]
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"""R2 — Conversations: messages and replies, kept, live, concurrent and stoppable."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from graphcore.public.live_testing import scenario
|
|
5
|
+
|
|
6
|
+
from agents.specs.dsl import AgentsDsl, Person, fails, opens, says
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@scenario('R2', 'The reply arrives as it is written')
|
|
10
|
+
async def _(a: AgentsDsl):
|
|
11
|
+
ana = a.as_('Ana')
|
|
12
|
+
conversation = await ana.starts_a_conversation(await ana.creates_an_agent('Troy'))
|
|
13
|
+
connection = await ana.connects()
|
|
14
|
+
a.the_model_replies(says('Three venues ', 'near Charleston.', pause=1.5))
|
|
15
|
+
|
|
16
|
+
await ana.asks(conversation, 'Find venues')
|
|
17
|
+
|
|
18
|
+
contents = [state['content'] for state in Person.message_states(connection, conversation)]
|
|
19
|
+
assert contents[0] == 'Three venues ' and contents[-1] == 'Three venues near Charleston.', contents
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@scenario('R2', 'A conversation picks up later with every message, tool call and result kept')
|
|
23
|
+
async def _(a: AgentsDsl):
|
|
24
|
+
ana = a.as_('Ana')
|
|
25
|
+
conversation = await ana.starts_a_conversation(await ana.creates_an_agent('Troy'))
|
|
26
|
+
model = a.the_model_replies(opens('/agents', text='Looking.'), says('You have one agent.'), says('Still one.'))
|
|
27
|
+
await ana.asks(conversation, 'What agents do I have?')
|
|
28
|
+
|
|
29
|
+
reply = await ana.asks(conversation, 'And now?')
|
|
30
|
+
|
|
31
|
+
assert reply == 'Still one.'
|
|
32
|
+
assert await ana.transcript(conversation, roles=('user', 'assistant')) == [
|
|
33
|
+
('user', 'What agents do I have?'), ('assistant', 'Looking.'), ('assistant', 'You have one agent.'),
|
|
34
|
+
('user', 'And now?'), ('assistant', 'Still one.'),
|
|
35
|
+
]
|
|
36
|
+
[call] = await ana.tool_calls_in(conversation)
|
|
37
|
+
assert (call['tool'], call['at'], call['state']) == ('open', '/agents', 'completed'), call
|
|
38
|
+
assert 'Troy' in call['result']
|
|
39
|
+
assert model.saw('What agents do I have?') and model.saw(call['result'])
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@scenario('R2', 'Several conversations with one agent run at once')
|
|
43
|
+
async def _(a: AgentsDsl):
|
|
44
|
+
ana = a.as_('Ana')
|
|
45
|
+
troy = await ana.creates_an_agent('Troy')
|
|
46
|
+
first, second = await ana.starts_a_conversation(troy), await ana.starts_a_conversation(troy)
|
|
47
|
+
model = a.the_model_replies(says('Answer.'), says('Answer.'), hold=True)
|
|
48
|
+
|
|
49
|
+
await ana.sends(first, 'One')
|
|
50
|
+
await ana.sends(second, 'Two')
|
|
51
|
+
await model.is_asked(2)
|
|
52
|
+
running = await ana.my_running_conversations()
|
|
53
|
+
model.answers(2)
|
|
54
|
+
|
|
55
|
+
assert sorted(running) == sorted([first, second])
|
|
56
|
+
for conversation in (first, second):
|
|
57
|
+
await ana.waits_for_the_turns(conversation)
|
|
58
|
+
assert await ana.last_reply(conversation) == 'Answer.'
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@scenario('R2', 'A person stops a turn in progress, and the next message starts afresh')
|
|
62
|
+
async def _(a: AgentsDsl):
|
|
63
|
+
ana = a.as_('Ana')
|
|
64
|
+
conversation = await ana.starts_a_conversation(await ana.creates_an_agent('Troy'))
|
|
65
|
+
model = a.the_model_replies(says('Reading forty documents'), says('Fresh start.'), hold=True)
|
|
66
|
+
await ana.sends(conversation, 'Read them all')
|
|
67
|
+
await model.is_asked(1)
|
|
68
|
+
|
|
69
|
+
stop = await ana.stops(conversation)
|
|
70
|
+
model.answers(1)
|
|
71
|
+
await ana.waits_for_the_turns(conversation)
|
|
72
|
+
|
|
73
|
+
assert stop['status'] == 'succeeded', stop
|
|
74
|
+
assert (await ana.transcript(conversation))[-1] == ('system', 'Stopped.')
|
|
75
|
+
assert (await ana.get(conversation))['stop_requested'] is False
|
|
76
|
+
model.answers(1)
|
|
77
|
+
assert await ana.asks(conversation, 'Start again') == 'Fresh start.'
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
@scenario('R2', 'Stopping a conversation with no turn running changes nothing')
|
|
81
|
+
async def _(a: AgentsDsl):
|
|
82
|
+
ana = a.as_('Ana')
|
|
83
|
+
conversation = await ana.starts_a_conversation(await ana.creates_an_agent('Troy'))
|
|
84
|
+
|
|
85
|
+
assert (await ana.stops(conversation))['status'] == 'succeeded'
|
|
86
|
+
|
|
87
|
+
state = await ana.get(conversation)
|
|
88
|
+
assert (state['status'], state['stop_requested']) == ('idle', False)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
@scenario('R2', 'A second message sent during a turn is read by that turn, and starts no other reply')
|
|
92
|
+
async def _(a: AgentsDsl):
|
|
93
|
+
ana = a.as_('Ana')
|
|
94
|
+
conversation = await ana.starts_a_conversation(await ana.creates_an_agent('Troy'))
|
|
95
|
+
model = a.the_model_replies(opens('/agents'), says('Agenda, with a budget slot.'), hold=True)
|
|
96
|
+
await ana.sends(conversation, 'Draft the agenda')
|
|
97
|
+
await model.is_asked(1)
|
|
98
|
+
|
|
99
|
+
await ana.sends(conversation, 'Add a slot for budget')
|
|
100
|
+
model.answers(1)
|
|
101
|
+
await model.is_asked(2)
|
|
102
|
+
model.answers(1)
|
|
103
|
+
await ana.waits_for_the_turns(conversation, at_least=2)
|
|
104
|
+
|
|
105
|
+
assert model.saw('Add a slot for budget', call=1)
|
|
106
|
+
assert model.call_count == 2
|
|
107
|
+
assert await ana.last_reply(conversation) == 'Agenda, with a budget slot.'
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
@scenario('R2', "Without the provider's key, the turn ends with a message naming it")
|
|
111
|
+
async def _(a: AgentsDsl):
|
|
112
|
+
ana = a.as_('Ana')
|
|
113
|
+
conversation = await ana.starts_a_conversation(await ana.creates_an_agent('Troy'))
|
|
114
|
+
|
|
115
|
+
await ana.asks(conversation, 'Hello?')
|
|
116
|
+
|
|
117
|
+
[turn] = await ana.turns_on(conversation)
|
|
118
|
+
role, content = (await ana.transcript(conversation))[-1]
|
|
119
|
+
assert turn['status'] == 'succeeded' and turn['ret_val']['status'] == 'error', turn
|
|
120
|
+
assert role == 'system' and 'ANTHROPIC_API_KEY' in content, content
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
@scenario('R2', 'A model that refuses a request ends the turn with its error, and the next message is answered')
|
|
124
|
+
async def _(a: AgentsDsl):
|
|
125
|
+
ana = a.as_('Ana')
|
|
126
|
+
conversation = await ana.starts_a_conversation(await ana.creates_an_agent('Troy'))
|
|
127
|
+
a.the_model_replies(fails('invalid_request', 'the prompt is malformed'), says('Back again.'))
|
|
128
|
+
|
|
129
|
+
await ana.asks(conversation, 'First try')
|
|
130
|
+
role, content = (await ana.transcript(conversation))[-1]
|
|
131
|
+
reply = await ana.asks(conversation, 'Second try')
|
|
132
|
+
|
|
133
|
+
assert role == 'system' and 'the prompt is malformed' in content, content
|
|
134
|
+
assert reply == 'Back again.'
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
@scenario('R2', 'A busy model is asked again, and the reply arrives')
|
|
138
|
+
async def _(a: AgentsDsl):
|
|
139
|
+
ana = a.as_('Ana')
|
|
140
|
+
conversation = await ana.starts_a_conversation(await ana.creates_an_agent('Troy'))
|
|
141
|
+
model = a.the_model_replies(fails('overloaded', 'busy', retryable=True), says('Made it.'))
|
|
142
|
+
|
|
143
|
+
reply = await ana.asks(conversation, 'Hello')
|
|
144
|
+
|
|
145
|
+
assert reply == 'Made it.'
|
|
146
|
+
assert model.call_count == 2
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"""R3 — The agent acts as the sender: with that person's permissions and
|
|
2
|
+
nothing more, and every strategy it ran on record."""
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from graphcore.public.live_testing import scenario
|
|
6
|
+
|
|
7
|
+
from agents.specs.dsl import INSTRUCTION, AgentsDsl, opens, runs, says
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@scenario('R3', "The agent's strategy runs as the sender and is recorded on its tool call")
|
|
11
|
+
async def _(a: AgentsDsl):
|
|
12
|
+
ana = a.as_('Ana')
|
|
13
|
+
conversation = await ana.starts_a_conversation(await ana.creates_an_agent('Troy'))
|
|
14
|
+
a.the_model_replies(runs('CreateInstruction', name='Venues', text='Three near Charleston.'), says('Saved.'))
|
|
15
|
+
|
|
16
|
+
await ana.asks(conversation, 'Keep a note of the venues')
|
|
17
|
+
|
|
18
|
+
[call] = await ana.tool_calls_in(conversation)
|
|
19
|
+
assert call['state'] == 'completed', call
|
|
20
|
+
request = await ana.get(call['request_id'])
|
|
21
|
+
assert (request['strategy_name'], request['owner']) == ('CreateInstruction', await ana.entity_id())
|
|
22
|
+
[note] = await ana.find(INSTRUCTION, name='Venues')
|
|
23
|
+
assert note['owner'] == await ana.entity_id()
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@scenario('R3', 'The agent cannot do what its sender may not: the refusal is the call\'s error')
|
|
27
|
+
async def _(a: AgentsDsl):
|
|
28
|
+
ana, ben = a.as_('Ana'), a.as_('Ben')
|
|
29
|
+
troy = await ana.creates_an_agent('Troy')
|
|
30
|
+
await ana.shares(troy, with_=ben, role='viewer')
|
|
31
|
+
conversation = await ben.starts_a_conversation(troy)
|
|
32
|
+
model = a.the_model_replies(runs('UpdateAgent', troy, name='Ben\'s now'), says('I may not rename it.'))
|
|
33
|
+
|
|
34
|
+
reply = await ben.asks(conversation, 'Rename yourself')
|
|
35
|
+
|
|
36
|
+
[call] = await ben.tool_calls_in(conversation)
|
|
37
|
+
assert call['state'] == 'error' and call['error'], call
|
|
38
|
+
assert reply == 'I may not rename it.'
|
|
39
|
+
assert model.saw(call['error'])
|
|
40
|
+
assert (await ana.get(troy))['name'] == 'Troy'
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@scenario('R3', 'Who owns the conversation confers nothing: each sender\'s turn has only their access')
|
|
44
|
+
async def _(a: AgentsDsl):
|
|
45
|
+
ana, ben = a.as_('Ana'), a.as_('Ben')
|
|
46
|
+
troy = await ana.creates_an_agent('Troy')
|
|
47
|
+
await ana.shares(troy, with_=ben)
|
|
48
|
+
conversation = await ana.starts_a_conversation(troy)
|
|
49
|
+
await ana.shares(conversation, with_=ben, role='editor')
|
|
50
|
+
private = await ana.writes_an_instruction('The budget is 40k.', name='Budget')
|
|
51
|
+
a.the_model_replies(opens(private), says('I cannot see it.'), opens(private), says('The budget is 40k.'))
|
|
52
|
+
|
|
53
|
+
await ben.asks(conversation, 'What is the budget?')
|
|
54
|
+
await ana.asks(conversation, 'What is the budget?')
|
|
55
|
+
|
|
56
|
+
bens, anas = await ana.tool_calls_in(conversation)
|
|
57
|
+
assert bens['state'] == 'error' and anas['state'] == 'completed', (bens, anas)
|
|
58
|
+
assert 'The budget is 40k.' in anas['result'] and 'The budget is 40k.' not in (bens['error'] or '')
|
|
59
|
+
[bens_turn], [anas_turn] = await ben.turns_on(conversation), await ana.turns_on(conversation)
|
|
60
|
+
assert (bens_turn['owner'], anas_turn['owner']) == (await ben.entity_id(), await ana.entity_id())
|