Skip to content

Commit 2240ada

Browse files
committed
Adds proper history handling for prompting
1 parent a6e8ebd commit 2240ada

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

‎docs/quick-start.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ See [our documentation](https://dialog.talkd.ai/settings#csv-knowledge-base) for
9898

9999
#### `.toml` prompt configuration
100100

101-
The `[prompt.header]`, `[prompt.suggested]`, and `[fallback.prompt]` fields are mandatory fields used for processing the conversation and connecting to the LLM.
101+
The `[prompt.header]`, `[prompt.history_header]`, `[prompt.suggested]`, and `[fallback.prompt]` fields are mandatory fields used for processing the conversation and connecting to the LLM.
102102

103103
The `[prompt.fallback]` field is used when the LLM does not find a compatible embedding in the database; that is, the `[prompt.header]` **is ignored** and the `[prompt.fallback]` is used. Without it, there could be hallucinations about possible answers to questions outside the scope of the embeddings.
104104

@@ -117,6 +117,8 @@ qualified service to high-end customers. Be brief in your answers, without being
117117
and objective in your responses. Never say that you are a model (AI), always answer as Avelino.
118118
Be polite and friendly!"""
119119
120+
history_header = """This is the history of the conversation with the user:"""
121+
120122
suggested = "Here is some possible content
121123
that could help the user in a better way."
122124

‎sample_data/prompt.toml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ suggested = """
1616
Here are some contents that you need to use in order to help you with the user's question:
1717
"""
1818

19+
history_header = """
20+
This is the history of the conversation:
21+
"""
22+
1923
[fallback]
2024
prompt = """
2125
Answer the following message to the user.

‎src/dialog/llm/agents/default.py‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from dialog.settings import Settings
1818
from dialog.db import get_session
1919

20+
2021
class DialogLLM(AbstractRAG):
2122
def __init__(self, *args, **kwargs):
2223
kwargs["dbsession"] = next(get_session())
@@ -47,24 +48,35 @@ def generate_prompt(self, text):
4748
self.config.get("fallback").get("prompt") # maintaining compatibility with the previous configuration
4849
header = prompt_config.get("header")
4950
suggested = prompt_config.get("suggested")
51+
history_header = prompt_config.get("history_header")
5052
messages = []
53+
54+
messages.append(SystemMessagePromptTemplate.from_template(header))
5155
if len(self.relevant_contents) > 0:
5256
context = "Context: \n".join(
5357
[f"{c.question}\n{c.content}\n" for c in self.relevant_contents]
5458
)
55-
messages.append(SystemMessagePromptTemplate.from_template(header))
56-
messages.append(SystemMessagePromptTemplate.from_template(
57-
f"{suggested}. {context}"))
5859
messages.append(
59-
MessagesPlaceholder(
60-
variable_name="chat_history", optional=True))
61-
messages.append(
62-
HumanMessagePromptTemplate.from_template("{user_message}"))
60+
SystemMessagePromptTemplate.from_template(f"{suggested}. {context}")
61+
)
6362
else:
6463
messages.append(
65-
SystemMessagePromptTemplate.from_template(fallback))
66-
messages.append(
67-
HumanMessagePromptTemplate.from_template("{user_message}"))
64+
SystemMessagePromptTemplate.from_template(fallback)
65+
)
66+
67+
messages.append(
68+
SystemMessagePromptTemplate.from_template(history_header)
69+
)
70+
71+
messages.append(
72+
MessagesPlaceholder(
73+
variable_name="chat_history"
74+
)
75+
)
76+
77+
messages.append(
78+
HumanMessagePromptTemplate.from_template("{user_message}")
79+
)
6880
self.prompt = ChatPromptTemplate.from_messages(messages)
6981

7082
if Settings().VERBOSE_LLM:

0 commit comments

Comments
 (0)