From 4eada502f0b9476db3e747c1e9d0fe85fef4f06e Mon Sep 17 00:00:00 2001 From: ziming <2216646743@qq.com> Date: Tue, 26 Sep 2023 15:54:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AE=8C=E6=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config copy.yaml | 87 ------------------------------- metagpt/actions/ga_action_base.py | 85 ------------------------------ metagpt/reflect/reflect.py | 2 +- metagpt/roles/role.py | 20 ++++--- 4 files changed, 14 insertions(+), 180 deletions(-) delete mode 100644 config/config copy.yaml delete mode 100644 metagpt/actions/ga_action_base.py diff --git a/config/config copy.yaml b/config/config copy.yaml deleted file mode 100644 index 7e37153d7..000000000 --- a/config/config copy.yaml +++ /dev/null @@ -1,87 +0,0 @@ -# DO NOT MODIFY THIS FILE, create a new key.yaml, define OPENAI_API_KEY. -# The configuration of key.yaml has a higher priority and will not enter git - -#### if OpenAI -## The official OPENAI_API_BASE is https://api.openai.com/v1 -## If the official OPENAI_API_BASE is not available, we recommend using the [openai-forward](https://github.com/beidongjiedeguang/openai-forward). -## Or, you can configure OPENAI_PROXY to access official OPENAI_API_BASE. -OPENAI_API_BASE: "https://api.openai.com/v1" -OPENAI_PROXY: "http://127.0.0.1:7000" -#OPENAI_API_KEY: "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" -OPENAI_API_MODEL: "gpt-3.5-turbo" -MAX_TOKENS: 1500 -RPM: 10 - -#### if Anthropic -#Anthropic_API_KEY: "YOUR_API_KEY" - -#### if AZURE, check https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat.ipynb -#### You can use ENGINE or DEPLOYMENT mode -#OPENAI_API_TYPE: "azure" -#OPENAI_API_BASE: "YOUR_AZURE_ENDPOINT" -#OPENAI_API_KEY: "YOUR_AZURE_API_KEY" -#OPENAI_API_VERSION: "YOUR_AZURE_API_VERSION" -#DEPLOYMENT_NAME: "YOUR_DEPLOYMENT_NAME" -#DEPLOYMENT_ID: "YOUR_DEPLOYMENT_ID" - -#### for Search - -## Supported values: serpapi/google/serper/ddg -#SEARCH_ENGINE: serpapi - -## Visit https://serpapi.com/ to get key. -#SERPAPI_API_KEY: "YOUR_API_KEY" - -## Visit https://console.cloud.google.com/apis/credentials to get key. -#GOOGLE_API_KEY: "YOUR_API_KEY" -## Visit https://programmablesearchengine.google.com/controlpanel/create to get id. -#GOOGLE_CSE_ID: "YOUR_CSE_ID" - -## Visit https://serper.dev/ to get key. -#SERPER_API_KEY: "YOUR_API_KEY" - -#### for web access - -## Supported values: playwright/selenium -#WEB_BROWSER_ENGINE: playwright - -## Supported values: chromium/firefox/webkit, visit https://playwright.dev/python/docs/api/class-browsertype -##PLAYWRIGHT_BROWSER_TYPE: chromium - -## Supported values: chrome/firefox/edge/ie, visit https://www.selenium.dev/documentation/webdriver/browsers/ -# SELENIUM_BROWSER_TYPE: chrome - -#### for TTS - -#AZURE_TTS_SUBSCRIPTION_KEY: "YOUR_API_KEY" -#AZURE_TTS_REGION: "eastus" - -#### for Stable Diffusion -## Use SD service, based on https://github.com/AUTOMATIC1111/stable-diffusion-webui -SD_URL: "YOUR_SD_URL" -SD_T2I_API: "/sdapi/v1/txt2img" - -#### for Execution -#LONG_TERM_MEMORY: false - -#### for Mermaid CLI -## If you installed mmdc (Mermaid CLI) only for metagpt then enable the following configuration. -#PUPPETEER_CONFIG: "./config/puppeteer-config.json" -#MMDC: "./node_modules/.bin/mmdc" - - -### for calc_usage -# CALC_USAGE: false - -### for Research -MODEL_FOR_RESEARCHER_SUMMARY: gpt-3.5-turbo -MODEL_FOR_RESEARCHER_REPORT: gpt-3.5-turbo-16k - -### choose the engine for mermaid conversion, -# default is nodejs, you can change it to playwright,pyppeteer or ink -# MERMAID_ENGINE: nodejs - -### browser path for pyppeteer engine, support Chrome, Chromium,MS Edge -#PYPPETEER_EXECUTABLE_PATH: "/usr/bin/google-chrome-stable" - -PROMPT_FORMAT: json #json or markdown \ No newline at end of file diff --git a/metagpt/actions/ga_action_base.py b/metagpt/actions/ga_action_base.py deleted file mode 100644 index 7250709d9..000000000 --- a/metagpt/actions/ga_action_base.py +++ /dev/null @@ -1,85 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -# @Desc : 调用GPT -# author: didi -# Date:9.25 - -import openai -from metagpt.llm import DEFAULT_LLM -# 直接调用Prompt生成 -# ga的prompt构建格式和metagpt完全不同。没有办法融合。 - - -# 特殊指令加入Prompt生成 - - -def final_response(prompt, special_instruction, example_output=None): - """ - 通过将特殊指令加入Prompt生成最终的响应。 - - 参数: - - prompt:要生成响应的提示文本。 - - special_instruction:要加入Prompt的特殊指令。 - - example_output(可选):示例输出的JSON字符串。 - - 返回: - 生成的最终响应。 - - """ - prompt = '"""\n' + prompt + '\n"""\n' - prompt += f"Output the response to the prompt above in json. {special_instruction}\n" - if example_output: - prompt += "Example output json:\n" - prompt += '{"output": "' + str(example_output) + '"}' - return DEFAULT_LLM.ask(prompt) - -# prompt填充模板 - - -def prompt_generate(curr_input, prompt_lib_file): - """ - Takes in the current input (e.g. comment that you want to classifiy) and - the path to a prompt file. The prompt file contains the raw str prompt that - will be used, which contains the following substr: !! -- this - function replaces this substr with the actual curr_input to produce the - final promopt that will be sent to the GPT3 server. - ARGS: - curr_input: the input we want to feed in (IF THERE ARE MORE THAN ONE - INPUT, THIS CAN BE A LIST.) - prompt_lib_file: the path to the promopt file. - RETURNS: - a str prompt that will be sent to OpenAI's GPT server. - """ - if type(curr_input) is type("string"): - curr_input = [curr_input] - curr_input = [str(i) for i in curr_input] - - f = open(prompt_lib_file, "r") - prompt = f.read() - f.close() - for count, i in enumerate(curr_input): - prompt = prompt.replace(f"!!", i) - if "###" in prompt: - prompt = prompt.split( - "###")[1] - return prompt.strip() - -# 使用OpenAI embedding库进行存储 - - -def embedding(query): - """ - Generates an embedding for the given query. - - Args: - query (str): The text query to be embedded. - - Returns: - str: The embedding key generated for the query. - """ - embedding_result = openai.Embedding.create( - model="text-embedding-ada-002", - input=query - ) - embedding_key = embedding_result['data'][0]["embedding"] - return embedding_key diff --git a/metagpt/reflect/reflect.py b/metagpt/reflect/reflect.py index c9771808f..1276ff83b 100644 --- a/metagpt/reflect/reflect.py +++ b/metagpt/reflect/reflect.py @@ -9,7 +9,7 @@ from metagpt.actions.ga_action_base import final_response ''' 等待Agent和memory更新,保留相关引用但可以忽略。 ''' -from metagpt.memory.GA_memory_storage import Agent_memory, Memory_basic +from metagpt.memory.ga_memory_storage import AgentMemory, MemoryBasic def agent_reflect(agent): diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index b1ae51cf5..fef31dc61 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -18,6 +18,7 @@ from metagpt.llm import LLM from metagpt.logs import logger from metagpt.memory import Memory, LongTermMemory from metagpt.schema import Message +from metagpt.reflect import agent_reflect PREFIX_TEMPLATE = """You are a {profile}, named {name}, your goal is {goal}, and the constraint is {constraints}. """ @@ -78,7 +79,8 @@ class RoleContext(BaseModel): def check(self, role_id: str): if hasattr(CONFIG, "long_term_memory") and CONFIG.long_term_memory: self.long_term_memory.recover_memory(role_id, self) - self.memory = self.long_term_memory # use memory to act as long_term_memory for unify operation + # use memory to act as long_term_memory for unify operation + self.memory = self.long_term_memory @property def important_memory(self) -> list[Message]: @@ -95,7 +97,8 @@ class Role: def __init__(self, name="", profile="", goal="", constraints="", desc=""): self._llm = LLM() - self._setting = RoleSetting(name=name, profile=profile, goal=goal, constraints=constraints, desc=desc) + self._setting = RoleSetting( + name=name, profile=profile, goal=goal, constraints=constraints, desc=desc) self._states = [] self._actions = [] self._role_id = str(self._setting) @@ -169,9 +172,10 @@ class Role: # logger.info(response) if isinstance(response, ActionOutput): msg = Message(content=response.content, instruct_content=response.instruct_content, - role=self.profile, cause_by=type(self._rc.todo)) + role=self.profile, cause_by=type(self._rc.todo)) else: - msg = Message(content=response, role=self.profile, cause_by=type(self._rc.todo)) + msg = Message(content=response, role=self.profile, + cause_by=type(self._rc.todo)) self._rc.memory.add(msg) # logger.debug(f"{response}") @@ -184,8 +188,9 @@ class Role: env_msgs = self._rc.env.memory.get() observed = self._rc.env.memory.get_by_actions(self._rc.watch) - - self._rc.news = self._rc.memory.remember(observed) # remember recent exact or similar memories + + # remember recent exact or similar memories + self._rc.news = self._rc.memory.remember(observed) for i in env_msgs: self.recv(i) @@ -205,7 +210,8 @@ class Role: async def _react(self) -> Message: """Think first, then act""" await self._think() - logger.debug(f"{self._setting}: {self._rc.state=}, will do {self._rc.todo}") + logger.debug( + f"{self._setting}: {self._rc.state=}, will do {self._rc.todo}") return await self._act() def recv(self, message: Message) -> None: