Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AI editor #1094

Draft
wants to merge 52 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
9baef8e
Dropdown menu for llm templates.
andrewsanchez Feb 11, 2025
906e157
Rename stuff.
andrewsanchez Feb 11, 2025
16c906a
Fix dq window display issue and run subprocess properly.
andrewsanchez Feb 11, 2025
277562a
Update note with LLM response.
andrewsanchez Feb 12, 2025
bf6a6cf
Check and install uv.
andrewsanchez Feb 12, 2025
4f87112
Install llm with uv.
andrewsanchez Feb 12, 2025
c0a4bc2
Fix uv run issue.
andrewsanchez Feb 12, 2025
f21819e
Install provides.
andrewsanchez Feb 12, 2025
82e2481
show diff.
andrewsanchez Feb 12, 2025
c0bdc0c
Update ankihub/labs/llm/llm.py
andrewsanchez Feb 12, 2025
4a284db
Show only anki templates.
andrewsanchez Feb 12, 2025
36ff308
Delete templates.py.
andrewsanchez Feb 12, 2025
ca24131
Add prompt templates.
andrewsanchez Feb 12, 2025
f7e1042
Preview prompt.
andrewsanchez Feb 12, 2025
3d0f67a
perf: get templates dir once.
andrewsanchez Feb 12, 2025
e3b30d2
Copy templates.
andrewsanchez Feb 12, 2025
af77d3c
Add support for linux
RisingOrange Feb 12, 2025
299d9e9
Fix error in AddCardsDialog
RisingOrange Feb 12, 2025
d58574a
Fix dialog focus after updating note
RisingOrange Feb 12, 2025
649677e
Add secrets dialog.
andrewsanchez Feb 13, 2025
63f5421
Improve the prompt preview dialog.
andrewsanchez Feb 13, 2025
c948f92
Merge remote-tracking branch 'origin/main' into ai-editor
andrewsanchez Feb 13, 2025
c8da4c2
subproject commit.
andrewsanchez Feb 13, 2025
f5a7106
Move import
andrewsanchez Feb 13, 2025
1bf8f15
Add yaml lib.
andrewsanchez Feb 13, 2025
6899aa6
enable labs.
andrewsanchez Feb 13, 2025
55e3d99
Create add-on first.
andrewsanchez Feb 13, 2025
e3498b9
Save template.
andrewsanchez Feb 14, 2025
ef94d36
always make addon file right away.
andrewsanchez Feb 14, 2025
afcd2b6
Accept result dialog before showing other dialog
RisingOrange Feb 14, 2025
80eba58
Try fix UI issue
RisingOrange Feb 14, 2025
fbf2a90
Use shell scripts and set PATH.
andrewsanchez Feb 14, 2025
df50cc1
Move scripts.
andrewsanchez Feb 14, 2025
33585d6
Make _install_llm work on Windows
RisingOrange Feb 14, 2025
f1b1e93
Fix focus issue
RisingOrange Feb 14, 2025
98e2ba9
Move labs depedenencies installation to background
RisingOrange Feb 14, 2025
3c9f64e
Use without_collection on query op for installing labs dependencies
RisingOrange Feb 14, 2025
8f84e8b
Use shell scripts.
andrewsanchez Feb 15, 2025
1d7b078
Make scripts excutable.
andrewsanchez Feb 15, 2025
90745e8
Combine scripts.
andrewsanchez Feb 15, 2025
44af1fe
Provide path to UV.
andrewsanchez Feb 15, 2025
b5b9b26
utf-8.
andrewsanchez Feb 15, 2025
185ffbe
utf-8
andrewsanchez Feb 15, 2025
bc5957b
FIx secrets dialog.
andrewsanchez Feb 15, 2025
57e5d47
Pass and update only the first field.
andrewsanchez Feb 15, 2025
3c6d2b8
Template.
andrewsanchez Feb 15, 2025
f29e542
Be sure to install providers.
andrewsanchez Feb 16, 2025
15e3abb
Merge branch 'main' into ai-editor
andrewsanchez Feb 16, 2025
ed25344
fix: Add install_providers cmd, don't make UI changes in background
RisingOrange Feb 17, 2025
60c7286
fix: Remove unused note_schema argument
RisingOrange Feb 17, 2025
eb450df
Add "Reset template" button to prompt preview
RisingOrange Feb 17, 2025
b6bf5cd
Don't reinstall providers every time
RisingOrange Feb 17, 2025
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
31 changes: 30 additions & 1 deletion ankihub/labs/llm/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

from ... import LOGGER
from ...gui.operations import AddonQueryOp
from ...gui.utils import active_window_or_mw
from ...gui.utils import active_window_or_mw, ask_user

PROMPT_SELECTOR_BTN_ID = "ankihub-btn-llm-prompt"

Expand Down Expand Up @@ -239,6 +239,11 @@ def _setup_ui(self) -> None:
save_button.clicked.connect(self._on_save)
button_layout.addWidget(save_button)

