In the continuation of my Weekly Newsletter summarization idea, I would like to create a smart rule that sends a document to an LLM for summarization and then appends the result to a Markdown file.
I have created a smart rule, that sends the document to the LLM for summarization, but I can’t figure out how to take the resulting text and append it to a Markdown file.
Furthermore, when I do this, can I count on the Smart Rule to be blocking and not run in parallel? I want to make sure I don’t competing writes to the Markdown file.
1 Like
You need to use a Set Script Input action using the Chat Response placeholder. This is passed to a Script with Input/Output action where theInput is used with an update record theRecord with text theInput mode appending command.
Does this need to be a smart rule or would an impromptu batch process be sufficient?
on scriptOutput(theRecord, theInput)
tell application id "DNtp"
update record theRecord with text (linefeed & linefeed & "---" & linefeed & theInput as string) mode appending
end tell
end scriptOutput
Furthermore, when I do this, can I count on the Smart Rule to be blocking and not run in parallel? I want to make sure I don’t competing writes to the Markdown file.
Please clarify this.
2 Likes
Too many years of programming experience here. If the smart rules are running in parallel, then I could end up with a race condition, where the same file is being written to by multiple threads.
This is an edge case, but I wanted to make sure.
1 Like
Smart rules don’t run concurrently.
1 Like
Darn I forgot to ask a supplementary question. In your example, it appears that the smart rule is appending to a Markdown file it found in the rule itself (theRecord). In my case, the document it finds is the thing being summarized. So how do I tell the AppleScript to append to append to a common Markdown file. For example: NewsletterSummaries.md?
Locating a document is commonly done via its UUID.
set commonDoc to get record with uuid "x-devonthink-item://9D336907-90A9-48B8-BFEE-1374424F9AE1" -- Logically, with the item link of your actual document
update record commonDoc…
2 Likes
Thanks it’s been a few days, but I finally got it up and running. Your code works well.
There is a still a lingering issue. With approx 1/5 → 1/10 of my documents, it fails to generate a summary. I’m guessing the model times out. Could I set this up with a retry mechanism? (ideally that would be built into as an option).
For summarization like this the speed isn’t critical, it’s a background process. I care more about it being reliable.
As an illustration in 41 documents, 14 failed to generate a summary. When I try to run it again, some will still fail.
Development would have to comment on this but there are already retries built into the mechanism.
Perhaps there already is. Let me reframe, this will still require development to weigh in.
A key element of DEVONthink is reliability and repeatability. Since the Chat command in a smart-rule isn’t reliable, what can we so I can trust that smart-rules will work, without me having to retry 4-5 times?
The command (and all other AI related features) already performs up to two retries. Sometimes an AI service is offline/unavailable like some Claude services yesterday in the morning (CET). Was anything logged to Window > Log?
You could e.g. check the input in the script and set a flag, label or tag if the record was successfully updated and exclude this flag/label/tag via the smart rule conditions.
1 Like
Thanks for the reply. How do Apply/Remove a Tag based the response (or lack thereof) from the Chat command?
I don’t see anything that looks like a conditional in the Actions section of the Smart Rule. Please help me see what I’m missing.
There is only one conditional action: Chat - Continue if. This is noted in the Automation > AI Assisted Automation section of the built-in Help and manual…