mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-06-05 14:55:18 +02:00
refine code. remove useless aio session, move lc parser to utils
This commit is contained in:
parent
e5b9633c62
commit
bcc29690e1
6 changed files with 36 additions and 96 deletions
|
|
@ -1,44 +0,0 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
@Time : 2023/5/23 21:51
|
||||
@Author : alexanderwu
|
||||
@File : parsers.py
|
||||
"""
|
||||
|
||||
import re
|
||||
from typing import Union
|
||||
from metagpt.logs import logger
|
||||
from langchain.schema import AgentAction, AgentFinish, OutputParserException
|
||||
|
||||
FINAL_ANSWER_ACTION = "Final Answer:"
|
||||
|
||||
|
||||
class BasicParser:
|
||||
def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
|
||||
if FINAL_ANSWER_ACTION in text:
|
||||
return AgentFinish(
|
||||
{"output": text.split(FINAL_ANSWER_ACTION)[-1].strip()}, text
|
||||
)
|
||||
# \s matches against tab/newline/whitespace
|
||||
regex = (
|
||||
r"Action\s*\d*\s*:[\s]*(.*?)[\s]*Action\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)"
|
||||
)
|
||||
match = re.search(regex, text, re.DOTALL)
|
||||
if not match:
|
||||
raise OutputParserException(f"Could not parse LLM output: `{text}`")
|
||||
action = match.group(1).strip()
|
||||
action_input = match.group(2)
|
||||
return AgentAction(action, action_input.strip(" ").strip('"'), text)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = BasicParser()
|
||||
action_sample = "I need to calculate the 0.23 power of Elon Musk's current age.\nAction: Calculator\nAction Input: 49 raised to the 0.23 power"
|
||||
final_answer_sample = "I now know the answer to the question.\nFinal Answer: 2.447626228522259"
|
||||
|
||||
rsp = parser.parse(action_sample)
|
||||
logger.info(rsp)
|
||||
|
||||
rsp = parser.parse(final_answer_sample)
|
||||
logger.info(rsp)
|
||||
|
|
@ -7,12 +7,11 @@
|
|||
"""
|
||||
from __future__ import annotations
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Optional, Type, TypedDict
|
||||
from typing import Type, TypedDict
|
||||
|
||||
from metagpt.logs import logger
|
||||
# from pydantic import BaseModel
|
||||
|
||||
# from metagpt.actions import Action
|
||||
|
||||
|
||||
class RawMessage(TypedDict):
|
||||
|
|
|
|||
|
|
@ -5,16 +5,12 @@
|
|||
@Author : alexanderwu
|
||||
@File : software_company.py
|
||||
"""
|
||||
import asyncio
|
||||
|
||||
import fire
|
||||
|
||||
from metagpt.config import Config
|
||||
from metagpt.actions import BossRequirement
|
||||
from metagpt.logs import logger
|
||||
from metagpt.environment import Environment
|
||||
from metagpt.roles import ProductManager, Architect, Engineer, QaEngineer, ProjectManager, Role
|
||||
from metagpt.manager import Manager
|
||||
from metagpt.schema import Message
|
||||
from metagpt.utils.common import NoMoneyException
|
||||
|
||||
|
|
|
|||
|
|
@ -6,14 +6,15 @@
|
|||
@File : common.py
|
||||
"""
|
||||
import os
|
||||
import re
|
||||
import ast
|
||||
import subprocess
|
||||
import inspect
|
||||
from pathlib import Path
|
||||
import re
|
||||
|
||||
from metagpt.const import PROJECT_ROOT, TMP
|
||||
from typing import Union
|
||||
from metagpt.logs import logger
|
||||
from langchain.schema import AgentAction, AgentFinish, OutputParserException
|
||||
|
||||
FINAL_ANSWER_ACTION = "Final Answer:"
|
||||
|
||||
|
||||
def check_cmd_exists(command) -> int:
|
||||
|
|
@ -124,3 +125,33 @@ def print_members(module, indent=0):
|
|||
print(f'{prefix}Function: {name}')
|
||||
elif inspect.ismethod(obj):
|
||||
print(f'{prefix}Method: {name}')
|
||||
|
||||
|
||||
class BasicParser:
|
||||
def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
|
||||
if FINAL_ANSWER_ACTION in text:
|
||||
return AgentFinish(
|
||||
{"output": text.split(FINAL_ANSWER_ACTION)[-1].strip()}, text
|
||||
)
|
||||
# \s matches against tab/newline/whitespace
|
||||
regex = (
|
||||
r"Action\s*\d*\s*:[\s]*(.*?)[\s]*Action\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)"
|
||||
)
|
||||
match = re.search(regex, text, re.DOTALL)
|
||||
if not match:
|
||||
raise OutputParserException(f"Could not parse LLM output: `{text}`")
|
||||
action = match.group(1).strip()
|
||||
action_input = match.group(2)
|
||||
return AgentAction(action, action_input.strip(" ").strip('"'), text)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = BasicParser()
|
||||
action_sample = "I need to calculate the 0.23 power of Elon Musk's current age.\nAction: Calculator\nAction Input: 49 raised to the 0.23 power"
|
||||
final_answer_sample = "I now know the answer to the question.\nFinal Answer: 2.447626228522259"
|
||||
|
||||
rsp = parser.parse(action_sample)
|
||||
logger.info(rsp)
|
||||
|
||||
rsp = parser.parse(final_answer_sample)
|
||||
logger.info(rsp)
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
@Time : 2023/5/7 16:43
|
||||
@Author : alexanderwu
|
||||
@File : custom_aio_session.py
|
||||
"""
|
||||
|
||||
import ssl
|
||||
import aiohttp
|
||||
import openai
|
||||
|
||||
|
||||
class CustomAioSession:
|
||||
async def __aenter__(self):
|
||||
"""暂时使用自签署的ssl,先忽略验证问题"""
|
||||
# ssl_context = ssl.create_default_context()
|
||||
# ssl_context.check_hostname = False
|
||||
# ssl_context.verify_mode = ssl.CERT_NONE
|
||||
headers = {"Accept-Encoding": "identity"} # Disable gzip encoding
|
||||
custom_session = aiohttp.ClientSession(headers=headers)
|
||||
openai.aiosession.set(custom_session)
|
||||
return custom_session
|
||||
|
||||
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
||||
session = openai.aiosession.get()
|
||||
if session:
|
||||
await session.close()
|
||||
openai.aiosession.set(None)
|
||||
|
|
@ -24,16 +24,3 @@ async def aask_batch(api: OpenAIGPTAPI):
|
|||
results = await api.aask_batch(['hi', 'write python hello world.'])
|
||||
logger.info(results)
|
||||
return results
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_custom_aio_session():
|
||||
logger.info("Start...")
|
||||
# 由于目前架设的https是自签署的,需要关闭ssl检验
|
||||
async with CustomAioSession():
|
||||
api = OpenAIGPTAPI()
|
||||
results = await try_hello(api)
|
||||
assert len(results) > 0
|
||||
results = await aask_batch(api)
|
||||
assert len(results) > 0
|
||||
logger.info("Done...")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue