From 5c47fc53617330106d342fcec0fba44ea2da1c4b Mon Sep 17 00:00:00 2001 From: better629 Date: Thu, 26 Oct 2023 21:58:27 +0800 Subject: [PATCH] openai ratelimit retry --- .../st_game/actions/gen_action_details.py | 5 +++- examples/st_game/actions/st_action.py | 24 +++++++++++-------- examples/st_game/utils/utils.py | 11 +++++++-- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/examples/st_game/actions/gen_action_details.py b/examples/st_game/actions/gen_action_details.py index dcdeda902..eef8e9e0e 100644 --- a/examples/st_game/actions/gen_action_details.py +++ b/examples/st_game/actions/gen_action_details.py @@ -393,7 +393,10 @@ class GenActionDetails(STAction): act_sector = GenActionSector().run(role, access_tile, act_desp) act_arena = GenActionArena().run(role, act_desp, act_world, act_sector) act_address = f"{act_world}:{act_sector}:{act_arena}" - act_game_object = GenActionObject().run(role, act_desp, act_address) + if not role.s_mem.get_str_accessible_arena_game_objects(act_address): + act_game_object = "" + else: + act_game_object = GenActionObject().run(role, act_desp, act_address) new_address = f"{act_world}:{act_sector}:{act_arena}:{act_game_object}" act_pron = GenPronunciatio().run(role, act_desp) act_event = GenEventTriple().run(role, act_desp) diff --git a/examples/st_game/actions/st_action.py b/examples/st_game/actions/st_action.py index 8d3729709..7a73a89c2 100644 --- a/examples/st_game/actions/st_action.py +++ b/examples/st_game/actions/st_action.py @@ -73,18 +73,22 @@ class STAction(Action): """ assert model_name in ["gpt-3.5-turbo-instruct", "text-davinci-002", "text-davinci-003"] for idx in range(retry): - tmp_model_name = self.llm.model - tmp_max_tokens_rsp = CONFIG.max_tokens_rsp - CONFIG.max_tokens_rsp = max_tokens - self.llm.model = model_name + try: + tmp_model_name = self.llm.model + tmp_max_tokens_rsp = CONFIG.max_tokens_rsp + CONFIG.max_tokens_rsp = max_tokens + self.llm.model = model_name - llm_resp = self._ask_nonchat(prompt) + llm_resp = self._ask_nonchat(prompt) - CONFIG.max_tokens_rsp = tmp_max_tokens_rsp - self.llm.model = tmp_model_name - logger.info(f"Action: {self.cls_name} llm _run_text_davinci raw resp: {llm_resp}") - if self._func_validate(llm_resp, prompt): - return self._func_cleanup(llm_resp, prompt) + CONFIG.max_tokens_rsp = tmp_max_tokens_rsp + self.llm.model = tmp_model_name + logger.info(f"Action: {self.cls_name} llm _run_text_davinci raw resp: {llm_resp}") + if self._func_validate(llm_resp, prompt): + return self._func_cleanup(llm_resp, prompt) + except Exception as exp: + logger.warning(f"Action: {self.cls_name} _run_text_davinci exp: {exp}") + time.sleep(5) return self.fail_default_resp def _run_gpt35(self, diff --git a/examples/st_game/utils/utils.py b/examples/st_game/utils/utils.py index fd547013a..832912210 100644 --- a/examples/st_game/utils/utils.py +++ b/examples/st_game/utils/utils.py @@ -7,6 +7,7 @@ import errno import json import os import shutil +import time from pathlib import Path from typing import Any, Union @@ -66,8 +67,14 @@ def get_embedding(text, model: str = "text-embedding-ada-002"): text = text.replace("\n", " ") if not text: text = "this is blank" - return openai.Embedding.create( - input=[text], model=model)['data'][0]['embedding'] + for idx in range(3): + try: + embedding = openai.Embedding.create(input=[text], model=model)['data'][0]['embedding'] + except Exception as exp: + time.sleep(5) + if not embedding: + raise ValueError("get_embedding failed") + return embedding def extract_first_json_dict(data_str: str) -> Union[None, dict]: