Automate ChatGPT with Workflow Engine built with Go (example of sending goal seek results by email)
Tags: chatgpt, openai, go, nocode
Published: 2023-03-20 09:52:47

---
title: Automate ChatGPT with Workflow Engine built with Go (example of sending goal seek results by email)
published: true
description: 
tags: chatgpt,openai,golang,nocode
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2023-03-20 09:34 +0000
---

## 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](https://storage.googleapis.com/zenn-user-upload/021dd4968a18-20230320.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u3sxmrpwcx77fj6t8y7o.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mmgo4u7niwuvrtb9ta05.png)

## 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. 
    ```sh
    brew install yohamta/tap/dagu
    ```
1. You can check if the installation is successful with the following command: 
    ```sh
    dagu version
    ```

### Launching Dagu Server

1. Dagu server can be launched by the following command.
    ```sh
    dagu server
    ```
2. Access http://localhost:8080 in your browser. You should see a screen similar to the following.
![top](https://storage.googleapis.com/zenn-user-upload/9a7bff83db11-20230320.png)

### 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](https://storage.googleapis.com/zenn-user-upload/6eb8db8e0dd7-20230320.png)
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](https://storage.googleapis.com/zenn-user-upload/aa0378658739-20230320.png)
4. paste the following into the edit screen and save it ('SAVE' button).
    ```yaml
    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.
    ```yaml
    env:
      - OPENAI_API_KEY="ChatGPT API Key"
    ```

2. set up a mail sending server
    Register with [Mailgun](https://www.mailgun.com/) 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.

    ```yaml
    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.
    ```yaml
    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](https://storage.googleapis.com/zenn-user-upload/b9d094da38eb-20230320.png)

The execution status is displayed in the browser.

![status](https://storage.googleapis.com/zenn-user-upload/3d31bbeb6982-20230320.png)

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](https://storage.googleapis.com/zenn-user-upload/047b862b46e4-20230320.png)

You will then receive the following email. Oh no.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w5787mxqyf2fv1mogu5u.png)



## 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](https://github.com/yohamta/dagu)