From a45323617226fa36c8772376a4a1c95b7c87c0a3 Mon Sep 17 00:00:00 2001 From: Sam Cox Date: Fri, 6 Oct 2023 16:33:38 -0700 Subject: [PATCH] simple agent and maketool (#3) * simple agent and maketool * forgot to change prompt, oops * renamed prompt * maketool module issue --- sanclone/agent/__init__.py | 3 +++ sanclone/agent/agent.py | 41 +++++++++++++++++++++++++++++++++++++ sanclone/agent/prompt.py | 8 ++++++++ sanclone/tools/__init__.py | 3 ++- sanclone/tools/maketools.py | 11 ++++++++++ 5 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 sanclone/agent/__init__.py create mode 100644 sanclone/agent/agent.py create mode 100644 sanclone/agent/prompt.py create mode 100644 sanclone/tools/maketools.py diff --git a/sanclone/agent/__init__.py b/sanclone/agent/__init__.py new file mode 100644 index 0000000..bf3eda0 --- /dev/null +++ b/sanclone/agent/__init__.py @@ -0,0 +1,3 @@ +from agent import SanCloneAgent + +__all__ = ["SanCloneAgent"] diff --git a/sanclone/agent/agent.py b/sanclone/agent/agent.py new file mode 100644 index 0000000..a86cad6 --- /dev/null +++ b/sanclone/agent/agent.py @@ -0,0 +1,41 @@ +from langchain.agents import AgentExecutor, ZeroShotAgent +from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent +from langchain.chat_models import ChatOpenAI + +from ..tools import make_tools +from .prompt import prompt_template + + +class AgentType: + valid_models = { + "ReactAgent": ZeroShotAgent, + "OpenAIFunctionsAgent": OpenAIFunctionsAgent, + } + + @classmethod + def get_agent(cls, model_name: str = "ReactAgent"): + return cls.valid_models[model_name] + + +class SanCloneAgent: + def __init__( + self, + tools=None, + llm=None, + openai_api_key=None, + temp=0.1, + agent_type: str = "OpenAIFunctionsAgent", + verbose=True, + ): + llm = ChatOpenAI(temperature=0.0, model="gpt-4", client=None) + + tools = make_tools(llm) + self.agent_instance = AgentExecutor.from_agent_and_tools( + tools=tools, + agent=AgentType.get_agent(agent_type).from_llm_and_tools(llm, tools), + return_intermediate_steps=True, + handle_parsing_errors=True, + ) + + def run(self, prompt: str): + return self.agent_instance.run(prompt_template.format(input=prompt)) diff --git a/sanclone/agent/prompt.py b/sanclone/agent/prompt.py new file mode 100644 index 0000000..9be2b86 --- /dev/null +++ b/sanclone/agent/prompt.py @@ -0,0 +1,8 @@ +# flake8: noqa +prompt_template = """ +You are an expert in molecular cloning. +You have a set of tools at your disposal. +Your task is to respond to the question or +solve the problem to the best of your ability using the provided tools. +Here is the question: {input} +""" diff --git a/sanclone/tools/__init__.py b/sanclone/tools/__init__.py index 1095f07..816f89e 100644 --- a/sanclone/tools/__init__.py +++ b/sanclone/tools/__init__.py @@ -1,3 +1,4 @@ from .echo import EchoTool +from .maketools import make_tools -__all__ = ["EchoTool"] +__all__ = ["EchoTool", "make_tools"] diff --git a/sanclone/tools/maketools.py b/sanclone/tools/maketools.py new file mode 100644 index 0000000..55ea737 --- /dev/null +++ b/sanclone/tools/maketools.py @@ -0,0 +1,11 @@ +from langchain import agents +from langchain.llms.base import BaseLanguageModel + + +def make_tools(llm: BaseLanguageModel): + # add human input tool + tools = agents.load_tools(["human"], llm) + + # append tools here + tools += [] + return tools