forked from Darwin-lfl/langmanus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.py
More file actions
23 lines (18 loc) · 891 Bytes
/
template.py
File metadata and controls
23 lines (18 loc) · 891 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import os
import re
from datetime import datetime
from langchain_core.prompts import PromptTemplate
from langgraph.prebuilt.chat_agent_executor import AgentState
def get_prompt_template(prompt_name: str) -> str:
template = open(os.path.join(os.path.dirname(__file__), f"{prompt_name}.md")).read()
# Escape curly braces using backslash
template = template.replace("{", "{{").replace("}", "}}")
# Replace `<<VAR>>` with `{VAR}`
template = re.sub(r"<<([^>>]+)>>", r"{\1}", template)
return template
def apply_prompt_template(prompt_name: str, state: AgentState) -> list:
system_prompt = PromptTemplate(
input_variables=["CURRENT_TIME"],
template=get_prompt_template(prompt_name),
).format(CURRENT_TIME=datetime.now().strftime("%a %b %d %Y %H:%M:%S %z"), **state)
return [{"role": "system", "content": system_prompt}] + state["messages"]