Add PromptComponent to langflow prompts
This commit is contained in:
parent
1f5f5d7006
commit
cc5e491d8a
2 changed files with 28 additions and 0 deletions
28
src/backend/langflow/components/prompts/Prompt.py
Normal file
28
src/backend/langflow/components/prompts/Prompt.py
Normal 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
|
||||||
0
src/backend/langflow/components/prompts/__init_.py
Normal file
0
src/backend/langflow/components/prompts/__init_.py
Normal file
Loading…
Add table
Add a link
Reference in a new issue