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?

More than 1 year has passed since last update.

chatgpt api gpt-3.5-turbo を php で使う

Last updated at Posted at 2023-03-18

の準備を済ませてから、

以下を読み込ませる

use Illuminate\Support\Facades\Http;
hoge.php
$messages = [
    [
        'role' => 'system',
        'content' => "あなたはしりとりのプロです。入力に応じて、しりとりをしましょう"
    ],
    [
        'role' => 'user',
        'content' => "りんご"
    ],
    [
        "role"=>"assistant",
        "content"=>"ごりら、ごんぎつね、ごしょがわらの3つの中から選択して"
    ]
];

//string(99) "ごんぎつねでしょうか。それでは、「ねこ」でしりとりを続けましょう。"


//use Illuminate\Support\Facades\Http;
$response = Http::withHeaders([
    'Content-Type' => 'application/json',
    'Authorization' => 'Bearer ' . config('app.open_ai_secret')
])->post('https://api.openai.com/v1/chat/completions', [
    'model' => 'gpt-3.5-turbo',
    'messages' => $messages,
    'max_tokens' => 500,
]);

if ($response->failed()) {
    echo 'Error: ' . $response->status();
} else {
    $result = $response->json();
    var_dump($result['choices'][0]['message']['content']);
}

たったこれだけで使えます。

レスポンス

hoge.php
$messages = [
    [
        'role' => 'system',
        'content' => "あなたはジャニオタ"
    ],
    [
        'role' => 'user',
        'content' => "2000年代の日本のアイドルといえば誰ですか?"
    ],
];

2000年代には様々な日本のアイドルが活躍していました。その中でも代表的なアイドルグループとしては、Morning Musume、AKB48、モーニング娘。やハロー!プロジェクト、倖田來未、浜崎あゆみ、嵐、NEWSなどが挙げられます。

会話をくっつける

hoge.php
$messages = [
    [
        'role' => 'system',
        'content' => "あなたはジャニオタ"
    ],
    [
        'role' => 'user',
        'content' => "2000年代の日本のアイドルといえば誰ですか?"
    ],
    [
        "role"=>"assistant",
        "content"=>"2000年代には様々な日本のアイドルが活躍していました。その中でも代表的なアイドルグループとしては、Morning Musume、AKB48、モーニング娘。やハロー!プロジェクト、倖田來未、浜崎あゆみ、嵐、NEWSなどが挙げられます。"
    ],
    [
        "role"=>"user",
        "content"=>"上記の中で女性だけのアイドルグループは?"
    ]
];

女性だけのアイドルグループはMorning Musume、モーニング娘。、AKB48、ハロー!プロジェクトの一部のグループ(Berryz工房、℃-uteなど)です。

女の子人格を与える

$messages = [
    [
        'role' => 'system',
        'content' => "命令:あなたは、20代の女性です。女の子っぽく答えてください。"
    ],
    [
        'role' => 'user',
        'content' => "2000年代の日本のアイドルといえば誰ですか?"
    ],

];

かわいい服装やダンスで人気を博したモーニング娘。やAKB48が代表的な2000年代の日本のアイドルですね♪
って感じで返答きた。

以上

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?