Skip to content

Commit d304352

Browse files
authored
refactor: (breaking change) using RichPromptTemplate instead of get_prompt_by_jinja2_template function (#680)
part of #664 - llamaindex already supports rendering prompt templates via the jinja2 rendering engine through `RichPromptTemplate`, so we don't need to `get_prompt_by_jinja2_template`. - We no longer need placeholders like <<query_str>> to avoid escaping, and can define prompt template variable placeholders uniformly using the {{xxx}} syntax. (breaking change)
1 parent 9db9ff6 commit d304352

File tree

5 files changed

+54
-121
lines changed

5 files changed

+54
-121
lines changed

backend/app/rag/chat/chat_flow.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from llama_index.core import get_response_synthesizer
1212
from llama_index.core.base.llms.types import ChatMessage, MessageRole
1313
from llama_index.core.schema import NodeWithScore
14+
from llama_index.core.prompts.rich import RichPromptTemplate
15+
1416
from sqlmodel import Session
1517
from app.core.config import settings
1618
from app.exceptions import ChatNotFound
@@ -33,7 +35,6 @@
3335
from app.rag.utils import parse_goal_response_format
3436
from app.repositories import chat_repo
3537
from app.site_settings import SiteSetting
36-
from app.utils.jinja2 import get_prompt_by_jinja2_template
3738
from app.utils.tracing import LangfuseContextManager
3839

3940
logger = logging.getLogger(__name__)
@@ -355,14 +356,13 @@ def _refine_user_question(
355356
),
356357
)
357358

359+
prompt_template = RichPromptTemplate(refined_question_prompt)
358360
refined_question = self._fast_llm.predict(
359-
get_prompt_by_jinja2_template(
360-
refined_question_prompt,
361-
graph_knowledges=knowledge_graph_context,
362-
chat_history=chat_history,
363-
question=user_question,
364-
current_date=datetime.now().strftime("%Y-%m-%d"),
365-
),
361+
prompt_template,
362+
graph_knowledges=knowledge_graph_context,
363+
chat_history=chat_history,
364+
question=user_question,
365+
current_date=datetime.now().strftime("%Y-%m-%d"),
366366
)
367367

