As I have posted, I wrote a Python API for ChatGPT (surprisingly it is not supported officially) that maps DEVONthink’s AppleScript APIs to Python APIs.
Also I’ve noticed the trending discussions about “integration of ChatGPT” for DEVONthink. Like:
- A Proposal for the Integration of DEVONthink and ChatGPT API
- More Thoughts on Potential ChatGPT Integration with DT3: Plugins and DT3 API
Truth be told, using the existing API provided by DEVONthink and OpenAI, the deep integration can already be done. The only problem is: AppleScript is too hard to write and when the code scales the scripts are impossible to maintain. That all can be solved if using the Python interface.
In fact, I’ve already wrote two interesting script with few lines of code. (With the Python interface and OpenAI’s official api)
Work With ChatGTP
Add Tags to Selected Records Using ChatGTP
Put this script into ~/Library/Application Scripts/com.devon-technologies.think3/Contextual Menu
and run it from contextual menu in DEVONthink (The record must be in selected state).
And voilà, the tags are added based on contents automatically.
Note: You are required to have an API key from OpenAI. The first time you run the script, a prompt will ask you to enter key.
The key will be store in Incoming group for DEVONthink (usually Inbox
). You can see the file __openai_api_key__
generated there. You can move it to other opened database but don’t change it’s name.
Auto Writing / Summarizing Using ChatGTP
This script lets you to insert <<TOKEN>>
into your text and then generate the text based on the token.
Put the script into ~/Library/Application Scripts/com.devon-technologies.think3/Toolbar
. Restart the DEVONthink and you will see the script in the toolbar customization window.
I can just put the source code here, it’s pretty simple. Half of the code are importing packages and prompting for API input from user.
import openai
from pydt3 import DEVONthink3
dtp = DEVONthink3()
def get_api_key():
result = dtp.search("name==__openai_api_key__")
if result:
api_key = result[0].plain_text
else:
response = dtp.display_dialog("Please enter your OpenAI API key", "")
api_key = response["textReturned"]
dtp.create_record_with({
"name": "__openai_api_key__",
"type": "txt",
"plain text": api_key,
})
return api_key
def generate_tags(content) -> list[str]:
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": f"Generate the tags for the following content. Tags should be concise and accurate and no more than 10. output the tags directly seperateted by ',':\n {content}"},
]
)
response = completion.choices[0]['message']['content']
print(response)
return [tag.strip() for tag in response.split(",")]
def add_tags_to_selected_records():
records = dtp.selected_records
for recod in records:
tags = generate_tags(recod.plain_text)
recod.tags = tags
if __name__ == '__main__':
openai.api_key = get_api_key()
add_tags_to_selected_records()
I don’t know how to end this topic. But that is what it is.