Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixbug: #1675 #1676

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions metagpt/ext/stanford_town/actions/st_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
from typing import Any, Optional, Union

from metagpt.actions.action import Action
from metagpt.config2 import config
from metagpt.ext.stanford_town.utils.const import PROMPTS_DIR
from metagpt.logs import logger
from metagpt.provider.base_llm import BaseLLM
from metagpt.provider.llm_provider_registry import create_llm_instance


class STAction(Action):
name: str = "STAction"
prompt_dir: Path = PROMPTS_DIR
fail_default_resp: Optional[str] = None
mx_token_llm: Optional[BaseLLM] = None

@property
def cls_name(self):
Expand Down Expand Up @@ -62,13 +64,8 @@ async def _aask(self, prompt: str) -> str:
async def _run_gpt35_max_tokens(self, prompt: str, max_tokens: int = 50, retry: int = 3):
for idx in range(retry):
try:
tmp_max_tokens_rsp = getattr(config.llm, "max_token", 1500)
setattr(config.llm, "max_token", max_tokens)
self.llm.use_system_prompt = False # to make it behave like a non-chat completions

llm_resp = await self._aask(prompt)

setattr(config.llm, "max_token", tmp_max_tokens_rsp)
llm = self._get_mx_llm(max_tokens)
llm_resp = await llm.aask(prompt)
logger.info(f"Action: {self.cls_name} llm _run_gpt35_max_tokens raw resp: {llm_resp}")
if self._func_validate(llm_resp, prompt):
return self._func_cleanup(llm_resp, prompt)
Expand Down Expand Up @@ -117,3 +114,13 @@ async def _run_gpt35_wo_extra_prompt(self, prompt: str, retry: int = 3) -> str:
async def run(self, *args, **kwargs):
"""Run action"""
raise NotImplementedError("The run method should be implemented in a subclass.")

def _get_mx_llm(self, max_tokens: int) -> BaseLLM:
if self.mx_token_llm:
return self.mx_token_llm

llm_config = self.llm.config.model_copy(deep=True)
llm_config.max_token = max_tokens
self.mx_token_llm = create_llm_instance(llm_config)
self.mx_token_llm.use_system_prompt = False # to make it behave like a non-chat completions
return self.mx_token_llm
Loading