update experimental (#8402)

some changes were made to experimental, porting them over
This commit is contained in:
Harrison Chase
2023-07-28 13:01:36 -07:00
committed by GitHub
parent af7e70d4af
commit 3a78450883
13 changed files with 58 additions and 22 deletions

View File

@@ -16,9 +16,14 @@ from langchain_experimental.plan_and_execute.schema import (
class PlanAndExecute(Chain):
"""Plan and execute a chain of steps."""
planner: BasePlanner
"""The planner to use."""
executor: BaseExecutor
"""The executor to use."""
step_container: BaseStepContainer = Field(default_factory=ListStepContainer)
"""The step container to use."""
input_key: str = "input"
output_key: str = "output"

View File

@@ -9,6 +9,8 @@ from langchain_experimental.plan_and_execute.schema import StepResponse
class BaseExecutor(BaseModel):
"""Base executor."""
@abstractmethod
def step(
self, inputs: dict, callbacks: Callbacks = None, **kwargs: Any
@@ -19,11 +21,14 @@ class BaseExecutor(BaseModel):
async def astep(
self, inputs: dict, callbacks: Callbacks = None, **kwargs: Any
) -> StepResponse:
"""Take step."""
"""Take async step."""
class ChainExecutor(BaseExecutor):
"""Chain executor."""
chain: Chain
"""The chain to use."""
def step(
self, inputs: dict, callbacks: Callbacks = None, **kwargs: Any

View File

@@ -9,6 +9,8 @@ from langchain_experimental.plan_and_execute.schema import Plan, PlanOutputParse
class BasePlanner(BaseModel):
"""Base planner."""
@abstractmethod
def plan(self, inputs: dict, callbacks: Callbacks = None, **kwargs: Any) -> Plan:
"""Given input, decide what to do."""
@@ -17,13 +19,18 @@ class BasePlanner(BaseModel):
async def aplan(
self, inputs: dict, callbacks: Callbacks = None, **kwargs: Any
) -> Plan:
"""Given input, decide what to do."""
"""Given input, asynchronously decide what to do."""
class LLMPlanner(BasePlanner):
"""LLM planner."""
llm_chain: LLMChain
"""The LLM chain to use."""
output_parser: PlanOutputParser
"""The output parser to use."""
stop: Optional[List] = None
"""The stop list to use."""
def plan(self, inputs: dict, callbacks: Callbacks = None, **kwargs: Any) -> Plan:
"""Given input, decide what to do."""
@@ -33,7 +40,7 @@ class LLMPlanner(BasePlanner):
async def aplan(
self, inputs: dict, callbacks: Callbacks = None, **kwargs: Any
) -> Plan:
"""Given input, decide what to do."""
"""Given input, asynchronously decide what to do."""
llm_response = await self.llm_chain.arun(
**inputs, stop=self.stop, callbacks=callbacks
)

View File

@@ -25,6 +25,8 @@ SYSTEM_PROMPT = (
class PlanningOutputParser(PlanOutputParser):
"""Planning output parser."""
def parse(self, text: str) -> Plan:
steps = [Step(value=v) for v in re.split("\n\s*\d+\. ", text)[1:]]
return Plan(steps=steps)
@@ -35,6 +37,7 @@ def load_chat_planner(
) -> LLMPlanner:
"""
Load a chat planner.
Args:
llm: Language model.
system_prompt: System prompt.

View File

@@ -6,18 +6,29 @@ from pydantic import BaseModel, Field
class Step(BaseModel):
"""Step."""
value: str
"""The value."""
class Plan(BaseModel):
"""Plan."""
steps: List[Step]
"""The steps."""
class StepResponse(BaseModel):
"""Step response."""
response: str
"""The response."""
class BaseStepContainer(BaseModel):
"""Base step container."""
@abstractmethod
def add_step(self, step: Step, step_response: StepResponse) -> None:
"""Add step and step response to the container."""
@@ -28,7 +39,10 @@ class BaseStepContainer(BaseModel):
class ListStepContainer(BaseStepContainer):
"""List step container."""
steps: List[Tuple[Step, StepResponse]] = Field(default_factory=list)
"""The steps."""
def add_step(self, step: Step, step_response: StepResponse) -> None:
self.steps.append((step, step_response))
@@ -41,6 +55,8 @@ class ListStepContainer(BaseStepContainer):
class PlanOutputParser(BaseOutputParser):
"""Plan output parser."""
@abstractmethod
def parse(self, text: str) -> Plan:
"""Parse into a plan."""