-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add output of estimated price of data
- Loading branch information
1 parent
f52d910
commit 05c6c6d
Showing
8 changed files
with
157 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace AIDemoData\Service\OpenAI; | ||
|
||
class OpenAIUsageTracker | ||
{ | ||
private static ?OpenAIUsageTracker $instance = null; | ||
|
||
/** | ||
* @var array<mixed> | ||
*/ | ||
private array $requests = []; | ||
|
||
/** | ||
* @var array<mixed> | ||
*/ | ||
private array $missingModelPrices = []; | ||
|
||
|
||
public static function getInstance(): OpenAIUsageTracker | ||
{ | ||
if (!self::$instance instanceof OpenAIUsageTracker) { | ||
self::$instance = new self(); | ||
} | ||
|
||
return self::$instance; | ||
} | ||
|
||
|
||
public function addRequest(string $prompt, float $costUSD): void | ||
{ | ||
$this->requests[] = [ | ||
'prompt' => $prompt, | ||
'costUSD' => $costUSD, | ||
]; | ||
} | ||
|
||
public function addMissingPrices(string $prompt, string $model): void | ||
{ | ||
$this->requests[] = [ | ||
'prompt' => $prompt, | ||
'costUSD' => 0, | ||
]; | ||
|
||
$this->missingModelPrices[] = $model; | ||
} | ||
|
||
public function getRequestCount(): int | ||
{ | ||
return count($this->requests); | ||
} | ||
|
||
/** | ||
* Gets the total costs rounded to 2 decimal places | ||
*/ | ||
public function getTotalCostsUSD(): float | ||
{ | ||
$totalCostsUSD = 0; | ||
foreach ($this->requests as $cost) { | ||
$totalCostsUSD += $cost['costUSD']; | ||
} | ||
|
||
return $totalCostsUSD; | ||
} | ||
|
||
/** | ||
* @return mixed[] | ||
*/ | ||
public function getMissingModelPrices(): array | ||
{ | ||
return $this->missingModelPrices; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"gpt-4": { | ||
"input": 0.00003, | ||
"output": 0.00006 | ||
}, | ||
"gpt-4-turbo": { | ||
"input": 0.00001, | ||
"output": 0.00003 | ||
}, | ||
"gpt-3.5-turbo": { | ||
"input": 0.0000015, | ||
"output": 0.000002 | ||
}, | ||
"gpt-3.5-turbo-instruct": { | ||
"input": 0.0000015, | ||
"output": 0.000002 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace AIDemoData\Traits; | ||
|
||
use AIDemoData\Service\OpenAI\OpenAIUsageTracker; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
trait CommandOutputTrait | ||
{ | ||
protected function showOpenAIUsageData(OutputInterface $output): void | ||
{ | ||
$tracker = OpenAIUsageTracker::getInstance(); | ||
|
||
$output->writeln("\n=== OpenAI Usage Summary ====================="); | ||
$output->writeln("Total Requests: " . $tracker->getRequestCount()); | ||
$output->writeln("Estimated Costs: " . $tracker->getTotalCostsUSD() . " USD (approx.)"); | ||
if (count($tracker->getMissingModelPrices()) > 0) { | ||
$output->writeln("Missing Model Prices: " . implode(', ', $tracker->getMissingModelPrices())); | ||
} | ||
$output->writeln("==============================================\n"); | ||
} | ||
} |