Add PromptComponent to langflow prompts

This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-02-07 20:28:29 -03:00
commit cc5e491d8a
2 changed files with 28 additions and 0 deletions

View file

@ -0,0 +1,28 @@
from langchain_core.prompts import PromptTemplate
from langflow import CustomComponent
from langflow.field_typing import Prompt, TemplateField, Text
class PromptComponent(CustomComponent):
display_name: str = "Prompt"
description: str = "A component for creating prompts using templates"
beta = True
def build_config(self):
return {
"template": TemplateField(display_name="Template"),
"code": TemplateField(advanced=True),
}
def build(
self,
template: Prompt,
**kwargs,
) -> Text:
prompt_template = PromptTemplate.from_template(template)
try:
formated_prompt = prompt_template.format(**kwargs)
except Exception as exc:
raise ValueError(f"Error formatting prompt: {exc}") from exc
self.status = f'Prompt: "{formated_prompt}"'
return formated_prompt