commit af0dcdc16c0e77e3c7ebe922386c1a9f4b399b72 from: Matthias L. Jugel date: Sun Jul 13 17:10:48 2025 UTC make sure to combine duplicate references, display them nicer commit - 7c634733a6f58eae9d9cbc7242fb529f23c19fe1 commit + af0dcdc16c0e77e3c7ebe922386c1a9f4b399b72 blob - 7eb4af358647391393cd2635a9178d34a6637c94 blob + 83352fe5e971b4612dd21262cc36f42e4effea83 --- configuration.py +++ configuration.py @@ -20,14 +20,14 @@ You are an expert assistant that provides well-sourced Use Markdown for all formatting (e.g., bold, italics, code blocks, lists, links). # CITATION REQUIREMENTS: -* After **EVERY** statement, add `[n]` where n is the source number is was derived from +* After **EVERY** statement, add `_[n]_` where n is the source number is was derived from * Source numbers correspond to the numbered sources below -* For answers supported by multiple sources, use `[1][2][3]` format +* For answers supported by multiple sources, use `_[1][2][3]_` format * Never cite sources that don't exist in the provided context * If information spans multiple sentences, cite after each relevant sentence ## EXAMPLE OUTPUT FORMAT: -The renewable energy sector has grown significantly [1]. Solar power installations increased by 20% last year [2]. Wind energy also showed strong growth [1][3]. +The renewable energy sector has grown significantly _[1]_. Solar power installations increased by 20% last year [2]. Wind energy also showed strong growth _[1][3]_. """ HUMAN_TEMPLATE: str = r""" blob - 46ac5804929253742081a57fe74fa51e3e116225 blob + ef43eb0adc92bafd8fe4bdd000effa4dfc3e5f92 --- rag_backend.py +++ rag_backend.py @@ -34,12 +34,24 @@ class RagBackend: for i, (doc, _score) in enumerate(context_docs) ]) - # prepare references for reply - references = [ - f"{':'.join(doc.metadata.get('id').split(':')[:2])}:" - for (doc, _score) in context_docs - ] + # Create a map of document IDs to the indices where they occur + ref_map = {} + for i, (doc, _score) in enumerate(context_docs): + doc_id = ':'.join(doc.metadata.get('id').split(':')[:2]) + if doc_id in ref_map: + ref_map[doc_id].append(i + 1) + else: + ref_map[doc_id] = [i + 1] + # Convert the map to a list of formatted strings + references = [] + for doc_id, indices in ref_map.items(): + # Format as "doc_id" with a mapping of indices + references.append({ + 'id': doc_id, + 'indices': indices + }) + prompt_template = ChatPromptTemplate.from_messages([ SystemMessage(content=SYSTEM_PROMPT), HumanMessagePromptTemplate.from_template(HUMAN_TEMPLATE)]) blob - 9442611fc77a5381cf224932c347a239d6f3fbf8 blob + aa2e95bb6b0d06817162bdf479a330818c8ec0a4 --- templates/page.html +++ templates/page.html @@ -105,33 +105,40 @@ } .references-list { - list-style-type: none; padding-left: 0; margin: 0; - counter-reset: refs; + list-style-type: none; } - .references-list li { + .reference-item { + display: flex; + align-items: center; margin-bottom: 8px; font-size: 0.85em; - counter-increment: refs; - margin-left: 5px; + white-space: nowrap; + overflow: hidden; } - .references-list li::before { - content: "[" counter(refs) "] "; + .ref-indices { font-weight: bold; + margin-right: 8px; + flex-shrink: 0; } + .ref-link-container { + overflow: hidden; + text-overflow: ellipsis; + flex-grow: 1; + } + .references-list a { color: #4a90e2; text-decoration: none; - display: inline-block; - max-width: calc(100% - 30px); + display: block; + width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; - vertical-align: bottom; position: relative; } @@ -310,18 +317,35 @@ refsBox.appendChild(refsTitle); // Create the reference list - const refsList = document.createElement('ol'); + const refsList = document.createElement('div'); refsList.className = 'references-list'; - // Add each reference as a list item with link - references.forEach((ref, index) => { - const refItem = document.createElement('li'); + // Add each reference as a div with indices and link + references.forEach((ref) => { + // Create a container for this reference item + const refItem = document.createElement('div'); + refItem.className = 'reference-item'; + // Extract the document ID and indices + // Format expected: object with 'id' and 'indices' properties + const docId = ref.id; + const indices = ref.indices; + // Parse the reference ID to get filename and page number - const idParts = ref.split(':'); + const idParts = docId.split(':'); const filename = idParts[0]; const pageNumber = idParts[1] || '1'; + // Create the indices prefix + const indicesSpan = document.createElement('span'); + indicesSpan.className = 'ref-indices'; + indicesSpan.textContent = indices.map(idx => `[${idx}]`).join(' '); + refItem.appendChild(indicesSpan); + + // Create a container for the link to maintain overflow control + const linkContainer = document.createElement('div'); + linkContainer.className = 'ref-link-container'; + // Create the link to the document const refLink = document.createElement('a'); refLink.href = `/static/files/${filename}?page=${pageNumber}#page=${pageNumber}`; @@ -330,7 +354,8 @@ refLink.title = `${filename} (page ${pageNumber})`; refLink.target = '_blank'; - refItem.appendChild(refLink); + linkContainer.appendChild(refLink); + refItem.appendChild(linkContainer); refsList.appendChild(refItem); });