ChatGPT Easy Integration with PyDT3

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:

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.

10 Likes

Very very interesting - thank you.

Seems quite helpful but I am not quite sure I follow how you made Python work with DT3.

Underlying the Python is just calling the AppleScript and retrieves the return value. But it’s using PyObjC (not subprocess.call(['osascript...] etc) so the performance loss should be acceptable.

2 Likes

Impressive.

But I’m a little bit confused with this. Well, not with the script, but how ChatGPT works. I have an Open AI account, I’ve generated a token, set as described here, and then tried the script. After some go-and-forth with macOS security, script did nothing.

I directly opened it and directly ran it with a PDF selected in DT. And I got an exception that I’ve exceeded my quota.

However, I still can chat with it via Web navigator. Does using it via token need a paid account? If yes, I can pay 20/mo, but in developer area it shows pay-by-tokens… I’m confused and documentation does not clear to me if the fixed amount allows access via scripts or not…

Can someone answer this off-topic question? Thanks.

First, let’s make sure we are on the same page. The “Please enter your api key” prompt window did show up right? If that window appears, the script itself runs correctly.

If that’s the case, the problem seems to be that you don’t have the API quota. The “chat with it via Web navigator” is a different thing from “API access”. There are three different things for ChatGPT.

  1. Web chat interface. It’s free as long as you have an account. The model used by it is ChatGPT 3.5.
  2. The Plus Account. This is the 20/mo thing. This plan gives you “ChatGPT 4.0” access and faster ChatGPT 3.5 reply speed.
  3. API Access. The Plus Account DOES NOT grant you API access. They are two different things. The API uses 3.5 model and it’s a “pay-by-token” thing (the prices are relatively low though). This let you use the API to access ChatGPT, which is essential if you want to use ChatGPT outside the web interface.

Yes, it asked it and now I have a pure text file with openai_api_key name and my API inside.

That’s what I needed. Zillion Zanks.

Enabling a payment method in https://platform.openai.com and generating a new API Key works perfect, but it is limited to little tokens and even some captured stuff from internet fails to generate the tags.

The tag generation ability is provided by the following prompts as a prove of concept and is still rudimentary.

Generate the tags for the following content. Tags should be concise and accurate and no more than 10. output the tags directly separated by ‘,’:\n {content}"

Do share if you find a more robust prompt.

Hello there. I have not understood of the integration allows you to:
a) extract via GPT the tags related to some Devonthink-stored content. You seem to show a paper that has in the info tags (entities) extracted by GPT
b) if ChatGPT can be “invoked” from within a Devonthink document to generate some other content to be pasted in the Devonthink environment. Could you kindly clarify?

And, if ever possible, will you ever make a video so that we can see all the steps for the installation of this solution ? Many thanks
Andrea

This is a great integration, it works very well for Tags, THANKS!

I want to extend the integration to include modification of the DT filename based on a prompt from another thread, "You are a program designed to generate filenames that could match the given text. Output exactly 1 filename that could fit the content and nothing else. Format the filename as the “Date of Statement or Bill (yyyy-mm)”“Biller Name”“Bill Description”_“Service Date (yyyy-mm)” present in the content, otherwise use the current date. Don’t output a file extension, separate words by space. No preamble, no extra output, only output the filename. Keep it concise.
" which works great in ChatGPT and via the OpenAI API…

I’m sure I’m just being dull, but I can’t find where your Python source code in the Apple script package to add my code to the above. Anyone point a self-professed idiot to the right place?

1 Like

Welcome @mrpeanut

I think I could just change the filename manually faster than typing all that :wink:

hahaha… the point is, it’s a right-click contextual script that automagically changes the filename to something like “2020-05_Medical Group_Statement_2020-03” for anything you point it to.

The IncomingScan script does an OK job at guessing at a filename but isn’t nearly as good as GPT.

1 Like