From f0ee6d62db1686898809f7a377f3b2304087b3bb Mon Sep 17 00:00:00 2001 From: ziming <2216646743@qq.com> Date: Tue, 26 Sep 2023 15:54:13 +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 --- metagpt/actions/ga_action_base.py | 84 +++++++++++++++++++++++++++++++ metagpt/reflect/reflect.py | 6 +-- 2 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 metagpt/actions/ga_action_base.py diff --git a/metagpt/actions/ga_action_base.py b/metagpt/actions/ga_action_base.py new file mode 100644 index 000000000..01bdca6b6 --- /dev/null +++ b/metagpt/actions/ga_action_base.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# 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 1276ff83b..84ac5c604 100644 --- a/metagpt/reflect/reflect.py +++ b/metagpt/reflect/reflect.py @@ -3,7 +3,7 @@ # @Desc : base class of reflection import json -from logging import Logger +from metagpt.logs import logger import time from metagpt.actions.ga_action_base import final_response ''' @@ -39,7 +39,7 @@ def generate_focus_point(memories_list, n=3): return (poi_dict['output']) except ValueError: print(out) - Logger.error('无法返回正常结果') + logger.error('无法返回正常结果') return out @@ -72,7 +72,7 @@ def generate_insights_and_evidence(agent, memories_list, question, n=5): if __name__ == "__main__": # 例子,构建John Agent,实现retrive John_iss = "John Lin is a pharmacy shopkeeper at the Willow Market and Pharmacy who loves to help people. He is always looking for ways to make the process of getting medication easier for his customers; John Lin is living with his wife, Mei Lin, who is a college professor, and son, Eddy Lin, who is a student studying music theory; John Lin loves his family very much; John Lin has known the old couple next-door, Sam Moore and Jennifer Moore, for a few years; John Lin thinks Sam Moore is a kind and nice man; John Lin knows his neighbor, Yuriko Yamamoto, well; John Lin knows of his neighbors, Tamara Taylor and Carmen Ortiz, but has not met them before; John Lin and Tom Moreno are colleagues at The Willows Market and Pharmacy; John Lin and Tom Moreno are friends and like to discuss local politics together; John Lin knows the Moreno family somewhat well — the husband Tom Moreno and the wife Jane Moreno." - John = Agent_memory( + John = AgentMemory( "John", John_iss, memory_path="agent_memories/John_memory.json") # John的相关信息:{'Had a friendly chat with Yuriko about her garden.': 2.4992317730827667, 'Helped Mrs. Moore carry groceries into her house.': 1.957656720441911, 'Discussed local politics with Tom Moreno.': 1.9458268038234035}