camel-ai 0.2.71a5__py3-none-any.whl → 0.2.71a7__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.
Potentially problematic release.
This version of camel-ai might be problematic. Click here for more details.
- camel/__init__.py +1 -1
- camel/agents/chat_agent.py +51 -1
- camel/societies/workforce/role_playing_worker.py +64 -7
- camel/societies/workforce/single_agent_worker.py +73 -18
- camel/societies/workforce/structured_output_handler.py +500 -0
- camel/societies/workforce/worker.py +2 -0
- camel/societies/workforce/workforce.py +312 -147
- camel/tasks/task.py +1 -1
- camel/toolkits/file_write_toolkit.py +179 -124
- camel/toolkits/hybrid_browser_toolkit/actions.py +235 -60
- camel/toolkits/hybrid_browser_toolkit/agent.py +25 -8
- camel/toolkits/hybrid_browser_toolkit/browser_session.py +574 -164
- camel/toolkits/hybrid_browser_toolkit/hybrid_browser_toolkit.py +996 -126
- camel/toolkits/hybrid_browser_toolkit/stealth_config.py +116 -0
- camel/toolkits/hybrid_browser_toolkit/stealth_script.js +0 -0
- camel/toolkits/note_taking_toolkit.py +7 -13
- {camel_ai-0.2.71a5.dist-info → camel_ai-0.2.71a7.dist-info}/METADATA +1 -1
- {camel_ai-0.2.71a5.dist-info → camel_ai-0.2.71a7.dist-info}/RECORD +20 -17
- {camel_ai-0.2.71a5.dist-info → camel_ai-0.2.71a7.dist-info}/WHEEL +0 -0
- {camel_ai-0.2.71a5.dist-info → camel_ai-0.2.71a7.dist-info}/licenses/LICENSE +0 -0
camel/__init__.py
CHANGED
camel/agents/chat_agent.py
CHANGED
|
@@ -3815,7 +3815,7 @@ class ChatAgent(BaseAgent):
|
|
|
3815
3815
|
self.memory.get_context_creator(), "token_limit", None
|
|
3816
3816
|
),
|
|
3817
3817
|
output_language=self._output_language,
|
|
3818
|
-
tools=
|
|
3818
|
+
tools=self._clone_tools(),
|
|
3819
3819
|
external_tools=[
|
|
3820
3820
|
schema for schema in self._external_tool_schemas.values()
|
|
3821
3821
|
],
|
|
@@ -3839,6 +3839,56 @@ class ChatAgent(BaseAgent):
|
|
|
3839
3839
|
|
|
3840
3840
|
return new_agent
|
|
3841
3841
|
|
|
3842
|
+
def _clone_tools(self) -> List[Union[FunctionTool, Callable]]:
|
|
3843
|
+
r"""Clone tools for new agent instance,
|
|
3844
|
+
handling stateful toolkits properly."""
|
|
3845
|
+
cloned_tools = []
|
|
3846
|
+
hybrid_browser_toolkits = {} # Cache for created toolkits
|
|
3847
|
+
|
|
3848
|
+
for tool in self._internal_tools.values():
|
|
3849
|
+
# Check if this is a HybridBrowserToolkit method
|
|
3850
|
+
if (
|
|
3851
|
+
hasattr(tool.func, '__self__')
|
|
3852
|
+
and tool.func.__self__.__class__.__name__
|
|
3853
|
+
== 'HybridBrowserToolkit'
|
|
3854
|
+
):
|
|
3855
|
+
toolkit_instance = tool.func.__self__
|
|
3856
|
+
toolkit_id = id(toolkit_instance)
|
|
3857
|
+
|
|
3858
|
+
# Check if we already created a clone for this toolkit
|
|
3859
|
+
if toolkit_id not in hybrid_browser_toolkits:
|
|
3860
|
+
try:
|
|
3861
|
+
import uuid
|
|
3862
|
+
|
|
3863
|
+
new_session_id = str(uuid.uuid4())[:8]
|
|
3864
|
+
new_toolkit = toolkit_instance.clone_for_new_session(
|
|
3865
|
+
new_session_id
|
|
3866
|
+
)
|
|
3867
|
+
hybrid_browser_toolkits[toolkit_id] = new_toolkit
|
|
3868
|
+
except Exception as e:
|
|
3869
|
+
logger.warning(
|
|
3870
|
+
f"Failed to clone HybridBrowserToolkit: {e}"
|
|
3871
|
+
)
|
|
3872
|
+
# Fallback to original function
|
|
3873
|
+
cloned_tools.append(tool.func)
|
|
3874
|
+
continue
|
|
3875
|
+
|
|
3876
|
+
# Get the corresponding method from the cloned toolkit
|
|
3877
|
+
new_toolkit = hybrid_browser_toolkits[toolkit_id]
|
|
3878
|
+
method_name = tool.func.__name__
|
|
3879
|
+
if hasattr(new_toolkit, method_name):
|
|
3880
|
+
new_method = getattr(new_toolkit, method_name)
|
|
3881
|
+
cloned_tools.append(new_method)
|
|
3882
|
+
else:
|
|
3883
|
+
# Fallback to original function
|
|
3884
|
+
cloned_tools.append(tool.func)
|
|
3885
|
+
else:
|
|
3886
|
+
# Regular tool or other stateless toolkit
|
|
3887
|
+
# just use the original function
|
|
3888
|
+
cloned_tools.append(tool.func)
|
|
3889
|
+
|
|
3890
|
+
return cloned_tools
|
|
3891
|
+
|
|
3842
3892
|
def __repr__(self) -> str:
|
|
3843
3893
|
r"""Returns a string representation of the :obj:`ChatAgent`.
|
|
3844
3894
|
|
|
@@ -13,7 +13,6 @@
|
|
|
13
13
|
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
|
|
14
14
|
from __future__ import annotations
|
|
15
15
|
|
|
16
|
-
import json
|
|
17
16
|
from typing import Dict, List, Optional
|
|
18
17
|
|
|
19
18
|
from colorama import Fore
|
|
@@ -25,6 +24,9 @@ from camel.societies.workforce.prompts import (
|
|
|
25
24
|
ROLEPLAY_PROCESS_TASK_PROMPT,
|
|
26
25
|
ROLEPLAY_SUMMARIZE_PROMPT,
|
|
27
26
|
)
|
|
27
|
+
from camel.societies.workforce.structured_output_handler import (
|
|
28
|
+
StructuredOutputHandler,
|
|
29
|
+
)
|
|
28
30
|
from camel.societies.workforce.utils import TaskResult
|
|
29
31
|
from camel.societies.workforce.worker import Worker
|
|
30
32
|
from camel.tasks.task import Task, TaskState, is_task_result_insufficient
|
|
@@ -48,6 +50,14 @@ class RolePlayingWorker(Worker):
|
|
|
48
50
|
(default: :obj:`None`)
|
|
49
51
|
chat_turn_limit (int): The maximum number of chat turns in the role
|
|
50
52
|
playing. (default: :obj:`20`)
|
|
53
|
+
use_structured_output_handler (bool, optional): Whether to use the
|
|
54
|
+
structured output handler instead of native structured output.
|
|
55
|
+
When enabled, the workforce will use prompts with structured
|
|
56
|
+
output instructions and regex extraction to parse responses.
|
|
57
|
+
This ensures compatibility with agents that don't reliably
|
|
58
|
+
support native structured output. When disabled, the workforce
|
|
59
|
+
uses the native response_format parameter.
|
|
60
|
+
(default: :obj:`True`)
|
|
51
61
|
"""
|
|
52
62
|
|
|
53
63
|
def __init__(
|
|
@@ -59,8 +69,15 @@ class RolePlayingWorker(Worker):
|
|
|
59
69
|
user_agent_kwargs: Optional[Dict] = None,
|
|
60
70
|
summarize_agent_kwargs: Optional[Dict] = None,
|
|
61
71
|
chat_turn_limit: int = 20,
|
|
72
|
+
use_structured_output_handler: bool = True,
|
|
62
73
|
) -> None:
|
|
63
74
|
super().__init__(description)
|
|
75
|
+
self.use_structured_output_handler = use_structured_output_handler
|
|
76
|
+
self.structured_handler = (
|
|
77
|
+
StructuredOutputHandler()
|
|
78
|
+
if use_structured_output_handler
|
|
79
|
+
else None
|
|
80
|
+
)
|
|
64
81
|
self.summarize_agent_kwargs = summarize_agent_kwargs
|
|
65
82
|
summ_sys_msg = BaseMessage.make_assistant_message(
|
|
66
83
|
role_name="Summarizer",
|
|
@@ -173,13 +190,53 @@ class RolePlayingWorker(Worker):
|
|
|
173
190
|
chat_history=chat_history_str,
|
|
174
191
|
additional_info=task.additional_info,
|
|
175
192
|
)
|
|
176
|
-
|
|
177
|
-
prompt
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
193
|
+
if self.use_structured_output_handler and self.structured_handler:
|
|
194
|
+
# Use structured output handler for prompt-based extraction
|
|
195
|
+
enhanced_prompt = (
|
|
196
|
+
self.structured_handler.generate_structured_prompt(
|
|
197
|
+
base_prompt=prompt,
|
|
198
|
+
schema=TaskResult,
|
|
199
|
+
examples=[
|
|
200
|
+
{
|
|
201
|
+
"content": "The assistant successfully completed "
|
|
202
|
+
"the task by...",
|
|
203
|
+
"failed": False,
|
|
204
|
+
}
|
|
205
|
+
],
|
|
206
|
+
additional_instructions=(
|
|
207
|
+
"Summarize the task execution based "
|
|
208
|
+
"on the chat history, clearly indicating whether "
|
|
209
|
+
"the task succeeded or failed."
|
|
210
|
+
),
|
|
211
|
+
)
|
|
212
|
+
)
|
|
213
|
+
response = self.summarize_agent.step(enhanced_prompt)
|
|
214
|
+
task_result = self.structured_handler.parse_structured_response(
|
|
215
|
+
response_text=response.msg.content if response.msg else "",
|
|
216
|
+
schema=TaskResult,
|
|
217
|
+
fallback_values={
|
|
218
|
+
"content": "Task summarization failed",
|
|
219
|
+
"failed": True,
|
|
220
|
+
},
|
|
221
|
+
)
|
|
222
|
+
else:
|
|
223
|
+
# Use native structured output if supported
|
|
224
|
+
response = self.summarize_agent.step(
|
|
225
|
+
prompt, response_format=TaskResult
|
|
226
|
+
)
|
|
227
|
+
if response.msg.parsed is None:
|
|
228
|
+
print(
|
|
229
|
+
f"{Fore.RED}Error in summarization: Invalid "
|
|
230
|
+
f"task result{Fore.RESET}"
|
|
231
|
+
)
|
|
232
|
+
task_result = TaskResult(
|
|
233
|
+
content="Failed to generate valid task summary.",
|
|
234
|
+
failed=True,
|
|
235
|
+
)
|
|
236
|
+
else:
|
|
237
|
+
task_result = response.msg.parsed
|
|
181
238
|
|
|
182
|
-
task.result = task_result.content
|
|
239
|
+
task.result = task_result.content # type: ignore[union-attr]
|
|
183
240
|
|
|
184
241
|
if is_task_result_insufficient(task):
|
|
185
242
|
print(
|
|
@@ -15,7 +15,6 @@ from __future__ import annotations
|
|
|
15
15
|
|
|
16
16
|
import asyncio
|
|
17
17
|
import datetime
|
|
18
|
-
import json
|
|
19
18
|
import time
|
|
20
19
|
from collections import deque
|
|
21
20
|
from typing import Any, List, Optional
|
|
@@ -24,6 +23,9 @@ from colorama import Fore
|
|
|
24
23
|
|
|
25
24
|
from camel.agents import ChatAgent
|
|
26
25
|
from camel.societies.workforce.prompts import PROCESS_TASK_PROMPT
|
|
26
|
+
from camel.societies.workforce.structured_output_handler import (
|
|
27
|
+
StructuredOutputHandler,
|
|
28
|
+
)
|
|
27
29
|
from camel.societies.workforce.utils import TaskResult
|
|
28
30
|
from camel.societies.workforce.worker import Worker
|
|
29
31
|
from camel.tasks.task import Task, TaskState, is_task_result_insufficient
|
|
@@ -186,6 +188,14 @@ class SingleAgentWorker(Worker):
|
|
|
186
188
|
(default: :obj:`10`)
|
|
187
189
|
auto_scale_pool (bool): Whether to auto-scale the agent pool.
|
|
188
190
|
(default: :obj:`True`)
|
|
191
|
+
use_structured_output_handler (bool, optional): Whether to use the
|
|
192
|
+
structured output handler instead of native structured output.
|
|
193
|
+
When enabled, the workforce will use prompts with structured
|
|
194
|
+
output instructions and regex extraction to parse responses.
|
|
195
|
+
This ensures compatibility with agents that don't reliably
|
|
196
|
+
support native structured output. When disabled, the workforce
|
|
197
|
+
uses the native response_format parameter.
|
|
198
|
+
(default: :obj:`True`)
|
|
189
199
|
"""
|
|
190
200
|
|
|
191
201
|
def __init__(
|
|
@@ -196,12 +206,19 @@ class SingleAgentWorker(Worker):
|
|
|
196
206
|
pool_initial_size: int = 1,
|
|
197
207
|
pool_max_size: int = 10,
|
|
198
208
|
auto_scale_pool: bool = True,
|
|
209
|
+
use_structured_output_handler: bool = True,
|
|
199
210
|
) -> None:
|
|
200
211
|
node_id = worker.agent_id
|
|
201
212
|
super().__init__(
|
|
202
213
|
description,
|
|
203
214
|
node_id=node_id,
|
|
204
215
|
)
|
|
216
|
+
self.use_structured_output_handler = use_structured_output_handler
|
|
217
|
+
self.structured_handler = (
|
|
218
|
+
StructuredOutputHandler()
|
|
219
|
+
if use_structured_output_handler
|
|
220
|
+
else None
|
|
221
|
+
)
|
|
205
222
|
self.worker = worker
|
|
206
223
|
self.use_agent_pool = use_agent_pool
|
|
207
224
|
|
|
@@ -278,9 +295,43 @@ class SingleAgentWorker(Worker):
|
|
|
278
295
|
additional_info=task.additional_info,
|
|
279
296
|
)
|
|
280
297
|
|
|
281
|
-
|
|
282
|
-
prompt
|
|
283
|
-
|
|
298
|
+
if self.use_structured_output_handler and self.structured_handler:
|
|
299
|
+
# Use structured output handler for prompt-based extraction
|
|
300
|
+
enhanced_prompt = (
|
|
301
|
+
self.structured_handler.generate_structured_prompt(
|
|
302
|
+
base_prompt=prompt,
|
|
303
|
+
schema=TaskResult,
|
|
304
|
+
examples=[
|
|
305
|
+
{
|
|
306
|
+
"content": "I have successfully completed the "
|
|
307
|
+
"task...",
|
|
308
|
+
"failed": False,
|
|
309
|
+
}
|
|
310
|
+
],
|
|
311
|
+
additional_instructions="Ensure you provide a clear "
|
|
312
|
+
"description of what was done and whether the task "
|
|
313
|
+
"succeeded or failed.",
|
|
314
|
+
)
|
|
315
|
+
)
|
|
316
|
+
response = await worker_agent.astep(enhanced_prompt)
|
|
317
|
+
task_result = (
|
|
318
|
+
self.structured_handler.parse_structured_response(
|
|
319
|
+
response_text=response.msg.content
|
|
320
|
+
if response.msg
|
|
321
|
+
else "",
|
|
322
|
+
schema=TaskResult,
|
|
323
|
+
fallback_values={
|
|
324
|
+
"content": "Task processing failed",
|
|
325
|
+
"failed": True,
|
|
326
|
+
},
|
|
327
|
+
)
|
|
328
|
+
)
|
|
329
|
+
else:
|
|
330
|
+
# Use native structured output if supported
|
|
331
|
+
response = await worker_agent.astep(
|
|
332
|
+
prompt, response_format=TaskResult
|
|
333
|
+
)
|
|
334
|
+
task_result = response.msg.parsed
|
|
284
335
|
|
|
285
336
|
# Get token usage from the response
|
|
286
337
|
usage_info = response.info.get("usage") or response.info.get(
|
|
@@ -293,6 +344,8 @@ class SingleAgentWorker(Worker):
|
|
|
293
344
|
f"{Fore.RED}Error processing task {task.id}: "
|
|
294
345
|
f"{type(e).__name__}: {e}{Fore.RESET}"
|
|
295
346
|
)
|
|
347
|
+
# Store error information in task result
|
|
348
|
+
task.result = f"{type(e).__name__}: {e!s}"
|
|
296
349
|
return TaskState.FAILED
|
|
297
350
|
finally:
|
|
298
351
|
# Return agent to pool or let it be garbage collected
|
|
@@ -331,25 +384,27 @@ class SingleAgentWorker(Worker):
|
|
|
331
384
|
|
|
332
385
|
print(f"======\n{Fore.GREEN}Response from {self}:{Fore.RESET}")
|
|
333
386
|
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
task_result
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
387
|
+
if not self.use_structured_output_handler:
|
|
388
|
+
# Handle native structured output parsing
|
|
389
|
+
if task_result is None:
|
|
390
|
+
print(
|
|
391
|
+
f"{Fore.RED}Error in worker step execution: Invalid "
|
|
392
|
+
f"task result{Fore.RESET}"
|
|
393
|
+
)
|
|
394
|
+
task_result = TaskResult(
|
|
395
|
+
content="Failed to generate valid task result.",
|
|
396
|
+
failed=True,
|
|
397
|
+
)
|
|
398
|
+
|
|
399
|
+
color = Fore.RED if task_result.failed else Fore.GREEN # type: ignore[union-attr]
|
|
345
400
|
print(
|
|
346
|
-
f"\n{color}{task_result.content}{Fore.RESET}\n======",
|
|
401
|
+
f"\n{color}{task_result.content}{Fore.RESET}\n======", # type: ignore[union-attr]
|
|
347
402
|
)
|
|
348
403
|
|
|
349
|
-
if task_result.failed:
|
|
404
|
+
if task_result.failed: # type: ignore[union-attr]
|
|
350
405
|
return TaskState.FAILED
|
|
351
406
|
|
|
352
|
-
task.result = task_result.content
|
|
407
|
+
task.result = task_result.content # type: ignore[union-attr]
|
|
353
408
|
|
|
354
409
|
if is_task_result_insufficient(task):
|
|
355
410
|
print(
|