# Reset button
reset_button = QPushButton("Reset Template")
reset_button.clicked.connect(self._on_reset)
button_layout.addWidget(reset_button)

# Cancel button
cancel_button = QPushButton("Cancel")
cancel_button.clicked.connect(self.reject)
Expand Down Expand Up @@ -272,6 +277,30 @@ def _on_execute(self) -> None:
self.accept()
_execute_prompt_template(self.editor, self.template_name, modified_content)

def _update_editors(self) -> None:
"""Update the YAML editors with the current yaml_data."""
for key, editor in self.section_editors.items():
if key in self.yaml_data:
editor.setPlainText(str(self.yaml_data[key]))
else:
editor.setPlainText("")

def _on_reset(self) -> None:
"""Reset the template to the version in the local templates directory."""
if ask_user(
"Are you sure you want to reset the template to the original version?"
):
local_template_path = (
TemplateManager._local_templates_dir / f"{self.template_name}.yaml"
)
self.template_content = local_template_path.read_text(encoding="utf-8")
self.yaml_data = yaml.safe_load(self.template_content)
if not isinstance(self.yaml_data, dict):
self.yaml_data = {}
self._update_editors() # Update the editors with the reset content
TemplateManager.save_template(self.template_name, self.template_content)
tooltip("Template reset.", parent=self)
Comment on lines +288 to +302
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why but this results in some funky yaml, @RisingOrange :

model: gpt-4o
system: "You are an expert at creating effective flashcards. Analyze the provided\
  \ cloze deletion note and respond with an improved note according to the users instructions.\n\
  Follow these guidelines:\n- Use cloze deletions to hide key information.\n- Use\
  \ the {{c1::hidden text}} format for cloze deletions. You can also use {{c2::hidden\
  \ text}}, {{c3::hidden text}}, and so on. This will result in multiple cards being\
  \ created for the note by Anki. Cloze deletions with the same number will be hidden\
  \ on the same card, while all the other cloze deletions won't be used for this card.\n\
  - Most notes should only have one cloze deletion. Focus on the highest yield concepts\
  \ and facts. \"high-yield\" means the information is most important or most frequently\
  \ tested on exams.\n- Create focused cards which tests a single concept or fact.\n\
  - Ensure the remaining context provides enough information for the user to recall\
  \ the hidden content.\n\n## Common Mistakes\nAvoid these common mistakes when creating\
  \ cloze deletion cards:\n- Occluding too much information, making the card impossible\
  \ to answer.\n  - E.g., \"The {{c1::sky}} is {{c1::blue}}.\" This card is impossible\
  \ to answer because both \"sky\" and \"blue\" are occluded.\n\n\nHere are some examples\
  \ of well-constructed cloze deletion notes:\n- Persistent {{c1::infection}} is the\
  \ most common symptom of chronic sinopulmonary disease\n- Chronic sinopulmonary\
  \ disease commonly presents with chronic {{c1::cough}} and {{c1::sputum}} production.\n\
  - 11-deoxycorticosterone (DOC) may be converted to {{c1::corticosterone}} via the\
  \ enzyme {{c2::11\u03B2-hydroxylase}}.\n- 11\u03B2-hydroxylase deficiency presents\
  \ with {{c1::hypokalemia}} and {{c1::hypertension}}.\n- The {{c1::orbit}} is the\
  \ bony socket housing the eyeball and contains muscles for {{c2::eye movement}}\
  \ and {{c2::eyelid opening}}.\n- The upper portion of the nasal septum is formed\
  \ by the {{c1::ethmoid}} bone; the lower portion is formed by the {{c2::vomer}}\
  \ bone.\n- {{c1::Bone marrow}} within bones stores {{c2::fat}} and produces {{c3::blood\
  \ cells}}.\n- Two main side effects of osmotic laxatives include {{c1::diarrhea}}\
  \ and {{c1::dehydration}}.\n- Newly synthesized cGMP (from guanylyl cyclase activity)\
  \ activates {{c1::protein kinase G}}, which phosphorylates specific proteins responsible\
  \ for physiologic actions.\n- The posterior lobe of the pituitary gland is derived\
  \ from {{c1::neural}} tissue.\n- The skeleton protects {{c1::internal organs}} and\
  \ stores minerals like {{c2::calcium}} and {{c2::phosphate}}.\n\nImprove the provided\
  \ cloze deletion note based on the instructions above. Ensure that the card is clear,\
  \ and effective for learning and recall. Respond only with the improved note. Do\
  \ not provide any pre-amble, explanation, or surrounding xml tags.\n"
prompt: 'Here is the card to improve:

  <original note>

  $input

  </original note>

  '

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually not sure if it's this or saving user changes. I'll take a look

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ya I think it happens when users try to save the template changes but not sure why yet.



def _check_and_install_uv() -> None:
"""Check if uv is installed and install it if not."""
Expand Down
Loading