diff --git a/__pycache__/cmd_perform.cpython-38.pyc b/__pycache__/cmd_perform.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..493ac8e1c0470d228656555fae59076ae9a1c98e
Binary files /dev/null and b/__pycache__/cmd_perform.cpython-38.pyc differ
diff --git a/__pycache__/create_sop.cpython-38.pyc b/__pycache__/create_sop.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..efbc02094578f61c2f7bb4682363b08240e81d5c
Binary files /dev/null and b/__pycache__/create_sop.cpython-38.pyc differ
diff --git a/__pycache__/gradio_base.cpython-38.pyc b/__pycache__/gradio_base.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..834dc0947838bd7195c844f441ee4da8d5992bbd
Binary files /dev/null and b/__pycache__/gradio_base.cpython-38.pyc differ
diff --git a/__pycache__/gradio_config.cpython-38.pyc b/__pycache__/gradio_config.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..8b1a6b223d2397882888daf87da1aaa88527733c
Binary files /dev/null and b/__pycache__/gradio_config.cpython-38.pyc differ
diff --git a/agents/Action/__init__.py b/agents/Action/__init__.py
new file mode 100644
index 0000000000000000000000000000000000000000..bb85ebbfc6ae1d83770263a1744fe14cb687931d
--- /dev/null
+++ b/agents/Action/__init__.py
@@ -0,0 +1 @@
+from .base_action import Action
\ No newline at end of file
diff --git a/agents/Action/__pycache__/__init__.cpython-38.pyc b/agents/Action/__pycache__/__init__.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..497b13e9fdc7f8a6242d2a4b4396c654dd5bc1b9
Binary files /dev/null and b/agents/Action/__pycache__/__init__.cpython-38.pyc differ
diff --git a/agents/Action/__pycache__/base_action.cpython-38.pyc b/agents/Action/__pycache__/base_action.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..bb9ce3b8892c895144fa62fe81f7f32ea5ee5933
Binary files /dev/null and b/agents/Action/__pycache__/base_action.cpython-38.pyc differ
diff --git a/agents/Action/base_action.py b/agents/Action/base_action.py
new file mode 100644
index 0000000000000000000000000000000000000000..7beeac9ac748e15229c2c0a609a07f5408fd0b3d
--- /dev/null
+++ b/agents/Action/base_action.py
@@ -0,0 +1,48 @@
+from Memory import Memory
+class Action:
+ """
+ The basic action unit of agent
+ """
+ def __init__(self,**kwargs):
+ self.response = None
+ self.is_user = False
+ self.res_dict = {}
+ self.name = ""
+ self.role = ""
+ for key,value in kwargs.items():
+ setattr(self,key,value)
+
+
+ def process(self):
+ """
+ processing action
+ Rerutn : memory(Memory)
+ """
+ response = self.response
+ send_name = self.name
+ send_role = self.role
+ all = ""
+ for res in response:
+ all += res
+ parse = f"{send_name}:"
+
+ # 将里面对话的第三人称删了
+ # The third person in the dialogue was deleted.
+ while parse in all:
+ index = all.index(parse) + len(parse)
+ all = all[index:]
+
+ if not self.is_user:
+ print(f"{send_name}({send_role}):{all}")
+ # for software
+ if "
" in all:
+ title = extract(all,"title")
+ python = extract(all,"python")
+ os.makedirs("output_code", exist_ok=True)
+ file_name = "output_code/" + title
+ with open(file_name, "w", encoding="utf-8") as f:
+ f.write(python)
+ memory = Memory(send_role, send_name, all)
+ return memory
+
+
diff --git a/agents/Agent/Agent.py b/agents/Agent/Agent.py
new file mode 100644
index 0000000000000000000000000000000000000000..e7f6ecc72682e8aeb74d9f933e6aa721656d350a
--- /dev/null
+++ b/agents/Agent/Agent.py
@@ -0,0 +1,243 @@
+# coding=utf-8
+# Copyright 2023 The AIWaves Inc. team.
+
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+"""LLM autonoumous agent"""
+from LLM.base_LLM import *
+from Component import *
+from Action import Action
+from Prompt import *
+
+headers = {
+ "Content-Type": "text/event-stream",
+ "Cache-Control": "no-cache",
+ "X-Accel-Buffering": "no",
+}
+
+
+
+
+class Agent:
+ """
+ Auto agent, input the JSON of SOP.
+ """
+
+ # Agent should have args: agents,states
+ def __init__(self, name, agent_state_roles, **kwargs) -> None:
+ self.state_roles = agent_state_roles
+ self.name = name
+
+ self.style = kwargs["style"]
+ self.LLMs = kwargs["LLMs"]
+ self.LLM = None
+ self.is_user = kwargs["is_user"]
+ self.begins = kwargs["begins"] if "begins" in kwargs else False
+ self.current_role = ""
+ self.long_term_memory = []
+ self.short_term_memory = ""
+ self.current_state = None
+ self.first_speak = True
+ self.environment = None
+
+
+ @classmethod
+ def from_config(cls, config_path):
+ """
+ Initialize agents based on json file
+ Return:
+ agents(dict) : key:agent_name;value:class(Agent)
+ names_to_roles(dict) : key:state_name value:(dict; (key:agent_name ; value:agent_role))
+ roles_to_names(dict) : key:state_name value:(dict; (key:agent_role ; value:agent_name))
+ """
+ with open(config_path) as f:
+ config = json.load(f)
+
+ roles_to_names = {}
+ names_to_roles = {}
+ agents = {}
+ user_names = json.loads(os.environ["User_Names"]) if "User_Names" in os.environ else []
+ for agent_name, agent_dict in config["agents"].items():
+ agent_state_roles = {}
+ agent_LLMs = {}
+ agent_begins = {}
+ for state_name, agent_role in agent_dict["roles"].items():
+
+ agent_begins[state_name] = {}
+
+ if state_name not in roles_to_names:
+ roles_to_names[state_name] = {}
+ if state_name not in names_to_roles:
+ names_to_roles[state_name] = {}
+ roles_to_names[state_name][agent_role] = agent_name
+ names_to_roles[state_name][agent_name] = agent_role
+ agent_state_roles[state_name] = agent_role
+ current_state = config["states"][state_name]
+
+ current_state_begin_role = current_state["begin_role"] if "begin_role" in current_state else current_state["roles"][0]
+ agent_begins[state_name]["is_begin"] = current_state_begin_role==agent_role if "begin_role" in current_state else False
+ agent_begins[state_name]["begin_query"] = current_state["begin_query"] if "begin_query" in current_state else " "
+ agent_LLMs[state_name] = init_LLM(f"logs/{agent_name}",**current_state["agent_states"][agent_role])
+ agents[agent_name] = cls(
+ agent_name,
+ agent_state_roles,
+ LLMs=agent_LLMs,
+ is_user=agent_name in user_names,
+ style = agent_dict["style"],
+ begins = agent_begins
+ )
+ assert len(config["agents"].keys()) != 2 or (roles_to_names[config["root"]][config["states"][config["root"]]["begin_role"]] not in user_names and "begin_query" in config["states"][config["root"]]),"In a single-agent scenario, there must be an opening statement and it must be the agent"
+ return agents, roles_to_names, names_to_roles
+
+ def step(self, current_state,input=""):
+ """
+ return actions by current state and environment
+ Return: action(Action)
+ """
+
+ current_state.chat_nums +=1
+ state_begin = current_state.is_begin
+ agent_begin = self.begins[current_state.name]["is_begin"]
+ self.begins[current_state.name]["is_begin"] = False
+ current_state.is_begin = False
+ environment = self.environment
+
+ self.current_state = current_state
+ # 先根据当前环境更新信息
+ # First update the information according to the current environment
+
+ response = " "
+ res_dict = {}
+
+ if self.is_user:
+ response = f"{self.name}:{input}"
+ else:
+ if len(environment.shared_memory["long_term_memory"])>0:
+ current_history = self.observe()
+ self.long_term_memory.append(current_history)
+ if agent_begin:
+ response = (char for char in self.begins[current_state.name]["begin_query"])
+ else:
+ response,res_dict = self.act()
+
+
+ action_dict = {
+ "response": response,
+ "res_dict": res_dict,
+ "role": self.state_roles[current_state.name],
+ "name": self.name,
+ "state_begin" : state_begin,
+ "agent_begin" : agent_begin,
+ "is_user" : self.is_user
+ }
+ return Action(**action_dict)
+
+ def act(self):
+ """
+ return actions by the current state
+ """
+ current_state = self.current_state
+ chat_history = self.long_term_memory
+ current_LLM = self.LLMs[current_state.name]
+
+ system_prompt, last_prompt, res_dict = self.compile()
+
+
+
+ response = current_LLM.get_response(
+ chat_history, system_prompt, last_prompt, stream=True
+ )
+ return response,res_dict
+
+ def update_memory(self, memory):
+ self.long_term_memory.append(
+ {"role": "assistant", "content": memory.content}
+ )
+
+ MAX_CHAT_HISTORY = eval(os.environ["MAX_CHAT_HISTORY"])
+ environment = self.environment
+ current_chat_history_idx = environment.current_chat_history_idx if environment.environment_type == "competive" else 0
+
+ current_long_term_memory = environment.shared_memory["long_term_memory"][current_chat_history_idx:]
+ last_conversation_idx = environment._get_agent_last_conversation_idx(self,current_long_term_memory)
+ if len(current_long_term_memory)-last_conversation_idx >= MAX_CHAT_HISTORY:
+ current_state = self.current_state
+ current_role = self.state_roles[current_state.name]
+ current_component_dict = current_state.components[current_role]
+
+ # get chat history from new conversation
+ conversations = environment._get_agent_new_memory(self,current_long_term_memory)
+
+ # get summary
+ summary_prompt = (
+ current_state.summary_prompt[current_role]
+ if current_state.summary_prompt
+ else f"""your name is {self.name},your role is{current_component_dict["style"].role},your task is {current_component_dict["task"].task}.\n"""
+ )
+ summary_prompt =eval(Agent_summary_system_prompt)
+ summary = self.LLMs[current_state.name].get_response(None, summary_prompt,stream = False)
+ self.short_term_memory = summary
+
+
+ def compile(self):
+ """
+ get prompt from state depend on your role
+ Return:
+ system_prompt:system_prompt for agents's LLM
+ last_prompt:last_prompt for agents's LLM
+ res_dict(dict): Other return from tool component.For example: search engine results
+ """
+ current_state = self.current_state
+ self.current_roles = self.state_roles[current_state.name]
+ current_state_name = current_state.name
+ self.LLM = self.LLMs[current_state_name]
+ components = current_state.components[self.state_roles[current_state_name]]
+
+ system_prompt = self.current_state.environment_prompt
+ last_prompt = ""
+
+ res_dict = {}
+ for component in components.values():
+ if isinstance(component, (OutputComponent, LastComponent)):
+ last_prompt = last_prompt + "\n" + component.get_prompt(self)
+ elif isinstance(component, PromptComponent):
+ system_prompt = (
+ system_prompt + "\n" + component.get_prompt(self)
+ )
+ elif isinstance(component, ToolComponent):
+ response = component.func(self)
+ if "prompt" in response and response["prompt"]:
+ last_prompt = last_prompt + "\n" + response["prompt"]
+ res_dict.update(response)
+
+ name = self.name
+ query = self.environment.shared_memory["long_term_memory"][-1]
+ last_prompt = eval(Agent_last_prompt)
+ system_prompt = eval(Agent_system_prompt)
+ return system_prompt, last_prompt, res_dict
+
+
+ def observe(self):
+ """
+ Update one's own memory according to the current environment, including: updating short-term memory; updating long-term memory
+ """
+ return self.environment._observe(self)
+
+
+ def generate_sop(self):
+ pass
+
+ def reflection(self):
+ pass
+
+
diff --git a/agents/Agent/__init__.py b/agents/Agent/__init__.py
new file mode 100644
index 0000000000000000000000000000000000000000..5919811a5cec1b9d44051cdb1e9ac26a21ee3064
--- /dev/null
+++ b/agents/Agent/__init__.py
@@ -0,0 +1 @@
+from .Agent import Agent
\ No newline at end of file
diff --git a/agents/Agent/__pycache__/Agent.cpython-38.pyc b/agents/Agent/__pycache__/Agent.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..2f8de37502515025c3083fbbbb3dbe9f7a19c0c8
Binary files /dev/null and b/agents/Agent/__pycache__/Agent.cpython-38.pyc differ
diff --git a/agents/Agent/__pycache__/__init__.cpython-38.pyc b/agents/Agent/__pycache__/__init__.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..bdc426e779afa5a77bf3a9b14a9096d371e54e10
Binary files /dev/null and b/agents/Agent/__pycache__/__init__.cpython-38.pyc differ
diff --git a/agents/Component/ExtraComponent.py b/agents/Component/ExtraComponent.py
new file mode 100644
index 0000000000000000000000000000000000000000..3ae6d6728434d03e8a7194befe0cc1be14b6653f
--- /dev/null
+++ b/agents/Component/ExtraComponent.py
@@ -0,0 +1,128 @@
+from .ToolComponent import ToolComponent
+import json
+from utils import flatten_dict,get_embedding,matching_category,search_with_api,limit_keys,limit_values
+import os
+
+
+class CategoryRequirementsComponent(ToolComponent):
+ def __init__(self, information_path):
+ super().__init__()
+ self.information_dataset = []
+ self.leaf_name = []
+ for toy_path in information_path:
+ with open(toy_path, encoding="utf-8") as json_file:
+ data = json.load(json_file)
+ for d in data:
+ if "/" in d["cat_leaf_name"]:
+ leaf_names = d["cat_leaf_name"].split("/") + [d["cat_leaf_name"]]
+ else:
+ leaf_names = [d["cat_leaf_name"]]
+ for name in leaf_names:
+ self.leaf_name.append(name)
+ new_d = d.copy()
+ new_d["cat_leaf_name"] = name
+ new_d["information"] = flatten_dict(new_d["information"])
+ self.information_dataset.append(new_d)
+
+ self.target_embbeding = get_embedding(
+ self.leaf_name
+ )
+
+ def search_information(self, category, information_dataset):
+ knowledge = {}
+ for d in information_dataset:
+ if category == d["cat_leaf_name"]:
+ knowledge = d["information"]
+ knowledge = {
+ key: value
+ for key, value in knowledge.items()
+ if (value and key != "相关分类")
+ }
+ break
+ return knowledge
+
+ def func(self, agent):
+ prompt = ""
+ messages = agent.long_term_memory
+ outputdict = {}
+ functions = [
+ {
+ "name": "search_information",
+ "description": "根据用户所需要购买商品的种类跟用户的需求去寻找用户所需要的商品",
+ "parameters": {
+ "type": "object",
+ "properties": {
+ "category": {
+ "type": "string",
+ "description": "用户现在所需要的商品类别,比如纸尿布,笔记本电脑等,注意,只能有一个",
+ },
+ "requirements": {
+ "type": "string",
+ "description": "用户现在的需求,比如说便宜,安踏品牌等等,可以有多个需求,中间以“ ”分隔",
+ },
+ },
+ "required": ["category", "requirements"],
+ },
+ }
+ ]
+
+ response = agent.LLM.get_response(
+ messages,
+ None,
+ None,
+ functions=functions,
+ stream=False,
+ function_call={"name": "search_information"},
+ )
+ response_message = json.loads(response["function_call"]["arguments"])
+ category = (
+ response_message["category"] if response_message["category"] else None
+ )
+ requirements = (
+ response_message["requirements"]
+ if response_message["requirements"]
+ else category
+ )
+ if not (category or requirements):
+ return {}
+
+ topk_result = matching_category(
+ category, self.leaf_name, None, self.target_embbeding, top_k=3
+ )
+
+ top1_score = topk_result[1][0]
+ request_items, top_category = search_with_api(requirements, category)
+
+
+ MIN_CATEGORY_SIM = eval(os.environ["MIN_CATEGORY_SIM"]
+ ) if "MIN_CATEGORY_SIM" in os.environ else 0.7
+
+ if top1_score > MIN_CATEGORY_SIM:
+ agent.environment.shared_memory["category"] = topk_result[0][0]
+ category = topk_result[0][0]
+ information = self.search_information(
+ topk_result[0][0], self.information_dataset
+ )
+ information = limit_keys(information, 3)
+ information = limit_values(information, 2)
+ prompt += f"""你需要知道的是:用户目前选择的商品是{category},该商品信息为{information}。你需要根据这些商品信息来详细介绍商品,比如详细介绍商品有哪些品牌,有哪些分类等等,并且询问用户是否有更多的需求。"""
+ if category in top_category:
+ top_category.remove(category)
+
+ recommend = "\n经过搜索后,推荐商品如下:\n"
+ prompt += "筛选出的商品如下:\n"
+
+ for i, request_item in enumerate(request_items):
+
+ itemTitle = request_item["itemTitle"]
+ itemPrice = request_item["itemPrice"]
+ itemPicUrl = request_item["itemPicUrl"]
+ recommend += f"[{i}.商品名称:{itemTitle},商品价格:{float(itemPrice)/100}]({itemPicUrl})\n"
+ prompt += f"[{i}.商品名称:{itemTitle},商品价格:{float(itemPrice)/100}]\n"
+ outputdict["recommend"] = recommend
+ print(recommend)
+ else:
+ prompt += f"""你需要知道的是:用户目前选择的商品是{category},而我们店里没有这类商品,但是我们店里有一些近似商品,如{top_category},{topk_result[0][0]},你需要对这些近似商品进行介绍,并引导用户购买"""
+ outputdict["prompt"] = prompt
+ return outputdict
+
diff --git a/agents/Component/PromptComponent.py b/agents/Component/PromptComponent.py
new file mode 100644
index 0000000000000000000000000000000000000000..dc590d4734e14cad93ab5560cb7b4f08bd45c416
--- /dev/null
+++ b/agents/Component/PromptComponent.py
@@ -0,0 +1,133 @@
+from abc import abstractmethod
+
+
+class PromptComponent:
+ def __init__(self):
+ pass
+
+ @abstractmethod
+ def get_prompt(self, agent):
+ pass
+
+class TaskComponent(PromptComponent):
+ def __init__(self, task):
+ super().__init__()
+ self.task = task
+
+ def get_prompt(self, agent):
+ return f"""The task you need to execute is: {self.task}.\n"""
+
+
+class OutputComponent(PromptComponent):
+ def __init__(self, output):
+ super().__init__()
+ self.output = output
+
+ def get_prompt(self, agent):
+ return f"""Please contact the above to extract <{self.output}> and {self.output}>, \
+ do not perform additional output, please output in strict accordance with the above format!\n"""
+
+
+class SystemComponent(PromptComponent):
+ def __init__(self,system_prompt):
+ super().__init__()
+ self.system_prompt = system_prompt
+
+ def get_prompt(self, agent):
+ return self.system_prompt
+
+class LastComponent(PromptComponent):
+ def __init__(self, last_prompt):
+ super().__init__()
+ self.last_prompt = last_prompt
+
+ def get_prompt(self, agent):
+ return self.last_prompt
+
+
+class StyleComponent(PromptComponent):
+ """
+ 角色、风格组件
+ """
+
+ def __init__(self, role):
+ super().__init__()
+ self.role = role
+
+ def get_prompt(self, agent):
+ name = agent.name
+ style = agent.style
+ return f"""Now your role is:\n{self.role}, your name is:\n{name}. \
+ You need to follow the output style:\n.\n"""
+
+
+class RuleComponent(PromptComponent):
+ def __init__(self, rule):
+ super().__init__()
+ self.rule = rule
+
+ def get_prompt(self, agent):
+ return f"""The rule you need to follow is:\n{self.rule}.\n"""
+
+
+class DemonstrationComponent(PromptComponent):
+ """
+ input a list,the example of answer.
+ """
+
+ def __init__(self, demonstrations):
+ super().__init__()
+ self.demonstrations = demonstrations
+
+ def add_demonstration(self, demonstration):
+ self.demonstrations.append(demonstration)
+
+ def get_prompt(self, agent):
+ prompt = "Here are demonstrations you can refer to:\n"
+ for demonstration in self.demonstrations:
+ prompt += "\n" + demonstration
+ prompt += "\n"
+ return prompt
+
+
+class CoTComponent(PromptComponent):
+ """
+ input a list,the example of answer.
+ """
+
+ def __init__(self, demonstrations):
+ super().__init__()
+ self.demonstrations = demonstrations
+
+ def add_demonstration(self, demonstration):
+ self.demonstrations.append(demonstration)
+
+ def get_prompt(self, agent):
+ prompt = "You need to think in detail before outputting, the thinking case is as follows:\n"
+ for demonstration in self.demonstrations:
+ prompt += "\n" + demonstration
+ prompt += "\n"
+ return prompt
+
+
+class CustomizeComponent(PromptComponent):
+ """
+ Custom template
+ template(str) : example: "i am {}"
+ keywords(list) : example : ["name"]
+ example : agent.environment.shared_memory["name"] = "Lilong"
+ the component will get the keyword attribute from the environment, and then add it to the template.
+ Return : "i am Lilong"
+ """
+ def __init__(self, template, keywords) -> None:
+ super().__init__()
+ self.template = template
+ self.keywords = keywords
+
+ def get_prompt(self, agent):
+ template_keyword = {}
+ for keyword in self.keywords:
+
+ current_keyword = agent.environment.shared_memory[keyword]
+ template_keyword[keyword] = current_keyword
+ return self.template.format(**template_keyword)
\ No newline at end of file
diff --git a/agents/Component/ToolComponent.py b/agents/Component/ToolComponent.py
new file mode 100644
index 0000000000000000000000000000000000000000..95da2abdb7e8b7b5283763587f23ecc29e8ec35f
--- /dev/null
+++ b/agents/Component/ToolComponent.py
@@ -0,0 +1,887 @@
+from abc import abstractmethod
+import uuid
+from text2vec import semantic_search
+from utils import (
+ get_relevant_history,
+ load_knowledge_base_qa,
+ load_knowledge_base_UnstructuredFile,
+ get_embedding,
+ extract,
+)
+import json
+from typing import Dict, List
+import os
+from googleapiclient.discovery import build
+import requests
+from selenium import webdriver
+from selenium.webdriver.common.by import By
+from selenium.webdriver.support.ui import WebDriverWait
+from selenium.webdriver.support import expected_conditions as EC
+from bs4 import BeautifulSoup
+import base64
+import re
+from datetime import datetime, timedelta
+from typing import Tuple, List, Any, Dict
+from email.mime.text import MIMEText
+from email.mime.multipart import MIMEMultipart
+from google.auth.transport.requests import Request
+from google.oauth2.credentials import Credentials
+from google_auth_oauthlib.flow import InstalledAppFlow
+from googleapiclient.discovery import build
+from googleapiclient.errors import HttpError
+from tqdm import tqdm
+
+class ToolComponent:
+ def __init__(self):
+ pass
+
+ @abstractmethod
+ def func(self):
+ pass
+
+class KnowledgeBaseComponent(ToolComponent):
+ """
+ Inject knowledge base
+ top_k : Top_k with the highest matching degree
+ type : "QA" or others
+ knowledge_base(json_path) : knowledge_base_path
+ """
+ def __init__(self, top_k, type, knowledge_base):
+ super().__init__()
+ self.top_k = top_k
+ self.type = type
+ self.knowledge_base = knowledge_base
+
+ if self.type == "QA":
+ (
+ self.kb_embeddings,
+ self.kb_questions,
+ self.kb_answers,
+ self.kb_chunks,
+ ) = load_knowledge_base_qa(self.knowledge_base)
+ else:
+ self.kb_embeddings, self.kb_chunks = load_knowledge_base_UnstructuredFile(
+ self.knowledge_base
+ )
+
+ def func(self, agent):
+ query = (
+ agent.long_term_memory[-1]["content"]
+ if len(agent.long_term_memory) > 0
+ else ""
+ )
+ knowledge = ""
+ query = extract(query, "query")
+ query_embedding = get_embedding(query)
+ hits = semantic_search(query_embedding, self.kb_embeddings, top_k=50)
+ hits = hits[0]
+ temp = []
+ if self.type == "QA":
+ for hit in hits:
+ matching_idx = hit["corpus_id"]
+ if self.kb_chunks[matching_idx] in temp:
+ pass
+ else:
+ knowledge = (
+ knowledge
+ + f"question:{self.kb_questions[matching_idx]},answer:{self.kb_answers[matching_idx]}\n\n"
+ )
+ temp.append(self.kb_answers[matching_idx])
+ if len(temp) == 1:
+ break
+ print(hits[0]["score"])
+ score = hits[0]["score"]
+ if score < 0.5:
+ return {"prompt": "No matching knowledge base"}
+ else:
+ return {"prompt": "The relevant content is: " + knowledge + "\n"}
+ else:
+ for hit in hits:
+ matching_idx = hit["corpus_id"]
+ if self.kb_chunks[matching_idx] in temp:
+ pass
+ else:
+ knowledge = knowledge + f"{self.kb_answers[matching_idx]}\n\n"
+ temp.append(self.kb_answers[matching_idx])
+ if len(temp) == self.top_k:
+ break
+ print(hits[0]["score"])
+ score = hits[0]["score"]
+ if score < 0.5:
+ return {"prompt": "No matching knowledge base"}
+ else:
+ print(knowledge)
+ return {"prompt": "The relevant content is: " + knowledge + "\n"}
+
+
+class StaticComponent(ToolComponent):
+ "Return static response"
+ def __init__(self, output):
+ super().__init__()
+ self.output = output
+
+ def func(self, agent):
+ outputdict = {"response": self.output}
+ return outputdict
+
+
+class ExtractComponent(ToolComponent):
+ """
+ Extract keywords based on the current scene and store them in the environment
+ extract_words(list) : Keywords to be extracted
+ system_prompt & last_prompt : Prompt to extract keywords
+ """
+ def __init__(
+ self,
+ extract_words,
+ system_prompt,
+ last_prompt=None,
+ ):
+ super().__init__()
+ self.extract_words = extract_words
+ self.system_prompt = system_prompt
+ self.default_prompt = (
+ "Please strictly adhere to the following format for outputting:\n"
+ )
+ for extract_word in extract_words:
+ self.default_prompt += (
+ f"<{extract_word}> the content you need to extract {extract_word}>"
+ )
+ self.last_prompt = last_prompt if last_prompt else self.default_prompt
+
+ def func(self, agent):
+ response = agent.LLM.get_response(
+ agent.long_term_memory,
+ self.system_prompt,
+ self.last_prompt,
+ stream=False,
+ )
+ for extract_word in self.extract_words:
+ key = extract(response, extract_word)
+ key = key if key else response
+ agent.environment.shared_memory[extract_word] = key
+
+ return {}
+
+
+"""Search sources: chatgpt/search engines/specific search sources/can even be multimodal (if it comes to clothing)"""
+
+
+class WebSearchComponent(ToolComponent):
+ """search engines"""
+
+ __ENGINE_NAME__: List = ["google", "bing"]
+
+ def __init__(self, engine_name: str, api: Dict):
+ """
+ :param engine_name: The name of the search engine used
+ :param api: Pass in a dictionary, such as {"bing":"key1", "google":"key2", ...}, of course each value can also be a list, or more complicated
+ """
+ super(WebSearchComponent, self).__init__()
+ """Determine whether the key and engine_name of the api are legal"""
+
+ assert engine_name in WebSearchComponent.__ENGINE_NAME__
+ for api_name in api:
+ assert api_name in WebSearchComponent.__ENGINE_NAME__
+
+ self.api = api
+ self.engine_name = engine_name
+
+ self.search: Dict = {"bing": self._bing_search, "google": self._google_search}
+
+ def _bing_search(self, query: str, **kwargs):
+ """Initialize search hyperparameters"""
+ subscription_key = self.api["bing"]
+ search_url = "https://api.bing.microsoft.com/v7.0/search"
+ headers = {"Ocp-Apim-Subscription-Key": subscription_key}
+ params = {
+ "q": query,
+ "textDecorations": True,
+ "textFormat": "HTML",
+ "count": 10,
+ }
+ """start searching"""
+ response = requests.get(search_url, headers=headers, params=params)
+ response.raise_for_status()
+ results = response.json()["webPages"]["value"]
+ """execute"""
+ metadata_results = []
+ for result in results:
+ metadata_result = {
+ "snippet": result["snippet"],
+ "title": result["name"],
+ "link": result["url"],
+ }
+ metadata_results.append(metadata_result)
+ return {"meta data": metadata_results}
+
+ def _google_search(self, query: str, **kwargs):
+ """Initialize search hyperparameters"""
+ api_key = self.api[self.engine_name]["api_key"]
+ cse_id = self.api[self.engine_name]["cse_id"]
+ service = build("customsearch", "v1", developerKey=api_key)
+ """start searching"""
+ results = (
+ service.cse().list(q=query, cx=cse_id, num=10, **kwargs).execute()["items"]
+ )
+ """execute"""
+ metadata_results = []
+ for result in results:
+ metadata_result = {
+ "snippet": result["snippet"],
+ "title": result["title"],
+ "link": result["link"],
+ }
+ metadata_results.append(metadata_result)
+ return {"meta data": metadata_results}
+
+ def func(self, agent, **kwargs) -> Dict:
+ query = (
+ agent.long_term_memory[-1]["content"]
+ if len(agent.long_term_memory) > 0
+ else " "
+ )
+ response = agent.LLM.get_response(
+ None,
+ system_prompt=f"Please analyze the provided conversation and identify keywords that can be used for a search engine query. Format the output as extracted keywords:\nConversation:\n{query}",
+ stream=False,
+ )
+ response = extract(response, "keywords")
+ query = response if response else query
+
+ search_results = self.search[self.engine_name](query=query, **kwargs)
+ information = ""
+ for i in search_results["meta data"][:5]:
+ information += i["snippet"]
+ return {
+ "prompt": "You can refer to the following information to reply:\n"
+ + information
+ }
+
+ def convert_search_engine_to(self, engine_name):
+ assert engine_name in WebSearchComponent.__ENGINE_NAME__
+ self.engine_name = engine_name
+
+
+class WebCrawlComponent(ToolComponent):
+ """Open a single web page for crawling"""
+
+ def __init__(self):
+ super(WebCrawlComponent, self).__init__()
+
+ def func(self, agent_dict) -> Dict:
+ url = agent_dict["url"]
+ print(f"crawling {url} ......")
+ content = ""
+ """Crawling content from url may need to be carried out according to different websites, such as wiki, baidu, zhihu, etc."""
+ driver = webdriver.Chrome()
+ try:
+ """open url"""
+ driver.get(url)
+
+ """wait 20 second"""
+ wait = WebDriverWait(driver, 20)
+ wait.until(EC.presence_of_element_located((By.TAG_NAME, "body")))
+
+ """crawl code"""
+ page_source = driver.page_source
+
+ """parse"""
+ soup = BeautifulSoup(page_source, "html.parser")
+
+ """concatenate"""
+ for paragraph in soup.find_all("p"):
+ content = f"{content}\n{paragraph.get_text()}"
+ except Exception as e:
+ print("Error:", e)
+ finally:
+ """quit"""
+ driver.quit()
+ return {"content": content.strip()}
+
+
+class MailComponent(ToolComponent):
+ __VALID_ACTION__ = ["read", "send"]
+
+ def __init__(
+ self, cfg_file: str, default_action: str = "read", name: str = "e-mail"
+ ):
+ """'../config/google_mail.json'"""
+ super(MailComponent, self).__init__(name)
+ self.name = name
+ assert (
+ default_action.lower() in self.__VALID_ACTION__
+ ), f"Action `{default_action}` is not allowed! The valid action is in `{self.__VALID_ACTION__}`"
+ self.action = default_action.lower()
+ self.credential = self._login(cfg_file)
+
+ def _login(self, cfg_file: str):
+ SCOPES = [
+ "https://www.googleapis.com/auth/gmail.readonly",
+ "https://www.googleapis.com/auth/gmail.send",
+ ]
+ creds = None
+ if os.path.exists("token.json"):
+ print("Login Successfully!")
+ creds = Credentials.from_authorized_user_file("token.json", SCOPES)
+ if not creds or not creds.valid:
+ print("Please authorize in an open browser.")
+ if creds and creds.expired and creds.refresh_token:
+ creds.refresh(Request())
+ else:
+ flow = InstalledAppFlow.from_client_secrets_file(cfg_file, SCOPES)
+ creds = flow.run_local_server(port=0)
+ # Save the credentials for the next run
+ with open("token.json", "w") as token:
+ token.write(creds.to_json())
+ return creds
+
+ def _read(self, mail_dict: dict):
+ credential = self.credential
+ state = mail_dict["state"] if "state" in mail_dict else None
+ time_between = (
+ mail_dict["time_between"] if "time_between" in mail_dict else None
+ )
+ sender_mail = mail_dict["sender_mail"] if "sender_mail" in mail_dict else None
+ only_both = mail_dict["only_both"] if "only_both" in mail_dict else False
+ order_by_time = (
+ mail_dict["order_by_time"] if "order_by_time" in mail_dict else "descend"
+ )
+ include_word = (
+ mail_dict["include_word"] if "include_word" in mail_dict else None
+ )
+ exclude_word = (
+ mail_dict["exclude_word"] if "exclude_word" in mail_dict else None
+ )
+ MAX_SEARCH_CNT = (
+ mail_dict["MAX_SEARCH_CNT"] if "MAX_SEARCH_CNT" in mail_dict else 50
+ )
+ number = mail_dict["number"] if "number" in mail_dict else 10
+ if state is None:
+ state = "all"
+ if time_between is not None:
+ assert isinstance(time_between, tuple)
+ assert len(time_between) == 2
+ assert state in ["all", "unread", "read", "sent"]
+ if only_both:
+ assert sender_mail is not None
+ if sender_mail is not None:
+ assert isinstance(sender_mail, str)
+ assert credential
+ assert order_by_time in ["descend", "ascend"]
+
+ def generate_query():
+ query = ""
+ if state in ["unread", "read"]:
+ query = f"is:{state}"
+ if state in ["sent"]:
+ query = f"in:{state}"
+ if only_both:
+ query = f"{query} from:{sender_mail} OR to:{sender_mail}"
+ if sender_mail is not None and not only_both:
+ query = f"{query} from:({sender_mail})"
+ if include_word is not None:
+ query = f"{query} {include_word}"
+ if exclude_word is not None:
+ query = f"{query} -{exclude_word}"
+ if time_between is not None:
+ TIME_FORMAT = "%Y/%m/%d"
+ t1, t2 = time_between
+ if t1 == "now":
+ t1 = datetime.now().strftime(TIME_FORMAT)
+ if t2 == "now":
+ t2 = datetime.now().strftime(TIME_FORMAT)
+ if isinstance(t1, str) and isinstance(t2, str):
+ t1 = datetime.strptime(t1, TIME_FORMAT)
+ t2 = datetime.strptime(t2, TIME_FORMAT)
+ elif isinstance(t1, str) and isinstance(t2, int):
+ t1 = datetime.strptime(t1, TIME_FORMAT)
+ t2 = t1 + timedelta(days=t2)
+ elif isinstance(t1, int) and isinstance(t2, str):
+ t2 = datetime.strptime(t2, TIME_FORMAT)
+ t1 = t2 + timedelta(days=t1)
+ else:
+ assert False, "invalid time"
+ if t1 > t2:
+ t1, t2 = t2, t1
+ query = f"{query} after:{t1.strftime(TIME_FORMAT)} before:{t2.strftime(TIME_FORMAT)}"
+ return query.strip()
+
+ def sort_by_time(data: List[Dict]):
+ if order_by_time == "descend":
+ reverse = True
+ else:
+ reverse = False
+ sorted_data = sorted(
+ data,
+ key=lambda x: datetime.strptime(x["time"], "%Y-%m-%d %H:%M:%S"),
+ reverse=reverse,
+ )
+ return sorted_data
+
+ try:
+ service = build("gmail", "v1", credentials=credential)
+ results = (
+ service.users()
+ .messages()
+ .list(userId="me", labelIds=["INBOX"], q=generate_query())
+ .execute()
+ )
+
+ messages = results.get("messages", [])
+ email_data = list()
+
+ if not messages:
+ print("No eligible emails.")
+ return None
+ else:
+ pbar = tqdm(total=min(MAX_SEARCH_CNT, len(messages)))
+ for cnt, message in enumerate(messages):
+ pbar.update(1)
+ if cnt >= MAX_SEARCH_CNT:
+ break
+ msg = (
+ service.users()
+ .messages()
+ .get(
+ userId="me",
+ id=message["id"],
+ format="full",
+ metadataHeaders=None,
+ )
+ .execute()
+ )
+
+ subject = ""
+ for header in msg["payload"]["headers"]:
+ if header["name"] == "Subject":
+ subject = header["value"]
+ break
+
+ sender = ""
+ for header in msg["payload"]["headers"]:
+ if header["name"] == "From":
+ sender = re.findall(
+ r"\b[\w\.-]+@[\w\.-]+\.\w+\b", header["value"]
+ )[0]
+ break
+ body = ""
+ if "parts" in msg["payload"]:
+ for part in msg["payload"]["parts"]:
+ if part["mimeType"] == "text/plain":
+ data = part["body"]["data"]
+ body = base64.urlsafe_b64decode(data).decode("utf-8")
+ break
+
+ email_info = {
+ "sender": sender,
+ "time": datetime.fromtimestamp(
+ int(msg["internalDate"]) / 1000
+ ).strftime("%Y-%m-%d %H:%M:%S"),
+ "subject": subject,
+ "body": body,
+ }
+ email_data.append(email_info)
+ pbar.close()
+ email_data = sort_by_time(email_data)[0:number]
+ return {"results": email_data}
+ except Exception as e:
+ print(e)
+ return None
+
+ def _send(self, mail_dict: dict):
+ recipient_mail = mail_dict["recipient_mail"]
+ subject = mail_dict["subject"]
+ body = mail_dict["body"]
+ credential = self.credential
+ service = build("gmail", "v1", credentials=credential)
+
+ message = MIMEMultipart()
+ message["to"] = recipient_mail
+ message["subject"] = subject
+
+ message.attach(MIMEText(body, "plain"))
+
+ raw_message = base64.urlsafe_b64encode(message.as_bytes()).decode("utf-8")
+ try:
+ message = (
+ service.users()
+ .messages()
+ .send(userId="me", body={"raw": raw_message})
+ .execute()
+ )
+ return {"state": True}
+ except HttpError as error:
+ print(error)
+ return {"state": False}
+
+ def func(self, mail_dict: dict):
+ if "action" in mail_dict:
+ assert mail_dict["action"].lower() in self.__VALID_ACTION__
+ self.action = mail_dict["action"]
+ functions = {"read": self._read, "send": self._send}
+ return functions[self.action](mail_dict)
+
+ def convert_action_to(self, action_name: str):
+ assert (
+ action_name.lower() in self.__VALID_ACTION__
+ ), f"Action `{action_name}` is not allowed! The valid action is in `{self.__VALID_ACTION__}`"
+ self.action = action_name.lower()
+
+
+class WeatherComponet(ToolComponent):
+ def __init__(self, api_key, name="weather", TIME_FORMAT="%Y-%m-%d"):
+ super(WeatherComponet, self).__init__(name)
+ self.name = name
+ self.TIME_FORMAT = TIME_FORMAT
+ self.api_key = api_key
+
+ def _parse(self, data):
+ dict_data: dict = {}
+ for item in data["data"]:
+ date = item["datetime"]
+ dict_data[date] = {}
+ if "weather" in item:
+ dict_data[date]["description"] = item["weather"]["description"]
+ mapping = {
+ "temp": "temperature",
+ "max_temp": "max_temperature",
+ "min_temp": "min_temperature",
+ "precip": "accumulated_precipitation",
+ }
+ for key in ["temp", "max_temp", "min_temp", "precip"]:
+ if key in item:
+ dict_data[date][mapping[key]] = item[key]
+ return dict_data
+
+ def _query(self, city_name, country_code, start_date, end_date):
+ """https://www.weatherbit.io/api/historical-weather-daily"""
+ # print(datetime.strftime(start_date, self.TIME_FORMAT), datetime.strftime(datetime.now(), self.TIME_FORMAT), end_date, datetime.strftime(datetime.now()+timedelta(days=1), self.TIME_FORMAT))
+ if start_date == datetime.strftime(
+ datetime.now(), self.TIME_FORMAT
+ ) and end_date == datetime.strftime(
+ datetime.now() + timedelta(days=1), self.TIME_FORMAT
+ ):
+ """today"""
+ url = f"https://api.weatherbit.io/v2.0/current?city={city_name}&country={country_code}&key={self.api_key}"
+ else:
+ url = f"https://api.weatherbit.io/v2.0/history/daily?&city={city_name}&country={country_code}&start_date={start_date}&end_date={end_date}&key={self.api_key}"
+ response = requests.get(url)
+ data = response.json()
+ return self._parse(data)
+
+ def func(self, weather_dict: Dict) -> Dict:
+ TIME_FORMAT = self.TIME_FORMAT
+ # Beijing, Shanghai
+ city_name = weather_dict["city_name"]
+ # CN, US
+ country_code = weather_dict["country_code"]
+ # 2020-02-02
+ start_date = datetime.strftime(
+ datetime.strptime(weather_dict["start_date"], self.TIME_FORMAT),
+ self.TIME_FORMAT,
+ )
+ end_date = weather_dict["end_date"] if "end_date" in weather_dict else None
+ if end_date is None:
+ end_date = datetime.strftime(
+ datetime.strptime(start_date, TIME_FORMAT) + timedelta(days=-1),
+ TIME_FORMAT,
+ )
+ else:
+ end_date = datetime.strftime(
+ datetime.strptime(weather_dict["end_date"], self.TIME_FORMAT),
+ self.TIME_FORMAT,
+ )
+ if datetime.strptime(start_date, TIME_FORMAT) > datetime.strptime(
+ end_date, TIME_FORMAT
+ ):
+ start_date, end_date = end_date, start_date
+ assert start_date != end_date
+ return self._query(city_name, country_code, start_date, end_date)
+
+
+class TranslateComponent(ToolComponent):
+ __SUPPORT_LANGUAGE__ = [
+ "af",
+ "am",
+ "ar",
+ "as",
+ "az",
+ "ba",
+ "bg",
+ "bn",
+ "bo",
+ "bs",
+ "ca",
+ "cs",
+ "cy",
+ "da",
+ "de",
+ "dsb",
+ "dv",
+ "el",
+ "en",
+ "es",
+ "et",
+ "eu",
+ "fa",
+ "fi",
+ "fil",
+ "fj",
+ "fo",
+ "fr",
+ "fr-CA",
+ "ga",
+ "gl",
+ "gom",
+ "gu",
+ "ha",
+ "he",
+ "hi",
+ "hr",
+ "hsb",
+ "ht",
+ "hu",
+ "hy",
+ "id",
+ "ig",
+ "ikt",
+ "is",
+ "it",
+ "iu",
+ "iu-Latn",
+ "ja",
+ "ka",
+ "kk",
+ "km",
+ "kmr",
+ "kn",
+ "ko",
+ "ku",
+ "ky",
+ "ln",
+ "lo",
+ "lt",
+ "lug",
+ "lv",
+ "lzh",
+ "mai",
+ "mg",
+ "mi",
+ "mk",
+ "ml",
+ "mn-Cyrl",
+ "mn-Mong",
+ "mr",
+ "ms",
+ "mt",
+ "mww",
+ "my",
+ "nb",
+ "ne",
+ "nl",
+ "nso",
+ "nya",
+ "or",
+ "otq",
+ "pa",
+ "pl",
+ "prs",
+ "ps",
+ "pt",
+ "pt-PT",
+ "ro",
+ "ru",
+ "run",
+ "rw",
+ "sd",
+ "si",
+ "sk",
+ "sl",
+ "sm",
+ "sn",
+ "so",
+ "sq",
+ "sr-Cyrl",
+ "sr-Latn",
+ "st",
+ "sv",
+ "sw",
+ "ta",
+ "te",
+ "th",
+ "ti",
+ "tk",
+ "tlh-Latn",
+ "tlh-Piqd",
+ "tn",
+ "to",
+ "tr",
+ "tt",
+ "ty",
+ "ug",
+ "uk",
+ "ur",
+ "uz",
+ "vi",
+ "xh",
+ "yo",
+ "yua",
+ "yue",
+ "zh-Hans",
+ "zh-Hant",
+ "zu",
+ ]
+
+ def __init__(
+ self, api_key, location, default_target_language="zh-cn", name="translate"
+ ):
+ super(TranslateComponent, self).__init__(name)
+ self.name = name
+ self.api_key = api_key
+ self.location = location
+ self.default_target_language = default_target_language
+
+ def func(self, translate_dict: Dict) -> Dict:
+ content = translate_dict["content"]
+ target_language = self.default_target_language
+ if "target_language" in translate_dict:
+ target_language = translate_dict["target_language"]
+ assert (
+ target_language in self.__SUPPORT_LANGUAGE__
+ ), f"language `{target_language}` is not supported."
+
+ endpoint = "https://api.cognitive.microsofttranslator.com"
+
+ path = "/translate"
+ constructed_url = endpoint + path
+
+ params = {"api-version": "3.0", "to": target_language}
+
+ headers = {
+ "Ocp-Apim-Subscription-Key": self.api_key,
+ "Ocp-Apim-Subscription-Region": self.location,
+ "Content-type": "application/json",
+ "X-ClientTraceId": str(uuid.uuid4()),
+ }
+
+ body = [{"text": content}]
+
+ request = requests.post(
+ constructed_url, params=params, headers=headers, json=body
+ )
+ response = request.json()
+ response = json.dumps(
+ response,
+ sort_keys=True,
+ ensure_ascii=False,
+ indent=4,
+ separators=(",", ": "),
+ )
+ response = eval(response)
+ return {"result": response[0]["translations"][0]["text"]}
+
+
+class APIComponent(ToolComponent):
+ def __init__(self):
+ super(APIComponent, self).__init__()
+
+ def func(self, agent) -> Dict:
+ pass
+
+
+class FunctionComponent(ToolComponent):
+ def __init__(
+ self,
+ functions,
+ function_call="auto",
+ response_type="response",
+ your_function=None,
+ ):
+ super().__init__()
+ self.functions = functions
+ self.function_call = function_call
+ self.parameters = {}
+ self.available_functions = {}
+ self.response_type = response_type
+ if your_function:
+ function_name = your_function["name"]
+ function_content = your_function["content"]
+ exec(function_content)
+ self.available_functions[function_name] = eval(function_name)
+
+ for function in self.functions:
+ self.parameters[function["name"]] = list(
+ function["parameters"]["properties"].keys()
+ )
+ self.available_functions[function["name"]] = eval(function["name"])
+
+ def func(self, agent):
+ messages = agent.long_term_memory
+ outputdict = {}
+ query = agent.long_term_memory[-1].content if len(agent.long_term_memory) > 0 else " "
+ relevant_history = get_relevant_history(
+ query,
+ agent.long_term_memory[:-1],
+ agent.chat_embeddings[:-1],
+ )
+ response = agent.LLM.get_response(
+ messages,
+ None,
+ functions=self.functions,
+ stream=False,
+ function_call=self.function_call,
+ relevant_history=relevant_history,
+ )
+ response_message = response
+ if response_message.get("function_call"):
+ function_name = response_message["function_call"]["name"]
+ fuction_to_call = self.available_functions[function_name]
+ function_args = json.loads(response_message["function_call"]["arguments"])
+ input_args = {}
+ for args_name in self.parameters[function_name]:
+ input_args[args_name] = function_args.get(args_name)
+ function_response = fuction_to_call(**input_args)
+ if self.response_type == "response":
+ outputdict["response"] = function_response
+ elif self.response_type == "prompt":
+ outputdict["prompt"] = function_response
+
+ return outputdict
+
+
+class CodeComponent(ToolComponent):
+ def __init__(self, file_name, keyword) -> None:
+ super().__init__()
+ self.file_name = file_name
+ self.keyword = keyword
+ self.system_prompt = (
+ "you need to extract the modified code as completely as possible."
+ )
+ self.last_prompt = (
+ f"Please strictly adhere to the following format for outputting: \n"
+ )
+ self.last_prompt += (
+ f"<{self.keyword}> the content you need to extract {self.keyword}>"
+ )
+
+ def func(self, agent):
+ response = agent.LLM.get_response(
+ agent.long_term_memory,
+ self.system_prompt,
+ self.last_prompt,
+ stream=False,
+ )
+ code = extract(response, self.keyword)
+ code = code if code else response
+ os.makedirs("output_code", exist_ok=True)
+ file_name = "output_code/" + self.file_name
+ codes = code.split("\n")
+ if codes[0] == "```python":
+ codes.remove(codes[0])
+ if codes[-1] == "```":
+ codes.remove(codes[-1])
+ code = "\n".join(codes)
+ with open(file_name, "w", encoding="utf-8") as f:
+ f.write(code)
+ return {}
diff --git a/agents/Component/__init__.py b/agents/Component/__init__.py
new file mode 100644
index 0000000000000000000000000000000000000000..61d0e26fcc092bfe6da96fdb5696586ec7d30045
--- /dev/null
+++ b/agents/Component/__init__.py
@@ -0,0 +1,3 @@
+from .ExtraComponent import *
+from .PromptComponent import *
+from .ToolComponent import *
\ No newline at end of file
diff --git a/agents/Component/__pycache__/ExtraComponent.cpython-38.pyc b/agents/Component/__pycache__/ExtraComponent.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..26cf3f6b07417702a29792a5ac7ee86268e89fef
Binary files /dev/null and b/agents/Component/__pycache__/ExtraComponent.cpython-38.pyc differ
diff --git a/agents/Component/__pycache__/PromptComponent.cpython-38.pyc b/agents/Component/__pycache__/PromptComponent.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..ecdb6fda1330fe300bca514fbd14b1d75f7e2de5
Binary files /dev/null and b/agents/Component/__pycache__/PromptComponent.cpython-38.pyc differ
diff --git a/agents/Component/__pycache__/ToolComponent.cpython-38.pyc b/agents/Component/__pycache__/ToolComponent.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..8ad8863c0c5f5481797eb5c54b45f9f527eba2ec
Binary files /dev/null and b/agents/Component/__pycache__/ToolComponent.cpython-38.pyc differ
diff --git a/agents/Component/__pycache__/__init__.cpython-38.pyc b/agents/Component/__pycache__/__init__.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..3744dc982f248a1f9092604e0c7e729950d735ef
Binary files /dev/null and b/agents/Component/__pycache__/__init__.cpython-38.pyc differ
diff --git a/agents/Environment/__init__.py b/agents/Environment/__init__.py
new file mode 100644
index 0000000000000000000000000000000000000000..3612cfec012dd670048a4d5f1ac844cf776b155c
--- /dev/null
+++ b/agents/Environment/__init__.py
@@ -0,0 +1 @@
+from .base_environment import Environment
\ No newline at end of file
diff --git a/agents/Environment/__pycache__/__init__.cpython-38.pyc b/agents/Environment/__pycache__/__init__.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..1e5c40e3ca109367f330aa285b3ec28c817c8d59
Binary files /dev/null and b/agents/Environment/__pycache__/__init__.cpython-38.pyc differ
diff --git a/agents/Environment/__pycache__/base_environment.cpython-38.pyc b/agents/Environment/__pycache__/base_environment.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..c220ac8f7f8ed8e91250061ae243f7476e6f4a09
Binary files /dev/null and b/agents/Environment/__pycache__/base_environment.cpython-38.pyc differ
diff --git a/agents/Environment/base_environment.py b/agents/Environment/base_environment.py
new file mode 100644
index 0000000000000000000000000000000000000000..2cf4f08bcd83f4f8c0437e0789db1456e13998e1
--- /dev/null
+++ b/agents/Environment/base_environment.py
@@ -0,0 +1,167 @@
+from utils import get_relevant_history, get_embedding
+import torch
+from LLM.base_LLM import *
+from Memory import Memory
+from Prompt import *
+import json
+class Environment:
+ """
+ The place where the agent activities, responsible for storing some shared memories
+ """
+ def __init__(self, config) -> None:
+ self.shared_memory = {"long_term_memory": [], "short_term_memory": None}
+ self.agents = None
+
+ self.summary_system_prompt = {}
+ self.summary_last_prompt = {}
+ self.environment_prompt = {}
+ self.environment_type = config["environment_type"] if "environment_type" in config else "cooperative"
+ self.current_chat_history_idx = 0
+ self.LLMs = {}
+
+ # 初始化每个state 的summary 方法
+ # Initialize the summary method for each state
+ for state_name, state_dict in config["states"].items():
+ if state_name != "end_state":
+ self.summary_system_prompt[state_name] = (
+ state_dict["summary_system_prompt"]
+ if "summary_system_prompt" in state_dict
+ else eval(Default_environment_summary_system_prompt)
+ )
+
+ self.summary_last_prompt[state_name] = (
+ state_dict["summary_last_prompt"]
+ if "summary_last_prompt" in state_dict
+ else eval(Default_environment_summary_last_prompt)
+ )
+
+ self.environment_prompt[state_name] = (
+ state_dict["environment_prompt"]
+ if "environment_prompt" in state_dict
+ else " "
+ )
+ self.LLMs[state_name] = init_LLM(f"logs/{state_name}",**state_dict)
+ self.roles_to_names = None
+ self.names_to_roles = None
+
+ @classmethod
+ def from_config(cls, config_path):
+ with open(config_path) as f:
+ config = json.load(f)
+ return cls(config)
+
+ def summary(self, current_state):
+ """
+ Summarize the situation in the current environment every once in a while
+ """
+ MAX_CHAT_HISTORY = eval(os.environ["MAX_CHAT_HISTORY"])
+ current_state_name = current_state.name
+
+ query = self.shared_memory["long_term_memory"][-1].content
+ relevant_history = get_relevant_history(
+ query,
+ self.shared_memory["long_term_memory"][:-1],
+ self.shared_memory["chat_embeddings"][:-1],
+ )
+
+ relevant_history = Memory.get_chat_history(relevant_history)
+ chat_history = Memory.get_chat_history(
+ self.shared_memory["long_term_memory"][-MAX_CHAT_HISTORY + 1 :]
+ )
+ summary = self.shared_memory["short_term_memory"]
+
+
+ # system prompt = environment prompt + current memory + system prompt
+ # current_memory = summary + chat history + relevant history
+ current_memory = eval(Environment_summary_memory)
+ environment_prompt = self.environment_prompt[current_state_name]
+ summary_system_prompt = self.summary_system_prompt[current_state_name]
+
+ environment_summary_system_prompt = eval(Environment_summary_system_prompt)
+ response = self.LLMs[current_state_name].get_response(None, environment_summary_system_prompt, stream=False)
+ return response
+
+ def update_memory(self, memory, current_state):
+ """
+ update chat embbedings and long term memory,short term memory,agents long term memory
+ """
+ MAX_CHAT_HISTORY = eval(os.environ["MAX_CHAT_HISTORY"])
+ self.shared_memory["long_term_memory"].append(memory)
+ current_embedding = get_embedding(memory.content)
+ if "chat_embeddings" not in self.shared_memory:
+ self.shared_memory["chat_embeddings"] = current_embedding
+ else:
+ self.shared_memory["chat_embeddings"] = torch.cat(
+ [self.shared_memory["chat_embeddings"], current_embedding], dim=0
+ )
+ if len(self.shared_memory["long_term_memory"]) % MAX_CHAT_HISTORY == 0:
+ summary = self.summary(current_state)
+ self.shared_memory["short_term_memory"] = summary
+
+ self.agents[memory.send_name].update_memory(memory)
+
+
+ def _get_agent_last_conversation_idx(self,agent,current_long_term_memory):
+ last_conversation_idx = -1
+ for i, history in enumerate(current_long_term_memory):
+ if history.send_name == agent.name:
+ last_conversation_idx = i
+ return last_conversation_idx
+
+
+ def _get_agent_new_memory(self,agent,current_long_term_memory):
+ # get new conversation
+ last_conversation_idx = self._get_agent_last_conversation_idx(agent,current_long_term_memory)
+
+ if last_conversation_idx == -1:
+ new_conversation =current_long_term_memory
+ elif (
+ last_conversation_idx
+ == len(current_long_term_memory) - 1
+ ):
+ new_conversation = []
+ else:
+ new_conversation = current_long_term_memory[
+ last_conversation_idx + 1 :
+ ]
+
+ # get chat history from new conversation
+ return Memory.get_chat_history(new_conversation)
+
+
+ def _observe(self,agent):
+ MAX_CHAT_HISTORY = eval(os.environ["MAX_CHAT_HISTORY"])
+ current_state = agent.current_state
+ current_role = agent.state_roles[current_state.name]
+ current_component_dict = current_state.components[current_role]
+
+ # cooperative:Sharing information between different states ; competive: No information is shared between different states
+ current_chat_history_idx = self.current_chat_history_idx if self.environment_type == "competive" else 0
+ current_long_term_memory = self.shared_memory["long_term_memory"][current_chat_history_idx:]
+ current_chat_embbedings = self.shared_memory["chat_embeddings"][current_chat_history_idx:]
+
+
+ # relevant_memory
+ query = current_long_term_memory[-1].content
+
+ relevant_memory = get_relevant_history(
+ query,
+ current_long_term_memory[:-1],
+ current_chat_embbedings[:-1],
+ )
+ relevant_memory = Memory.get_chat_history(relevant_memory,agent.name)
+
+ relevant_memory = eval(Agent_observe_relevant_memory)
+ agent.relevant_memory = relevant_memory
+
+
+ # get chat history from new conversation
+ conversations = self._get_agent_new_memory(agent,current_long_term_memory)
+
+ # memory = relevant_memory + summary + history + query
+ query = current_long_term_memory[-1]
+ current_memory = eval(Agent_observe_memory)
+
+ return {"role": "user", "content": current_memory}
+
+
diff --git a/agents/LLM/__init__.py b/agents/LLM/__init__.py
new file mode 100644
index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
diff --git a/agents/LLM/__pycache__/__init__.cpython-38.pyc b/agents/LLM/__pycache__/__init__.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..9a47463537d41e79a8becd4f3fd031179a81afce
Binary files /dev/null and b/agents/LLM/__pycache__/__init__.cpython-38.pyc differ
diff --git a/agents/LLM/__pycache__/base_LLM.cpython-38.pyc b/agents/LLM/__pycache__/base_LLM.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..b529e04ab6a7c816a84ab450bc4b406ef7ecf482
Binary files /dev/null and b/agents/LLM/__pycache__/base_LLM.cpython-38.pyc differ
diff --git a/agents/LLM/base_LLM.py b/agents/LLM/base_LLM.py
new file mode 100644
index 0000000000000000000000000000000000000000..2a78f03560f60d934dd7b34ea1b3460741237eae
--- /dev/null
+++ b/agents/LLM/base_LLM.py
@@ -0,0 +1,133 @@
+from abc import abstractclassmethod
+import openai
+import os
+import time
+from Memory import Memory
+from utils import save_logs
+
+class LLM:
+ def __init__(self) -> None:
+ pass
+
+ @abstractclassmethod
+ def get_response():
+ pass
+
+
+class OpenAILLM(LLM):
+ def __init__(self,**kwargs) -> None:
+ super().__init__()
+ self.MAX_CHAT_HISTORY = eval(
+ os.environ["MAX_CHAT_HISTORY"]) if "MAX_CHAT_HISTORY" in os.environ else 10
+
+ self.model = kwargs["model"] if "model" in kwargs else "gpt-3.5-turbo-16k-0613"
+ self.temperature = kwargs["temperature"] if "temperature" in kwargs else 0.3
+ self.log_path = kwargs["log_path"] if "log_path" in kwargs else "logs"
+
+
+ def get_stream(self,response, log_path, messages):
+ ans = ""
+ for res in response:
+ if res:
+ r = (res.choices[0]["delta"].get("content")
+ if res.choices[0]["delta"].get("content") else "")
+ ans += r
+ yield r
+
+ save_logs(log_path, messages, ans)
+
+
+
+ def get_response(self,
+ chat_history,
+ system_prompt,
+ last_prompt=None,
+ stream=False,
+ functions=None,
+ function_call="auto",
+ WAIT_TIME=20,
+ **kwargs):
+ """
+ return LLM's response
+ """
+ openai.api_key = os.environ["API_KEY"]
+ if "PROXY" in os.environ:
+ assert "http:" in os.environ["PROXY"] or "socks" in os.environ["PROXY"],"PROXY error,PROXY must be http or socks"
+ openai.proxy = os.environ["PROXY"]
+ if "API_BASE" in os.environ:
+ openai.api_base = os.environ["API_BASE"]
+ active_mode = True if ("ACTIVE_MODE" in os.environ and os.environ["ACTIVE_MODE"] == "0") else False
+ model = self.model
+ temperature = self.temperature
+
+
+ if active_mode:
+ system_prompt = system_prompt + "Please keep your reply as concise as possible,Within three sentences, the total word count should not exceed 30"
+
+ messages = [{
+ "role": "system",
+ "content": system_prompt
+ }] if system_prompt else []
+
+ if chat_history:
+ if len(chat_history) > self.MAX_CHAT_HISTORY:
+ chat_history = chat_history[- self.MAX_CHAT_HISTORY:]
+ if isinstance(chat_history[0],dict):
+ messages += chat_history
+ elif isinstance(chat_history[0],Memory):
+ messages += [memory.get_gpt_message("user") for memory in chat_history]
+
+ if last_prompt:
+ if active_mode:
+ last_prompt = last_prompt + "Please keep your reply as concise as possible,Within three sentences, the total word count should not exceed 30"
+ # messages += [{"role": "system", "content": f"{last_prompt}"}]
+ messages[-1]["content"] += last_prompt
+
+
+ while True:
+ try:
+ if functions:
+ response = openai.ChatCompletion.create(
+ model=model,
+ messages=messages,
+ functions=functions,
+ function_call=function_call,
+ temperature=temperature,
+ )
+ else:
+ response = openai.ChatCompletion.create(
+ model=model,
+ messages=messages,
+ temperature=temperature,
+ stream=stream)
+ break
+ except Exception as e:
+ print(e)
+ if "maximum context length is" in str(e):
+ assert False, "exceed max length"
+ break
+ else:
+ print(f"Please wait {WAIT_TIME} seconds and resend later ...")
+ time.sleep(WAIT_TIME)
+
+ if functions:
+ save_logs(self.log_path, messages, response)
+ return response.choices[0].message
+ elif stream:
+ return self.get_stream(response, self.log_path, messages)
+ else:
+ save_logs(self.log_path, messages, response)
+ return response.choices[0].message["content"]
+
+
+def init_LLM(default_log_path,**kwargs):
+ LLM_type = kwargs["LLM_type"] if "LLM_type" in kwargs else "OpenAI"
+ log_path = kwargs["log_path"] if "log_path" in kwargs else default_log_path
+ if LLM_type == "OpenAI":
+ LLM = (
+ OpenAILLM(**kwargs["LLM"])
+ if "LLM" in kwargs
+ else OpenAILLM(model = "gpt-3.5-turbo-16k-0613",temperature=0.3,log_path=log_path)
+ )
+ return LLM
+
\ No newline at end of file
diff --git a/agents/Memory/__init__.py b/agents/Memory/__init__.py
new file mode 100644
index 0000000000000000000000000000000000000000..56f3aa09d927077ebc7f1a925f956dee78cb1c26
--- /dev/null
+++ b/agents/Memory/__init__.py
@@ -0,0 +1 @@
+from .base_Memory import Memory
\ No newline at end of file
diff --git a/agents/Memory/__pycache__/__init__.cpython-38.pyc b/agents/Memory/__pycache__/__init__.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..4998d267c25b820cbf5af0a6ebca0848d5125dc2
Binary files /dev/null and b/agents/Memory/__pycache__/__init__.cpython-38.pyc differ
diff --git a/agents/Memory/__pycache__/base_Memory.cpython-38.pyc b/agents/Memory/__pycache__/base_Memory.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..dfd99b819f8aca7e8f096e6730f3eb6b70a5ab92
Binary files /dev/null and b/agents/Memory/__pycache__/base_Memory.cpython-38.pyc differ
diff --git a/agents/Memory/base_Memory.py b/agents/Memory/base_Memory.py
new file mode 100644
index 0000000000000000000000000000000000000000..9312bc0e50f35ac5136d49dff70585c5baaa3a17
--- /dev/null
+++ b/agents/Memory/base_Memory.py
@@ -0,0 +1,32 @@
+from Prompt import *
+class Memory:
+ def __init__(self,role,name,content) -> None:
+ self.send_role = role
+ self.send_name = name
+ self.content = content
+
+ def get_gpt_message(self,role):
+ return {"role":role,"content":self.content}
+
+ @classmethod
+ def get_chat_history(self,messages,agent_name =None):
+ """
+ Splice a memory list into a sentence
+ input :
+ messages(list) : list of memory(Memory)
+ Return :
+ chat_history(str) : One sentence after integration
+ """
+ chat_history = ""
+ for message in messages:
+ name,role,content = message.send_name,message.send_role,message.content
+ if agent_name and agent_name==name:
+ name = "you"
+ chat_history += eval(Single_message)
+ chat_history = eval(Chat_total_message)
+ return chat_history
+
+ def get_query(self):
+ "Return : query(str):last sentence"
+ name,role,content = self.send_name,self.send_role,self.content
+ return eval(Single_message)
\ No newline at end of file
diff --git a/agents/Prompt/__init__.py b/agents/Prompt/__init__.py
new file mode 100644
index 0000000000000000000000000000000000000000..da69c35ed2c4ec583721339c324a53d5622429d1
--- /dev/null
+++ b/agents/Prompt/__init__.py
@@ -0,0 +1 @@
+from .base_Prompts import *
\ No newline at end of file
diff --git a/agents/Prompt/__pycache__/__init__.cpython-38.pyc b/agents/Prompt/__pycache__/__init__.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..e5e298d12177fbcb0c09994da67aecabb5e1e155
Binary files /dev/null and b/agents/Prompt/__pycache__/__init__.cpython-38.pyc differ
diff --git a/agents/Prompt/__pycache__/base_Prompts.cpython-38.pyc b/agents/Prompt/__pycache__/base_Prompts.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..b5d92ab21b5b970c2199227faf900d06fce2597d
Binary files /dev/null and b/agents/Prompt/__pycache__/base_Prompts.cpython-38.pyc differ
diff --git a/agents/Prompt/base_Prompts.py b/agents/Prompt/base_Prompts.py
new file mode 100644
index 0000000000000000000000000000000000000000..f33fcdb84d0665a87bc2a6b49dd636bbb7a0980a
--- /dev/null
+++ b/agents/Prompt/base_Prompts.py
@@ -0,0 +1,83 @@
+
+# SOP========================================================================================================
+# "environment_prompt"
+# current_state , self(sop)
+Get_environment_prompt = "f\"The current scenario is as follows {self.current_state.environment_prompt} \""
+
+
+# sop.transit
+#================================================================
+Transit_system_prompt = "f\"{environment_prompt};{judge_system_prompt}\""
+
+# transit chat message
+# "environment_prompt" is get from "Get_environment_prompt" ; "chat_history_message" if from Memory
+Transit_message = "f\"{environment_summary};The chat history is as follows:\\n {chat_history_message}\\n;You especially need to pay attention to the last query\\n{query}\\n and the relevant conversation \\n{relevant_history} \\n\\n\""
+
+
+Transit_last_prompt = "f\"{judge_last_prompt}\""
+#sop.transit================================================================
+
+# sop.call
+#================================================================
+# help controller to determine the next role to speak.(the {} is agent role) call_prompt + allocate_component
+Allocate_component = "f\"If it's currently supposed to be speaking for {role}, then output {role}.\\n\""
+
+# environment_prompt is get from "Get_environment_prompt" ; "chat_history_message" if from Memory
+Call_system_prompt = "f\"{environment_prompt};{call_system_prompt};{allocate_prompt}\""
+
+#
+Call_last_prompt = "f\"You especially need to pay attention to the last query\\n{query}\\n and the relevant conversation \\n{relevant_history} \\n\\n;Now please choose the person to speak according to the following rules :{allocate_prompt};Note: The person whose turn it is now cannot be the same as the person who spoke last time, so {last_name} cannot be output\\n.\""
+
+Call_message = "f\"The chat history is as follows:\\n\\n{chat_history_message}\\n;The last person to speak is: {last_name}\\n. \""
+#sop.call================================================================
+# SOP========================================================================================================
+
+
+
+
+
+
+# Memory========================================================================================================
+Single_message = "f\"{name} said that :{content}\""
+
+Chat_total_message = "f\"{chat_history}\""
+# Memory========================================================================================================
+
+
+
+
+
+
+# Environment========================================================================================================
+Default_environment_summary_system_prompt = "\"\\nYour task is to summarize the historical dialogue records according to the current scene, and summarize the most important information\""
+
+Default_environment_summary_last_prompt = "\"Please make a summary based on the historical chat records, the output format is history summary: \{your summary content\} \""
+
+Environment_summary_memory = "f\"The information you need to know is as follows:\\n\\n\
+ The summary of the previous dialogue history is:\\n{summary}\\n.\
+ The latest conversation record is as follows:\\n {chat_history}\\n,\
+ the relevant chat history you may need is:{relevant_history}\""
+
+Environment_summary_system_prompt = "f\"{environment_prompt};{current_memory};{summary_system_prompt};\""
+
+
+# observe
+Agent_observe_relevant_memory = "f\"The relevant chat history are as follows:\\n{relevant_memory} \\n\""
+
+
+Agent_observe_memory = "f\"Here's what you need to know(Remember, this is just information, Try not to repeat what's inside):\\n\\n{relevant_memory};\
+ The previous summary of chat history is as follows :\\n{agent.short_term_memory}\\n.\
+ The new chat history is as follows:\\n {conversations}\\n\\n\
+ \""
+# Environment========================================================================================================
+
+
+
+
+# Agent========================================================================================================
+Agent_summary_system_prompt = "f\"{summary_prompt};Please summarize past key summary \\n\\n {self.short_term_memory} and new chat_history as follows: \\n{conversations}\""
+
+Agent_last_prompt = "f\"{last_prompt};\\nPlease continue the talk based on your known information,Make an effort to make the conversation more coherent and try to respond differently from your existing knowledge, avoiding repeating what others have said.\""
+
+Agent_system_prompt = "f\"{system_prompt},\""
+# Agent========================================================================================================
diff --git a/agents/SOP.py b/agents/SOP.py
new file mode 100644
index 0000000000000000000000000000000000000000..7fc3e2f5e0c496774d9967fb88593fa4c88347e2
--- /dev/null
+++ b/agents/SOP.py
@@ -0,0 +1,296 @@
+# coding=utf-8
+# Copyright 2023 The AIWaves Inc. team.
+
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+"""standard operation procedure of an LLM Autonomous agent"""
+import random
+from LLM.base_LLM import *
+from State import State
+from utils import extract, get_relevant_history
+from Memory import Memory
+from Prompt import *
+import json
+import os
+
+class SOP:
+ """
+ Responsible for managing the operational processes of all agents
+ """
+
+ # SOP should have args : "states" "relations" "root"
+
+ def __init__(self, **kwargs):
+ self.controller_dict = {}
+ self.LLM = init_LLM("logs/god",**kwargs)
+
+ self.states = {}
+ self.init_states(kwargs["states"])
+ self.init_relation(kwargs["relations"])
+ for state_name, states_dict in kwargs["states"].items():
+ if state_name != "end_state" and "controller" in states_dict:
+ self.controller_dict[state_name] = states_dict["controller"]
+
+ self.user_names = kwargs["user_names"] if "user_names" in kwargs else []
+ self.root = self.states[kwargs["root"]]
+ self.current_state = self.root
+ self.finish_state_name = (
+ kwargs["finish_state_name"]
+ if "finish_state_name" in kwargs
+ else "end_state"
+ )
+ self.roles_to_names = None
+ self.names_to_roles = None
+ self.finished = False
+
+ @classmethod
+ def from_config(cls, config_path):
+ with open(config_path) as f:
+ config = json.load(f)
+ os.environ.clear()
+ for key,value in config["config"].items():
+ if key == "API_BASE":
+ if value == "":
+ pass
+ else:
+ os.environ[key] = value
+ # assert "API_KEY" in os.environ and os.environ["API_KEY"] != "API_KEY","Please go to config.json to set API_KEY"
+
+ sop = SOP(**config)
+ return sop
+
+ def init_states(self, states_dict):
+ for state_name, state_dict in states_dict.items():
+ state_dict["name"] = state_name
+ self.states[state_name] = State(**state_dict)
+
+ def init_relation(self, relations):
+ for state_name, state_relation in relations.items():
+ for idx, next_state_name in state_relation.items():
+ self.states[state_name].next_states[idx] = self.states[next_state_name]
+
+ def transit(self, chat_history, **kwargs):
+ """
+ Determine the next state based on the current situation
+ Return :
+ next_state(State) : the next state
+ """
+ # 如果是单一循环节点,则一直循环即可
+ # If it is a single loop node, just keep looping
+ if len(self.current_state.next_states) == 1:
+ next_state = "0"
+
+ # 否则则需要controller去判断进入哪一节点
+ # Otherwise, the controller needs to determine which node to enter.
+ else:
+ current_state = self.current_state
+ controller_dict = self.controller_dict[current_state.name]
+ relevant_history = kwargs["relevant_history"]
+
+ max_chat_nums = controller_dict["max_chat_nums"] if "max_chat_nums" in controller_dict else 1000
+ if current_state.chat_nums>=max_chat_nums:
+ return self.current_state.next_states["1"]
+
+
+ # 否则则让controller判断是否结束
+ # Otherwise, let the controller judge whether to end
+ judge_system_prompt = controller_dict["judge_system_prompt"]
+ environment_prompt = eval(Get_environment_prompt) if current_state.environment_prompt else ""
+ transit_system_prompt = eval(Transit_system_prompt)
+
+ judge_last_prompt = controller_dict["judge_last_prompt"]
+ transit_last_prompt = eval(Transit_last_prompt)
+
+
+
+ environment = kwargs["environment"]
+ environment_summary = environment.shared_memory["short_term_memory"]
+ chat_history_message = Memory.get_chat_history(chat_history)
+ query = chat_history[-1].get_query()
+
+ chat_messages = [
+ {
+ "role": "user",
+ "content": eval(Transit_message)
+ }
+ ]
+
+ extract_words = controller_dict["judge_extract_words"] if "judge_extract_words" in controller_dict else "end"
+
+
+ response = self.LLM.get_response(
+ chat_messages, transit_system_prompt, transit_last_prompt, stream=False, **kwargs
+ )
+ next_state = (
+ response if response.isdigit() else extract(response, extract_words)
+ )
+
+ # 如果没有parse出来则继续循环
+ # If no parse comes out, continue looping
+ if not next_state.isdigit():
+ next_state = "0"
+
+ next_state = self.current_state.next_states[next_state]
+ return next_state
+
+
+ def route(self, chat_history, **kwargs):
+ """
+ Determine the role that needs action based on the current situation
+ Return :
+ current_agent(Agent) : the next act agent
+ """
+
+ agents = kwargs["agents"]
+
+ # 知道进入哪一状态后开始分配角色,如果该状态下只有一个角色则直接分配给他
+ # Start assigning roles after knowing which state you have entered. If there is only one role in that state, assign it directly to him.
+ if len(self.current_state.roles) == 1:
+ next_role = self.current_state.roles[0]
+
+
+
+ # 否则controller进行分配
+ # Otherwise the controller determines
+ else:
+ relevant_history = kwargs["relevant_history"]
+ controller_type = (
+ self.controller_dict[self.current_state.name]["controller_type"]
+ if "controller_type" in self.controller_dict[self.current_state.name]
+ else "order"
+ )
+
+
+ # 如果是rule 控制器,则交由LLM进行分配角色
+ # If controller type is rule, it is left to LLM to assign roles.
+ if controller_type == "rule":
+ controller_dict = self.controller_dict[self.current_state.name]
+
+ call_last_prompt = controller_dict["call_last_prompt"] if "call_last_prompt" in controller_dict else ""
+
+ allocate_prompt = ""
+ roles = list(set(self.current_state.roles))
+ for role in roles:
+ allocate_prompt += eval(Allocate_component)
+
+ call_system_prompt = controller_dict["call_system_prompt"] if "call_system_prompt" in controller_dict else ""
+ environment_prompt = eval(Get_environment_prompt) if self.current_state.environment_prompt else ""
+ # call_system_prompt + environment + allocate_prompt
+ call_system_prompt = eval(Call_system_prompt)
+
+ query = chat_history[-1].get_query()
+ last_name = chat_history[-1].send_name
+ # last_prompt: note + last_prompt + query
+ call_last_prompt =eval(Call_last_prompt)
+
+
+ chat_history_message = Memory.get_chat_history(chat_history)
+ # Intermediate historical conversation records
+ chat_messages = [
+ {
+ "role": "user",
+ "content": eval(Call_message),
+ }
+ ]
+
+ extract_words = controller_dict["call_extract_words"] if "call_extract_words" in controller_dict else "end"
+
+ response = self.LLM.get_response(
+ chat_messages, call_system_prompt, call_last_prompt, stream=False, **kwargs
+ )
+
+ # get next role
+ next_role = extract(response, extract_words)
+
+ # Speak in order
+ elif controller_type == "order":
+ # If there is no begin role, it will be given directly to the first person.
+ if not self.current_state.current_role:
+ next_role = self.current_state.roles[0]
+ # otherwise first
+ else:
+ self.current_state.index += 1
+ self.current_state.index = (self.current_state.index) % len(self.current_state.roles)
+ next_role = self.current_state.roles[self.current_state.index]
+ # random speak
+ elif controller_type == "random":
+ next_role = random.choice(self.current_state.roles)
+
+ # 如果下一角色不在,则随机挑选一个
+ # If the next character is not available, pick one at random
+ if next_role not in self.current_state.roles:
+ next_role = random.choice(self.current_state.roles)
+
+ self.current_state.current_role = next_role
+
+ next_agent = agents[self.roles_to_names[self.current_state.name][next_role]]
+
+ return next_agent
+
+ def next(self, environment, agents):
+ """
+ Determine the next state and the agent that needs action based on the current situation
+ """
+
+ # 如果是第一次进入该状态
+ # If it is the first time to enter this state
+
+ if self.current_state.is_begin:
+ agent_name = self.roles_to_names[self.current_state.name][self.current_state.begin_role]
+ agent = agents[agent_name]
+ return self.current_state,agent
+
+
+ # get relevant history
+ query = environment.shared_memory["long_term_memory"][-1].content
+ relevant_history = get_relevant_history(
+ query,
+ environment.shared_memory["long_term_memory"][:-1],
+ environment.shared_memory["chat_embeddings"][:-1],
+ )
+ relevant_history = Memory.get_chat_history(relevant_history)
+
+
+
+ next_state = self.transit(
+ chat_history=environment.shared_memory["long_term_memory"][
+ environment.current_chat_history_idx :
+ ],
+ relevant_history=relevant_history,
+ environment=environment,
+ )
+ # 如果进入终止节点,则直接终止
+ # If you enter the termination node, terminate directly
+ if next_state.name == self.finish_state_name:
+ self.finished = True
+ return None, None
+
+ self.current_state = next_state
+
+ # 如果是首次进入该节点且有开场白,则直接分配给开场角色
+ # If it is the first time to enter the state and there is a begin query, it will be directly assigned to the begin role.
+ if self.current_state.is_begin and self.current_state.begin_role:
+ agent_name = self.roles_to_names[self.current_state.name][self.current_state.begin_role]
+ agent = agents[agent_name]
+ return self.current_state,agent
+
+
+ next_agent = self.route(
+ chat_history=environment.shared_memory["long_term_memory"][
+ environment.current_chat_history_idx :
+ ],
+ agents = agents,
+ relevant_history=relevant_history,
+ )
+
+ return self.current_state, next_agent
diff --git a/agents/State.py b/agents/State.py
new file mode 100644
index 0000000000000000000000000000000000000000..fa4b050eb09fba46a9a9431f39ac281d2abca016
--- /dev/null
+++ b/agents/State.py
@@ -0,0 +1,142 @@
+from Component import *
+
+
+class State:
+ """
+ Sub-scenes of role activities, responsible for storing the tasks that each role needs to do
+ """
+ def __init__(self, **kwargs):
+ self.next_states = {}
+ self.name = kwargs["name"]
+
+ self.environment_prompt = (
+ kwargs["environment_prompt"] if "environment_prompt" in kwargs else ""
+ )
+
+ self.roles = kwargs["roles"] if "roles" in kwargs else (list(kwargs["agent_states"].keys()) if "agent_states" in kwargs else [0])
+ if len(self.roles) == 0:
+ self.roles = [0]
+ self.begin_role = (
+ kwargs["begin_role"] if "begin_role" in kwargs else self.roles[0]
+ )
+ self.begin_query = kwargs["begin_query"] if "begin_query" in kwargs else None
+
+ self.is_begin = True
+
+ self.summary_prompt = (
+ kwargs["summary_prompt"] if "summary_prompt" in kwargs else None
+ )
+ self.current_role = self.begin_role
+ self.components = (
+ self.init_components(kwargs["agent_states"])
+ if "agent_states" in kwargs
+ else {}
+ )
+ self.index = (
+ self.roles.index(self.begin_role) if self.begin_role in self.roles else 0
+ )
+ self.chat_nums = 0
+
+ def init_components(self, agent_states_dict: dict):
+ agent_states = {}
+ for role, components in agent_states_dict.items():
+ component_dict = {}
+ for component, component_args in components.items():
+ if component:
+ # "role" "style"
+ if component == "style":
+ component_dict["style"] = StyleComponent(component_args["role"])
+
+ # "task"
+ elif component == "task":
+ component_dict["task"] = TaskComponent(component_args["task"])
+
+ # "rule"
+ elif component == "rule":
+ component_dict["rule"] = RuleComponent(component_args["rule"])
+
+ # "demonstration"
+ elif component == "demonstrations":
+ component_dict["demonstrations"] = DemonstrationComponent(
+ component_args["demonstrations"]
+ )
+
+ # "output"
+ elif component == "output":
+ component_dict["output"] = OutputComponent(
+ component_args["output"]
+ )
+
+ elif component == "last":
+ component_dict["last"] = LastComponent(
+ component_args["last_prompt"]
+ )
+
+ # "demonstrations"
+ elif component == "cot":
+ component_dict["cot"] = CoTComponent(
+ component_args["demonstrations"]
+ )
+ elif component == "CustomizeComponent":
+ component_dict["CustomizeComponent"] = CustomizeComponent(
+ component_args["template"], component_args["keywords"]
+ )
+
+ elif component == "system" :
+ component_dict["system"] = SystemComponent(
+ component_args["system_prompt"]
+ )
+
+ # =================================================================================#
+
+ # "output"
+ elif component == "StaticComponent":
+ component_dict["StaticComponent"] = StaticComponent(
+ component_args["output"]
+ )
+
+ # "top_k" "type" "knowledge_base" "system_prompt" "last_prompt"
+ elif component == "KnowledgeBaseComponent":
+ component_dict["tool"] = KnowledgeBaseComponent(
+ component_args["top_k"],
+ component_args["type"],
+ component_args["knowledge_path"],
+ )
+
+ elif component == "CategoryRequirementsComponent":
+ component_dict[
+ "CategoryRequirementsComponent"
+ ] = CategoryRequirementsComponent(
+ component_args["information_path"]
+ )
+
+ elif component == "FunctionComponent":
+ component_dict["FunctionComponent"] = FunctionComponent(component_args[""])
+ # "short_memory_extract_words" "long_memory_extract_words" "system_prompt" "last_prompt"
+ elif component == "ExtractComponent":
+ component_dict["ExtractComponent"] = ExtractComponent(
+ component_args["extract_words"],
+ component_args["system_prompt"],
+ component_args["last_prompt"],
+ )
+ elif component == "WebSearchComponent":
+ component_dict["WebSearchComponent"] = WebSearchComponent(
+ component_args["engine_name"], component_args["api"]
+ )
+ elif component == "WebCrawlComponent":
+ component_dict["WebCrawlComponent"] = WebCrawlComponent(
+ component_args["name"]
+ )
+
+ elif component == "CodeComponent":
+ component_dict["CodeComponent"] = CodeComponent(
+ component_args["file_name"], component_args["keyword"]
+ )
+
+ # ====================================================
+ else:
+ continue
+
+ agent_states[role] = component_dict
+
+ return agent_states
diff --git a/agents/__init__.py b/agents/__init__.py
new file mode 100644
index 0000000000000000000000000000000000000000..69b468b54240b0a357eac1ba7573971cf65b412c
--- /dev/null
+++ b/agents/__init__.py
@@ -0,0 +1,4 @@
+from .evolve import *
+from .SOP import *
+from .State import *
+from .utils import *
\ No newline at end of file
diff --git a/agents/__pycache__/SOP.cpython-38.pyc b/agents/__pycache__/SOP.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..859b99f8ccf092fd86157e7713b8c6d14477f674
Binary files /dev/null and b/agents/__pycache__/SOP.cpython-38.pyc differ
diff --git a/agents/__pycache__/State.cpython-38.pyc b/agents/__pycache__/State.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..db66146bc6a239b3c50fef6fd954d0acda3f6143
Binary files /dev/null and b/agents/__pycache__/State.cpython-38.pyc differ
diff --git a/agents/__pycache__/__init__.cpython-38.pyc b/agents/__pycache__/__init__.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..c9600089a1c58d0f74fd0804e89d52987daf9442
Binary files /dev/null and b/agents/__pycache__/__init__.cpython-38.pyc differ
diff --git a/agents/__pycache__/evolve.cpython-38.pyc b/agents/__pycache__/evolve.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..c57c030129d6784e654d718dd25b48f96ce430f8
Binary files /dev/null and b/agents/__pycache__/evolve.cpython-38.pyc differ
diff --git a/agents/__pycache__/utils.cpython-38.pyc b/agents/__pycache__/utils.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..8307bf4fb54ceb14fda87764521cf31f1fa258fa
Binary files /dev/null and b/agents/__pycache__/utils.cpython-38.pyc differ
diff --git a/agents/evolve.py b/agents/evolve.py
new file mode 100644
index 0000000000000000000000000000000000000000..0dce6460ddbba15c7f6af050b9b2d02b8919a174
--- /dev/null
+++ b/agents/evolve.py
@@ -0,0 +1,17 @@
+# coding=utf-8
+# Copyright 2023 The AIWaves Inc. team.
+
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""self evolution of an LLM autonoumous agent"""
diff --git a/agents/template.py b/agents/template.py
new file mode 100644
index 0000000000000000000000000000000000000000..194c9f2c3bad4be9589b72f520660971e2bc4e5a
--- /dev/null
+++ b/agents/template.py
@@ -0,0 +1,111 @@
+## default { "temperature": 0.3, "model": "gpt-3.5-turbo-16k-0613","log_path": "logs/{your name}"}
+LLM = {
+ "temperature": 0.0,
+ "model": "gpt-3.5-turbo-16k-0613",
+ "log_path": "logs/god"
+}
+
+
+Agents = {
+ "Lilong" : {
+ "style" : "professional",
+ "roles" : {
+ "company" : "coder",
+ "state2" : "role2",
+ },
+ "name2" : {
+ "style" : "professional",
+ "roles" : {
+ "company" : "coder",
+ "state2" : "role2",
+ },
+ }
+ }
+}
+
+# indispensable parameter: "controller_type"("order","random","rule")
+# default extract words: "end". You can choose not to fill in this parameter
+controller = {
+ "controller_type": "order",
+ "max_chat_nums" : 12,
+ "judge_system_prompt": "",
+ "judge_last_prompt": "",
+ "judge_extract_words": "end",
+ "call_system_prompt" : "",
+ "call_last_prompt": "",
+ "call_extract_words": ""
+}
+
+#
+Agent_state = {
+ "role": {
+ "LLM_type": "OpenAI",
+ "LLM": LLM,
+ "style": {
+ "role": "Opening Advocate for the Affirmative",
+ "style": "professional"
+ },
+ "task": {
+ "task": ""
+ },
+ "rule": {
+ "rule": ""
+ }
+ },
+}
+
+
+# indispensable parameter: "agent_states","controller"
+# "roles" determines the speaking order when the rule is order. If not set, it is the default order.
+# "begin_query" & "begin_role" determines the first speaker.It often determines the direction of the next speech. If you do not set it, it will default to the first agent.
+# "environment_prompt" : Responsible for setting the scene for the current environment
+State = {
+ "controller": controller,
+ "begin_role": "",
+ "begin_query": "",
+ "environment_prompt": "",
+ "roles": ["role1","role2"],
+ "LLM_type": "OpenAI",
+ "LLM": LLM,
+ "agent_state" : Agent_state,
+}
+
+
+
+States = {
+ "end_state":{
+ "agent_states":{}
+ },
+ "state1" : State
+
+}
+
+
+# default finish_state_name is "end_state"
+# "environment_type" : "competive" : different states not share the memory; "cooperative":diffrent states share the memory
+SOP = {
+ "config" : {
+ "API_KEY" : "Your key",
+ "PROXY" : "Your PROXY",
+ "MAX_CHAT_HISTORY" : "5",
+ "User_Names" : "[\"alexander\"]"
+ },
+ "environment_type" : "competive",
+ "LLM_type": "OpenAI",
+ "LLM" :LLM,
+ "root": "state1",
+ "finish_state_name" : "end_state",
+ "relations": {
+ "state1": {
+ "0": "state1",
+ "1": "state2"
+ },
+ "state2":{
+ "0":"state2",
+ "1":"end_state"
+ }
+ },
+ "agents": Agents,
+ "states": States,
+}
+
diff --git a/agents/utils.py b/agents/utils.py
new file mode 100644
index 0000000000000000000000000000000000000000..8c826e72fa3c0f5c7e2e4ee46970501bc8b9b609
--- /dev/null
+++ b/agents/utils.py
@@ -0,0 +1,480 @@
+# coding=utf-8
+# Copyright 2023 The AIWaves Inc. team.
+
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+"""helper functions for an LLM autonoumous agent"""
+import csv
+import random
+import json
+import pandas
+import numpy as np
+import requests
+import torch
+from tqdm import tqdm
+from text2vec import semantic_search
+import re
+import datetime
+from langchain.document_loaders import UnstructuredFileLoader
+from langchain.text_splitter import CharacterTextSplitter
+from sentence_transformers import SentenceTransformer
+import string
+import random
+import os
+import openai
+
+embed_model_name = os.environ["Embed_Model"] if "Embed_Model" in os.environ else "text-embedding-ada-002"
+if embed_model_name in ["text-embedding-ada-002"]:
+ pass
+else:
+ embedding_model = SentenceTransformer(
+ embed_model_name, device=torch.device("cpu")
+ )
+
+def get_embedding(sentence):
+ if embed_model_name in ["text-embedding-ada-002"]:
+ openai.api_key = os.environ["API_KEY"]
+ if "PROXY" in os.environ:
+ assert "http:" in os.environ["PROXY"] or "socks" in os.environ["PROXY"],"PROXY error,PROXY must be http or socks"
+ openai.proxy = os.environ["PROXY"]
+ if "API_BASE" in os.environ:
+ openai.api_base = os.environ["API_BASE"]
+ embedding_model = openai.Embedding
+ embed = embedding_model.create(
+ model=embed_model_name,
+ input=sentence
+ )
+ embed = embed["data"][0]["embedding"]
+ embed = torch.tensor(embed,dtype=torch.float32)
+ else:
+ embed = embedding_model.encode(sentence,convert_to_tensor=True)
+ if len(embed.shape)==1:
+ embed = embed.unsqueeze(0)
+ return embed
+
+
+def get_code():
+ return "".join(random.sample(string.ascii_letters + string.digits, 8))
+
+
+def get_content_between_a_b(start_tag, end_tag, text):
+ """
+
+ Args:
+ start_tag (str): start_tag
+ end_tag (str): end_tag
+ text (str): complete sentence
+
+ Returns:
+ str: the content between start_tag and end_tag
+ """
+ extracted_text = ""
+ start_index = text.find(start_tag)
+ while start_index != -1:
+ end_index = text.find(end_tag, start_index + len(start_tag))
+ if end_index != -1:
+ extracted_text += text[start_index +
+ len(start_tag):end_index] + " "
+ start_index = text.find(start_tag, end_index + len(end_tag))
+ else:
+ break
+
+ return extracted_text.strip()
+
+
+def extract(text, type):
+ """extract the content between
+
+ Args:
+ text (str): complete sentence
+ type (str): tag
+
+ Returns:
+ str: content between
+ """
+ target_str = get_content_between_a_b(f"<{type}>", f"{type}>", text)
+ return target_str
+
+def count_files_in_directory(directory):
+ # 获取指定目录下的文件数目
+ file_count = len([f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))])
+ return file_count
+
+def delete_oldest_files(directory, num_to_keep):
+ # 获取目录下文件列表,并按修改时间排序
+ files = [(f, os.path.getmtime(os.path.join(directory, f))) for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))]
+
+ # 删除最开始的 num_to_keep 个文件
+ for i in range(min(num_to_keep, len(files))):
+ file_to_delete = os.path.join(directory, files[i][0])
+ os.remove(file_to_delete)
+
+def delete_files_if_exceed_threshold(directory, threshold, num_to_keep):
+ # 获取文件数目并进行处理
+ file_count = count_files_in_directory(directory)
+ if file_count > threshold:
+ delete_count = file_count - num_to_keep
+ delete_oldest_files(directory, delete_count)
+
+def save_logs(log_path, messages, response):
+ if not os.path.exists(log_path):
+ os.mkdir(log_path)
+ delete_files_if_exceed_threshold(log_path, 20, 10)
+ log_path = log_path if log_path else "logs"
+ log = {}
+ log["input"] = messages
+ log["output"] = response
+ os.makedirs(log_path, exist_ok=True)
+ log_file = os.path.join(
+ log_path,
+ datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S") + ".json")
+ with open(log_file, "w", encoding="utf-8") as f:
+ json.dump(log, f, ensure_ascii=False, indent=2)
+
+
+
+def semantic_search_word2vec(query_embedding, kb_embeddings, top_k):
+ return semantic_search(query_embedding, kb_embeddings, top_k=top_k)
+
+
+def cut_sent(para):
+ para = re.sub("([。!?\?])([^”’])", r"\1\n\2", para)
+ para = re.sub("(\.{6})([^”’])", r"\1\n\2", para)
+ para = re.sub("(\…{2})([^”’])", r"\1\n\2", para)
+ para = re.sub("([。!?\?][”’])([^,。!?\?])", r"\1\n\2", para)
+ para = para.rstrip()
+ pieces = [i for i in para.split("\n") if i]
+ batch_size = 3
+ chucks = [
+ " ".join(pieces[i:i + batch_size])
+ for i in range(0, len(pieces), batch_size)
+ ]
+ return chucks
+
+
+def process_document(file_path):
+ """
+ Save QA_csv to json.
+ Args:
+ model: LLM to generate embeddings
+ qa_dict: A dict contains Q&A
+ save_path: where to save the json file.
+ Json format:
+ Dict[num,Dict[q:str,a:str,chunk:str,emb:List[float]]
+ """
+ final_dict = {}
+ count = 0
+ if file_path.endswith(".csv"):
+ dataset = pandas.read_csv(file_path)
+ questions = dataset["question"]
+ answers = dataset["answer"]
+ # embedding q+chunk
+ for q, a in zip(questions, answers):
+ for text in cut_sent(a):
+ temp_dict = {}
+ temp_dict["q"] = q
+ temp_dict["a"] = a
+ temp_dict["chunk"] = text
+ temp_dict["emb"] = get_embedding(q + text).tolist()
+ final_dict[count] = temp_dict
+ count += 1
+ # embedding chunk
+ for q, a in zip(questions, answers):
+ for text in cut_sent(a):
+ temp_dict = {}
+ temp_dict["q"] = q
+ temp_dict["a"] = a
+ temp_dict["chunk"] = text
+ temp_dict["emb"] = get_embedding(text).tolist()
+ final_dict[count] = temp_dict
+ count += 1
+ # embedding q
+ for q, a in zip(questions, answers):
+ temp_dict = {}
+ temp_dict["q"] = q
+ temp_dict["a"] = a
+ temp_dict["chunk"] = a
+ temp_dict["emb"] = get_embedding(q).tolist()
+ final_dict[count] = temp_dict
+ count += 1
+ # embedding q+a
+ for q, a in zip(questions, answers):
+ temp_dict = {}
+ temp_dict["q"] = q
+ temp_dict["a"] = a
+ temp_dict["chunk"] = a
+ temp_dict["emb"] = get_embedding(q + a).tolist()
+ final_dict[count] = temp_dict
+ count += 1
+ # embedding a
+ for q, a in zip(questions, answers):
+ temp_dict = {}
+ temp_dict["q"] = q
+ temp_dict["a"] = a
+ temp_dict["chunk"] = a
+ temp_dict["emb"] = get_embedding(a).tolist()
+ final_dict[count] = temp_dict
+ count += 1
+ print(f"finish updating {len(final_dict)} data!")
+ os.makedirs("temp_database", exist_ok=True)
+ save_path = os.path.join(
+ "temp_database/",
+ file_path.split("/")[-1].replace("." + file_path.split(".")[1],
+ ".json"),
+ )
+ print(save_path)
+ with open(save_path, "w") as f:
+ json.dump(final_dict, f, ensure_ascii=False, indent=2)
+ return {"knowledge_base": save_path, "type": "QA"}
+ else:
+ loader = UnstructuredFileLoader(file_path)
+ docs = loader.load()
+ text_spiltter = CharacterTextSplitter(chunk_size=200,
+ chunk_overlap=100)
+ docs = text_spiltter.split_text(docs[0].page_content)
+ os.makedirs("temp_database", exist_ok=True)
+ save_path = os.path.join(
+ "temp_database/",
+ file_path.replace("." + file_path.split(".")[1], ".json"))
+ final_dict = {}
+ count = 0
+ for c in tqdm(docs):
+ temp_dict = {}
+ temp_dict["chunk"] = c
+ temp_dict["emb"] = get_embedding(c).tolist()
+ final_dict[count] = temp_dict
+ count += 1
+ print(f"finish updating {len(final_dict)} data!")
+ with open(save_path, "w") as f:
+ json.dump(final_dict, f, ensure_ascii=False, indent=2)
+ return {"knowledge_base": save_path, "type": "UnstructuredFile"}
+
+def load_knowledge_base_qa(path):
+ """
+ Load json format knowledge base.
+ """
+ print("path", path)
+ with open(path, "r") as f:
+ data = json.load(f)
+ embeddings = []
+ questions = []
+ answers = []
+ chunks = []
+ for idx in range(len(data.keys())):
+ embeddings.append(data[str(idx)]["emb"])
+ questions.append(data[str(idx)]["q"])
+ answers.append(data[str(idx)]["a"])
+ chunks.append(data[str(idx)]["chunk"])
+ embeddings = np.array(embeddings, dtype=np.float32)
+ embeddings = torch.from_numpy(embeddings).squeeze()
+ return embeddings, questions, answers, chunks
+
+
+def load_knowledge_base_UnstructuredFile(path):
+ """
+ Load json format knowledge base.
+ """
+ with open(path, "r") as f:
+ data = json.load(f)
+ embeddings = []
+ chunks = []
+ for idx in range(len(data.keys())):
+ embeddings.append(data[str(idx)]["emb"])
+ chunks.append(data[str(idx)]["chunk"])
+ embeddings = np.array(embeddings, dtype=np.float32)
+ embeddings = torch.from_numpy(embeddings).squeeze()
+ return embeddings, chunks
+
+
+def cos_sim(a: torch.Tensor, b: torch.Tensor):
+ """
+ Computes the cosine similarity cos_sim(a[i], b[j]) for all i and j.
+ :return: Matrix with res[i][j] = cos_sim(a[i], b[j])
+ """
+ if not isinstance(a, torch.Tensor):
+ a = torch.tensor(a)
+
+ if not isinstance(b, torch.Tensor):
+ b = torch.tensor(b)
+
+ if len(a.shape) == 1:
+ a = a.unsqueeze(0)
+
+ if len(b.shape) == 1:
+ b = b.unsqueeze(0)
+
+ a_norm = torch.nn.functional.normalize(a, p=2, dim=1)
+ b_norm = torch.nn.functional.normalize(b, p=2, dim=1)
+ return torch.mm(a_norm, b_norm.transpose(0, 1))
+
+
+def matching_a_b(a, b, requirements=None):
+ a_embedder = get_embedding(a)
+ # 获取embedder
+ b_embeder = get_embedding(b)
+ sim_scores = cos_sim(a_embedder, b_embeder)[0]
+ return sim_scores
+
+
+def matching_category(inputtext,
+ forest_name,
+ requirements=None,
+ cat_embedder=None,
+ top_k=3):
+ """
+ Args:
+ inputtext: the category name to be matched
+ forest: search tree
+ top_k: the default three highest scoring results
+ Return:
+ topk matching_result. List[List] [[top1_name,top2_name,top3_name],[top1_score,top2_score,top3_score]]
+ """
+
+ sim_scores = torch.zeros([100])
+ if inputtext:
+ input_embeder = get_embedding(inputtext)
+ sim_scores = cos_sim(input_embeder, cat_embedder)[0]
+
+ if requirements:
+ requirements = requirements.split(" ")
+ requirements_embedder = get_embedding(requirements)
+ req_scores = cos_sim(requirements_embedder, cat_embedder)
+ req_scores = torch.mean(req_scores, dim=0)
+ total_scores = req_scores
+ else:
+ total_scores = sim_scores
+
+ top_k_cat = torch.topk(total_scores, k=top_k)
+ top_k_score, top_k_idx = top_k_cat[0], top_k_cat[1]
+ top_k_name = [forest_name[top_k_idx[i]] for i in range(0, top_k)]
+
+ return [top_k_name, top_k_score.tolist(), top_k_idx]
+
+
+def sample_with_order_preserved(lst, num):
+ """Randomly sample from the list while maintaining the original order."""
+ indices = list(range(len(lst)))
+ sampled_indices = random.sample(indices, num)
+ sampled_indices.sort() # 保持原顺序
+ return [lst[i] for i in sampled_indices]
+
+
+def limit_values(data, max_values):
+ """Reduce each key-value list in the dictionary to the specified size, keeping the order of the original list unchanged."""
+ for key, values in data.items():
+ if len(values) > max_values:
+ data[key] = sample_with_order_preserved(values, max_values)
+ return data
+
+
+def limit_keys(data, max_keys):
+ """Reduce the dictionary to the specified number of keys."""
+ keys = list(data.keys())
+ if len(keys) > max_keys:
+ keys = sample_with_order_preserved(keys, max_keys)
+ data = {key: data[key] for key in keys}
+ return data
+
+
+def flatten_dict(nested_dict):
+ """
+ flatten the dictionary
+ """
+ flattened_dict = {}
+ for key, value in nested_dict.items():
+ if isinstance(value, dict):
+ flattened_subdict = flatten_dict(value)
+ flattened_dict.update(flattened_subdict)
+ else:
+ flattened_dict[key] = value
+ return flattened_dict
+
+
+def merge_list(list1, list2):
+ for l in list2:
+ if l not in list1:
+ list1.append(l)
+ return list1
+
+
+def Search_Engines(req):
+ FETSIZE = eval(os.environ["FETSIZE"]) if "FETSIZE" in os.environ else 5
+
+ new_dict = {"keyword": req, "catLeafName": "", "fetchSize": FETSIZE}
+ url = os.environ["SHOPPING_SEARCH"]
+ res = requests.post(
+ url= url,
+ json=new_dict,
+ )
+ user_dict = json.loads(res.text)
+ if "data" in user_dict.keys():
+ request_items = user_dict["data"]["items"] # 查询到的商品信息JSON
+ top_category = user_dict["data"]["topCategories"]
+ return request_items, top_category
+ else:
+ return []
+
+
+def search_with_api(requirements, categery):
+
+ FETSIZE = eval(os.environ["FETSIZE"]) if "FETSIZE" in os.environ else 5
+
+ request_items = []
+ all_req_list = requirements.split(" ")
+ count = 0
+
+ while len(request_items) < FETSIZE and len(all_req_list) > 0:
+ if count:
+ all_req_list.pop(0)
+ all_req = (" ").join(all_req_list)
+ if categery not in all_req_list:
+ all_req = all_req + " " + categery
+ now_request_items, top_category = Search_Engines(all_req)
+ request_items = merge_list(request_items, now_request_items)
+ count += 1
+ new_top = []
+ for category in top_category:
+ if "其它" in category or "其它" in category:
+ continue
+ else:
+ new_top.append(category)
+ if len(request_items) > FETSIZE:
+ request_items = request_items[:FETSIZE]
+ return request_items, new_top
+
+
+
+def get_relevant_history(query,history,embeddings):
+ """
+ Retrieve a list of key history entries based on a query using semantic search.
+
+ Args:
+ query (str): The input query for which key history is to be retrieved.
+ history (list): A list of historical key entries.
+ embeddings (numpy.ndarray): An array of embedding vectors for historical entries.
+
+ Returns:
+ list: A list of key history entries most similar to the query.
+ """
+ TOP_K = eval(os.environ["TOP_K"]) if "TOP_K" in os.environ else 2
+ relevant_history = []
+ query_embedding = get_embedding(query)
+ hits = semantic_search(query_embedding, embeddings, top_k=min(TOP_K,embeddings.shape[0]))
+ hits = hits[0]
+ for hit in hits:
+ matching_idx = hit["corpus_id"]
+ try:
+ relevant_history.append(history[matching_idx])
+ except:
+ return []
+ return relevant_history
diff --git a/app.py b/app.py
index d54b9c01c357e121a03feaca61f15542aa99c478..3ea799b6dd1c8a7338bd3c77579bda51235afbe5 100644
--- a/app.py
+++ b/app.py
@@ -95,8 +95,6 @@ class NovelUI(WebUI):
def construct_ui(self):
with gr.Blocks(css=gc.CSS) as demo:
- gr.Markdown("""# Agents""")
- gr.Markdown("""**Agents** is an open-source library/framework for building autonomous language agents.if you want to know more about **Agents**, please check our📄 Paper and📦 Github. Here is a demo of **Agents**. You can use it to write a novel.""")
with gr.Column():
self.progress = gr.HTML(
value=sc.FORMAT.format(
@@ -111,6 +109,11 @@ class NovelUI(WebUI):
label="Dialog",
height=500
)
+ self.text_api = gr.Textbox(
+ value = self.cache["api_key"],
+ placeholder="openai key",
+ label="Please input valid openai key for gpt-3.5-turbo-16k."
+ )
with gr.Row():
self.text_requirement = gr.Textbox(
placeholder="Requirement of the novel",
@@ -145,7 +148,7 @@ class NovelUI(WebUI):
# ===============Event Listener===============
self.btn_start.click(
fn=self.btn_start_when_click,
- inputs=[self.text_requirement],
+ inputs=[self.text_requirement, self.text_api],
outputs=[self.chatbot, self.chat_record, self.btn_start, self.text_requirement]
).then(
fn=self.btn_start_after_click,
@@ -169,7 +172,7 @@ class NovelUI(WebUI):
# ===========================================
self.demo = demo
- def btn_start_when_click(self, text_requirement:str):
+ def btn_start_when_click(self, text_requirement:str, api_key:str):
"""
inputs=[self.text_requirement],
outputs=[self.chatbot, self.chat_record, self.btn_start, self.text_requirement]
@@ -179,7 +182,7 @@ class NovelUI(WebUI):
gr.Chatbot.update(visible=True),\
gr.Button.update(interactive=False, value="Running"),\
gr.Textbox.update(value="", interactive=False)
- self.send_start_cmd({'requirement': text_requirement})
+ self.send_start_cmd({'requirement': text_requirement, "api_key": api_key})
return
def btn_start_after_click(self, history:List, record):
@@ -283,4 +286,4 @@ class NovelUI(WebUI):
if __name__ == '__main__':
ui = NovelUI(client_cmd=["python","gradio_backend.py"])
ui.construct_ui()
- ui.run(share=True)
\ No newline at end of file
+ ui.run()
\ No newline at end of file
diff --git a/create_sop.py b/create_sop.py
index 6c97c33777a9bf497d23ddb6e523dba306e831e7..a6008344a232b3bdfe8e835b7aa58985a2ad5236 100644
--- a/create_sop.py
+++ b/create_sop.py
@@ -37,8 +37,7 @@ def create_sop(folder_name: str = "novel_outline", encoding: str = "utf-8", save
sop_file = f"./{save_name}.json"
sop_dict = {
"config": {
- "API_KEY": "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",
- "PROXY": "",
+ "API_KEY": "sk-xxxxxxxxxxxxxxxxxxxx",
"MAX_CHAT_HISTORY" : "100",
"TOP_K" : "1",
"ACTIVE_MODE" : "0",
diff --git a/gradio_backend.py b/gradio_backend.py
index 58be099bbc3ffbeae99ab9ef0309e7809e9bcd52..636405b928512997ecb9775c1759d256800e41b7 100644
--- a/gradio_backend.py
+++ b/gradio_backend.py
@@ -1,16 +1,16 @@
import sys
sys.path.append("./novel-server")
-
+sys.path.append("agents")
import yaml
import os
import argparse
import random
-from agents.SOP import SOP
-from agents.Agent import Agent
-from agents.Environment import Environment
+from SOP import SOP
+from Agent import Agent
+from Environment import Environment
from gradio_base import Client
-from agents.Memory import Memory
+from Memory import Memory
from myagent import Node, MyAgent, ask_gpt
from typing import List, Tuple
@@ -62,10 +62,13 @@ if __name__ == "__main__":
"agents_name": ['Elmo','Abby', 'Zoe', 'Ernie', 'Bert', 'Oscar'],
"nodes_name": ['Node 1','Node 2','Node 3', 'Node 4', 'state1', 'state2', 'state3', 'state4'],
"output_file_path": f"{os.getcwd()+'/novel_outline'}",
- "requirement": NOVEL_PROMPT['Node 1']["task"]
+ "requirement": NOVEL_PROMPT['Node 1']["task"],
+ "api_key": "sk-xxxxxxxxxxxxxxxxxxxx"
}
)
client.listening_for_start_()
+ os.environ["API_KEY"] = client.cache["api_key"]
+ MyAgent.API_KEY = client.cache["api_key"]
NOVEL_PROMPT['Node 1']['task'] = client.cache['requirement']
print("Received: ", client.cache['requirement'])
outline = run_node_1(
@@ -92,5 +95,4 @@ if __name__ == "__main__":
show_in_gradio(30, str(name_list), " ", " ")
agents,sop,environment = init("novel_outline.json")
- run(agents,sop,environment)
-
+ run(agents,sop,environment)
\ No newline at end of file
diff --git a/gradio_base.py b/gradio_base.py
index ff6d222a6d5edb1917965b1caa84b0f31d2345d9..bb37210b9ee545b9974dfc305282406dd9fc2cfb 100644
--- a/gradio_base.py
+++ b/gradio_base.py
@@ -25,7 +25,22 @@ import socket
import psutil
import os
from abc import abstractmethod
+import openai
+def test_apikey_connection(api_key=None, model="gpt-3.5-turbo"):
+ openai.api_key = api_key if api_key is not None else os.environ["API_KEY"]
+ if "PROXY" in os.environ:
+ openai.proxy = os.environ["PROXY"]
+ messages = [{"role": "user", "content": "what's your name?"}]
+ try:
+ response = openai.ChatCompletion.create(
+ model=model,
+ messages=messages,
+ )
+ return True
+ except:
+ return False
+
def convert2list4agentname(sop):
"""
Extract the agent names of all states
@@ -304,7 +319,7 @@ class Client:
self.send_message(message=message)
def send_message(self, message):
- """Send the messaget to the server."""
+ """Send the message to the server."""
if isinstance(message, list) or isinstance(message, dict):
message = str(message)
assert isinstance(message, str)
@@ -339,7 +354,7 @@ class Client:
continue
else:
remaining = list_dataset[-1]
- # Recieve successfully
+ # Receive successfully
list_dataset = list_dataset[:-1]
return_value = []
for item in list_dataset:
@@ -488,12 +503,12 @@ class WebUI:
def _second_send(self, message:dict):
# Send the modified message.
- # It will be executed in `self.send_start_cmd()` automtically.
+ # It will be executed in `self.send_start_cmd()` automatically.
self.send_message(str(message))
def _third_send(self):
# Send start command.
- # It will be executed in `self.send_start_cmd()` automtically.
+ # It will be executed in `self.send_start_cmd()` automatically.
self.send_message(self.SIGN['START'])
def send_start_cmd(self, message:dict={"hello":"hello"}):
@@ -526,7 +541,7 @@ class WebUI:
self._connect()
def _start_client(self):
- print(f"server: excuting `{' '.join(self.client_cmd)}` ...")
+ print(f"server: executing `{' '.join(self.client_cmd)}` ...")
self.backend = subprocess.Popen(self.client_cmd)
def _close_client(self):
@@ -556,4 +571,4 @@ class WebUI:
if __name__ == '__main__':
- pass
+ pass
\ No newline at end of file
diff --git a/image.jpg b/image.jpg
new file mode 100644
index 0000000000000000000000000000000000000000..9f0c3b3d47066aff3e2c0a43800aacfadc38cef8
Binary files /dev/null and b/image.jpg differ
diff --git a/novel-server/__pycache__/PROMPT.cpython-38.pyc b/novel-server/__pycache__/PROMPT.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..6774edc2e5b5a1a8acce106d408d4c3ec365539a
Binary files /dev/null and b/novel-server/__pycache__/PROMPT.cpython-38.pyc differ
diff --git a/novel-server/__pycache__/cmd_outline.cpython-38.pyc b/novel-server/__pycache__/cmd_outline.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..936574563c11164e7568c4554480964f8f0c7aa3
Binary files /dev/null and b/novel-server/__pycache__/cmd_outline.cpython-38.pyc differ
diff --git a/novel-server/__pycache__/myagent.cpython-38.pyc b/novel-server/__pycache__/myagent.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..c0188600ed4859f729db4e9d657d092138af1954
Binary files /dev/null and b/novel-server/__pycache__/myagent.cpython-38.pyc differ
diff --git a/novel-server/__pycache__/myutils.cpython-38.pyc b/novel-server/__pycache__/myutils.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..7f03b2367423e2cd7d29b7f5c5ae19342e8ebf27
Binary files /dev/null and b/novel-server/__pycache__/myutils.cpython-38.pyc differ
diff --git a/novel-server/__pycache__/tree.cpython-38.pyc b/novel-server/__pycache__/tree.cpython-38.pyc
new file mode 100644
index 0000000000000000000000000000000000000000..a685aa12a1ad057e4b60ad41c3480997028af948
Binary files /dev/null and b/novel-server/__pycache__/tree.cpython-38.pyc differ
diff --git a/novel_outline.json b/novel_outline.json
index f1c80c82853915135993b121467fe0b921fcaec8..9f82d5fd247cbb38c64ef7713d5bc80075cfefd8 100644
--- a/novel_outline.json
+++ b/novel_outline.json
@@ -1,7 +1,6 @@
{
"config": {
- "API_KEY": "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",
- "PROXY": "",
+ "API_KEY": "sk-xxxxxxxxxxxxxxxxxxxx",
"MAX_CHAT_HISTORY": "100",
"TOP_K": "1",
"ACTIVE_MODE": "0",