| 1 | from pydantic import BaseModel |
| 2 | from typing import Any, Optional |
| 3 | from configs.prompts import EVALUATE_TASK_PROMPT |
| 4 | from configs.general import Colors |
| 5 | from src.utils import get_completion |
| 6 | import json |
| 7 | import time |
| 8 | |
| 9 | |
| 10 | class Assistant(BaseModel): |
| 11 | log_flag: bool |
| 12 | name: Optional[str] = None |
| 13 | instance: Optional[Any] = None |
| 14 | tools: Optional[list] = None |
| 15 | current_task_id: str = None |
| 16 | sub_assistants: Optional[list] = None |
| 17 | runs: list = [] |
| 18 | context: Optional[dict] = {} |
| 19 | planner: str = 'sequential' #default to sequential |
| 20 | |
| 21 | |
| 22 | def initialize_history(self): |
| 23 | self.context['history'] = [] |
| 24 | |
| 25 | def add_user_message(self, message): |
| 26 | self.context['history'].append({'task_id':self.current_task_id,'role':'user','content':message}) |
| 27 | |
| 28 | def add_assistant_message(self, message): |
| 29 | self.context['history'].append({'task_id':self.current_task_id,'role':'assistant','content':message}) |
| 30 | |
| 31 | def add_tool_message(self, message): |
| 32 | self.context['history'].append({'task_id':self.current_task_id,'role':'user','tool':message}) |
| 33 | |
| 34 | def print_conversation(self): |
| 35 | print(f"\n{Colors.GREY}Conversation with Assistant: {self.name}{Colors.ENDC}\n") |
| 36 | |
| 37 | # Group messages by run_id |
| 38 | messages_by_task_id = {} |
| 39 | for message in self.context['history']: |
| 40 | task_id = message['task_id'] |
| 41 | if task_id not in messages_by_task_id: |
| 42 | messages_by_task_id[task_id] = [] |
| 43 | messages_by_task_id[task_id].append(message) |
| 44 | |
| 45 | # Print messages for each run_id |
| 46 | for task_id, messages in messages_by_task_id.items(): |
| 47 | print(f"{Colors.OKCYAN}Task ID: {task_id}{Colors.ENDC}") |
| 48 | for message in messages: |
| 49 | if 'role' in message and message['role'] == 'user': |
| 50 | print(f"{Colors.OKBLUE}User:{Colors.ENDC} {message['content']}") |
| 51 | elif 'tool' in message: |
| 52 | tool_message = message['tool'] |
| 53 | tool_args = ', '.join([f"{arg}: {value}" for arg, value in tool_message['args'].items()]) |
| 54 | print(f"{Colors.OKGREEN}Tool:{Colors.ENDC} {tool_message['tool']}({tool_args})") |
| 55 | elif 'role' in message and message['role'] == 'assistant': |
| 56 | print(f"{Colors.HEADER}Assistant:{Colors.ENDC} {message['content']}") |
| 57 | print("\n") |
| 58 | |
| 59 | def evaluate(self, client, task, plan_log): |
| 60 | '''Evaluates the assistant's performance on a task''' |
| 61 | output = get_completion(client, [{'role': 'user', 'content': EVALUATE_TASK_PROMPT.format(task.description, plan_log)}]) |
| 62 | output.content = output.content.replace("'",'"') |
| 63 | try: |
| 64 | return json.loads(output.content) |
| 65 | except json.JSONDecodeError: |
| 66 | print("An error occurred while decoding the JSON.") |
| 67 | return None |
| 68 | |
| 69 | def save_conversation(self,test=False): |
| 70 | timestamp = time.strftime("%Y%m%d-%H%M%S") |
| 71 | if not test: |
| 72 | filename = f'logs/session_{timestamp}.json' |
| 73 | else: |
| 74 | filename = f'tests/test_runs/test_{timestamp}.json' |
| 75 | |
| 76 | with open(filename, 'w') as file: |
| 77 | json.dump(self.context['history'], file) |
| 78 | |
| 79 | def pass_context(self,assistant): |
| 80 | '''Passes the context of the conversation to the assistant''' |
| 81 | assistant.context['history'] = self.context['history'] |