Skip to content

Commit

Permalink
feat(#85): remove q) on markdown output (#96)
Browse files Browse the repository at this point in the history
- [ ] I have considered whether this PR needs review, and requested a review if necessary.

Fixes issue #

# Notes for reviewers
Reviewers can skip X, but should pay attention to Y.
  • Loading branch information
MartinBernstorff authored Mar 30, 2024
1 parent ad57adc commit 4d1fab3
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 29 deletions.
22 changes: 17 additions & 5 deletions memorymarker/persist_questions/__snapshots__/test_markdown.ambr
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
# serializer version: 1
# name: test_single_q_to_markdown
# name: test_highlight_to_md[highlight0]
'''
Q) What is the meaning of life?

> [!NOTE]- Highlight
> ==42==
> What is the answer to the ultimate question of life, the universe, and everything?
> 42.
> Of course, this was a very simple question for the supercomputer Deep Thought, the second greatest computer of all time, the greatest being the Earth itself.
>
> Deep Thought was created to answer the ultimate question of life, the universe, and everything. It took Deep Thought 7½ million years to compute and check the answer, which turns out to be 42.
>
> [Link](https://en.wikipedia.org/wiki/The_Hitchhiker%27s_Guide_to_the_Galaxy#meaning_of_life)

What is the meaning of life?
42


'''
# ---
# name: test_single_q_to_markdown
'''
What is the meaning of life?
42

'''
# ---
17 changes: 1 addition & 16 deletions memorymarker/persist_questions/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
if TYPE_CHECKING:
from pathlib import Path

from memorymarker.question_generator.qa_responses import QAPrompt
from memorymarker.question_generator.reasoned_highlight import ReasonedHighlight


Expand All @@ -15,23 +14,9 @@ def _clean_filename(filename: str) -> str:
return without_duplicate_spaces


def q_to_markdown(prompt: "QAPrompt") -> str:
highlight = prompt.hydrated_highlight
return f"""Q) {prompt.question}
> [!NOTE]- Highlight
> {highlight.prefix + " " or ""}=={highlight.highlighted_text}=={highlight.suffix.strip() + " " or ""}
> [Link]({highlight.source_document.uri})
\n"""


def write_qa_prompt_to_md(highlight: "ReasonedHighlight", save_dir: "Path") -> None:
"""Write markdown to file. Append if exists"""
contents = "/n".join(
[q_to_markdown(prompt) for prompt in highlight.question_answer_pairs]
)

with (save_dir / f"{_clean_filename(highlight.source_document.title)}.md").open(
mode="a"
) as f:
f.write(contents + "\n")
f.write(highlight.to_markdown())
25 changes: 19 additions & 6 deletions memorymarker/persist_questions/test_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@ class FakeHydratedHighlight(ReasonedHighlight):

updated_at: dt.datetime = dt.datetime.now()

prefix: str = ""
highlighted_text: str = "42"
suffix: str = ""

question_answer_pairs: Sequence[QAPrompt] = []
prefix: str = "What is the answer to the ultimate question of life, the universe, and everything?"
highlighted_text: str = "42."
suffix: str = "Of course, this was a very simple question for the supercomputer Deep Thought, the second greatest computer of all time, the greatest being the Earth itself.\n\nDeep Thought was created to answer the ultimate question of life, the universe, and everything. It took Deep Thought 7½ million years to compute and check the answer, which turns out to be 42."

question_answer_pairs: Sequence[QAPrompt] = [
QAPrompt(
question="What is the meaning of life?",
answer="42",
hydrated_highlight=None,
title="The Hitchhiker's Guide to the Galaxy",
)
]

pipeline_name: str = "fake_pipeline"

Expand All @@ -33,6 +40,12 @@ class FakeHydratedHighlight(ReasonedHighlight):
qa_string: str = ""


@pytest.mark.parametrize(("highlight"), [FakeHydratedHighlight()])
def test_highlight_to_md(highlight: FakeHydratedHighlight, snapshot) -> None: # noqa: ANN001
input_md = highlight.to_markdown()
assert snapshot == input_md


@pytest.fixture(scope="module")
def question() -> QAPrompt:
return QAPrompt(
Expand All @@ -44,7 +57,7 @@ def question() -> QAPrompt:


def test_single_q_to_markdown(question: QAPrompt, snapshot) -> None: # noqa: ANN001
input_md = markdown.q_to_markdown(question)
input_md = question.to_markdown()
assert snapshot == input_md


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async def __call__(self, prompt: str) -> str:
if not completion_str:
raise ValueError(f"Completion was not a string: {completion_str}")

return completion_str
return completion_str # type: ignore


class ModelCompleter(Protocol):
Expand Down
7 changes: 6 additions & 1 deletion memorymarker/question_generator/qa_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@

@dataclass(frozen=True)
class QAPrompt:
hydrated_highlight: "ReasonedHighlight"
hydrated_highlight: "ReasonedHighlight | None"
question: str
answer: str
title: str

def to_markdown(self) -> str:
return f"""{self.question}
{self.answer}
"""


class QAPromptResponseModel(BaseModel):
question: str = Field(
Expand Down
14 changes: 14 additions & 0 deletions memorymarker/question_generator/reasoned_highlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class SourceDocument:
uri: str


def to_markdown_quote(text: str) -> str:
lines = [f"> {line}" for line in text.splitlines()]
return "\n".join(lines)


class ReasonedHighlight(BaseModel):
source_document: SourceDocument

Expand All @@ -32,3 +37,12 @@ class ReasonedHighlight(BaseModel):
@property
def context(self) -> str:
return f"{self.prefix}{self.highlighted_text}{self.suffix}"

def to_markdown(self) -> str:
prefix = to_markdown_quote(self.prefix) if self.prefix else ""
highlighted = to_markdown_quote(self.highlighted_text)
suffix = to_markdown_quote(self.suffix.strip()) if self.suffix else ""
link = f"> \n> [Link]({self.source_document.uri})"

qa_md = [prompt.to_markdown() for prompt in self.question_answer_pairs]
return "\n".join((prefix, highlighted, suffix, link, "", *qa_md, ""))

0 comments on commit 4d1fab3

Please sign in to comment.