summaryrefslogtreecommitdiff
path: root/rag/generator/ollama.py
diff options
context:
space:
mode:
authorGustaf Rydholm <gustaf.rydholm@gmail.com>2024-04-11 00:28:29 +0200
committerGustaf Rydholm <gustaf.rydholm@gmail.com>2024-04-11 00:28:29 +0200
commit98f8d1d535c30d8c1ca6c7b52e634a99b88acf10 (patch)
treeade5dc6480a8023ce4c6210b0f737c99e346e433 /rag/generator/ollama.py
parent90838792e10cf80689e91a50b32f628e0d308cbf (diff)
Add some prompt engineering
Diffstat (limited to 'rag/generator/ollama.py')
-rw-r--r--rag/generator/ollama.py44
1 files changed, 9 insertions, 35 deletions
diff --git a/rag/generator/ollama.py b/rag/generator/ollama.py
index 87f9672..abd5599 100644
--- a/rag/generator/ollama.py
+++ b/rag/generator/ollama.py
@@ -9,16 +9,6 @@ from rag.retriever.vector import Document
from .abstract import AbstractGenerator
from .prompt import Prompt
-SYSTEM_PROMPT = (
- "# System Preamble"
- "## Basic Rules"
- "When you answer the user's requests, you cite your sources in your answers, according to those instructions."
- "Answer the following question using the provided context.\n"
- "## Style Guide"
- "Unless the user asks for a different style of answer, you should answer "
- "in full sentences, using proper grammar and spelling."
-)
-
class Ollama(metaclass=AbstractGenerator):
def __init__(self) -> None:
@@ -32,37 +22,21 @@ class Ollama(metaclass=AbstractGenerator):
return "\n".join(results)
def __metaprompt(self, prompt: Prompt) -> str:
- # Include sources
metaprompt = (
- f'Question: "{prompt.query.strip()}"\n\n'
- "Context:\n"
- "<result>\n"
+ "Answer the question based only on the following context:"
+ "<context>\n"
f"{self.__context(prompt.documents)}\n\n"
- "</result>\n"
- "Carefully perform the following instructions, in order, starting each "
- "with a new line.\n"
- "Firstly, Decide which of the retrieved documents are relevant to the "
- "user's last input by writing 'Relevant Documents:' followed by "
- "comma-separated list of document numbers.\n If none are relevant, you "
- "should instead write 'None'.\n"
- "Secondly, Decide which of the retrieved documents contain facts that "
- "should be cited in a good answer to the user's last input by writing "
- "'Cited Documents:' followed a comma-separated list of document numbers. "
- "If you dont want to cite any of them, you should instead write 'None'.\n"
- "Thirdly, Write 'Answer:' followed by a response to the user's last input "
- "in high quality natural english. Use the retrieved documents to help you. "
- "Do not insert any citations or grounding markup.\n"
- "Finally, Write 'Grounded answer:' followed by a response to the user's "
- "last input in high quality natural english. Use the symbols <co: doc> and "
- "</co: doc> to indicate when a fact comes from a document in the search "
- "result, e.g <co: 0>my fact</co: 0> for a fact from document 0."
+ "</context>\n"
+ "Given the context information and not prior knowledge, answer the question."
+ "If the context is irrelevant to the question, answer that you do not know "
+ "the answer to the question given the context.\n"
+ f"Question: {prompt.query.strip()}\n\n"
+ "Answer:"
)
return metaprompt
def generate(self, prompt: Prompt) -> Generator[Any, Any, Any]:
log.debug("Generating answer with ollama...")
metaprompt = self.__metaprompt(prompt)
- for chunk in ollama.generate(
- model=self.model, prompt=metaprompt, system=SYSTEM_PROMPT, stream=True
- ):
+ for chunk in ollama.generate(model=self.model, prompt=metaprompt, stream=True):
yield chunk["response"]