mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-24 03:52:10 +00:00
Harrison/official pre release (#8106)
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
from typing import List
|
||||
|
||||
from langchain.agents.agent import AgentExecutor
|
||||
from langchain.agents.structured_chat.base import StructuredChatAgent
|
||||
from langchain.schema.language_model import BaseLanguageModel
|
||||
from langchain.tools import BaseTool
|
||||
|
||||
from langchain_experimental.plan_and_execute.executors.base import ChainExecutor
|
||||
|
||||
HUMAN_MESSAGE_TEMPLATE = """Previous steps: {previous_steps}
|
||||
|
||||
Current objective: {current_step}
|
||||
|
||||
{agent_scratchpad}"""
|
||||
|
||||
TASK_PREFIX = """{objective}
|
||||
|
||||
"""
|
||||
|
||||
|
||||
def load_agent_executor(
|
||||
llm: BaseLanguageModel,
|
||||
tools: List[BaseTool],
|
||||
verbose: bool = False,
|
||||
include_task_in_prompt: bool = False,
|
||||
) -> ChainExecutor:
|
||||
"""
|
||||
Load an agent executor.
|
||||
|
||||
Args:
|
||||
llm: BaseLanguageModel
|
||||
tools: List[BaseTool]
|
||||
verbose: bool. Defaults to False.
|
||||
include_task_in_prompt: bool. Defaults to False.
|
||||
|
||||
Returns:
|
||||
ChainExecutor
|
||||
"""
|
||||
input_variables = ["previous_steps", "current_step", "agent_scratchpad"]
|
||||
template = HUMAN_MESSAGE_TEMPLATE
|
||||
|
||||
if include_task_in_prompt:
|
||||
input_variables.append("objective")
|
||||
template = TASK_PREFIX + template
|
||||
|
||||
agent = StructuredChatAgent.from_llm_and_tools(
|
||||
llm,
|
||||
tools,
|
||||
human_message_template=template,
|
||||
input_variables=input_variables,
|
||||
)
|
||||
agent_executor = AgentExecutor.from_agent_and_tools(
|
||||
agent=agent, tools=tools, verbose=verbose
|
||||
)
|
||||
return ChainExecutor(chain=agent_executor)
|
@@ -0,0 +1,40 @@
|
||||
from abc import abstractmethod
|
||||
from typing import Any
|
||||
|
||||
from langchain.callbacks.manager import Callbacks
|
||||
from langchain.chains.base import Chain
|
||||
from pydantic import BaseModel
|
||||
|
||||
from langchain_experimental.plan_and_execute.schema import StepResponse
|
||||
|
||||
|
||||
class BaseExecutor(BaseModel):
|
||||
@abstractmethod
|
||||
def step(
|
||||
self, inputs: dict, callbacks: Callbacks = None, **kwargs: Any
|
||||
) -> StepResponse:
|
||||
"""Take step."""
|
||||
|
||||
@abstractmethod
|
||||
async def astep(
|
||||
self, inputs: dict, callbacks: Callbacks = None, **kwargs: Any
|
||||
) -> StepResponse:
|
||||
"""Take step."""
|
||||
|
||||
|
||||
class ChainExecutor(BaseExecutor):
|
||||
chain: Chain
|
||||
|
||||
def step(
|
||||
self, inputs: dict, callbacks: Callbacks = None, **kwargs: Any
|
||||
) -> StepResponse:
|
||||
"""Take step."""
|
||||
response = self.chain.run(**inputs, callbacks=callbacks)
|
||||
return StepResponse(response=response)
|
||||
|
||||
async def astep(
|
||||
self, inputs: dict, callbacks: Callbacks = None, **kwargs: Any
|
||||
) -> StepResponse:
|
||||
"""Take step."""
|
||||
response = await self.chain.arun(**inputs, callbacks=callbacks)
|
||||
return StepResponse(response=response)
|
Reference in New Issue
Block a user