PHP code example of onekb / chat-gpt

1. Go to this page and download the library: Download onekb/chat-gpt library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

onekb / chat-gpt example snippets


composer 

// 你的key
// your key
$apiKey = 'sk-PXxxxxxxxx';

// 最大提交聊天记录数,内容太多会消耗更多tokens
// The maximum number of submitted chat records, too much content will consume more tokens
$limit = 10;

$chatGPT=new \Onekb\ChatGpt\ChatGpt($apiKey, $limit);

// 简单使用
var_dump($chatGPT->ask('你好'));

// 设置代理
\Onekb\ChatGpt\Di::set('proxy', 'http://127.0.0.1:8899');

// 重新发起对话 Reinitiate a conversation
// $chatGpt->clearHistory();

// 自定义聊天记录 Custom chat history
//$chatGpt->history = [
//    [
//        'role' => 'user',
//        'content' => '你好',
//    ],
//    [
//        'role' => 'assistant',
//        'content' => '你好',
//    ],
//];

php start.php