mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-06-23 15:48:11 +02:00
修改完毕
This commit is contained in:
parent
f0f329f503
commit
4eada502f0
4 changed files with 14 additions and 180 deletions
|
|
@ -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: !<INPUT>! -- 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"!<INPUT {count}>!", i)
|
||||
if "<commentblockmarker>###</commentblockmarker>" in prompt:
|
||||
prompt = prompt.split(
|
||||
"<commentblockmarker>###</commentblockmarker>")[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
|
||||
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue