🐛(agent) add the current date in the system prompt
We add the current date to the system prompt. Warning: the timezone is not the user's one...
This commit is contained in:
@@ -22,6 +22,7 @@ and this project adheres to
|
||||
- ✨(web-search) add RAG capability to do web search #7
|
||||
- ✨(chat) add document RAG on document uploaded by user #8
|
||||
- ✨(backend) allow use to stop conversation streaming #14
|
||||
- 🐛(agent) add the current date in the system prompt #18
|
||||
|
||||
|
||||
[unreleased]: https://github.com/numerique-gouv/conversations/compare/HEAD...main
|
||||
|
||||
@@ -17,6 +17,7 @@ from typing import Dict, List, Optional, Tuple
|
||||
from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.utils import formats, timezone
|
||||
from django.utils.module_loading import import_string
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
@@ -86,6 +87,17 @@ def _build_pydantic_agent(mcp_servers) -> Agent[None, str]:
|
||||
tools=[get_pydantic_tools_by_name(tool_name) for tool_name in settings.AI_AGENT_TOOLS],
|
||||
)
|
||||
|
||||
@agent.system_prompt
|
||||
def add_the_date() -> str:
|
||||
"""
|
||||
Dynamic system prompt function to add the current date.
|
||||
|
||||
Warning: this will always use the date in the server timezone,
|
||||
not the user's timezone...
|
||||
"""
|
||||
_formatted_date = formats.date_format(timezone.now(), "l d/m/Y", use_l10n=False)
|
||||
return f"Today is {_formatted_date}."
|
||||
|
||||
return agent
|
||||
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
|
||||
import pytest
|
||||
from freezegun import freeze_time
|
||||
from pydantic_ai import Agent
|
||||
from pydantic_ai.models.openai import OpenAIModel
|
||||
|
||||
@@ -72,3 +73,13 @@ def test_build_pydantic_agent_missing_model(settings):
|
||||
|
||||
with pytest.raises(ImproperlyConfigured, match="AIChatService configuration not set"):
|
||||
_build_pydantic_agent([])
|
||||
|
||||
|
||||
@freeze_time("2025-07-25T10:36:35.297675Z")
|
||||
def test_add_the_date_system_prompt():
|
||||
"""Ensure add_the_date system prompt is registered and returns the formatted date string."""
|
||||
agent = _build_pydantic_agent([])
|
||||
|
||||
assert len(agent._system_prompt_functions) == 1
|
||||
assert agent._system_prompt_functions[0].function.__name__ == "add_the_date"
|
||||
assert agent._system_prompt_functions[0].function() == "Today is Friday 25/07/2025."
|
||||
|
||||
@@ -174,6 +174,12 @@ def test_post_conversation_data_protocol(api_client, mock_openai_stream, mock_uu
|
||||
"part_kind": "system-prompt",
|
||||
"timestamp": "2025-07-25T10:36:35.297675Z",
|
||||
},
|
||||
{
|
||||
"content": "Today is Friday 25/07/2025.",
|
||||
"dynamic_ref": None,
|
||||
"part_kind": "system-prompt",
|
||||
"timestamp": "2025-07-25T10:36:35.297675Z",
|
||||
},
|
||||
{
|
||||
"content": ["Hello"],
|
||||
"part_kind": "user-prompt",
|
||||
@@ -281,6 +287,12 @@ def test_post_conversation_text_protocol(api_client, mock_openai_stream, mock_uu
|
||||
"part_kind": "system-prompt",
|
||||
"timestamp": "2025-07-25T10:36:35.297675Z",
|
||||
},
|
||||
{
|
||||
"content": "Today is Friday 25/07/2025.",
|
||||
"dynamic_ref": None,
|
||||
"part_kind": "system-prompt",
|
||||
"timestamp": "2025-07-25T10:36:35.297675Z",
|
||||
},
|
||||
{
|
||||
"content": ["Hello"],
|
||||
"part_kind": "user-prompt",
|
||||
@@ -365,6 +377,7 @@ def test_post_conversation_with_image(api_client, mock_openai_stream_image, mock
|
||||
"You can use Markdown to format your answers. ",
|
||||
"role": "system",
|
||||
},
|
||||
{"content": "Today is Friday 25/07/2025.", "role": "system"},
|
||||
{
|
||||
"content": [
|
||||
{"text": "Hello, what do you see on this picture?", "type": "text"},
|
||||
@@ -458,6 +471,12 @@ def test_post_conversation_with_image(api_client, mock_openai_stream_image, mock
|
||||
"part_kind": "system-prompt",
|
||||
"timestamp": "2025-07-25T10:36:35.297675Z",
|
||||
},
|
||||
{
|
||||
"content": "Today is Friday 25/07/2025.",
|
||||
"dynamic_ref": None,
|
||||
"part_kind": "system-prompt",
|
||||
"timestamp": "2025-07-25T10:36:35.297675Z",
|
||||
},
|
||||
{
|
||||
"content": [
|
||||
"Hello, what do you see on this picture?",
|
||||
@@ -549,6 +568,7 @@ def test_post_conversation_tool_call(api_client, mock_openai_stream_tool, mock_u
|
||||
"You can use Markdown to format your answers. ",
|
||||
"role": "system",
|
||||
},
|
||||
{"content": "Today is Friday 25/07/2025.", "role": "system"},
|
||||
{"content": [{"text": "Weather in Paris?", "type": "text"}], "role": "user"},
|
||||
]
|
||||
|
||||
@@ -615,6 +635,12 @@ def test_post_conversation_tool_call(api_client, mock_openai_stream_tool, mock_u
|
||||
"part_kind": "system-prompt",
|
||||
"timestamp": "2025-07-25T10:36:35.297675Z",
|
||||
},
|
||||
{
|
||||
"content": "Today is Friday 25/07/2025.",
|
||||
"dynamic_ref": None,
|
||||
"part_kind": "system-prompt",
|
||||
"timestamp": "2025-07-25T10:36:35.297675Z",
|
||||
},
|
||||
{
|
||||
"content": ["Weather in Paris?"],
|
||||
"part_kind": "user-prompt",
|
||||
@@ -732,6 +758,7 @@ def test_post_conversation_tool_call_fails(
|
||||
"You can use Markdown to format your answers. ",
|
||||
"role": "system",
|
||||
},
|
||||
{"content": "Today is Friday 25/07/2025.", "role": "system"},
|
||||
{"content": [{"text": "Weather in Paris?", "type": "text"}], "role": "user"},
|
||||
]
|
||||
|
||||
@@ -798,6 +825,12 @@ def test_post_conversation_tool_call_fails(
|
||||
"part_kind": "system-prompt",
|
||||
"timestamp": "2025-07-25T10:36:35.297675Z",
|
||||
},
|
||||
{
|
||||
"content": "Today is Friday 25/07/2025.",
|
||||
"dynamic_ref": None,
|
||||
"part_kind": "system-prompt",
|
||||
"timestamp": "2025-07-25T10:36:35.297675Z",
|
||||
},
|
||||
{
|
||||
"content": ["Weather in Paris?"],
|
||||
"part_kind": "user-prompt",
|
||||
|
||||
+6
@@ -416,6 +416,12 @@ def test_post_conversation_with_document_upload( # noqa:PLR0913 # pylint: disa
|
||||
"part_kind": "system-prompt",
|
||||
"timestamp": "2025-07-25T10:36:35.297675Z",
|
||||
},
|
||||
{
|
||||
"content": "Today is Friday 25/07/2025.",
|
||||
"dynamic_ref": None,
|
||||
"part_kind": "system-prompt",
|
||||
"timestamp": "2025-07-25T10:36:35.297675Z",
|
||||
},
|
||||
{
|
||||
"content": [
|
||||
"Based on the following document contents:\n"
|
||||
|
||||
+12
-4
@@ -368,9 +368,9 @@ def test_conversation_with_forced_web_search_no_history(
|
||||
]
|
||||
|
||||
_user_request_parts = chat_conversation.pydantic_messages[0].pop("parts")
|
||||
assert len(_user_request_parts) == 2
|
||||
assert len(_user_request_parts) == 3
|
||||
|
||||
assert _user_request_parts[0] == {
|
||||
assert _user_request_parts.pop(0) == {
|
||||
"content": "You are a helpful assistant. Escape formulas or any "
|
||||
"math notation between `$$`, like `$$x^2 + y^2 = "
|
||||
"z^2$$` or `$$C_l$$`. You can use Markdown to format "
|
||||
@@ -380,7 +380,15 @@ def test_conversation_with_forced_web_search_no_history(
|
||||
"timestamp": "2025-07-25T10:36:35.297675Z",
|
||||
}
|
||||
|
||||
_user_request_parts_1_content = _user_request_parts[1].pop("content")
|
||||
assert _user_request_parts.pop(0) == {
|
||||
"content": "Today is Friday 25/07/2025.",
|
||||
"dynamic_ref": None,
|
||||
"part_kind": "system-prompt",
|
||||
"timestamp": "2025-07-25T10:36:35.297675Z",
|
||||
}
|
||||
|
||||
_last_user_request_part = _user_request_parts.pop(0)
|
||||
_user_request_parts_1_content = _last_user_request_part.pop("content")
|
||||
assert len(_user_request_parts_1_content) == 1
|
||||
# check the web result are properly prompted
|
||||
assert "Based on the following web search results:\n" in _user_request_parts_1_content[0]
|
||||
@@ -391,7 +399,7 @@ def test_conversation_with_forced_web_search_no_history(
|
||||
# check the web search results are included
|
||||
assert "le JWST a aidé à caractériser plusieurs" in _user_request_parts_1_content[0]
|
||||
|
||||
assert _user_request_parts[1] == {
|
||||
assert _last_user_request_part == {
|
||||
"part_kind": "user-prompt",
|
||||
"timestamp": "2025-07-25T10:36:35.297675Z",
|
||||
# content as been tested above
|
||||
|
||||
Reference in New Issue
Block a user