PHP code example of tenko / ai-tools-php

1. Go to this page and download the library: Download tenko/ai-tools-php 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/ */

    

tenko / ai-tools-php example snippets




$config = new OpenAiConfig();
$config->setApiKeys([
    'key1',
    'key2',
    // more...
]);

// proxy if you need, "https://api.openai.com" will be replaced by "https://example.com/" in request url.
$config->setAgencyUrl('https://example.com/');

$service = new OpenAI($this->getOpenAiConfig());

$service->setContext(
    new GptContext(GptRoleEnum::SYSTEM, '现在你将模仿一只猫娘,与我对话每一句话后面都要加上“喵”,我是你的主人。简短回复,不要回复长文本'),
    new GptContext(GptRoleEnum::USER, '你好')
);
// or
$service->setContext(...GptContext::construct([
    [
        'role' => 'system',
        'content' => '现在你将模仿一只猫娘,与我对话每一句话后面都要加上“喵”,我是你的主人。简短回复,不要回复长文本'
    ],
    [
        'role' => 'user',
        'content' => '你好'
    ]
]));

$response = $service->chat();

var_dump($response);

echo $response->getResponse();

$config = new AzureConfig();
$config->setApiVersion('');
$config->setDeployments('');
$config->setResourceName('');
$config->setApiKeys([
    'key1',
    'key2',
    // more...
]);

$service = new Azure($this->getAzureConfig());
$service->setContext(
    new GptContext(GptRoleEnum::SYSTEM, '现在你将模仿一只猫娘,与我对话每一句话后面都要加上“喵”,我是你的主人。简短回复,不要回复长文本'),
    new GptContext(GptRoleEnum::USER, '你好')
);

$response = $service->chat();

var_dump($response);

echo $response->getResponse();
shell
composer