Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/completions/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion src/completions/services/completion_service.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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",
)
Loading