From 6487ae84b0dd1a55053ea396d6fbd3444cda4072 Mon Sep 17 00:00:00 2001 From: Azure Wang Date: Mon, 11 Mar 2024 22:27:59 +0800 Subject: [PATCH 1/2] - updated MAX-TOKEN according to openai document - minior optimization of code style - fixed issues `RuntimeError: fail to reduce message length` --- metagpt/actions/research.py | 2 +- metagpt/config2.py | 3 +-- metagpt/utils/token_counter.py | 33 ++++++++++++++++----------------- tests/mock/mock_llm.py | 3 ++- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/metagpt/actions/research.py b/metagpt/actions/research.py index ce8d8a967..65962de31 100644 --- a/metagpt/actions/research.py +++ b/metagpt/actions/research.py @@ -134,7 +134,7 @@ def gen_msg(): break model_name = config.llm.model - prompt = reduce_message_length(gen_msg(), model_name, system_text, 4096) + prompt = reduce_message_length(gen_msg(), model_name, system_text, 0) logger.debug(prompt) queries = await self._aask(prompt, [system_text]) try: diff --git a/metagpt/config2.py b/metagpt/config2.py index bc6af18c6..25569bd93 100644 --- a/metagpt/config2.py +++ b/metagpt/config2.py @@ -92,7 +92,7 @@ def default(cls): """ default_config_paths: List[Path] = [ METAGPT_ROOT / "config/config2.yaml", - Path.home() / ".metagpt/config2.yaml", + CONFIG_ROOT / "config2.yaml", ] dicts = [dict(os.environ)] @@ -134,4 +134,3 @@ def merge_dict(dicts: Iterable[Dict]) -> Dict: return result -config = Config.default() diff --git a/metagpt/utils/token_counter.py b/metagpt/utils/token_counter.py index f7c53706b..bb53cfe5e 100644 --- a/metagpt/utils/token_counter.py +++ b/metagpt/utils/token_counter.py @@ -140,25 +140,24 @@ "mixtral-8x7b": {"prompt": 0.4, "completion": 1.6}, } +# https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo TOKEN_MAX = { - "gpt-3.5-turbo": 4096, - "gpt-3.5-turbo-0301": 4096, - "gpt-3.5-turbo-0613": 4096, - "gpt-3.5-turbo-16k": 16384, - "gpt-3.5-turbo-16k-0613": 16384, - "gpt-35-turbo": 4096, - "gpt-35-turbo-16k": 16384, - "gpt-3.5-turbo-1106": 16384, - "gpt-4-0314": 8192, - "gpt-4": 8192, - "gpt-4-32k": 32768, - "gpt-4-32k-0314": 32768, - "gpt-4-0613": 8192, - "gpt-4-turbo-preview": 128000, "gpt-4-0125-preview": 128000, + "gpt-4-turbo-preview": 128000, "gpt-4-1106-preview": 128000, "gpt-4-vision-preview": 128000, "gpt-4-1106-vision-preview": 128000, + "gpt-4": 8192, + "gpt-4-0613": 8192, + "gpt-4-32k": 32768, + "gpt-4-32k-0613": 32768, + "gpt-3.5-turbo-0125": 16385, + "gpt-3.5-turbo": 16385, + "gpt-3.5-turbo-1106": 16385, + "gpt-3.5-turbo-instruct": 4096, + "gpt-3.5-turbo-16k": 16385, + "gpt-3.5-turbo-0613": 4096, + "gpt-3.5-turbo-16k-0613": 16385, "text-embedding-ada-002": 8192, "glm-3-turbo": 128000, "glm-4": 128000, @@ -179,7 +178,7 @@ } -def count_message_tokens(messages, model="gpt-3.5-turbo-0613"): +def count_message_tokens(messages, model="gpt-3.5-turbo-0125"): """Return the number of tokens used by a list of messages.""" try: encoding = tiktoken.encoding_for_model(model) @@ -209,8 +208,8 @@ def count_message_tokens(messages, model="gpt-3.5-turbo-0613"): tokens_per_message = 4 # every message follows <|start|>{role/name}\n{content}<|end|>\n tokens_per_name = -1 # if there's a name, the role is omitted elif "gpt-3.5-turbo" == model: - print("Warning: gpt-3.5-turbo may update over time. Returning num tokens assuming gpt-3.5-turbo-0613.") - return count_message_tokens(messages, model="gpt-3.5-turbo-0613") + print("Warning: gpt-3.5-turbo may update over time. Returning num tokens assuming gpt-3.5-turbo-0125.") + return count_message_tokens(messages, model="gpt-3.5-turbo-0125") elif "gpt-4" == model: print("Warning: gpt-4 may update over time. Returning num tokens assuming gpt-4-0613.") return count_message_tokens(messages, model="gpt-4-0613") diff --git a/tests/mock/mock_llm.py b/tests/mock/mock_llm.py index b2052e2b3..9c159b4ed 100644 --- a/tests/mock/mock_llm.py +++ b/tests/mock/mock_llm.py @@ -1,13 +1,14 @@ import json from typing import Optional, Union -from metagpt.config2 import config +from metagpt.config2 import Config from metagpt.configs.llm_config import LLMType from metagpt.logs import logger from metagpt.provider.azure_openai_api import AzureOpenAILLM from metagpt.provider.openai_api import OpenAILLM from metagpt.schema import Message +config = Config.default() OriginalLLM = OpenAILLM if config.llm.api_type == LLMType.OPENAI else AzureOpenAILLM From 73d0d29e81d2bcde9a6e1f9f511be7877a619a01 Mon Sep 17 00:00:00 2001 From: Azure Wang Date: Tue, 12 Mar 2024 16:49:04 +0800 Subject: [PATCH 2/2] - change reserved parameter back to `config.llm.max_token` --- metagpt/actions/research.py | 2 +- metagpt/config2.py | 1 + tests/mock/mock_llm.py | 3 +-- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/metagpt/actions/research.py b/metagpt/actions/research.py index 65962de31..2a99a8d99 100644 --- a/metagpt/actions/research.py +++ b/metagpt/actions/research.py @@ -134,7 +134,7 @@ def gen_msg(): break model_name = config.llm.model - prompt = reduce_message_length(gen_msg(), model_name, system_text, 0) + prompt = reduce_message_length(gen_msg(), model_name, system_text, config.llm.max_token) logger.debug(prompt) queries = await self._aask(prompt, [system_text]) try: diff --git a/metagpt/config2.py b/metagpt/config2.py index 25569bd93..ed1d23fa5 100644 --- a/metagpt/config2.py +++ b/metagpt/config2.py @@ -134,3 +134,4 @@ def merge_dict(dicts: Iterable[Dict]) -> Dict: return result +config = Config.default() diff --git a/tests/mock/mock_llm.py b/tests/mock/mock_llm.py index 9c159b4ed..b2052e2b3 100644 --- a/tests/mock/mock_llm.py +++ b/tests/mock/mock_llm.py @@ -1,14 +1,13 @@ import json from typing import Optional, Union -from metagpt.config2 import Config +from metagpt.config2 import config from metagpt.configs.llm_config import LLMType from metagpt.logs import logger from metagpt.provider.azure_openai_api import AzureOpenAILLM from metagpt.provider.openai_api import OpenAILLM from metagpt.schema import Message -config = Config.default() OriginalLLM = OpenAILLM if config.llm.api_type == LLMType.OPENAI else AzureOpenAILLM