2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

🧠 はじめに

近年、LLM(大規模言語モデル)の進化により、APIを活用した業務自動化が急速に進んでいます。本記事では、ChatGPT API を使って、フルオートメーションを実現する方法を詳しく解説します。

Python と Node.js を用いた実装を中心に、API 連携やスクリプト自動化のテクニックを紹介し、実践的なノウハウを共有します。これを読めば、単なるチャットボットを超えた生産性向上ツール を構築できるでしょう!


📌 1. ChatGPT APIの基本理解

🎯 ChatGPT APIとは?

ChatGPT API は OpenAI が提供する 大規模言語モデルのインターフェース です。開発者はこのAPIを活用し、文章生成・要約・翻訳・コード補完など 多岐にわたる用途で使用できます。

🏗️ APIキーの取得方法

まず、OpenAIの公式サイト にアクセスし、APIキーを取得しましょう。

  • アカウント登録
  • APIキーの発行
  • 無料枠の確認(使用上限あり)

🏗️ 2. PythonでのChatGPT API活用

Python はデータ処理や自動化に適した言語です。ここでは requests ライブラリ を使った基本的なAPI連携を解説します。

🔧 必要なライブラリのインストール

pip install openai

📝 APIリクエストの基本コード

import openai

openai.api_key = "your_api_key_here"

response = openai.ChatCompletion.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "こんにちは、ChatGPT!"}]
)

print(response["choices"][0]["message"]["content"])

このスクリプトを実行すると、ChatGPT からの応答が表示されます。


⚡ 3. Node.jsでのChatGPT API活用

Node.js は非同期処理に優れ、リアルタイムアプリケーション に最適です。

📦 必要なライブラリのインストール

npm install openai axios

📝 APIリクエストの基本コード

const { Configuration, OpenAIApi } = require("openai");
const config = new Configuration({ apiKey: "your_api_key_here" });
const openai = new OpenAIApi(config);

async function askGPT() {
    const response = await openai.createChatCompletion({
        model: "gpt-4",
        messages: [{ role: "user", content: "こんにちは、ChatGPT!" }],
    });
    console.log(response.data.choices[0].message.content);
}
askGPT();

このコードを実行すると、Python 同様に ChatGPT からのレスポンスが返ってきます。


🤖 4. ChatGPTを活用した自動化スクリプト

ChatGPT API を活用すると、以下のような自動化が可能です:

メールの自動返信システム
カスタマーサポートチャットボット
Slack/Twitterの自動投稿
文章要約・翻訳の自動化
レポートや記事の自動生成

📩 例:メールの自動返信(Python)

import openai
import smtplib

openai.api_key = "your_api_key_here"

# メール本文の生成
def generate_reply(email_content):
    response = openai.ChatCompletion.create(
        model="gpt-4",
        messages=[{"role": "user", "content": email_content}]
    )
    return response["choices"][0]["message"]["content"]

# メール送信処理
def send_email(to, subject, body):
    server = smtplib.SMTP("smtp.example.com", 587)
    server.starttls()
    server.login("your_email@example.com", "your_password")
    message = f"Subject: {subject}\n\n{body}"
    server.sendmail("your_email@example.com", to, message)
    server.quit()

このスクリプトを実行すると、受信したメールに対して ChatGPT が自動で返信を生成できます。


🏁 まとめ & 次のステップ

ChatGPT API を活用すれば、業務効率化やタスクの自動化 を簡単に実現できます。本記事では Python & Node.js を使った API 連携方法と、応用的な自動化スクリプトの例を紹介しました。

次回の記事では、「Gitの達人になるための10の秘伝コマンド」 を詳しく解説する予定です。お楽しみに!

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?