DT4 - My take on using AI to enrich my feeds

Hey there,

this is my take on enriching helpful feeds with tags and summaries I want to share with the community.

Why do I use my own scripts instead of the default ones coming with DT4?

  • Understand what I’m doing and getting comfortable with the technolog
  • I’m quite picky with upper and lower case tags.
<editor> ~/Library/Script Libraries/ai-add-tags-to-feeds-2.scpt
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

property theGlobalPrompt : "You're an experienced journalist with a solid background in investment banking and technology. 1. Remove all unnecessary HTML code include consent banners from the response 2. Create a German (field summary_de) and an English (field summary_en) summary of about max 300 words each 3. Extract the most 5 relevant words from the English summary. Generate a list of max 5 tags in English (save as my_tags) based on the extracted words and prefix each tag with 'ai-'. 4. Add the tag 'ai-market-relevant' if the news item is very important on moving stock markets - inflation, economy, unemployment rate, economic outcome. 5. Add the industry sector based on S&P500 industry sectors to the result - field industry_sector. 6. Make all tags lower case 7. Return the result as plain JSON with NO markdown"

-- test performSmartRule in my editor
-- tell application id "DNtp" to my performSmartRule(selected records)
-- return

on performSmartRule(theRecords)
	tell application id "DNtp"
		try
			local ScriptHandler
			set ScriptHandler to script "ai-add-tags-to-feeds-2"
			ScriptHandler's ForAll(theRecords, ChatGPT, "gpt-4o", missing value, "abstract")
			
		on error error_message number error_number
			if the error_number is not -128 then display alert "DNtp" message error_message as warning
			return
		end try
	end tell
end performSmartRule

-- Run job for all given records
-- Uses default model for given engine
on ForAll(theRecords, theEngine, theModel, thePrompt, theAttribute)
	tell application id "DNtp"
		show progress indicator "Run job for record" steps (count of theRecords)
		
		repeat with theRecord in theRecords
			set theName to the name of theRecord
			step progress indicator theName
			my ForOne(theRecord, theEngine, theModel, thePrompt, theAttribute)
		end repeat
		
		hide progress indicator
	end tell
	
	log "[INFO] Job completed."
end ForAll

-- Run job for single record
-- Uses default model for given engine
on ForOne(theRecord, theEngine, theModel, thePrompt)
	local AiBase
	set AiBase to script "ai-base"
	
	if thePrompt is missing value then
		set thePrompt to theGlobalPrompt
	end if
	
	tell application id "DNtp"
		log "[DEBUG] Validate chosen AI engine"
		set theEngine to AiBase's ValidateAndReturnEngine(theEngine)
		
		local theUrl
		set theUrl to URL of theRecord
		
		if theUrl is missing value then
			log "[WARN] No URL. Exit"
			return
		end if
		
		local theResponse
		set theResponse to AiBase's AskModelForUrl(theUrl, theEngine, theModel, thePrompt, "JSON")
		
		if theResponse is missing value then
			log "[WARN] No (valid) result from AI"
			return missing value
		end if
		
		local newTags
		set newTags to my_tags of theResponse
		
		local theSummary
		set theSummary to "**English Summary**
" & summary_en of theResponse & "

**German Summary**
" & summary_de of theResponse
		
		add custom meta data theSummary for "abstract" to theRecord
		
		return theSummary
	end tell
end ForOne
<editor> ~/Library/Script Libraries/ai-base.scpt
on AskModelForUrl(theUrl, theEngine, theModel, thePrompt, theResultFormat)
	if theResultFormat is missing value then
		set theResultFormat to "JSON"
	end if
	
	tell application id "DNtp"
		with timeout of 60 seconds
			local theResponse
			
			log "[DEBUG] Get response based on engine and model selection for URL"
			
			if theModel is missing value then
				set theResponse to get chat response for message thePrompt engine theEngine URL theUrl temperature 0 as theResultFormat
			else
				set theResponse to get chat response for message thePrompt engine theEngine model theModel URL theUrl temperature 0 as theResultFormat
			end if
			
			return theResponse
		end timeout
	end tell
end AskModelForUrl
2 Likes

Thanks for sharing this!

Internally the URL parameter doesn’t use the raw downloaded HTML, instead either a (stripped) plain text or Markdown representation is used to save tokens depending on the used model. But the parameter supports also online images and PDF documents (again depending on the model), by the way.