langchain/libs/experimental/langchain_experimental/plan_and_execute/schema.py

64 lines
1.4 KiB
Python
Raw Normal View History

2023-05-10 04:07:56 +00:00
from abc import abstractmethod
from typing import List, Tuple
from langchain_core.output_parsers import BaseOutputParser
from langchain_experimental.pydantic_v1 import BaseModel, Field
2023-05-10 04:07:56 +00:00
class Step(BaseModel):
"""Step."""
2023-05-10 04:07:56 +00:00
value: str
"""The value."""
2023-05-10 04:07:56 +00:00
class Plan(BaseModel):
"""Plan."""
2023-05-10 04:07:56 +00:00
steps: List[Step]
"""The steps."""
2023-05-10 04:07:56 +00:00
class StepResponse(BaseModel):
"""Step response."""
2023-05-10 04:07:56 +00:00
response: str
"""The response."""
2023-05-10 04:07:56 +00:00
class BaseStepContainer(BaseModel):
"""Base step container."""
2023-05-10 04:07:56 +00:00
@abstractmethod
def add_step(self, step: Step, step_response: StepResponse) -> None:
"""Add step and step response to the container."""
@abstractmethod
def get_final_response(self) -> str:
"""Return the final response based on steps taken."""
class ListStepContainer(BaseStepContainer):
"""Container for List of steps."""
2023-05-10 04:07:56 +00:00
steps: List[Tuple[Step, StepResponse]] = Field(default_factory=list)
"""The steps."""
2023-05-10 04:07:56 +00:00
def add_step(self, step: Step, step_response: StepResponse) -> None:
self.steps.append((step, step_response))
def get_steps(self) -> List[Tuple[Step, StepResponse]]:
return self.steps
def get_final_response(self) -> str:
return self.steps[-1][1].response
class PlanOutputParser(BaseOutputParser):
"""Plan output parser."""
2023-05-10 04:07:56 +00:00
@abstractmethod
def parse(self, text: str) -> Plan:
"""Parse into a plan."""