diff --git a/src/completions/prompts.py b/src/completions/prompts.py index 8e24760..e1489de 100644 --- a/src/completions/prompts.py +++ b/src/completions/prompts.py @@ -4,9 +4,9 @@ 1. Answer using ONLY the information in the provided sources below. Do not use any other knowledge. 2. If the sources do not contain enough information to answer, say so directly. Do not guess or make up answers. 3. Be concise and direct. Students want clear answers, not long essays. -4. At the end of your answer, list the source URLs you used under a "Sources:" header. +4. Refrain from adding a "sources" section or listing source URLs in your response, they are provided to the user automatically. -Sources will be provided in this format: +Sources will be provided to you in this format, they are not intended for the user: --- [Source: https://example.com/page-1] content of chunk 1 diff --git a/src/completions/services/completion_service.py b/src/completions/services/completion_service.py index c269f38..0e4f729 100644 --- a/src/completions/services/completion_service.py +++ b/src/completions/services/completion_service.py @@ -1,3 +1,5 @@ +import re + from src.completions.prompts import build_messages from src.config.logger import get_logger from src.domain.types import Answer, RetrievedChunk, Source @@ -14,6 +16,13 @@ def _format_context(chunks: list[RetrievedChunk]) -> str: return "---\n" + "\n\n".join(parts) + "\n---" +def _strip_trailing_sources_block(text: str) -> str: + matches = list(re.finditer(r"^Sources:", text, flags=re.MULTILINE)) + if not matches: + return text + return text[: matches[-1].start()].rstrip() + + def _extract_sources(chunks: list[RetrievedChunk]) -> list[Source]: seen: dict[str, Source] = {} for rc in chunks: @@ -48,7 +57,7 @@ async def ask(question: str) -> Answer: log.info("ask_complete", n_sources=len(chunks)) return Answer( - text=response_text, + text=_strip_trailing_sources_block(response_text), sources=_extract_sources(chunks), confidence="high", )