Skip to content

Generate Guide with ChatGPT API #21

Generate Guide with ChatGPT API

Generate Guide with ChatGPT API #21

Workflow file for this run

name: Generate Guide with ChatGPT API
on:
workflow_dispatch:
inputs:
title:
description: 'Title of the Guide'
required: true
description:
description: 'Description of the Guide'
required: true
jobs:
generate-guide:
runs-on: ubuntu-latest
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
ASSISTANT_ID: 'asst_401AkHGtgCQEtJBJQecK01Cb' # Replace with your actual assistant ID
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Set up variables
run: |
echo "TITLE=${{ github.event.inputs.title }}" >> $GITHUB_ENV
echo "DESCRIPTION=${{ github.event.inputs.description }}" >> $GITHUB_ENV
- name: Install jq
run: sudo apt-get install -y jq
- name: Create a thread
id: create_thread
run: |
THREAD_RESPONSE=$(curl -s https://api.openai.com/v1/threads \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "OpenAI-Beta: assistants=v2" \
-d '')
THREAD_ID=$(echo "$THREAD_RESPONSE" | jq -r '.id')
echo "THREAD_ID=$THREAD_ID" >> $GITHUB_ENV
shell: bash
- name: Add a message to the thread
run: |
MESSAGE_CONTENT="${{ env.TITLE }}: ${{ env.DESCRIPTION }}"
curl -s https://api.openai.com/v1/threads/${{ env.THREAD_ID }}/messages \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "OpenAI-Beta: assistants=v2" \
-d "{\"role\": \"user\", \"content\": \"$MESSAGE_CONTENT\"}"
shell: bash
- name: Create a run
id: create_run
run: |
RUN_RESPONSE=$(curl -s https://api.openai.com/v1/threads/${{ env.THREAD_ID }}/runs \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-H "OpenAI-Beta: assistants=v2" \
-d "{
\"assistant_id\": \"${{ env.ASSISTANT_ID }}\",
\"instructions\": \"Please assist with the provided title and description.\"
}")
echo "Run Response: $RUN_RESPONSE"
RUN_ID=$(echo "$RUN_RESPONSE" | jq -r '.id')
echo "RUN_ID=$RUN_ID" >> $GITHUB_ENV
shell: bash
- name: Wait for run completion
run: |
STATUS="running"
while [[ "$STATUS" == "running" || "$STATUS" == "queued" || "$STATUS" == "in_progress" ]]; do
sleep 5
RUN_STATUS_RESPONSE=$(curl -s https://api.openai.com/v1/threads/${{ env.THREAD_ID }}/runs/${{ env.RUN_ID }} \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "OpenAI-Beta: assistants=v2")
STATUS=$(echo "$RUN_STATUS_RESPONSE" | jq -r '.status')
echo "Current run status: $STATUS"
done
if [[ "$STATUS" != "succeeded" && "$STATUS" != "completed" ]]; then
echo "Run failed with status: $STATUS"
exit 1
fi
shell: bash
- name: Retrieve assistant's response
run: |
MESSAGES=$(curl -s https://api.openai.com/v1/threads/${{ env.THREAD_ID }}/messages \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "OpenAI-Beta: assistants=v2")
ASSISTANT_MESSAGE=$(echo "$MESSAGES" | jq -r '.data[] | select(.role=="assistant") | .content')
echo "Assistant's Response:"
echo "$ASSISTANT_MESSAGE" > guide.md
shell: bash
- name: Configure Git
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
- name: Create and Push Branch with Guide
run: |
git checkout -b generate-guide-branch
git add guide.md
git commit -m "Add generated guide: ${{ github.event.inputs.title }}"
git push origin generate-guide-branch
- name: Create Pull Request
uses: peter-evans/create-pull-request@v5
with:
branch: generate-guide-branch
title: "Add Generated Guide: ${{ github.event.inputs.title }}"
body: "This PR adds a new guide generated based on the provided title and description."
base: main
labels: "documentation,auto-generated"