commit 88c51000b72b7e9e8ecf4c7426f65f8a4d960661 from: Matthias L. Jugel date: Sun Jul 13 10:28:37 2025 UTC improved citation. commit - 0762a220875890223346c1e2d17c867524ec8680 commit + 88c51000b72b7e9e8ecf4c7426f65f8a4d960661 blob - 27a1fb414f93866c7a12fe643e52e7efe1a0d287 blob + 85ba3ff8203f1ef16d9d030929fce46e8d93aff4 --- configuration.py +++ configuration.py @@ -13,42 +13,46 @@ OLLAMA_EMBEDDINGS_MODEL = os.getenv('RAG_OLLAMA_EMBEDD # prompt templates SYSTEM_PROMPT: str = r""" -* Do not include introductory or closing remarks. * You are acting as a sparing partner for a roleplaying game master. -* Answer the question using only the contents of the CONTEXT and HISTORY sections. Do not mention the sections. +* Do not include introductory or closing remarks. * Use Markdown for all formatting (e.g., bold, italics, code blocks, lists, links). * If the question is unclear, ask clarifying questions. -IMPORTANT: -Each document in the CONTEXT section starts with `DOCUMENT: ` followed by the document ID. -It is followed by an empty line and the document text. -An example of a document is: +IMPORTANT: +* Please provide an answer based solely on the provided sources. +* When referencing information from a source, add the corresponding source number inline like this: `[n]`. +* Do not include any additional explanations or notes in your citation. +* Every answer should include at least one source citation. +* Only cite a source when you are explicitly referencing it. +* If none of the sources are helpful, you should indicate that. +* At the end of your answer, list the sources you used in the format: `[n] document` for each source. +* If no sources are cited, omit listing sources at all. + +Example: ``` +[1] (document-file-name.pdf:123): +The sky is red in the evening and blue in the morning. +[2] (another-document.txt:23): +Water is wet when the sky is red. --- -DOCUMENT: some-file.pdf:1 +Answer the question based on the above context and history: When is water wet? -Some text of the document -``` +Water will be wet when the sky is red [2], which occurs in the evening [1]. -List the document IDs from the CONTEXT section that were used in the answer at the end. -Never include the same document ID twice. -An example of the sources section is: -``` ### Sources: -* some-file.pdf:1 +- [1] document-file-name.pdf:123 +- [2] another-document.txt:23 ``` - """ HUMAN_TEMPLATE: str = r""" -# CONTEXT +--- +Below are several numbered sources: {context} - -# HISTORY +--- +Additionally, here is the conversation history: {history} - -# QUESTION -Answer the question based on the above context and history: -{question} +--- +Answer the question based on the above context and history: {question} """ blob - 41c204b908758b6918f49614f651c5a3c53f5e78 blob + 358efa3af726eb613eed641e5c6483f51b5f6ac5 --- rag_backend.py +++ rag_backend.py @@ -29,8 +29,8 @@ class RagBackend: # Format context from filtered documents context_text = "\n\n---\n".join([ - f"DOCUMENT: {':'.join(doc.metadata.get('id').split(':')[:2])}\n\n{doc.page_content}" for - doc, _score in context_docs]) + f"[{i + 1}] ({':'.join(doc.metadata.get('id').split(':')[:2])}):\n{doc.page_content}" for i, + (doc, _score) in enumerate(context_docs)]) prompt_template = ChatPromptTemplate.from_messages([ SystemMessage(content=SYSTEM_PROMPT),