Implementing OpenAI's ChatGPT Interaction using GitHub Actions


GitHub Actions help you to automate your software workflows with CI/CD capabilities. This includes continuous integration (building and testing your project), continuous delivery, and continuous deployment.

To integrate GitHub Actions with ChatGPT, follow these steps. These instructions will help you create an action that triggers when you push a commit, and it will connect with OpenAI’s API to send a request to ChatGPT.

For instance, this will show you how to setup GitHub Actions to send a message to ChatGPT and get a response.

Step 1: Create a new GitHub repository

Navigate to GitHub and create a new repository. Give it a name, a description, and initialize it with a README file. Then, clone it to your local machine.

Step 2: Generate an OpenAI API key

Go to the OpenAI website, navigate to your dashboard, and generate a new API key. This will be used to authenticate your requests to the ChatGPT API.

Step 3: Setup a GitHub Action

Create a new file in your repository with the following path: .github/workflows/chat.yml. This file will define your GitHub Action.

Add the following content to chat.yml:

name: Chat with GPT

on: [push]

jobs:
chat:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Send message to GPT
run: |
curl -X POST \
https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${{ secrets.OPENAI_KEY }}" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Say this is a test!"}],
"temperature": 0.7
}'
env:
OPENAI_KEY: ${{ secrets.OPENAI_KEY }}

This action will trigger every time you push a commit. It will send a request to the OpenAI API with a hardcoded prompt. Please replace the {} with the English text that you want to translate.

Step 4: Add your API key to GitHub Secrets

To avoid exposing your API key, add it to the GitHub Secrets. Go to your GitHub repository, click on “Settings” > “Secrets” > “New repository secret”, and add your API key with the name OPENAI_KEY.

Step 5: Commit and push

Add your changes to git, commit them, and push to GitHub. This will trigger your GitHub Action.


Author: robot learner
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source robot learner !
  TOC