PHP code example of tourze / volcano-ark-api-bundle
1. Go to this page and download the library: Download tourze/volcano-ark-api-bundle 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/ */
tourze / volcano-ark-api-bundle example snippets
use Tourze\VolcanoArkApiBundle\Service\ApiKeyService;
class MyService
{
public function __construct(
private readonly ApiKeyService $apiKeyService,
) {
}
public function createApiKey(): void
{
$key = $this->apiKeyService->createKey(
'My API Key',
'api-key-value',
'secret-key-value',
'cn-beijing'
);
$this->apiKeyService->activateKey($key);
}
public function getCurrentKey(): \Tourze\VolcanoArkApiBundle\Entity\ApiKey
{
return $this->apiKeyService->getCurrentKey();
}
}
use Tourze\VolcanoArkApiBundle\Service\UsageService;
class UsageController
{
public function __construct(
private readonly UsageService $usageService,
) {
}
public function getUsageData(): array
{
$startTime = strtotime('-7 days');
$endTime = time();
return $this->usageService->getUsage(
startTime: $startTime,
endTime: $endTime,
interval: 3600, // 1小时间隔
scenes: ['chat'],
projectName: 'my-project'
);
}
}
use Tourze\VolcanoArkApiBundle\Service\AuditLogService;
class AuditController
{
public function __construct(
private readonly AuditLogService $auditLogService,
) {
}
public function getAuditLogs(): array
{
$startTime = strtotime('-7 days');
$endTime = time();
return $this->auditLogService->getAuditLogs(
startTime: $startTime,
endTime: $endTime,
limit: 100
);
}
}
use Tourze\VolcanoArkApiBundle\Service\VolcanoArkOpenAiClient;
class ChatController
{
public function __construct(
private readonly VolcanoArkOpenAiClient $client,
) {
}
public function chat(string $message): array
{
$response = $this->client->chatCompletion([
'model' => 'your-model-id',
'messages' => [
['role' => 'user', 'content' => $message]
],
'temperature' => 0.7,
'max_tokens' => 1000,
]);
return $response->getChoices();
}
}
use Tourze\VolcanoArkApiBundle\Client\VolcanoArkApiClient;
use Tourze\VolcanoArkApiBundle\Request\VolcanoArkChatCompletionRequest;
class CustomService
{
public function __construct(
private readonly VolcanoArkApiClient $client,
) {
}
public function customRequest(): array
{
$request = new VolcanoArkChatCompletionRequest([
'model' => 'your-model-id',
'messages' => [
['role' => 'user', 'content' => 'Hello!']
],
]);
return $this->client->send($request);
}
}
use Tourze\VolcanoArkApiBundle\Event\ApiKeyUsedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ApiKeyUsageSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
ApiKeyUsedEvent::class => 'onApiKeyUsed',
];
}
public function onApiKeyUsed(ApiKeyUsedEvent $event): void
{
$apiKey = $event->getApiKey();
$request = $event->getRequest();
// 执行您的自定义逻辑
}
}