camel-ai 0.2.1__py3-none-any.whl → 0.2.1a0__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
CHANGED
camel/agents/chat_agent.py
CHANGED
|
@@ -15,6 +15,8 @@ from __future__ import annotations
|
|
|
15
15
|
|
|
16
16
|
import json
|
|
17
17
|
import logging
|
|
18
|
+
import re
|
|
19
|
+
import uuid
|
|
18
20
|
from collections import defaultdict
|
|
19
21
|
from typing import (
|
|
20
22
|
TYPE_CHECKING,
|
|
@@ -28,6 +30,7 @@ from typing import (
|
|
|
28
30
|
)
|
|
29
31
|
|
|
30
32
|
from openai.types.chat import ChatCompletionMessageToolCall
|
|
33
|
+
from openai.types.chat.chat_completion_message_tool_call import Function
|
|
31
34
|
from pydantic import BaseModel
|
|
32
35
|
|
|
33
36
|
from camel.agents.base import BaseAgent
|
|
@@ -190,7 +193,7 @@ class ChatAgent(BaseAgent):
|
|
|
190
193
|
tool.get_openai_tool_schema() for tool in all_tools
|
|
191
194
|
]
|
|
192
195
|
self.model_backend.model_config_dict['tools'] = tool_schema_list
|
|
193
|
-
|
|
196
|
+
self.tool_schema_list = tool_schema_list
|
|
194
197
|
self.model_config_dict = self.model_backend.model_config_dict
|
|
195
198
|
|
|
196
199
|
self.model_token_limit = token_limit or self.model_backend.token_limit
|
|
@@ -206,6 +209,56 @@ class ChatAgent(BaseAgent):
|
|
|
206
209
|
self.response_terminators = response_terminators or []
|
|
207
210
|
self.init_messages()
|
|
208
211
|
|
|
212
|
+
# ruff: noqa: E501
|
|
213
|
+
def _generate_tool_prompt(self, tool_schema_list: List[Dict]) -> str:
|
|
214
|
+
tool_prompts = []
|
|
215
|
+
|
|
216
|
+
for tool in tool_schema_list:
|
|
217
|
+
tool_info = tool['function']
|
|
218
|
+
tool_name = tool_info['name']
|
|
219
|
+
tool_description = tool_info['description']
|
|
220
|
+
tool_json = json.dumps(tool_info, indent=4)
|
|
221
|
+
|
|
222
|
+
prompt = f"Use the function '{tool_name}' to '{tool_description}':\n{tool_json}\n"
|
|
223
|
+
tool_prompts.append(prompt)
|
|
224
|
+
|
|
225
|
+
tool_prompt_str = "\n".join(tool_prompts)
|
|
226
|
+
|
|
227
|
+
final_prompt = f'''
|
|
228
|
+
# Tool prompt
|
|
229
|
+
TOOL_PROMPT = f"""
|
|
230
|
+
You have access to the following functions:
|
|
231
|
+
|
|
232
|
+
{tool_prompt_str}
|
|
233
|
+
|
|
234
|
+
If you choose to call a function ONLY reply in the following format with no prefix or suffix:
|
|
235
|
+
|
|
236
|
+
<function=example_function_name>{{"example_name": "example_value"}}</function>
|
|
237
|
+
|
|
238
|
+
Reminder:
|
|
239
|
+
- Function calls MUST follow the specified format, start with <function= and end with </function>
|
|
240
|
+
- Required parameters MUST be specified
|
|
241
|
+
- Only call one function at a time
|
|
242
|
+
- Put the entire function call reply on one line
|
|
243
|
+
- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls
|
|
244
|
+
"""
|
|
245
|
+
'''
|
|
246
|
+
return final_prompt
|
|
247
|
+
|
|
248
|
+
def _parse_tool_response(self, response: str):
|
|
249
|
+
function_regex = r"<function=(\w+)>(.*?)</function>"
|
|
250
|
+
match = re.search(function_regex, response)
|
|
251
|
+
|
|
252
|
+
if match:
|
|
253
|
+
function_name, args_string = match.groups()
|
|
254
|
+
try:
|
|
255
|
+
args = json.loads(args_string)
|
|
256
|
+
return {"function": function_name, "arguments": args}
|
|
257
|
+
except json.JSONDecodeError as error:
|
|
258
|
+
print(f"Error parsing function arguments: {error}")
|
|
259
|
+
return None
|
|
260
|
+
return None
|
|
261
|
+
|
|
209
262
|
def reset(self):
|
|
210
263
|
r"""Resets the :obj:`ChatAgent` to its initial state and returns the
|
|
211
264
|
stored messages.
|
|
@@ -367,89 +420,221 @@ class ChatAgent(BaseAgent):
|
|
|
367
420
|
a boolean indicating whether the chat session has terminated,
|
|
368
421
|
and information about the chat session.
|
|
369
422
|
"""
|
|
370
|
-
|
|
423
|
+
if (
|
|
424
|
+
isinstance(self.model_type, ModelType)
|
|
425
|
+
and "lama" in self.model_type.value
|
|
426
|
+
or isinstance(self.model_type, str)
|
|
427
|
+
and "lama" in self.model_type
|
|
428
|
+
):
|
|
429
|
+
if self.model_backend.model_config_dict['tools']:
|
|
430
|
+
tool_prompt = self._generate_tool_prompt(self.tool_schema_list)
|
|
431
|
+
|
|
432
|
+
tool_sys_msg = BaseMessage.make_assistant_message(
|
|
433
|
+
role_name="Assistant",
|
|
434
|
+
content=tool_prompt,
|
|
435
|
+
)
|
|
371
436
|
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
437
|
+
self.update_memory(tool_sys_msg, OpenAIBackendRole.SYSTEM)
|
|
438
|
+
|
|
439
|
+
self.update_memory(input_message, OpenAIBackendRole.USER)
|
|
440
|
+
|
|
441
|
+
tool_call_records: List[FunctionCallingRecord] = []
|
|
442
|
+
while True:
|
|
443
|
+
# Check if token has exceeded
|
|
444
|
+
try:
|
|
445
|
+
openai_messages, num_tokens = self.memory.get_context()
|
|
446
|
+
except RuntimeError as e:
|
|
447
|
+
return self._step_token_exceed(
|
|
448
|
+
e.args[1], tool_call_records, "max_tokens_exceeded"
|
|
449
|
+
)
|
|
450
|
+
|
|
451
|
+
(
|
|
452
|
+
response,
|
|
453
|
+
output_messages,
|
|
454
|
+
finish_reasons,
|
|
455
|
+
usage_dict,
|
|
456
|
+
response_id,
|
|
457
|
+
) = self._step_model_response(openai_messages, num_tokens)
|
|
458
|
+
# If the model response is not a function call, meaning the
|
|
459
|
+
# model has generated a message response, break the loop
|
|
460
|
+
if (
|
|
461
|
+
not self.is_tools_added()
|
|
462
|
+
or not isinstance(response, ChatCompletion)
|
|
463
|
+
or "</function>" not in response.choices[0].message.content # type: ignore[operator]
|
|
464
|
+
):
|
|
465
|
+
break
|
|
466
|
+
|
|
467
|
+
parsed_content = self._parse_tool_response(
|
|
468
|
+
response.choices[0].message.content # type: ignore[arg-type]
|
|
380
469
|
)
|
|
381
470
|
|
|
382
|
-
|
|
383
|
-
|
|
471
|
+
response.choices[0].message.tool_calls = [
|
|
472
|
+
ChatCompletionMessageToolCall(
|
|
473
|
+
id=str(uuid.uuid4()),
|
|
474
|
+
function=Function(
|
|
475
|
+
arguments=str(parsed_content["arguments"]).replace(
|
|
476
|
+
"'", '"'
|
|
477
|
+
),
|
|
478
|
+
name=str(parsed_content["function"]),
|
|
479
|
+
),
|
|
480
|
+
type="function",
|
|
481
|
+
)
|
|
482
|
+
]
|
|
483
|
+
|
|
484
|
+
# Check for external tool call
|
|
485
|
+
tool_call_request = response.choices[0].message.tool_calls[0]
|
|
486
|
+
if tool_call_request.function.name in self.external_tool_names:
|
|
487
|
+
# if model calls an external tool, directly return the
|
|
488
|
+
# request
|
|
489
|
+
info = self._step_get_info(
|
|
490
|
+
output_messages,
|
|
491
|
+
finish_reasons,
|
|
492
|
+
usage_dict,
|
|
493
|
+
response_id,
|
|
494
|
+
tool_call_records,
|
|
495
|
+
num_tokens,
|
|
496
|
+
tool_call_request,
|
|
497
|
+
)
|
|
498
|
+
return ChatAgentResponse(
|
|
499
|
+
msgs=output_messages,
|
|
500
|
+
terminated=self.terminated,
|
|
501
|
+
info=info,
|
|
502
|
+
)
|
|
503
|
+
|
|
504
|
+
# Normal function calling
|
|
505
|
+
tool_call_records.append(
|
|
506
|
+
self._step_tool_call_and_update(response)
|
|
507
|
+
)
|
|
508
|
+
|
|
509
|
+
if (
|
|
510
|
+
output_schema is not None
|
|
511
|
+
and self.model_type.supports_tool_calling
|
|
512
|
+
):
|
|
513
|
+
(
|
|
514
|
+
output_messages,
|
|
515
|
+
finish_reasons,
|
|
516
|
+
usage_dict,
|
|
517
|
+
response_id,
|
|
518
|
+
tool_call,
|
|
519
|
+
num_tokens,
|
|
520
|
+
) = self._structure_output_with_function(output_schema)
|
|
521
|
+
tool_call_records.append(tool_call)
|
|
522
|
+
|
|
523
|
+
info = self._step_get_info(
|
|
384
524
|
output_messages,
|
|
385
525
|
finish_reasons,
|
|
386
526
|
usage_dict,
|
|
387
527
|
response_id,
|
|
388
|
-
|
|
528
|
+
tool_call_records,
|
|
529
|
+
num_tokens,
|
|
530
|
+
)
|
|
389
531
|
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
532
|
+
if len(output_messages) == 1:
|
|
533
|
+
# Auto record if the output result is a single message
|
|
534
|
+
self.record_message(output_messages[0])
|
|
535
|
+
else:
|
|
536
|
+
logger.warning(
|
|
537
|
+
"Multiple messages returned in `step()`, message won't be "
|
|
538
|
+
"recorded automatically. Please call `record_message()` "
|
|
539
|
+
"to record the selected message manually."
|
|
540
|
+
)
|
|
398
541
|
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
542
|
+
return ChatAgentResponse(
|
|
543
|
+
msgs=output_messages, terminated=self.terminated, info=info
|
|
544
|
+
)
|
|
545
|
+
|
|
546
|
+
else:
|
|
547
|
+
self.update_memory(input_message, OpenAIBackendRole.USER)
|
|
548
|
+
|
|
549
|
+
tool_call_records: List[FunctionCallingRecord] = [] # type: ignore[no-redef]
|
|
550
|
+
while True:
|
|
551
|
+
# Check if token has exceeded
|
|
552
|
+
try:
|
|
553
|
+
openai_messages, num_tokens = self.memory.get_context()
|
|
554
|
+
except RuntimeError as e:
|
|
555
|
+
return self._step_token_exceed(
|
|
556
|
+
e.args[1], tool_call_records, "max_tokens_exceeded"
|
|
557
|
+
)
|
|
558
|
+
|
|
559
|
+
(
|
|
560
|
+
response,
|
|
404
561
|
output_messages,
|
|
405
562
|
finish_reasons,
|
|
406
563
|
usage_dict,
|
|
407
564
|
response_id,
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
565
|
+
) = self._step_model_response(openai_messages, num_tokens)
|
|
566
|
+
# If the model response is not a function call, meaning the
|
|
567
|
+
# model has generated a message response, break the loop
|
|
568
|
+
if (
|
|
569
|
+
not self.is_tools_added()
|
|
570
|
+
or not isinstance(response, ChatCompletion)
|
|
571
|
+
or response.choices[0].message.tool_calls is None
|
|
572
|
+
):
|
|
573
|
+
break
|
|
574
|
+
|
|
575
|
+
# Check for external tool call
|
|
576
|
+
tool_call_request = response.choices[0].message.tool_calls[0]
|
|
577
|
+
|
|
578
|
+
if tool_call_request.function.name in self.external_tool_names:
|
|
579
|
+
# if model calls an external tool, directly return the
|
|
580
|
+
# request
|
|
581
|
+
info = self._step_get_info(
|
|
582
|
+
output_messages,
|
|
583
|
+
finish_reasons,
|
|
584
|
+
usage_dict,
|
|
585
|
+
response_id,
|
|
586
|
+
tool_call_records,
|
|
587
|
+
num_tokens,
|
|
588
|
+
tool_call_request,
|
|
589
|
+
)
|
|
590
|
+
return ChatAgentResponse(
|
|
591
|
+
msgs=output_messages,
|
|
592
|
+
terminated=self.terminated,
|
|
593
|
+
info=info,
|
|
594
|
+
)
|
|
595
|
+
|
|
596
|
+
# Normal function calling
|
|
597
|
+
tool_call_records.append(
|
|
598
|
+
self._step_tool_call_and_update(response)
|
|
414
599
|
)
|
|
415
600
|
|
|
416
|
-
|
|
417
|
-
|
|
601
|
+
if (
|
|
602
|
+
output_schema is not None
|
|
603
|
+
and self.model_type.supports_tool_calling
|
|
604
|
+
):
|
|
605
|
+
(
|
|
606
|
+
output_messages,
|
|
607
|
+
finish_reasons,
|
|
608
|
+
usage_dict,
|
|
609
|
+
response_id,
|
|
610
|
+
tool_call,
|
|
611
|
+
num_tokens,
|
|
612
|
+
) = self._structure_output_with_function(output_schema)
|
|
613
|
+
tool_call_records.append(tool_call)
|
|
418
614
|
|
|
419
|
-
|
|
420
|
-
(
|
|
615
|
+
info = self._step_get_info(
|
|
421
616
|
output_messages,
|
|
422
617
|
finish_reasons,
|
|
423
618
|
usage_dict,
|
|
424
619
|
response_id,
|
|
425
|
-
|
|
620
|
+
tool_call_records,
|
|
426
621
|
num_tokens,
|
|
427
|
-
)
|
|
428
|
-
tool_call_records.append(tool_call)
|
|
622
|
+
)
|
|
429
623
|
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
624
|
+
if len(output_messages) == 1:
|
|
625
|
+
# Auto record if the output result is a single message
|
|
626
|
+
self.record_message(output_messages[0])
|
|
627
|
+
else:
|
|
628
|
+
logger.warning(
|
|
629
|
+
"Multiple messages returned in `step()`, message won't be "
|
|
630
|
+
"recorded automatically. Please call `record_message()` "
|
|
631
|
+
"to record the selected message manually."
|
|
632
|
+
)
|
|
438
633
|
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
self.record_message(output_messages[0])
|
|
442
|
-
else:
|
|
443
|
-
logger.warning(
|
|
444
|
-
"Multiple messages returned in `step()`, message won't be "
|
|
445
|
-
"recorded automatically. Please call `record_message()` to "
|
|
446
|
-
"record the selected message manually."
|
|
634
|
+
return ChatAgentResponse(
|
|
635
|
+
msgs=output_messages, terminated=self.terminated, info=info
|
|
447
636
|
)
|
|
448
637
|
|
|
449
|
-
return ChatAgentResponse(
|
|
450
|
-
msgs=output_messages, terminated=self.terminated, info=info
|
|
451
|
-
)
|
|
452
|
-
|
|
453
638
|
async def step_async(
|
|
454
639
|
self,
|
|
455
640
|
input_message: BaseMessage,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: camel-ai
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.1a0
|
|
4
4
|
Summary: Communicative Agents for AI Society Study
|
|
5
5
|
Home-page: https://www.camel-ai.org/
|
|
6
6
|
License: Apache-2.0
|
|
@@ -214,7 +214,7 @@ conda create --name camel python=3.10
|
|
|
214
214
|
conda activate camel
|
|
215
215
|
|
|
216
216
|
# Clone github repo
|
|
217
|
-
git clone -b v0.2.
|
|
217
|
+
git clone -b v0.2.1a https://github.com/camel-ai/camel.git
|
|
218
218
|
|
|
219
219
|
# Change directory into project directory
|
|
220
220
|
cd camel
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
camel/__init__.py,sha256=
|
|
1
|
+
camel/__init__.py,sha256=9N1NbcJdF9CwOgasAudDEk2Sa1Qn-BsqHDza_y6SIck,779
|
|
2
2
|
camel/agents/__init__.py,sha256=SSU1wbhZXWwQnE0rRxkpyN57kEu72KklsZNcdLkXfTs,1551
|
|
3
3
|
camel/agents/base.py,sha256=X39qWSiT1WnDqaJ9k3gQrTpOQSwUKzNEVpp5AY6fDH8,1130
|
|
4
|
-
camel/agents/chat_agent.py,sha256=
|
|
4
|
+
camel/agents/chat_agent.py,sha256=JvtLmy3UDYH0s1dkZ8IUUMBuRC1613YMLIraDhPyesc,43721
|
|
5
5
|
camel/agents/critic_agent.py,sha256=To-istnO-9Eb0iabdeIDrgfvkxYYfsdX9xIZiSrc3oM,7493
|
|
6
6
|
camel/agents/deductive_reasoner_agent.py,sha256=49vwglWYHgXf-VRftdMN9OFGOwqdsXyTt45PP6z-pbg,13473
|
|
7
7
|
camel/agents/embodied_agent.py,sha256=3ABuiRQXBpplKbuhPY5KNLJyKc6Z8SgXgzIges3ZwVs,7542
|
|
@@ -190,6 +190,6 @@ camel/workforce/task_channel.py,sha256=oQJ-qzXWBqZljwV3EdZ7Kpo7RExZfI_tqg0bop14c
|
|
|
190
190
|
camel/workforce/utils.py,sha256=kBrjA_k9BblJBT13kW0eQYcBDjetyjOUHMbRuh_IfPM,2270
|
|
191
191
|
camel/workforce/worker.py,sha256=oZtl3PhxgpizKy7W_wB94Qm4Z2Tw4k1SQsqAGAi_XoI,3844
|
|
192
192
|
camel/workforce/workforce.py,sha256=lF0ZeGtol91N3kAlAFwDxnO6YYVKmZPohxNb5ezHEUc,18185
|
|
193
|
-
camel_ai-0.2.
|
|
194
|
-
camel_ai-0.2.
|
|
195
|
-
camel_ai-0.2.
|
|
193
|
+
camel_ai-0.2.1a0.dist-info/METADATA,sha256=Pv74n2TNKom6GR_Rs0i1B3Y52Fs3jCGXDkNG2sMecW4,24680
|
|
194
|
+
camel_ai-0.2.1a0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
|
195
|
+
camel_ai-0.2.1a0.dist-info/RECORD,,
|
|
File without changes
|