Skip to content

Commit

Permalink
Add user config ability to save and fetch queries from db
Browse files Browse the repository at this point in the history
  • Loading branch information
joshembling committed Dec 3, 2023
1 parent 2adee3c commit a60ec9c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
5 changes: 5 additions & 0 deletions config/laragenie.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
'instructions' => 'Write only in markdown format. Only write factual data that can be pulled from indexed chunks.',
],

'database' => [
'fetch' => true,
'save' => true,
],

'openai' => [
'embedding' => [
'model' => 'text-embedding-ada-002',
Expand Down
24 changes: 14 additions & 10 deletions src/Commands/LaragenieCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ public function userQuestion(OpenAI\Client $openai, Pinecone $pinecone)

$formattedQuestion = $ai ? Str::remove('--ai', $question) : $question;

$laragenie = LaragenieModel::firstOrNew([
'question' => $formattedQuestion,
]);
if (config('laragenie.database.fetch') || config('laragenie.database.save')) {
$laragenie = LaragenieModel::firstOrNew([
'question' => $formattedQuestion,
]);
}

if ($laragenie->exists && ! $ai) {
if ($laragenie->exists && config('laragenie.database.fetch') && ! $ai) {
$this->info($laragenie->answer);
} else {
$this->question('Asking '.config('laragenie.bot.name').", '{$question}'...");
Expand All @@ -80,13 +82,15 @@ public function userQuestion(OpenAI\Client $openai, Pinecone $pinecone)
$tokens = $botResponse->usage->totalTokens;
$calculatedCost = $this->calculateCost($tokens);

$laragenie->fill([
'answer' => $answer,
'cost' => $calculatedCost,
'vectors' => $questionResponse['vectors'],
]);
if (config('laragenie.database.save')) {
$laragenie->fill([
'answer' => $answer,
'cost' => $calculatedCost,
'vectors' => $questionResponse['vectors'],
]);

$laragenie->save();
$laragenie->save();
}

$this->info($answer);
$this->costResponse($calculatedCost);
Expand Down

0 comments on commit a60ec9c

Please sign in to comment.