Skip to content

Generate Guide with ChatGPT API #3

Generate Guide with ChatGPT API

Generate Guide with ChatGPT API #3

Workflow file for this run

name: Create Guide from ChatGPT
on:
workflow_dispatch:
inputs:
title:
description: "The title of the guide"
required: true
description:
description: "The description for the guide"
required: true
jobs:
generate_and_create_pr:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up Node.js environment
uses: actions/setup-node@v2
with:
node-version: '16'
- name: Install dependencies
run: npm install axios
- name: Generate guide with ChatGPT API
id: generate_guide
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
TITLE: ${{ github.event.inputs.title }}
DESCRIPTION: ${{ github.event.inputs.description }}
run: |
echo "const axios = require('axios');
const fs = require('fs');
const title = process.env.TITLE;
const description = process.env.DESCRIPTION;
const prompt = \`Create a guide for Port (docs.getport.io) titled \"${title}\" with the following description: ${description}. Format the guide in markdown.\`;
(async () => {
const response = await axios.post('https://api.openai.com/v1/chat/completions', {
model: 'gpt-4',
messages: [{ role: 'user', content: prompt }]
}, {
headers: {
'Authorization': \`Bearer ${process.env.OPENAI_API_KEY}\`,
'Content-Type': 'application/json'
}
});
const guideMarkdown = response.data.choices[0].message.content;
fs.writeFileSync('guide.md', guideMarkdown);
})();" > generateGuide.js
node generateGuide.js
- name: Create new branch
run: |
BRANCH_NAME="guide/${{ github.event.inputs.title }}-$(date +%Y%m%d%H%M%S)"
git checkout -b $BRANCH_NAME
echo "branch_name=$BRANCH_NAME" >> $GITHUB_ENV
- name: Add guide file and commit
run: |
git add guide.md
git commit -m "Add guide: ${{ github.event.inputs.title }}"
- name: Push branch to origin
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git push -u origin ${{ env.branch_name }}
- name: Create pull request
uses: peter-evans/create-pull-request@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ env.branch_name }}
title: "Add guide: ${{ github.event.inputs.title }}"
body: "This pull request adds a new guide generated by ChatGPT."
base: main