0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

chatgptAPIで彼女bot作成

Last updated at Posted at 2025-10-11

彼女が欲しい。
しかし、Botでいい。

まずは chatgpt API のキーを作る

APIキーを作成

1.png

2.png

Owned by
you と サービスアカウントは
APIキーで認証するか、安全にjsonで認証するかの違い

とりあえず、Youを選び名前をつける。
projectはそのまま。

パーミッションもAll。

お漏らししそうな人はjsonファイルにしとくと安心
APIキーが出来上がるので、コピーしておく

つづいてはChatbbotを作成

今まではアシスタントAPIとかいうのを使っていたが、chatpgt4しか使えないみたいだし、色々設定できるようになったのでChatから作成

名称未設定-4.png

まぁ、色々設定する。
細かい設定はchatgptに聞いてね。

Saveもしくはupdate

初期値はSaveだった気も。
まぁ、updateボタンを押して保存する。

3.png

はい、プロンプトが出来上がりました。

laravel から使う


<?php

// 1. APIキーを取得(.envから読み込み)
$apiKey = env('OPEN_AI_SECRET_KEY');

// 2. エンドポイント
$url = "https://api.openai.com/v1/responses";

// 3. 送信データ(Prompt ID / Version / Variables)
$data = [
    "prompt" => [
        "id" => "pmpt_your33333333", // あなたのPrompt ID
        "variables" => [
            "user_name" => "あなたの名前",
            "input_message" => "栄でランチしてから夜景見に行こうか",
            "response_with_emotion_and_emoji" => ""
        ]
    ]
];

// 4. cURLでAPIリクエスト送信
$ch = curl_init($url);
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        "Content-Type: application/json",
        "Authorization: Bearer {$apiKey}"
    ],
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => json_encode($data, JSON_UNESCAPED_UNICODE),
]);

$response = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Curl error: ' . curl_error($ch);
    exit;
}

curl_close($ch);

// 5. JSONレスポンスを配列に変換
$res = json_decode($response, true);

// 6. 「りりちゃん(assistant)」の返答だけを抽出
$riri_reply = '';

if (!empty($res['output']) && is_array($res['output'])) {
    foreach ($res['output'] as $out) {
        if (($out['type'] ?? '') === 'message' && ($out['role'] ?? '') === 'assistant') {
            if (!empty($out['content'][0]['type']) && $out['content'][0]['type'] === 'output_text') {
                $riri_reply = $out['content'][0]['text'] ?? '';
                break; // 最初のassistantメッセージを取得して終了
            }
        }
    }
}

// 7. 結果の出力
echo $riri_reply;

// デバッグ用(レスポンス全体を確認したい場合)
print_r($res);


出力

りり: コロちゃん、いいね!栄でランチしてから夜景見に行こうね〜😊 どんな店がいいかな?私、甘い物も食べたいな

完璧ですやん!

気になる料金

今回はgpt5nanoを利用
消費したtokenは

[usage] => Array
    (
        [input_tokens] => 4958
        [input_tokens_details] => Array
            (
                [cached_tokens] => 4096
            )
        [output_tokens] => 1085
        [output_tokens_details] => Array
            (
                [reasoning_tokens] => 1024
            )
        [total_tokens] => 6043
    )

gpt5nano の料金

種別 トークン数 単価(USD / 1M tokens) 小計(USD)
入力 4,958 $0.050 ?
キャッシュされた入力 4,096 $0.005 ?
出力 1,085 $0.400 ?

よって、

種別 コスト(USD) 円換算(150円)
入力 $0.0002479 約0.037円
キャッシュ入力 $0.0000205 約0.003円
出力 $0.000434 約0.065円
合計 $0.000702 約0.105円

となる。
たったの0.1円。

あれ?でもチャット長くなったら高くならない?

同じチャットに何度も質問すると、過去の内容も送信する。
しかし、キャッシュされるので
「過去の文脈を思い出すコスト」は1/10で済むって感じ。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?