Skip to content

Commit

Permalink
fix: add experimental function PlanAndExecute
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu007 committed May 5, 2024
1 parent 4aff430 commit d3d80d0
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 13 deletions.
2 changes: 1 addition & 1 deletion healthcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ APP_START_PY="/app/server.py"
if ! pgrep -f "python ${APP_START_PY}" >/dev/null; then
log "${APP_START_PY} is not running. Starting it now..."
# open_interpreterを起動
if bash -c "eval $(pyenv init -) && python ${APP_START_PY} >> $LOG_FILE 2>&1" & then
if bash -c "eval $(pyenv init -) && python ${APP_START_PY} 2>&1 | tee $LOG_FILE" & then
log "${APP_START_PY} started successfully."
sleep 3600
exit 0
Expand Down
18 changes: 15 additions & 3 deletions src/codeinterpreterapi/agents/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
from langchain.chat_models.base import BaseChatModel
from langchain.memory.buffer import ConversationBufferMemory
from langchain_core.prompts.chat import MessagesPlaceholder
from langchain_experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner
from langchain_openai import AzureChatOpenAI, ChatOpenAI

from codeinterpreterapi.config import settings


class CodeInterpreterAgent:
@staticmethod
def choose_agent(
def choose_single_chat_agent(
llm,
tools,
) -> BaseSingleActionAgent:
Expand Down Expand Up @@ -42,12 +43,12 @@ def choose_agent(
@staticmethod
def create_agent_and_executor(llm, tools, verbose, chat_memory, callbacks) -> AgentExecutor:
# agent
agent = CodeInterpreterAgent.choose_agent(llm, tools)
agent = CodeInterpreterAgent.choose_single_chat_agent(llm, tools)
print("create_agent_and_executor agent=", str(type(agent)))
# pprint.pprint(agent)

# agent_executor
agent_executor = AgentExecutor.from_agent_and_tools(
agent_executor = load_agent_executor(
agent=agent,
max_iterations=settings.MAX_ITERATIONS,
tools=tools,
Expand All @@ -64,3 +65,14 @@ def create_agent_and_executor(llm, tools, verbose, chat_memory, callbacks) -> Ag
pprint.pprint(tool)

return agent_executor

@staticmethod
def create_agent_and_executor_experimental(llm, tools, verbose) -> AgentExecutor:
# agent
agent = CodeInterpreterAgent.choose_single_chat_agent(llm, tools)
print("create_agent_and_executor agent=", str(type(agent)))

# agent_executor
agent_executor = load_agent_executor(llm, tools, verbose=verbose)

return agent_executor
1 change: 1 addition & 0 deletions src/codeinterpreterapi/planners/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

12 changes: 12 additions & 0 deletions src/codeinterpreterapi/planners/planners.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from langchain.base_language import BaseLanguageModel
from langchain_experimental.plan_and_execute import load_chat_planner
from langchain_experimental.plan_and_execute.planners.base import LLMPlanner


class CodeInterpreterPlanner:
@staticmethod
def choose_planner(
llm: BaseLanguageModel,
) -> LLMPlanner:
planner = load_chat_planner(llm)
return planner
51 changes: 43 additions & 8 deletions src/codeinterpreterapi/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from langchain_core.chat_history import BaseChatMessageHistory
from langchain_core.language_models import BaseLanguageModel
from langchain_core.tools import BaseTool
from langchain_experimental.plan_and_execute.planners.base import LLMPlanner

from codeinterpreterapi.chains import (
aget_file_modifications,
Expand All @@ -31,6 +32,8 @@

from .agents.agents import CodeInterpreterAgent
from .llm.llm import CodeInterpreterLlm
from .planners.planners import CodeInterpreterPlanner
from .supervisors.supervisors import CodeInterpreterSupervisor, MySupervisorChain
from .tools.tools import CodeInterpreterTools


Expand Down Expand Up @@ -67,6 +70,8 @@ def __init__(

self.callbacks = callbacks
self.agent_executor: Optional[AgentExecutor] = None
self.llm_planner: Optional[LLMPlanner] = None
self.supervisor: Optional[MySupervisorChain] = None
self.input_files: list[File] = []
self.output_files: list[File] = []
self.code_log: list[tuple[str, str]] = []
Expand All @@ -82,20 +87,40 @@ def from_id(cls, session_id: UUID, **kwargs: Any) -> "CodeInterpreterSession":
def session_id(self) -> Optional[UUID]:
return self.codebox.session_id

def initialize(self):
self.initialize_agent_executor()
self.initialize_llm_planner()
self.initialize_supervisor()

def initialize_agent_executor(self):
agent_executor = CodeInterpreterAgent.create_agent_and_executor(
# self.agent_executor = CodeInterpreterAgent.create_agent_and_executor(
# llm=self.llm,
# tools=self.tools,
# verbose=self.verbose,
# chat_memory=self._history_backend(),
# callbacks=self.callbacks,
# )
self.agent_executor = CodeInterpreterAgent.create_agent_and_executor_experimental(
llm=self.llm,
tools=self.tools,
verbose=self.verbose,
chat_memory=self._history_backend(),
callbacks=self.callbacks,
)
return agent_executor

def initialize_llm_planner(self):
self.llm_planner = CodeInterpreterPlanner.choose_planner(
llm=self.llm,
)

def initialize_supervisor(self):
self.supervisor = CodeInterpreterSupervisor.choose_supervisor(
planner=self.llm_planner,
executor=self.agent_executor,
)

def start(self) -> SessionStatus:
print("start")
status = SessionStatus.from_codebox_status(self.codebox.start())
self.agent_executor = self.initialize_agent_executor()
self.initialize()
self.codebox.run(
f"!pip install -q {' '.join(settings.CUSTOM_PACKAGES)}",
)
Expand All @@ -104,21 +129,22 @@ def start(self) -> SessionStatus:
async def astart(self) -> SessionStatus:
print("astart")
status = SessionStatus.from_codebox_status(await self.codebox.astart())
self.agent_executor = self.initialize_agent_executor()
self.initialize()
await self.codebox.arun(
f"!pip install -q {' '.join(settings.CUSTOM_PACKAGES)}",
)
return status

def start_local(self) -> SessionStatus:
print("start_local")
self.agent_executor = self.initialize_agent_executor()
self.initialize()
status = SessionStatus(status="started")
return status

async def astart_local(self) -> SessionStatus:
print("astart_local")
status = self.start_local()
self.initialize()
return status

def _history_backend(self) -> BaseChatMessageHistory:
Expand Down Expand Up @@ -360,7 +386,12 @@ def generate_response(
self._input_handler(user_request)
assert self.agent_executor, "Session not initialized."
print(user_request.content)
response = self.agent_executor.invoke(input=user_request.content)

# ======= ↓↓↓↓ LLM invoke ↓↓↓↓ #=======
# response = self.agent_executor.invoke(input=user_request.content)
response = self.supervisor.invoke(input=user_request.content)
# ======= ↑↑↑↑ LLM invoke ↑↑↑↑ #=======

output = response["output"]
print("agent_executor.invoke output=", output)
return self._output_handler(output)
Expand All @@ -387,7 +418,11 @@ async def agenerate_response(
try:
await self._ainput_handler(user_request)
assert self.agent_executor, "Session not initialized."

# ======= ↓↓↓↓ LLM invoke ↓↓↓↓ #=======
response = await self.agent_executor.ainvoke(input=user_request.content)
# ======= ↑↑↑↑ LLM invoke ↑↑↑↑ #=======

output = response["output"]
print("agent_executor.invoke output=", output)
return await self._aoutput_handler(output)
Expand Down
15 changes: 15 additions & 0 deletions src/codeinterpreterapi/supervisors/supervisors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from langchain.agents import AgentExecutor
from langchain.chains.base import Chain
from langchain_experimental.plan_and_execute.agent_executor import PlanAndExecute
from langchain_experimental.plan_and_execute.planners.base import LLMPlanner


class MySupervisorChain(Chain):
pass


class CodeInterpreterSupervisor:
@staticmethod
def choose_supervisor(planner: LLMPlanner, executor: AgentExecutor) -> MySupervisorChain:
supervisor = PlanAndExecute(planner=planner, executor=executor, verbose=True)
return supervisor
1 change: 0 additions & 1 deletion src/codeinterpreterapi/tools/tools.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from langchain.agents.tools import Tool
from langchain_community.tools.shell.tool import ShellInput
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_core.tools import BaseTool, StructuredTool
Expand Down

0 comments on commit d3d80d0

Please sign in to comment.