clean up accidentally switched workernames

Former-commit-id: 5194209802d9ff7a2b3d4c71dd860bc2abbefe37
pull/160/head
Kye 2 years ago
parent d1ed96e883
commit c429874fa1

@ -17,6 +17,8 @@ class WorkerNode:
"""Useful for when you need to spawn an autonomous agent instance as a worker to accomplish complex tasks, it can search the internet or spawn child multi-modality models to process and generate images and text or audio and so on""" """Useful for when you need to spawn an autonomous agent instance as a worker to accomplish complex tasks, it can search the internet or spawn child multi-modality models to process and generate images and text or audio and so on"""
def __init__(self, llm, tools, vectorstore): def __init__(self, llm, tools, vectorstore):
if not llm or not tools or not vectorstore:
raise ValueError("llm, tools, and vectorstore cannot be None")
self.llm = llm self.llm = llm
self.tools = tools self.tools = tools
self.vectorstore = vectorstore self.vectorstore = vectorstore
@ -24,18 +26,26 @@ class WorkerNode:
def create_agent(self, ai_name, ai_role, human_in_the_loop, search_kwargs): def create_agent(self, ai_name, ai_role, human_in_the_loop, search_kwargs):
logging.info("Creating agent in WorkerNode") logging.info("Creating agent in WorkerNode")
self.agent = AutoGPT.from_llm_and_tools( try:
ai_name=ai_name,
ai_role=ai_role, self.agent = AutoGPT.from_llm_and_tools(
tools=self.tools, ai_name=ai_name,
llm=self.llm, ai_role=ai_role,
memory=self.vectorstore.as_retriever(search_kwargs=search_kwargs), tools=self.tools,
human_in_the_loop=human_in_the_loop, llm=self.llm,
chat_history_memory=FileChatMessageHistory("chat_history.txt"), memory=self.vectorstore.as_retriever(search_kwargs=search_kwargs),
) human_in_the_loop=human_in_the_loop,
self.agent.chain.verbose = True chat_history_memory=FileChatMessageHistory("chat_history.txt"),
)
self.agent.chain.verbose = True
except Exception as e:
logging.error(f"Error while creating agent: {str(e)}")
raise e
def add_tool(self, tool: Tool): def add_tool(self, tool: Tool):
if not isinstance(tool, Tool):
raise TypeError("Tool must be an instance of Tool")
self.tools.append(tool) self.tools.append(tool)
def run(self, prompt: str) -> str: def run(self, prompt: str) -> str:
@ -44,26 +54,40 @@ class WorkerNode:
if not prompt: if not prompt:
raise ValueError("Prompt is empty") raise ValueError("Prompt is empty")
self.agent.run([f"{prompt}"]) try:
return "Task completed by WorkerNode"
self.agent.run([f"{prompt}"])
return "Task completed by WorkerNode"
except Exception as e:
logging.error(f"While running the agent: {str(e)}")
raise e
worker_tool = Tool( # worker_tool = Tool(
name="WorkerNode AI Agent", # name="WorkerNode AI Agent",
func=WorkerNode.run, # func=WorkerNode.run,
description="Useful for when you need to spawn an autonomous agent instance as a worker to accomplish complex tasks, it can search the internet or spawn child multi-modality models to process and generate images and text or audio and so on" # description="Useful for when you need to spawn an autonomous agent instance as a worker to accomplish complex tasks, it can search the internet or spawn child multi-modality models to process and generate images and text or audio and so on"
) # )
class WorkerNodeInitializer: class WorkerNodeInitializer:
def __init__(self, openai_api_key): def __init__(self, openai_api_key):
if not openai_api_key:
raise ValueError("openai_api_key cannot be None")
self.openai_api_key = openai_api_key self.openai_api_key = openai_api_key
def initialize_llm(self, llm_class, temperature=0.5): def initialize_llm(self, llm_class, temperature=0.5):
if not llm_class:
raise ValueError("llm_class cannot be None")
return llm_class(openai_api_key=self.openai_api_key, temperature=temperature) return llm_class(openai_api_key=self.openai_api_key, temperature=temperature)
def initialize_tools(self, llm_class): def initialize_tools(self, llm_class):
if not llm_class:
raise ValueError("llm_class cannot be none")
logging.info('Creating WorkerNode')
llm = self.initialize_llm(llm_class) llm = self.initialize_llm(llm_class)
web_search = DuckDuckGoSearchRun() web_search = DuckDuckGoSearchRun()
tools = [ tools = [
@ -82,6 +106,8 @@ class WorkerNodeInitializer:
return FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {}) return FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {})
def create_worker_node(self, llm_class=ChatOpenAI): def create_worker_node(self, llm_class=ChatOpenAI):
if not llm_class:
raise ValueError("llm_class cannot be None")
worker_tools = self.initialize_tools(llm_class) worker_tools = self.initialize_tools(llm_class)
vectorstore = self.initialize_vectorstore() vectorstore = self.initialize_vectorstore()
worker_node = WorkerNode(llm=self.initialize_llm(llm_class), tools=worker_tools, vectorstore=vectorstore) worker_node = WorkerNode(llm=self.initialize_llm(llm_class), tools=worker_tools, vectorstore=vectorstore)
@ -89,6 +115,8 @@ class WorkerNodeInitializer:
return worker_node return worker_node
def worker_node(openai_api_key): def worker_node(openai_api_key):
if not openai_api_key:
raise ValueError("openai_api_key cannot be none")
initializer = WorkerNodeInitializer(openai_api_key) initializer = WorkerNodeInitializer(openai_api_key)
worker_node = initializer.create_worker_node() worker_node = initializer.create_worker_node()
return worker_node return worker_node

Loading…
Cancel
Save