add simplest debate example

This commit is contained in:
geekan 2023-12-22 13:05:27 +08:00
parent dd57c45bbe
commit 49377c9db0
7 changed files with 71 additions and 21 deletions

View file

@ -12,6 +12,7 @@ from typing import Any, Optional, Union
from pydantic import BaseModel, Field
from metagpt.actions.action_node import ActionNode
from metagpt.llm import LLM
from metagpt.provider.base_gpt_api import BaseGPTAPI
from metagpt.schema import (
@ -30,7 +31,7 @@ class Action(BaseModel):
context: Union[dict, CodingContext, CodeSummarizeContext, TestingContext, RunCodeContext, str, None] = ""
prefix = "" # aask*时会加上prefix作为system_message
desc = "" # for skill manager
# node: ActionNode = Field(default_factory=ActionNode, exclude=True)
node: ActionNode = Field(default=None, exclude=True)
# builtin variables
builtin_class_name: str = ""
@ -38,6 +39,11 @@ class Action(BaseModel):
class Config:
arbitrary_types_allowed = True
def __init_with_instruction(self, instruction: str):
"""Initialize action with instruction"""
self.node = ActionNode(key=self.name, expected_type=str, instruction=instruction, example="")
return self
def __init__(self, **kwargs: Any):
super().__init__(**kwargs)
@ -45,6 +51,9 @@ class Action(BaseModel):
object.__setattr__(self, "builtin_class_name", self.__class__.__name__)
self.__fields__["builtin_class_name"].default = self.__class__.__name__
if "instruction" in kwargs:
self.__init_with_instruction(kwargs["instruction"])
def __init_subclass__(cls, **kwargs: Any) -> None:
super().__init_subclass__(**kwargs)
action_subclass_registry[cls.__name__] = cls
@ -58,6 +67,9 @@ class Action(BaseModel):
def set_prefix(self, prefix):
"""Set prefix for later usage"""
self.prefix = prefix
self.llm.system_prompt = prefix
if self.node:
self.node.llm = self.llm
return self
def __str__(self):
@ -68,11 +80,16 @@ class Action(BaseModel):
async def _aask(self, prompt: str, system_msgs: Optional[list[str]] = None) -> str:
"""Append default prefix"""
if not system_msgs:
system_msgs = []
system_msgs.append(self.prefix)
return await self.llm.aask(prompt, system_msgs)
async def _run_action_node(self, *args, **kwargs):
"""Run action node"""
msgs = args[0]
context = "\n".join([f"Msg {idx}: {i}" for idx, i in enumerate(reversed(msgs))])
return await self.node.fill(context=context, llm=self.llm)
async def run(self, *args, **kwargs):
"""Run action"""
if self.node:
return await self._run_action_node(*args, **kwargs)
raise NotImplementedError("The run method should be implemented in a subclass.")

View file

@ -91,7 +91,8 @@ class ActionNode:
def __str__(self):
return (
f"{self.key}, {self.expected_type}, {self.instruction}, {self.example}" f", {self.content}, {self.children}"
f"{self.key}, {repr(self.expected_type)}, {self.instruction}, {self.example}"
f", {self.content}, {self.children}"
)
def __repr__(self):
@ -225,16 +226,16 @@ class ActionNode:
# FIXME: json instruction会带来格式问题"Project name": "web_2048 # 项目名称使用下划线",
# compile example暂时不支持markdown
self.instruction = self.compile_instruction(schema="markdown", mode=mode)
self.example = self.compile_example(schema=schema, tag=TAG, mode=mode)
instruction = self.compile_instruction(schema="markdown", mode=mode)
example = self.compile_example(schema=schema, tag=TAG, mode=mode)
# nodes = ", ".join(self.to_dict(mode=mode).keys())
constraints = [LANGUAGE_CONSTRAINT, FORMAT_CONSTRAINT]
constraint = "\n".join(constraints)
prompt = template.format(
context=context,
example=self.example,
instruction=self.instruction,
example=example,
instruction=instruction,
constraint=constraint,
)
return prompt