368368
if not annotation_silent:
@@ -403,19 +403,18 @@ def _clarify_question(
403403
"knowledge_graph_context": knowledge_graph_context,
404404
},
405405
) as span:
406-
clarity_result = (
407-
self._fast_llm.predict(
408-
prompt=get_prompt_by_jinja2_template(
409-
self.engine_config.llm.clarifying_question_prompt,
410-
graph_knowledges=knowledge_graph_context,
411-
chat_history=chat_history,
412-
question=user_question,
413-
),
414-
)
415-
.strip()
416-
.strip(".\"'!")
406+
prompt_template = RichPromptTemplate(
407+
self.engine_config.llm.clarifying_question_prompt
417408
)
418409

410+
prediction = self._fast_llm.predict(
411+
prompt_template,
412+
graph_knowledges=knowledge_graph_context,
413+
chat_history=chat_history,
414+
question=user_question,
415+
)
416+
# TODO: using structured output to get the clarity result.
417+
clarity_result = prediction.strip().strip(".\"'!")
419418
need_clarify = clarity_result.lower() != "false"
420419
need_clarify_response = clarity_result if need_clarify else ""
421420

@@ -468,8 +467,10 @@ def _generate_answer(
468467
name="generate_answer", input=user_question
469468
) as span:
470469
# Initialize response synthesizer.
471-
text_qa_template = get_prompt_by_jinja2_template(
472-
self.engine_config.llm.text_qa_prompt,
470+
text_qa_template = RichPromptTemplate(
471+
template_str=self.engine_config.llm.text_qa_prompt
472+
)
473+
text_qa_template = text_qa_template.partial_format(
473474
current_date=datetime.now().strftime("%Y-%m-%d"),
474475
graph_knowledges=knowledge_graph_context,
475476
original_question=self.user_question,

backend/app/rag/chat/chat_service.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
from app.repositories.embedding_model import embedding_model_repo
4646
from app.repositories.llm import llm_repo
4747
from app.site_settings import SiteSetting
48-
from app.utils.jinja2 import get_prompt_by_jinja2_template
48+
from llama_index.core.prompts.rich import RichPromptTemplate
4949

5050
logger = logging.getLogger(__name__)
5151

@@ -283,11 +283,12 @@ def get_chat_message_recommend_questions(
283283
if questions is not None:
284284
return questions
285285

286+
prompt_template = RichPromptTemplate(
287+
chat_engine_config.llm.further_questions_prompt
288+
)
286289
recommend_questions = llm.predict(
287-
prompt=get_prompt_by_jinja2_template(
288-
chat_engine_config.llm.further_questions_prompt,
289-
chat_message_content=chat_message.content,
290-
),
290+
prompt_template,
291+
chat_message_content=chat_message.content,
291292
)
292293
recommend_question_list = recommend_questions.splitlines()
293294
recommend_question_list = [
@@ -311,10 +312,8 @@ def get_chat_message_recommend_questions(
311312
"""
312313
# with format or too long for per question, it's not a question list, generate again
313314
recommend_questions = llm.predict(
314-
prompt=get_prompt_by_jinja2_template(
315-
chat_engine_config.llm.further_questions_prompt,
316-
chat_message_content=regenerate_content,
317-
),
315+
prompt_template,
316+
chat_message_content=regenerate_content,
318317
)
319318

320319
db_session.add(

backend/app/rag/chat/retrieve/retrieve_flow.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
from llama_index.core.instrumentation import get_dispatcher
66
from llama_index.core.llms import LLM
77
from llama_index.core.schema import NodeWithScore, QueryBundle
8+
from llama_index.core.prompts.rich import RichPromptTemplate
89
from pydantic import BaseModel
910
from sqlmodel import Session
1011

1112
from app.models import (
1213
Document as DBDocument,
1314
KnowledgeBase,
1415
)
15-
from app.utils.jinja2 import get_prompt_by_jinja2_template
1616
from app.rag.chat.config import ChatEngineConfig
1717
from app.rag.retrievers.knowledge_graph.fusion_retriever import (
1818
KnowledgeGraphFusionRetriever,
@@ -102,31 +102,34 @@ def _get_knowledge_graph_context(
102102
self, knowledge_graph: KnowledgeGraphRetrievalResult
103103
) -> str:
104104
if self.engine_config.knowledge_graph.using_intent_search:
105-
kg_context_template = get_prompt_by_jinja2_template(
106-
self.engine_config.llm.intent_graph_knowledge,
107-
# For forward compatibility considerations.
105+
kg_context_template = RichPromptTemplate(
106+
self.engine_config.llm.intent_graph_knowledge
107+
)
108+
return kg_context_template.format(
108109
sub_queries=knowledge_graph.to_subqueries_dict(),
109110
)
110-
return kg_context_template.template
111111
else:
112-
kg_context_template = get_prompt_by_jinja2_template(
113-
self.engine_config.llm.normal_graph_knowledge,
112+
kg_context_template = RichPromptTemplate(
113+
self.engine_config.llm.normal_graph_knowledge
114+
)
115+
return kg_context_template.format(
114116
entities=knowledge_graph.entities,
115117
relationships=knowledge_graph.relationships,
116118
)
117-
return kg_context_template.template
118119

119120
def _refine_user_question(
120121
self, user_question: str, knowledge_graph_context: str
121122
) -> str:
122-
return self._fast_llm.predict(
123-
get_prompt_by_jinja2_template(
124-
self.engine_config.llm.condense_question_prompt,
125-
graph_knowledges=knowledge_graph_context,
126-
question=user_question,
127-
current_date=datetime.now().strftime("%Y-%m-%d"),
128-
),
123+
prompt_template = RichPromptTemplate(
124+
self.engine_config.llm.condense_question_prompt
125+
)
126+
refined_question = self._fast_llm.predict(
127+
prompt_template,
128+
graph_knowledges=knowledge_graph_context,
129+
question=user_question,
130+
current_date=datetime.now().strftime("%Y-%m-%d"),
129131
)
132+
return refined_question.strip().strip(".\"'!")
130133

131134
def search_relevant_chunks(self, user_question: str) -> List[NodeWithScore]:
132135
retriever = ChunkFusionRetriever(

backend/app/rag/default_prompt.py

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,15 @@
88
Sub-query: {{ sub_query }}
99
1010
- Entities:
11-
1211
{% for entity in data['entities'] %}
13-
1412
- Name: {{ entity.name }}
15-
- Description: {{ entity.description }}
16-
13+
Description: {{ entity.description }}
1714
{% endfor %}
1815
1916
- Relationships:
20-
2117
{% for relationship in data['relationships'] %}
22-
2318
- Description: {{ relationship.rag_description }}
24-
- Weight: {{ relationship.weight }}
25-
19+
Weight: {{ relationship.weight }}
2620
{% endfor %}
2721
2822
{% endfor %}
@@ -35,10 +29,8 @@
3529
Entities:
3630
3731
{% for entity in entities %}
38-
3932
- Name: {{ entity.name }}
40-
- Description: {{ entity.description }}
41-
33+
Description: {{ entity.description }}
4234
{% endfor %}
4335
4436
---------------------
@@ -192,7 +184,6 @@
192184
Refined standalone question:
193185
"""
194186

195-
196187
DEFAULT_TEXT_QA_PROMPT = """\
197188
Current Date: {{current_date}}
198189
---------------------
@@ -205,7 +196,7 @@
205196
Context information is below.
206197
---------------------
207198
208-
<<context_str>>
199+
{{context_str}}
209200
210201
---------------------
211202
@@ -242,45 +233,10 @@
242233
{{original_question}}
243234
244235
The Refined Question used to search:
245-
<<query_str>>
246-
247-
Answer:
248-
"""
249-
250-
DEFAULT_REFINE_PROMPT = """\
251-
The Original questions is:
252-
253-
{{original_question}}
254-
255-
Refined Question used to search:
256-
<<query_str>>
257-
258-
---------------------
259-
We have provided an existing answer:
260-
---------------------
261-
262-
<<existing_answer>>
263-
264-
---------------------
265-
We have the opportunity to refine the existing answer (only if needed) with some more knowledge graph and context information below.
266-
267-
---------------------
268-
Knowledge graph information is below
269-
---------------------
270236
271-
{{graph_knowledges}}
272-
273-
---------------------
274-
Context information is below.
275-
---------------------
237+
{{query_str}}
276238
277-
<<context_msg>>
278-
279-
---------------------
280-
Given the new context, refine the original answer to better answer the query. If the context isn't useful, return the original answer.
281-
And the answer should use the same language with the question. If the answer has different language with the original question, please translate it to the same language with the question.
282-
283-
Refined Answer:
239+
Answer:
284240
"""
285241

286242
DEFAULT_FURTHER_QUESTIONS_PROMPT = """\

backend/app/utils/jinja2.py

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)