DEV Community

Yota Hamada
Yota Hamada

Posted on • Updated on

Automate ChatGPT with Workflow Engine built with Go (example of sending goal seek results by email)

Introduction.

This article shows how ChatGPT can be automated using Dagu, a free workflow engine. Specifically, we will show you how to use YAML definitions to run ChatGPT's API and automatically email the results.

What is a DAG workflow engine?

A workflow engine is, simply put, a tool for automatically executing the tasks you do on your computer. It does not simply execute tasks in simple sequence, but can also specify dependencies between tasks and execute tasks in parallel at the same time. The following graph is a DAG (Directed acyclic graph).

DAG

What is Dagu?

Dagu is a free, open source DAG workflow engine.

Features

  • DAGs can be created with no code (no programming required).
  • All operations, including creating and running DAGs, can be done from a browser.
  • Just install the command on your Mac and use it.

Example of calling ChatGPT API and sending an answer by email

The following is an example of calling ChatGPT's API and sending the results by email.

Example workflow

What each step does is simple.

  1. call the ChatGPT API with the specified question
  2. extract the answers from the API response
  3. convert the response into an HTML email ready to be sent
  4. send the email.

As a result, you will receive an email that looks like this

mail

Steps to use ChatGPT to automate goal seek questions

In this section, the Goal Seek prompt is used to automate Goal Seek. Note that any prompt can be automated in the same way by rewriting the contents of the DAG.

The prompt used:

[Goal] Goal to achieve.
a[0,0]=[Goal]
a[i,1-5]=5 ideas to achieve a[i,0].
a[i+1,0]=a[i,1-5], which is the best idea to achieve a[i+1,0]=a[i,1-5]

Use this to calculate up to i=0-5 and put the results in an HTML table with 5 rows and 5 columns, 25 cells in total.

Installing Dagu

  1. Dagu can be installed using Homebrew.

    brew install yohamta/tap/dagu
    
  2. You can check if the installation is successful with the following command:

    dagu version
    

Launching Dagu Server

  1. Dagu server can be launched by the following command.

    dagu server
    
  2. Access http://localhost:8080 in your browser. You should see a screen similar to the following.
    top

Creating a ChatGPT workflow.

  1. click on the three-line menu on the left. The following is what you will see in my environment. list
  2. press the 'NEW' button in the top right-hand corner and enter the name of the new DAG chatgpt_goalseek_example.
  3. press the 'EDIT' button in the bottom right-hand corner. Edit
  4. paste the following into the edit screen and save it ('SAVE' button).

    params: GOAL="Unique and interesting bullet-hell game."
    env:
      - OPENAI_API_KEY="ChatGPT API Key"
      - MY_EMAIL="Recipient email address"
      - FORMAT: >-
          a[0,0]=[Goal]\n
          Five ideas to achieve a[i,1-5]=a[i,0]. \n
          The best idea to achieve a[i+1,0]=a[i,1-5]. \n
          \n
          Use this to calculate up to i=0-5 and put the results into a HTML table with 5 rows and 5 columns, 25 squares in total. \n
    
    smtp:
      host: "smtp.mailgun.org"
      port: "587"
      username: "Mailgun username"
      password: "Mailgun password"
    
    steps:
    
      - name: ask chatgpt
        executor:
          type: http
          config:
            timeout: 1200
            headers:
              Authorization: "Bearer $OPENAI_API_KEY"
              Content-Type: "application/json"
            silent: true
            body: |
              { "model": "gpt-3.5-turbo", "messages": [
                  {"role": "system", "content": "Act as a state-of-art chat AI. Please output the result in a table format with 5 rows and 5 columns. Format your reply in HTML code styled with beautiful CSS."},
                  {"role": "user", "content": "[Goal]${GOAL}\n${FORMAT}"}
                ]
              }
        command: POST https://api.openai.com/v1/chat/completions
        output: API_RESPONSE
    
      - name: get result
        executor:
          type: jq
          config:
            raw: true
        command: ".choices[0].message.content"
        script: "$API_RESPONSE"
        output: MESSAGE_CONTENT
        depends:
          - ask chatgpt
    
      - name: convert escaped unicode to plain HTML
        command: "sh"
        script: |
          input="${MESSAGE_CONTENT}"
          unescaped=$(echo -e "$input" | sed 's/\\u003c/</g' | sed 's/\\u003e/>/g')
          echo "$unescaped"
        output: MESSAGE_CONTENT
        depends:
          - get result
    
      - name: send mail
        executor:
          type: mail
          config:
            to: "$MY_EMAIL"
            from: "$MY_EMAIL"
            subject: "goal seek result"
            message: "$MESSAGE_CONTENT"
        depends:
          - convert escaped unicode to plain HTML
    

Setting up the ChatGPT API key and email sending server

  1. set up the ChatGPT API key
    Rewrite the ChatGPT API Key below with your ChatGPT API key.

    env:
      - OPENAI_API_KEY="ChatGPT API Key"
    
  2. set up a mail sending server
    Register with Mailgun and you can send emails for free. Rewrite the following Mailgun username and Mailgun password with your Mailgun username and password. You can also use other email services.

    smtp:
      host: "smtp.mailgun.org"
      port: "587"
      username: "Mailgun Username"
      password: "Mailgun Password"
    
  3. set the recipient email address
    Replace the following Recipient email address with the email address to which you want to send the ChatGPT response.

    env:
      - MY_EMAIL="Recipient email address".
    

Try it out and run it.

Press the 'START' button in the top right-hand corner to run the DAG. In the text box, put in what you want to ask for in the Goal Seek prompt. For example, put in The best and most interesting roguelike game idea.
dialog

The execution status is displayed in the browser.

status

If an error occurs, press the 'RETRY' button in the top right-hand corner; the error may occur if ChatGPT's API is heavy.

If successful, you will see the following screen.
success

You will then receive the following email. Oh no.
Image description

Summary

As you can see, the workflow engine allows you to automate ChatGPT API calls with no-code.

If you always run similar prompts, it may be useful to define a DAG.

You can do more interesting things by combining multiple inputs and outputs of ChatGPT's API. For example, ChatGPT responses could be passed to further APIs to improve or transform the results, and if defined as a DAG, it could be useful to combine multiple roles of ChatGPT, as the APIs can be called in parallel.

I can only dream of the things we could do with it!

Dagu is open source software that I am developing using the Go language.
I would be happy if you could participate in its development.

https://github.com/yohamta/dagu

Top comments (0)