| """Runs a meeting with LLM agents.""" |
|
|
| import time |
| from pathlib import Path |
| from typing import Literal |
|
|
| from openai import OpenAI |
| from tqdm import trange, tqdm |
|
|
| from virtual_lab.agent import Agent |
| from virtual_lab.constants import CONSISTENT_TEMPERATURE, PUBMED_TOOL_DESCRIPTION |
| from virtual_lab.prompts import ( |
| individual_meeting_agent_prompt, |
| individual_meeting_critic_prompt, |
| individual_meeting_start_prompt, |
| SCIENTIFIC_CRITIC, |
| team_meeting_start_prompt, |
| team_meeting_team_lead_initial_prompt, |
| team_meeting_team_lead_intermediate_prompt, |
| team_meeting_team_lead_final_prompt, |
| team_meeting_team_member_prompt, |
| ) |
| from virtual_lab.utils import ( |
| convert_messages_to_discussion, |
| count_discussion_tokens, |
| count_tokens, |
| get_messages, |
| get_summary, |
| print_cost_and_time, |
| run_tools, |
| save_meeting, |
| ) |
|
|
|
|
| def run_meeting( |
| meeting_type: Literal["team", "individual"], |
| agenda: str, |
| save_dir: Path, |
| save_name: str = "discussion", |
| team_lead: Agent | None = None, |
| team_members: tuple[Agent, ...] | None = None, |
| team_member: Agent | None = None, |
| agenda_questions: tuple[str, ...] = (), |
| agenda_rules: tuple[str, ...] = (), |
| summaries: tuple[str, ...] = (), |
| contexts: tuple[str, ...] = (), |
| num_rounds: int = 0, |
| temperature: float = CONSISTENT_TEMPERATURE, |
| pubmed_search: bool = False, |
| return_summary: bool = False, |
| ) -> str: |
| """Runs a meeting with a LLM agents. |
| |
| :param meeting_type: The type of meeting. |
| :param agenda: The agenda for the meeting. |
| :param save_dir: The directory to save the discussion. |
| :param save_name: The name of the discussion file that will be saved. |
| :param team_lead: The team lead for a team meeting (None for individual meeting). |
| :param team_members: The team members for a team meeting (None for individual meeting). |
| :param team_member: The team member for an individual meeting (None for team meeting). |
| :param agenda_questions: The agenda questions to answer by the end of the meeting. |
| :param agenda_rules: The rules for the meeting. |
| :param summaries: The summaries of previous meetings. |
| :param contexts: The contexts for the meeting. |
| :param num_rounds: The number of rounds of discussion. |
| :param temperature: The sampling temperature. |
| :param pubmed_search: Whether to include a PubMed search tool. |
| :param return_summary: Whether to return the summary of the meeting. |
| :return: The summary of the meeting (i.e., the last message) if return_summary is True, else None. |
| """ |
| |
| if meeting_type == "team": |
| if team_lead is None or team_members is None or len(team_members) == 0: |
| raise ValueError("Team meeting requires team lead and team members") |
| if team_member is not None: |
| raise ValueError("Team meeting does not require individual team member") |
| if team_lead in team_members: |
| raise ValueError("Team lead must be separate from team members") |
| if len(set(team_members)) != len(team_members): |
| raise ValueError("Team members must be unique") |
| elif meeting_type == "individual": |
| if team_member is None: |
| raise ValueError("Individual meeting requires individual team member") |
| if team_lead is not None or team_members is not None: |
| raise ValueError( |
| "Individual meeting does not require team lead or team members" |
| ) |
| else: |
| raise ValueError(f"Invalid meeting type: {meeting_type}") |
|
|
| |
| start_time = time.time() |
|
|
| |
| client = OpenAI() |
|
|
| |
| if meeting_type == "team": |
| team = [team_lead] + list(team_members) |
| else: |
| team = [team_member] + [SCIENTIFIC_CRITIC] |
|
|
| |
| assistant_params = {"tools": [PUBMED_TOOL_DESCRIPTION]} if pubmed_search else {} |
|
|
| |
| agent_to_assistant = { |
| agent: client.beta.assistants.create( |
| name=agent.title, |
| instructions=agent.prompt, |
| model=agent.model, |
| **assistant_params, |
| ) |
| for agent in team |
| } |
|
|
| |
| assistant_id_to_title = { |
| assistant.id: agent.title for agent, assistant in agent_to_assistant.items() |
| } |
|
|
| |
| tool_token_count = 0 |
|
|
| |
| thread = client.beta.threads.create() |
|
|
| |
| if meeting_type == "team": |
| client.beta.threads.messages.create( |
| thread_id=thread.id, |
| role="user", |
| content=team_meeting_start_prompt( |
| team_lead=team_lead, |
| team_members=team_members, |
| agenda=agenda, |
| agenda_questions=agenda_questions, |
| agenda_rules=agenda_rules, |
| summaries=summaries, |
| contexts=contexts, |
| num_rounds=num_rounds, |
| ), |
| ) |
|
|
| |
| for round_index in trange(num_rounds + 1, desc="Rounds (+ Final Round)"): |
| round_num = round_index + 1 |
|
|
| |
| for agent in tqdm(team, desc="Team"): |
| |
| if meeting_type == "team": |
| |
| if agent == team_lead: |
| if round_index == 0: |
| prompt = team_meeting_team_lead_initial_prompt( |
| team_lead=team_lead |
| ) |
| elif round_index == num_rounds: |
| prompt = team_meeting_team_lead_final_prompt( |
| team_lead=team_lead, |
| agenda=agenda, |
| agenda_questions=agenda_questions, |
| agenda_rules=agenda_rules, |
| ) |
| else: |
| prompt = team_meeting_team_lead_intermediate_prompt( |
| team_lead=team_lead, |
| round_num=round_num - 1, |
| num_rounds=num_rounds, |
| ) |
| else: |
| prompt = team_meeting_team_member_prompt( |
| team_member=agent, round_num=round_num, num_rounds=num_rounds |
| ) |
| else: |
| |
| if agent == SCIENTIFIC_CRITIC: |
| prompt = individual_meeting_critic_prompt( |
| critic=SCIENTIFIC_CRITIC, agent=team_member |
| ) |
| else: |
| if round_index == 0: |
| prompt = individual_meeting_start_prompt( |
| team_member=team_member, |
| agenda=agenda, |
| agenda_questions=agenda_questions, |
| agenda_rules=agenda_rules, |
| summaries=summaries, |
| contexts=contexts, |
| ) |
| else: |
| prompt = individual_meeting_agent_prompt( |
| critic=SCIENTIFIC_CRITIC, agent=team_member |
| ) |
|
|
| |
| client.beta.threads.messages.create( |
| thread_id=thread.id, |
| role="user", |
| content=prompt, |
| ) |
|
|
| |
| run = client.beta.threads.runs.create_and_poll( |
| thread_id=thread.id, |
| assistant_id=agent_to_assistant[agent].id, |
| model=agent.model, |
| temperature=temperature, |
| ) |
|
|
| |
| if run.status == "requires_action": |
| |
| tool_outputs = run_tools(run=run) |
|
|
| |
| tool_token_count += sum( |
| count_tokens(tool_output["output"]) for tool_output in tool_outputs |
| ) |
|
|
| |
| run = client.beta.threads.runs.submit_tool_outputs_and_poll( |
| thread_id=thread.id, run_id=run.id, tool_outputs=tool_outputs |
| ) |
|
|
| |
| client.beta.threads.messages.create( |
| thread_id=thread.id, |
| role="user", |
| content="Tool Output:\n\n" |
| + "\n\n".join( |
| tool_output["output"] for tool_output in tool_outputs |
| ), |
| ) |
|
|
| |
| if run.status != "completed": |
| raise ValueError(f"Run failed: {run.status}") |
|
|
| |
| if round_index == num_rounds: |
| break |
|
|
| |
| messages = get_messages(client=client, thread_id=thread.id) |
|
|
| |
| discussion = convert_messages_to_discussion( |
| messages=messages, assistant_id_to_title=assistant_id_to_title |
| ) |
|
|
| |
| token_counts = count_discussion_tokens(discussion=discussion) |
|
|
| |
| token_counts["tool"] = tool_token_count |
|
|
| |
| |
| print_cost_and_time( |
| token_counts=token_counts, |
| model=team_lead.model if meeting_type == "team" else team_member.model, |
| elapsed_time=time.time() - start_time, |
| ) |
|
|
| |
| save_meeting( |
| save_dir=save_dir, |
| save_name=save_name, |
| discussion=discussion, |
| ) |
|
|
| |
| if return_summary: |
| return get_summary(discussion) |
|
|