From 60535268aa78c4d4a615e5fecacc05f91135f280 Mon Sep 17 00:00:00 2001 From: better629 Date: Wed, 11 Oct 2023 16:50:11 +0800 Subject: [PATCH] fix api rate limit --- examples/st_game/actions/run_reflect_action.py | 11 ++++++++--- examples/st_game/actions/st_action.py | 8 +++++--- examples/st_game/plan/st_plan.py | 15 ++++++++++++--- examples/st_game/reflect/reflect.py | 15 ++++++++++++--- examples/st_game/run_st_game.py | 2 -- 5 files changed, 37 insertions(+), 14 deletions(-) diff --git a/examples/st_game/actions/run_reflect_action.py b/examples/st_game/actions/run_reflect_action.py index 86c556e52..82cdae29d 100644 --- a/examples/st_game/actions/run_reflect_action.py +++ b/examples/st_game/actions/run_reflect_action.py @@ -71,8 +71,12 @@ class AgentInsightAndGuidance(STAction): llm_resp = "1. " + llm_resp.strip() ret = dict() for i in llm_resp.split("\n"): - row = i.split(". ")[-1] + row = " ".join(i.split(". ")[1:]) + if "(because of " not in row: + continue thought = row.split("(because of ")[0].strip() + if ")" not in row.split("(because of ")[1]: + continue evi_raw = row.split("(because of ")[1].split(")")[0].strip() evi_raw = re.findall(r'\d+', evi_raw) evi_raw = [int(i.strip()) for i in evi_raw] @@ -81,8 +85,8 @@ class AgentInsightAndGuidance(STAction): except Exception as exp: logger.error(f"{self.cls_name} with error {exp}") - def _func_fail_default_resp(self) -> str: - pass + def _func_fail_default_resp(self, n: int) -> str: + return ["I am hungry"] * n def run(self, role: "STRole", statements: str, n: int, test_input=None) -> dict: def create_prompt_input(role, statements, n, test_input=None): @@ -93,6 +97,7 @@ class AgentInsightAndGuidance(STAction): prompt = self.generate_prompt_with_tmpl_filename(prompt_input, "insight_and_evidence_v1.txt") + self.fail_default_resp = self._func_fail_default_resp(n) output = self._run_text_davinci(prompt, max_tokens=150) logger.info(f"Role: {role.name} Action: {self.cls_name} output: {output}") return output diff --git a/examples/st_game/actions/st_action.py b/examples/st_game/actions/st_action.py index d9274abf4..8d3729709 100644 --- a/examples/st_game/actions/st_action.py +++ b/examples/st_game/actions/st_action.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- # @Desc : StanfordTown Action - +import time from typing import Union, Optional, Any from abc import abstractmethod import json @@ -109,7 +109,8 @@ class STAction(Action): if self._func_validate(llm_resp, prompt): return self._func_cleanup(llm_resp, prompt) except Exception as exp: - pass + logger.warning(f"Action: {self.cls_name} _run_gpt35 exp: {exp}") + time.sleep(5) # usually avoid `Rate limit` return False def _run_gpt35_wo_extra_prompt(self, @@ -122,7 +123,8 @@ class STAction(Action): if self._func_validate(llm_resp, prompt): return self._func_cleanup(llm_resp, prompt) except Exception as exp: - pass + logger.warning(f"Action: {self.cls_name} _run_gpt35_wo_extra_prompt exp: {exp}") + time.sleep(5) # usually avoid `Rate limit` return self.fail_default_resp def run(self, *args, **kwargs): diff --git a/examples/st_game/plan/st_plan.py b/examples/st_game/plan/st_plan.py index 82c4269e1..4e63bc7a1 100644 --- a/examples/st_game/plan/st_plan.py +++ b/examples/st_game/plan/st_plan.py @@ -59,7 +59,7 @@ def plan(role: "STRole", maze: Maze, roles: dict["STRole"], new_day: bool, retri if reaction_mode: # If we do want to chat, then we generate conversation if reaction_mode[:9] == "chat with": - _chat_react(maze, role, focused_event, reaction_mode, roles) + _chat_react(maze, role, reaction_mode, roles) elif reaction_mode[:4] == "wait": _wait_react(role, reaction_mode) @@ -143,6 +143,10 @@ def _should_react(role: "STRole", retrieved: dict, roles: dict): """ def lets_talk(init_role: "STRole", target_role: "STRole", retrieved: dict): + if init_role.name == target_role.name: + logger.info(f"Role: {role.name} _should_react lets_talk meet same role, return False") + return False + scratch = init_role._rc.scratch target_scratch = target_role._rc.scratch if (not target_scratch.act_address @@ -175,6 +179,10 @@ def _should_react(role: "STRole", retrieved: dict, roles: dict): return False def lets_react(init_role: "STRole", target_role: "STRole", retrieved: dict): + if init_role.name == target_role.name: + logger.info(f"Role: {role.name} _should_react lets_react meet same role, return False") + return False + scratch = init_role._rc.scratch target_scratch = target_role._rc.scratch if (not target_scratch.act_address @@ -225,6 +233,7 @@ def _should_react(role: "STRole", retrieved: dict, roles: dict): # Recall that retrieved takes the following form: # dictionary {["curr_event"] = } curr_event = retrieved["curr_event"] + logger.info(f"Role: {role.name} _should_react curr_event.subject: {curr_event.subject}") if ":" not in curr_event.subject: # this is a role event. @@ -260,13 +269,13 @@ def _chat_react(maze: Maze, role: "STRole", reaction_mode: str, roles: dict["STR for role, p in [("init", init_role), ("target", target_role)]: if role == "init": - act_address = f" {target_role.name}" + act_address = f" {target_role.name}" act_event = (p.name, "chat with", target_role.name) chatting_with = target_role.name chatting_with_buffer = {} chatting_with_buffer[target_role.name] = 800 elif role == "target": - act_address = f" {init_role.name}" + act_address = f" {init_role.name}" act_event = (p.name, "chat with", init_role.name) chatting_with = init_role.name chatting_with_buffer = {} diff --git a/examples/st_game/reflect/reflect.py b/examples/st_game/reflect/reflect.py index 7b5f05851..91de2de5d 100644 --- a/examples/st_game/reflect/reflect.py +++ b/examples/st_game/reflect/reflect.py @@ -3,6 +3,7 @@ # @Desc : Reflect function import datetime +import time from metagpt.logs import logger from examples.st_game.utils.utils import get_embedding @@ -126,7 +127,8 @@ def run_reflect(role: "STRole"): created, expiration, s, p, o, thought, keywords, thought_poignancy, thought_embedding_pair, evidence ) - logger.info(f"add thought memory: {thought}") + logger.info(f"add thought memory: {thought}, evidence: {evidence}") + time.sleep(2) # avoid Rate limit def reflection_trigger(role: "STRole"): @@ -182,16 +184,23 @@ def role_reflect(role: "STRole"): reset_reflection_counter(role) if role.scratch.chatting_end_time: - if role.scratch.curr_time + datetime.timedelta(0, 10) == role.scratch.chatting_end_time: + # update 10 to it's real sec_per_step value + if role.scratch.curr_time + datetime.timedelta(0, role.sec_per_step) == role.scratch.chatting_end_time: all_utt = "" if role.scratch.chat: for row in role.scratch.chat: all_utt += f"{row[0]}: {row[1]}\n" - evidence = [role.memory.get_last_chat(role.scratch.chatting_with).memory_id] + last_chat = role.memory.get_last_chat(role.scratch.chatting_with) + if last_chat: + evidence = [last_chat.memory_id] + else: + logger.info(f"Role: {role.name} get_last_chat: {last_chat}") + return planning_thought = generate_planning_thought_on_convo(role, all_utt) planning_thought = f"For {role.scratch.name}'s planning: {planning_thought}" + logger.info(f"Role: {role.name} planning_thought: {planning_thought}") created = role.scratch.curr_time expiration = created + datetime.timedelta(days=30) diff --git a/examples/st_game/run_st_game.py b/examples/st_game/run_st_game.py index af98da50c..255c65ce2 100644 --- a/examples/st_game/run_st_game.py +++ b/examples/st_game/run_st_game.py @@ -26,7 +26,6 @@ async def startup(idea: str, sim_path = STORAGE_PATH.joinpath(sim_code) sim_path.mkdir(exist_ok=True) for idx, role_name in enumerate(reverie_meta["persona_names"]): - role_stg_path = STORAGE_PATH.joinpath(fork_sim_code).joinpath(f"personas/{role_name}") has_inner_voice = True if idx == 0 else False role = STRole(name=role_name, sim_code=sim_code, @@ -36,7 +35,6 @@ async def startup(idea: str, curr_time=reverie_meta.get("curr_time"), sec_per_step=reverie_meta.get("sec_per_step"), has_inner_voice=has_inner_voice) - role.load_from(role_stg_path) roles.append(role) # init temp_storage