-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.py
2042 lines (1725 loc) · 71.9 KB
/
agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import threading
import asyncio
import concurrent.futures
import json
import logging
import os
import random
import sys
import time
import uuid
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
import toml
import yaml
from loguru import logger
from pydantic import BaseModel
from swarms_cloud.schema.agent_api_schemas import (
AgentChatCompletionResponse,
)
from swarms_cloud.schema.cog_vlm_schemas import (
ChatCompletionResponseChoice,
ChatMessageResponse,
UsageInfo,
)
from termcolor import colored
from swarms.memory.base_vectordb import BaseVectorDatabase
from swarms.models.tiktoken_wrapper import TikTokenizer
from swarms.prompts.agent_system_prompts import AGENT_SYSTEM_PROMPT_3
from swarms.prompts.aot_prompt import algorithm_of_thoughts_sop
from swarms.prompts.multi_modal_autonomous_instruction_prompt import (
MULTI_MODAL_AUTO_AGENT_SYSTEM_PROMPT_1,
)
from swarms.prompts.tools import tool_sop_prompt
from swarms.schemas.schemas import ManySteps, Step
from swarms.structs.conversation import Conversation
from swarms.tools.func_calling_utils import (
prepare_output_for_output_model,
pydantic_model_to_json_str,
)
from swarms.tools.prebuilt.code_executor import CodeExecutor
from swarms.tools.prebuilt.code_interpreter import (
SubprocessCodeInterpreter,
)
from swarms.tools.py_func_to_openai_func_str import (
get_openai_function_schema_from_func,
)
from swarms.tools.pydantic_to_json import (
multi_base_model_to_openai_function,
)
from swarms.tools.tool_parse_exec import parse_and_execute_json
from swarms.utils.data_to_text import data_to_text
from swarms.utils.file_processing import create_file_in_folder
from swarms.utils.parse_code import extract_code_from_markdown
from swarms.utils.pdf_to_text import pdf_to_text
# Utils
# Custom stopping condition
def stop_when_repeats(response: str) -> bool:
# Stop if the word stop appears in the response
return "stop" in response.lower()
# Parse done token
def parse_done_token(response: str) -> bool:
"""Parse the response to see if the done token is present"""
return "<DONE>" in response
# Agent ID generator
def agent_id():
"""Generate an agent id"""
return uuid.uuid4().hex
def exists(val):
return val is not None
# Agent output types
agent_output_type = Union[BaseModel, dict, str]
ToolUsageType = Union[BaseModel, Dict[str, Any]]
# [FEAT][AGENT]
class Agent:
"""
Agent is the backbone to connect LLMs with tools and long term memory. Agent also provides the ability to
ingest any type of docs like PDFs, Txts, Markdown, Json, and etc for the agent. Here is a list of features.
Args:
llm (Any): The language model to use
template (str): The template to use
max_loops (int): The maximum number of loops to run
stopping_condition (Callable): The stopping condition to use
loop_interval (int): The loop interval
retry_attempts (int): The number of retry attempts
retry_interval (int): The retry interval
return_history (bool): Return the history
stopping_token (str): The stopping token
dynamic_loops (bool): Enable dynamic loops
interactive (bool): Enable interactive mode
dashboard (bool): Enable dashboard
agent_name (str): The name of the agent
agent_description (str): The description of the agent
system_prompt (str): The system prompt
tools (List[BaseTool]): The tools to use
dynamic_temperature_enabled (bool): Enable dynamic temperature
sop (str): The standard operating procedure
sop_list (List[str]): The standard operating procedure list
saved_state_path (str): The path to the saved state
autosave (bool): Autosave the state
context_length (int): The context length
user_name (str): The user name
self_healing_enabled (bool): Enable self healing
code_interpreter (bool): Enable code interpreter
multi_modal (bool): Enable multimodal
pdf_path (str): The path to the pdf
list_of_pdf (str): The list of pdf
tokenizer (Any): The tokenizer
memory (BaseVectorDatabase): The memory
preset_stopping_token (bool): Enable preset stopping token
traceback (Any): The traceback
traceback_handlers (Any): The traceback handlers
streaming_on (bool): Enable streaming
Methods:
run: Run the agent
run_concurrent: Run the agent concurrently
bulk_run: Run the agent in bulk
save: Save the agent
load: Load the agent
validate_response: Validate the response
print_history_and_memory: Print the history and memory
step: Step through the agent
graceful_shutdown: Gracefully shutdown the agent
run_with_timeout: Run the agent with a timeout
analyze_feedback: Analyze the feedback
undo_last: Undo the last response
add_response_filter: Add a response filter
apply_response_filters: Apply the response filters
filtered_run: Run the agent with filtered responses
interactive_run: Run the agent in interactive mode
streamed_generation: Stream the generation of the response
save_state: Save the state
load_state: Load the state
truncate_history: Truncate the history
add_task_to_memory: Add the task to the memory
add_message_to_memory: Add the message to the memory
add_message_to_memory_and_truncate: Add the message to the memory and truncate
print_dashboard: Print the dashboard
loop_count_print: Print the loop count
streaming: Stream the content
_history: Generate the history
_dynamic_prompt_setup: Setup the dynamic prompt
run_async: Run the agent asynchronously
run_async_concurrent: Run the agent asynchronously and concurrently
run_async_concurrent: Run the agent asynchronously and concurrently
construct_dynamic_prompt: Construct the dynamic prompt
construct_dynamic_prompt: Construct the dynamic prompt
Examples:
>>> from swarms.models import OpenAIChat
>>> from swarms.structs import Agent
>>> llm = OpenAIChat()
>>> agent = Agent(llm=llm, max_loops=1)
>>> response = agent.run("Generate a report on the financials.")
>>> print(response)
>>> # Generate a report on the financials.
"""
def __init__(
self,
agent_id: Optional[str] = agent_id(),
id: Optional[str] = agent_id(),
llm: Optional[Any] = None,
template: Optional[str] = None,
max_loops: Optional[int] = 1,
stopping_condition: Optional[Callable[[str], bool]] = None,
loop_interval: Optional[int] = 0,
retry_attempts: Optional[int] = 3,
retry_interval: Optional[int] = 1,
return_history: Optional[bool] = False,
stopping_token: Optional[str] = None,
dynamic_loops: Optional[bool] = False,
interactive: Optional[bool] = False,
dashboard: Optional[bool] = False,
agent_name: Optional[str] = "swarm-worker-01",
agent_description: Optional[str] = None,
system_prompt: Optional[str] = AGENT_SYSTEM_PROMPT_3,
# TODO: Change to callable, then parse the callable to a string
tools: List[Callable] = None,
dynamic_temperature_enabled: Optional[bool] = False,
sop: Optional[str] = None,
sop_list: Optional[List[str]] = None,
saved_state_path: Optional[str] = None,
autosave: Optional[bool] = False,
context_length: Optional[int] = 8192,
user_name: Optional[str] = "Human:",
self_healing_enabled: Optional[bool] = False,
code_interpreter: Optional[bool] = False,
multi_modal: Optional[bool] = None,
pdf_path: Optional[str] = None,
list_of_pdf: Optional[str] = None,
tokenizer: Optional[Any] = TikTokenizer(),
long_term_memory: Optional[BaseVectorDatabase] = None,
preset_stopping_token: Optional[bool] = False,
traceback: Optional[Any] = None,
traceback_handlers: Optional[Any] = None,
streaming_on: Optional[bool] = False,
docs: List[str] = None,
docs_folder: Optional[str] = None,
verbose: Optional[bool] = False,
parser: Optional[Callable] = None,
best_of_n: Optional[int] = None,
callback: Optional[Callable] = None,
metadata: Optional[Dict[str, Any]] = None,
callbacks: Optional[List[Callable]] = None,
logger_handler: Optional[Any] = sys.stderr,
search_algorithm: Optional[Callable] = None,
logs_to_filename: Optional[str] = None,
evaluator: Optional[Callable] = None, # Custom LLM or agent
output_json: Optional[bool] = False,
stopping_func: Optional[Callable] = None,
custom_loop_condition: Optional[Callable] = None,
sentiment_threshold: Optional[
float
] = None, # Evaluate on output using an external model
custom_exit_command: Optional[str] = "exit",
sentiment_analyzer: Optional[Callable] = None,
limit_tokens_from_string: Optional[Callable] = None,
# [Tools]
custom_tools_prompt: Optional[Callable] = None,
tool_schema: ToolUsageType = None,
output_type: agent_output_type = None,
function_calling_type: str = "json",
output_cleaner: Optional[Callable] = None,
function_calling_format_type: Optional[str] = "OpenAI",
list_base_models: Optional[List[BaseModel]] = None,
metadata_output_type: str = "json",
state_save_file_type: str = "json",
chain_of_thoughts: bool = False,
algorithm_of_thoughts: bool = False,
tree_of_thoughts: bool = False,
tool_choice: str = "auto",
execute_tool: bool = False,
rules: str = None,
planning: Optional[str] = False,
planning_prompt: Optional[str] = None,
device: str = None,
custom_planning_prompt: str = None,
memory_chunk_size: int = 2000,
agent_ops_on: bool = False,
log_directory: str = None,
tool_system_prompt: str = tool_sop_prompt(),
max_tokens: int = 4096,
top_p: float = 0.9,
top_k: int = None,
frequency_penalty: float = 0.0,
presence_penalty: float = 0.0,
temperature: float = 0.1,
workspace_dir: str = "agent_workspace",
timeout: Optional[int] = None,
# short_memory: Optional[str] = None,
created_at: float = time.time(),
return_step_meta: Optional[bool] = False,
tags: Optional[List[str]] = None,
use_cases: Optional[List[Dict[str, str]]] = None,
*args,
**kwargs,
):
# super().__init__(*args, **kwargs)
self.agent_id = agent_id
self.id = id
self.llm = llm
self.template = template
self.max_loops = max_loops
self.stopping_condition = stopping_condition
self.loop_interval = loop_interval
self.retry_attempts = retry_attempts
self.retry_interval = retry_interval
self.task = None
self.stopping_token = stopping_token
self.interactive = interactive
self.dashboard = dashboard
self.return_history = return_history
self.dynamic_temperature_enabled = dynamic_temperature_enabled
self.dynamic_loops = dynamic_loops
self.user_name = user_name
self.context_length = context_length
self.sop = sop
self.sop_list = sop_list
self.tools = tools
self.system_prompt = system_prompt
self.agent_name = agent_name
self.agent_description = agent_description
self.saved_state_path = f"{self.agent_name}_state.json"
self.autosave = autosave
self.response_filters = []
self.self_healing_enabled = self_healing_enabled
self.code_interpreter = code_interpreter
self.multi_modal = multi_modal
self.pdf_path = pdf_path
self.list_of_pdf = list_of_pdf
self.tokenizer = tokenizer
self.long_term_memory = long_term_memory
self.preset_stopping_token = preset_stopping_token
self.traceback = traceback
self.traceback_handlers = traceback_handlers
self.streaming_on = streaming_on
self.docs = docs
self.docs_folder = docs_folder
self.verbose = verbose
self.parser = parser
self.best_of_n = best_of_n
self.callback = callback
self.metadata = metadata
self.callbacks = callbacks
self.logger_handler = logger_handler
self.search_algorithm = search_algorithm
self.logs_to_filename = logs_to_filename
self.evaluator = evaluator
self.output_json = output_json
self.stopping_func = stopping_func
self.custom_loop_condition = custom_loop_condition
self.sentiment_threshold = sentiment_threshold
self.custom_exit_command = custom_exit_command
self.sentiment_analyzer = sentiment_analyzer
self.limit_tokens_from_string = limit_tokens_from_string
self.tool_schema = tool_schema
self.output_type = output_type
self.function_calling_type = function_calling_type
self.output_cleaner = output_cleaner
self.function_calling_format_type = function_calling_format_type
self.list_base_models = list_base_models
self.metadata_output_type = metadata_output_type
self.state_save_file_type = state_save_file_type
self.chain_of_thoughts = chain_of_thoughts
self.algorithm_of_thoughts = algorithm_of_thoughts
self.tree_of_thoughts = tree_of_thoughts
self.tool_choice = tool_choice
self.execute_tool = execute_tool
self.planning = planning
self.planning_prompt = planning_prompt
self.device = device
self.custom_planning_prompt = custom_planning_prompt
self.rules = rules
self.custom_tools_prompt = custom_tools_prompt
self.memory_chunk_size = memory_chunk_size
self.agent_ops_on = agent_ops_on
self.log_directory = log_directory
self.tool_system_prompt = tool_system_prompt
self.max_tokens = max_tokens
self.top_p = top_p
self.top_k = top_k
self.frequency_penalty = frequency_penalty
self.presence_penalty = presence_penalty
self.temperature = temperature
self.workspace_dir = workspace_dir
self.timeout = timeout
self.created_at = created_at
self.return_step_meta = return_step_meta
self.tags = tags
self.use_cases = use_cases
# Name
self.name = agent_name
self.description = agent_description
# Agentic stuff
self.reply = ""
self.question = None
self.answer = ""
# The max_loops will be set dynamically if the dynamic_loop
if self.dynamic_loops is True:
logger.info("Dynamic loops enabled")
self.max_loops = "auto"
# If multimodal = yes then set the sop to the multimodal sop
if self.multi_modal is True:
self.sop = MULTI_MODAL_AUTO_AGENT_SYSTEM_PROMPT_1
# Memory
self.feedback = []
# If the preset stopping token is enabled then set the stopping token to the preset stopping token
if preset_stopping_token is not None:
self.stopping_token = "<DONE>"
# If the system prompt is provided then set the system prompt
# Initialize the short term memory
self.short_memory = Conversation(
system_prompt=system_prompt,
time_enabled=True,
user=user_name,
rules=rules,
*args,
**kwargs,
)
# Check the parameters
self.agent_initialization()
# If the docs exist then ingest the docs
if exists(self.docs):
self.ingest_docs(self.docs)
# If docs folder exists then get the docs from docs folder
if exists(self.docs_folder):
self.get_docs_from_doc_folders()
if tools is not None:
logger.info(
"Tools provided make sure the functions have documentation ++ type hints, otherwise tool execution won't be reliable."
)
# Add the tool prompt to the memory
self.short_memory.add(
role="system", content=tool_system_prompt
)
# Log the tools
logger.info(f"Tools provided: Accessing {len(tools)} tools")
# Transform the tools into an openai schema
self.convert_tool_into_openai_schema()
# Now create a function calling map for every tools
self.function_map = {tool.__name__: tool for tool in tools}
# Set the logger handler
if exists(logger_handler):
log_file_path = os.path.join(
self.workspace_dir, f"{self.agent_name}.log"
)
logger.add(
log_file_path,
level="INFO",
colorize=True,
format=("<green>{time}</green> <level>{message}</level>"),
backtrace=True,
diagnose=True,
)
# If the tool types are provided
if self.tool_schema is not None:
# Log the tool schema
logger.info(
"Tool schema provided, Automatically converting to OpenAI function"
)
tool_schema_str = pydantic_model_to_json_str(
self.tool_schema, indent=4
)
logger.info(f"Tool Schema: {tool_schema_str}")
# Add the tool schema to the short memory
self.short_memory.add(
role=self.user_name, content=tool_schema_str
)
# If multiple base models, then conver them.
if self.list_base_models is not None:
self.handle_multiple_base_models()
# If the algorithm of thoughts is enabled then set the sop to the algorithm of thoughts
if self.algorithm_of_thoughts is not False:
self.short_memory.add(
role=self.agent_name,
content=algorithm_of_thoughts_sop(objective=self.task),
)
# Return the history
if return_history is True:
logger.info(f"Beginning of Agent {self.agent_name} History")
logger.info(self.short_memory.return_history_as_string())
logger.info(f"End of Agent {self.agent_name} History")
# If the user inputs a list of strings for the sop then join them and set the sop
if exists(self.sop_list):
self.sop = "\n".join(self.sop_list)
self.short_memory.add(role=self.user_name, content=self.sop)
if exists(self.sop):
self.short_memory.add(role=self.user_name, content=self.sop)
# If agent_ops is on => activate agentops
if agent_ops_on is True:
self.activate_agentops()
# Code Executor
if code_interpreter is True:
self.code_executor = CodeExecutor(
max_output_length=1000,
artifacts_directory=self.workspace_dir,
)
# Telemetry Processor to log agent data
new_thread = threading.Thread(target=self.log_agent_data)
new_thread.start()
def set_system_prompt(self, system_prompt: str):
"""Set the system prompt"""
self.system_prompt = system_prompt
def provide_feedback(self, feedback: str) -> None:
"""Allow users to provide feedback on the responses."""
self.feedback.append(feedback)
logging.info(f"Feedback received: {feedback}")
# TODO: Implement the function
# def initialize_llm(self, llm: Any) -> None:
# return llm(
# system_prompt=self.system_prompt,
# max_tokens=self.max_tokens,
# context_length=self.context_length,
# temperature=self.temperature,
# top_p=self.top_p,
# top_k=self.top_k,
# frequency_penalty=self.frequency_penalty,
# presence_penalty=self.presence_penalty,
# stop=self.stopping_token,
# )
def agent_initialization(self):
try:
logger.info(
f"Initializing Autonomous Agent {self.agent_name}..."
)
self.check_parameters()
logger.info("Agent Initialized Successfully.")
logger.info(
f"Autonomous Agent {self.agent_name} Activated, all systems operational. Executing task..."
)
if self.dashboard is True:
self.print_dashboard()
except ValueError as e:
logger.info(f"Error initializing agent: {e}")
raise e
def _check_stopping_condition(self, response: str) -> bool:
"""Check if the stopping condition is met."""
try:
if self.stopping_condition:
return self.stopping_condition(response)
return False
except Exception as error:
print(
colored(
f"Error checking stopping condition: {error}",
"red",
)
)
def dynamic_temperature(self):
"""
1. Check the self.llm object for the temperature
2. If the temperature is not present, then use the default temperature
3. If the temperature is present, then dynamically change the temperature
4. for every loop you can randomly change the temperature on a scale from 0.0 to 1.0
"""
try:
if hasattr(self.llm, "temperature"):
# Randomly change the temperature attribute of self.llm object
self.llm.temperature = random.uniform(0.0, 1.0)
logger.info(f"Temperature: {self.llm.temperature}")
else:
# Use a default temperature
self.llm.temperature = 0.7
except Exception as error:
print(
colored(f"Error dynamically changing temperature: {error}")
)
def format_prompt(self, template, **kwargs: Any) -> str:
"""Format the template with the provided kwargs using f-string interpolation."""
return template.format(**kwargs)
def add_message_to_memory(self, message: str, *args, **kwargs):
"""Add the message to the memory"""
try:
logger.info(f"Adding message to memory: {message}")
self.short_memory.add(
role=self.agent_name, content=message, *args, **kwargs
)
except Exception as error:
print(
colored(f"Error adding message to memory: {error}", "red")
)
# def add_message_to_memory_and_truncate(self, message: str):
# """Add the message to the memory and truncate"""
# self.short_memory[-1].append(message)
# self.truncate_history()
def print_dashboard(self):
"""Print dashboard"""
print(colored("Initializing Agent Dashboard...", "yellow"))
data = self.to_dict()
# Beautify the data
# data = json.dumps(data, indent=4)
# json_data = json.dumps(data, indent=4)
print(
colored(
f"""
Agent Dashboard
--------------------------------------------
Agent {self.agent_name} is initializing for {self.max_loops} with the following configuration:
----------------------------------------
Agent Configuration:
Configuration: {data}
----------------------------------------
""",
"green",
)
)
def loop_count_print(self, loop_count, max_loops):
"""loop_count_print summary
Args:
loop_count (_type_): _description_
max_loops (_type_): _description_
"""
print(colored(f"\nLoop {loop_count} of {max_loops}", "cyan"))
print("\n")
def check_parameters(self):
if self.llm is None:
raise ValueError("Language model is not provided")
if self.max_loops is None:
raise ValueError("Max loops is not provided")
if self.max_tokens == 0:
raise ValueError("Max tokens is not provided")
if self.context_length == 0:
raise ValueError("Context length is not provided")
########################## FUNCTION CALLING ##########################
def run(
self,
task: Optional[str] = None,
img: Optional[str] = None,
video: Optional[str] = None,
is_last: bool = False,
*args,
**kwargs,
) -> Any:
"""
Run the autonomous agent loop
"""
try:
# self.agent_initialization()
# Add task to memory
self.short_memory.add(role=self.user_name, content=task)
# Set the loop count
loop_count = 0
# Clear the short memory
response = None
all_responses = []
steps_pool = []
# if self.tokenizer is not None:
# self.check_available_tokens()
while self.max_loops == "auto" or loop_count < self.max_loops:
loop_count += 1
self.loop_count_print(loop_count, self.max_loops)
print("\n")
# Dynamic temperature
if self.dynamic_temperature_enabled is True:
self.dynamic_temperature()
# Task prompt
task_prompt = self.short_memory.return_history_as_string()
# Parameters
attempt = 0
success = False
while attempt < self.retry_attempts and not success:
try:
if self.long_term_memory is not None:
logger.info("Querying long term memory...")
self.memory_query(task_prompt)
else:
response_args = (
(task_prompt, *args)
if img is None
else (task_prompt, img, *args)
)
response = self.llm(*response_args, **kwargs)
# Conver to a str if the response is not a str
response = self.llm_output_parser(response)
# Print
if self.streaming_on is True:
self.stream_response(response)
else:
print(response)
# Add the response to the memory
self.short_memory.add(
role=self.agent_name, content=response
)
# Add to all responses
all_responses.append(response)
# Log the step
if self.return_step_meta is True:
out_step = self.log_step_metadata(response)
steps_pool.append(out_step)
# TODO: Implement reliablity check
if self.tools is not None:
# self.parse_function_call_and_execute(response)
self.parse_and_execute_tools(response)
if self.code_interpreter is True:
# Parse the code and execute
logger.info("Parsing code and executing...")
code = extract_code_from_markdown(response)
output = self.code_executor.execute(code)
# Add to memory
self.short_memory.add(
role=self.agent_name, content=output
)
# Run the llm on the output
response = self.llm(
self.short_memory.return_history_as_string()
)
# Add to all responses
all_responses.append(response)
self.short_memory.add(
role=self.agent_name, content=response
)
if self.evaluator:
logger.info("Evaluating response...")
evaluated_response = self.evaluator(response)
print(
"Evaluated Response:"
f" {evaluated_response}"
)
self.short_memory.add(
role=self.agent_name,
content=evaluated_response,
)
# all_responses.append(evaluated_response)
# Sentiment analysis
if self.sentiment_analyzer:
logger.info("Analyzing sentiment...")
self.sentiment_analysis_handler(response)
# print(response)
success = True # Mark as successful to exit the retry loop
except Exception as e:
logger.error(
f"Attempt {attempt+1}: Error generating"
f" response: {e}"
)
attempt += 1
if not success:
logger.error(
"Failed to generate a valid response after"
" retry attempts."
)
break # Exit the loop if all retry attempts fail
# # Check stopping conditions
# if self.stopping_token in response:
# break
if (
self.stopping_condition is not None
and self._check_stopping_condition(response)
):
logger.info("Stopping condition met.")
break
elif self.stopping_func is not None and self.stopping_func(
response
):
logger.info("Stopping function met.")
break
if self.interactive:
logger.info("Interactive mode enabled.")
user_input = colored(input("You: "), "red")
# User-defined exit command
if (
user_input.lower()
== self.custom_exit_command.lower()
):
print("Exiting as per user request.")
break
self.short_memory.add(
role=self.user_name, content=user_input
)
if self.loop_interval:
logger.info(
f"Sleeping for {self.loop_interval} seconds"
)
time.sleep(self.loop_interval)
if self.autosave is True:
logger.info("Autosaving agent state.")
self.save_state(self.saved_state_path)
# Apply the cleaner function to the response
if self.output_cleaner is not None:
logger.info("Applying output cleaner to response.")
response = self.output_cleaner(response)
logger.info(f"Response after output cleaner: {response}")
# print(response)
if self.agent_ops_on is True and is_last is True:
self.check_end_session_agentops()
# final_response = " ".join(all_responses)
all_responses = [
response
for response in all_responses
if response is not None
]
final_response = " ".join(all_responses)
# logger.info(f"Final Response: {final_response}")
if self.return_history:
return self.short_memory.return_history_as_string()
elif self.return_step_meta:
log = ManySteps(
agent_id=self.agent_id,
agent_name=self.agent_name,
task=task,
number_of_steps=self.max_loops,
steps=steps_pool,
full_history=self.short_memory.return_history_as_string(),
total_tokens=self.tokenizer.count_tokens(
self.short_memory.return_history_as_string()
),
)
return log.model_dump_json(indent=4)
else:
return final_response
except Exception as error:
logger.info(
f"Error running agent: {error} optimize your input parameters"
)
raise error
async def astream_events(
self, task: str = None, img: str = None, *args, **kwargs
):
"""
Run the Agent with LangChain's astream_events API.
Only works with LangChain-based models.
"""
try:
async for evt in self.llm.astream_events(task, version="v1"):
yield evt
except Exception as e:
print(f"Error streaming events: {e}")
def __call__(self, task: str = None, img: str = None, *args, **kwargs):
"""Call the agent
Args:
task (str): _description_
img (str, optional): _description_. Defaults to None.
"""
try:
return self.run(task, img, *args, **kwargs)
except Exception as error:
logger.error(f"Error calling agent: {error}")
raise error
def parse_and_execute_tools(self, response: str, *args, **kwargs):
# Extract json from markdown
# response = extract_code_from_markdown(response)
# Try executing the tool
if self.execute_tool is not False:
try:
logger.info("Executing tool...")
# try to Execute the tool and return a string
out = parse_and_execute_json(
self.tools, response, parse_md=True, *args, **kwargs
)
print(f"Tool Output: {out}")
# Add the output to the memory
self.short_memory.add(
role=self.agent_name,
content=out,
)
except Exception as error:
logger.error(f"Error executing tool: {error}")
print(
colored(
f"Error executing tool: {error}",
"red",
)
)
# def long_term_memory_prompt(self, query: str, *args, **kwargs):
# """
# Generate the agent long term memory prompt
# Args:
# system_prompt (str): The system prompt
# history (List[str]): The history of the conversation
# Returns:
# str: The agent history prompt
# """
# try:
# logger.info(f"Querying long term memory database for {query}")
# ltr = self.long_term_memory.query(query, *args, **kwargs)
# # Count the tokens
# logger.info("Couting tokens of retrieved document")
# ltr_count = self.tokenizer.count_tokens(ltr)
# logger.info(f"Retrieved document token count {ltr_count}")
# if ltr_count > self.memory_chunk_size:
# logger.info(
# f"Truncating memory by {self.memory_chunk_size}"
# )
# out = self.truncate_string_by_tokens(
# ltr, self.memory_chunk_size
# )
# logger.info(
# f"Memory truncated by {self.memory_chunk_size}"
# )
# # Retrieve only the chunk size of the memory
# return out
# except Exception as error:
# logger.error(f"Error querying long term memory: {error}")
# raise error
def add_memory(self, message: str):
"""Add a memory to the agent
Args:
message (str): _description_
Returns:
_type_: _description_
"""
logger.info(f"Adding memory: {message}")
return self.short_memory.add(role=self.agent_name, content=message)
def plan(self, task: str, *args, **kwargs):
"""
Plan the task
Args:
task (str): The task to plan
"""
try:
if exists(self.planning_prompt):
# Join the plan and the task