mirror of
https://github.com/langgenius/dify.git
synced 2024-11-16 19:59:50 +08:00
42a5b3ec17
Co-authored-by: takatost <takatost@gmail.com>
25 lines
996 B
Python
25 lines
996 B
Python
from langchain.schema import BaseMessage, SystemMessage, AIMessage, HumanMessage
|
|
|
|
from core.prompt.prompt_template import PromptTemplateParser
|
|
|
|
|
|
class PromptBuilder:
|
|
@classmethod
|
|
def parse_prompt(cls, prompt: str, inputs: dict) -> str:
|
|
prompt_template = PromptTemplateParser(prompt)
|
|
prompt_inputs = {k: inputs[k] for k in prompt_template.variable_keys if k in inputs}
|
|
prompt = prompt_template.format(prompt_inputs)
|
|
return prompt
|
|
|
|
@classmethod
|
|
def to_system_message(cls, prompt_content: str, inputs: dict) -> BaseMessage:
|
|
return SystemMessage(content=cls.parse_prompt(prompt_content, inputs))
|
|
|
|
@classmethod
|
|
def to_ai_message(cls, prompt_content: str, inputs: dict) -> BaseMessage:
|
|
return AIMessage(content=cls.parse_prompt(prompt_content, inputs))
|
|
|
|
@classmethod
|
|
def to_human_message(cls, prompt_content: str, inputs: dict) -> BaseMessage:
|
|
return HumanMessage(content=cls.parse_prompt(prompt_content, inputs))
|