shenchucheng commited on
Commit ·
6a3fde1
1
Parent(s): 0f9c423
format error msg
Browse files6 unmodified lines789101112131425 unmodified lines40414243444517 unmodified lines6364656667686914 unmodified lines84858687888990919293949596973 unmodified lines1011021031041051061071 unmodified line1091101111121131141155 unmodified lines1211221231241251261271281291301311321331341351361371381397 unmodified lines14714814915015115215315446 unmodified lines2012022032042052062072082092102112122132142156 unmodified linesimport contextlibfrom functools import partialimport shutilimport urllib.parsefrom datetime import datetimeimport uuidfrom enum import Enumfrom metagpt.logs import set_llm_stream_logfunc25 unmodified linesTEXT = "text"HIHT = "hint"ACTION = "action"class MessageStatus(Enum):17 unmodified linesrole: Optional[str] = Noneskill: Optional[str] = Nonedescription: Optional[str] = Nonetimestamp: str = datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f%z")status: strcontents: list[dict]14 unmodified linesid: strstatus: strtitle: strtimestamp: strdescription: strcontent: Sentence = Noneclass ThinkActPrompt(BaseModel):message_id: int = Nonetimestamp: str = datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f%z")step: ThinkActStep = Noneskill: Optional[str] = Nonerole: Optional[str] = None3 unmodified linesid=str(tc_id),status="running",title=action.desc,timestamp=datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f%z"),description=action.desc,)1 unmodified lineif is_finished:self.step.status = "finish"self.step.content = Sentence(type="text",id=str(1),value=SentenceValue(answer=message.content if is_finished else message),is_finished=is_finished,5 unmodified lines@propertydef prompt(self):v = self.json(exclude_unset=True)return urllib.parse.quote(v)class MessageJsonModel(BaseModel):steps: list[Sentences]qa_type: strcreated_at: datetime = datetime.now()query_time: datetime = datetime.now()answer_time: datetime = datetime.now()score: Optional[int] = Nonefeedback: Optional[str] = Nonedef add_think_act(self, think_act_prompt: ThinkActPrompt):s = Sentences(action=think_act_prompt.step.title,7 unmodified lines@propertydef prompt(self):v = self.json(exclude_unset=True)return urllib.parse.quote(v)async def create_message(req_model: NewMsg, request: Request):46 unmodified linesbreakawait asyncio.sleep(0.5)act_result = await taskthink_act_prompt.update_act(act_result)yield think_act_prompt.prompt + "\n\n"answer.add_think_act(think_act_prompt)yield answer.prompt + "\n\n" # Notify the front-end that the message is complete.finally:shutil.rmtree(CONFIG.WORKSPACE_PATH)default_llm_stream_log = partial(print, end="")6 unmodified lines789101112131425 unmodified lines4041424344454617 unmodified lines6465666768697014 unmodified lines85868788899091929394959697983 unmodified lines1021031041051061071 unmodified line1091101111121131141155 unmodified lines1211221231241251261271281291301311321331341351361371381391401411421431441451461471481491507 unmodified lines15815916016116216316446 unmodified lines2112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452466 unmodified linesimport contextlibfrom functools import partialimport shutilfrom datetime import datetimeimport tracebackimport uuidfrom enum import Enumfrom metagpt.logs import set_llm_stream_logfunc25 unmodified linesTEXT = "text"HIHT = "hint"ACTION = "action"ERROR = "error"class MessageStatus(Enum):17 unmodified linesrole: Optional[str] = Noneskill: Optional[str] = Nonedescription: Optional[str] = Nonetimestamp: str = Field(default_factory=lambda: datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f%z"))status: strcontents: list[dict]14 unmodified linesid: strstatus: strtitle: strtimestamp: str = Field(default_factory=lambda: datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f%z"))description: strcontent: Sentence = Noneclass ThinkActPrompt(BaseModel):message_id: int = Nonetimestamp: str = Field(default_factory=lambda: datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f%z"))step: ThinkActStep = Noneskill: Optional[str] = Nonerole: Optional[str] = None3 unmodified linesid=str(tc_id),status="running",title=action.desc,description=action.desc,)1 unmodified lineif is_finished:self.step.status = "finish"self.step.content = Sentence(type=SentenceType.TEXT.value,id=str(1),value=SentenceValue(answer=message.content if is_finished else message),is_finished=is_finished,5 unmodified lines@propertydef prompt(self):return self.json(exclude_unset=True)class MessageJsonModel(BaseModel):steps: list[Sentences]qa_type: strcreated_at: datetime = Field(default_factory=datetime.now)query_time: datetime = Field(default_factory=datetime.now)answer_time: datetime = Field(default_factory=datetime.now)score: Optional[int] = Nonefeedback: Optional[str] = Nonedef add_think_act(self, think_act_prompt: ThinkActPrompt):s = Sentences(action=think_act_prompt.step.title,skill=think_act_prompt.skill,description=think_act_prompt.step.description,timestamp=think_act_prompt.timestamp,status=think_act_prompt.step.status,contents=[think_act_prompt.step.content.dict()],)self.steps.append(s)def add_think_act(self, think_act_prompt: ThinkActPrompt):s = Sentences(action=think_act_prompt.step.title,7 unmodified lines@propertydef prompt(self):return self.json(exclude_unset=True)async def create_message(req_model: NewMsg, request: Request):46 unmodified linesbreakawait asyncio.sleep(0.5)else:task.cancel()returnact_result = await taskthink_act_prompt.update_act(act_result)yield think_act_prompt.prompt + "\n\n"answer.add_think_act(think_act_prompt)yield answer.prompt + "\n\n" # Notify the front-end that the message is complete.except Exception as ex:description = str(ex)answer = f"```python\n\n{traceback.format_exc()}\n\n```"step = ThinkActStep(id=tc_id,status="failed",title=description,description=description,content=Sentence(type=SentenceType.ERROR.value,id=1,value=SentenceValue(answer=answer),is_finished=True),)think_act_prompt = ThinkActPrompt(step=step)yield think_act_prompt.prompt + "\n\n"finally:shutil.rmtree(CONFIG.WORKSPACE_PATH)default_llm_stream_log = partial(print, end="")
app.py+44-13