feat: implement node templates

This commit is contained in:
Gabriel Almeida 2023-03-26 10:03:08 -03:00
commit d93ecee876
4 changed files with 92 additions and 38 deletions

View file

@ -1,42 +1,6 @@
from langchain.agents.mrkl import prompt
from langflow.node.nodes import ZeroShotPromptNode
def get_custom_prompts():
"""Get custom prompts."""
return {
"ZeroShotPrompt": {
"template": {
"_type": "zero_shot",
"prefix": {
"type": "str",
"required": False,
"placeholder": "",
"list": False,
"show": True,
"multiline": True,
"value": prompt.PREFIX,
},
"suffix": {
"type": "str",
"required": True,
"placeholder": "",
"list": False,
"show": True,
"multiline": True,
"value": prompt.SUFFIX,
},
"format_instructions": {
"type": "str",
"required": False,
"placeholder": "",
"list": False,
"show": True,
"multiline": True,
"value": prompt.FORMAT_INSTRUCTIONS,
},
},
"description": "Prompt template for Zero Shot Agent.",
"base_classes": ["BasePromptTemplate"],
}
}
return ZeroShotPromptNode().to_dict()

View file

View file

@ -0,0 +1,46 @@
from langflow.node.template import Field, FrontendNode, Template
from langchain.agents.mrkl import prompt
class ZeroShotPromptNode(FrontendNode):
_name = "ZeroShotPrompt"
template = Template(
type_name="zero_shot",
fields=[
Field(
field_type="str",
required=False,
placeholder="",
is_list=False,
show=True,
multiline=True,
value=prompt.PREFIX,
name="prefix",
),
Field(
field_type="str",
required=True,
placeholder="",
is_list=False,
show=True,
multiline=True,
value=prompt.SUFFIX,
name="suffix",
),
Field(
field_type="str",
required=False,
placeholder="",
is_list=False,
show=True,
multiline=True,
value=prompt.FORMAT_INSTRUCTIONS,
name="format_instructions",
),
],
)
description = "Prompt template for Zero Shot Agent."
base_classes = ["BasePromptTemplate"]
def to_dict(self):
return super().to_dict()

View file

@ -0,0 +1,44 @@
from typing import Any
from pydantic import BaseModel
class Field(BaseModel):
field_type: str = "str"
required: bool = False
placeholder: str = ""
is_list: bool = False
show: bool = True
multiline: bool = False
value: Any = None
# _name will be used to store the name of the field
# in the template
name: str = None
def to_dict(self):
return self.dict()
class Template(BaseModel):
type_name: str
fields: list[Field]
def to_dict(self):
result = {field.name: field.to_dict() for field in self.fields}
result["_type"] = self.type_name
return result
class FrontendNode(BaseModel):
template: Template
description: str
base_classes: list
_name: str = None
def to_dict(self):
return {
self._name: {
"template": self.template.to_dict(),
"description": self.description,
"base_classes": self.base_classes,
}
}