概要
ChatDevでGPT-4モデルを利用すると、1分あたりのトークン数上限(Rate limit)に引っかかったので、回避する方法をメモ。
詳しくは、こちらのIssueの内容から。
https://github.com/OpenBMB/ChatDev/issues/26
原因
そもそも、ChatDevが内部で利用しているライブラリCamel側の問題。
よって、cloneして来たソースコードのCamel側のコードを修正する。
修正箇所
修正箇所は、以下の二箇所。
camel/model_backend.py
最初に
from tenacity import retry, wait_exponential
というimport文を追加。
以下のクラスのクラスメソッドに、デコレータを追加。
class OpenAIModel(ModelBackend):
r"""OpenAI API in a unified ModelBackend interface."""
@retry(wait=wait_exponential(multiplier=1, min=4, max=10))
def run(self, *args, **kwargs) -> Dict[str, Any]:
camel/agents/role_playing.py
最初に
from tenacity import retry, wait_exponential
というimport文を追加。
以下のクラスメソッドに、デコレータを追加。
@retry(wait=wait_exponential(multiplier=60, max=100))
def step(
self,
user_msg: ChatMessage,
assistant_only: bool,
) -> Tuple[ChatAgentResponse, ChatAgentResponse]:
以上で、GPT-4を利用した場合でも、Rate limitエラーが出ずに実行が完了するようになった。