Skip to content

Commit

Permalink
Fix number of chunks that are sent to OpenAI, to match topK input
Browse files Browse the repository at this point in the history
  • Loading branch information
joshembling committed Feb 17, 2024
1 parent f27baad commit a45a31a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/Helpers/Chatbot.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public function askBot(string $question): array
}

return [
'data' => $pinecone_res->json()['matches'][0]['metadata']['text'],
'data' => $pinecone_res->json()['matches'],
'vectors' => $openai_res->embeddings[0]->toArray()['embedding'],
];
}

public function botResponse($chunks, string $question): CreateResponse
public function botResponse(string $chunks, string $question): CreateResponse
{
$this->textNote('Generating answer...');

Expand All @@ -46,7 +46,7 @@ public function botResponse($chunks, string $question): CreateResponse
'messages' => [
[
'role' => 'system',
'content' => config('laragenie.bot.instruction').$chunks,
'content' => config('laragenie.bot.instructions').$chunks,
],
[
'role' => 'user',
Expand Down
14 changes: 11 additions & 3 deletions src/Helpers/Questions.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,16 @@ public function userQuestion(string $user_question): void
if ($laragenie->exists && config('laragenie.database.fetch') && ! $ai) {
$this->textOutput($laragenie->answer);
} else {
$questionResponse = $this->askBot($formattedQuestion);
$botResponse = $this->botResponse($questionResponse['data'], $question);
$results = (array) $this->askBot($formattedQuestion);
$embeddings = [];

foreach ($results['data'] as $key => $result) {
$embeddings[$key] = $result['metadata']['text'];
}

$chunks = implode(',', $embeddings);

$botResponse = $this->botResponse($chunks, $question);

if ($botResponse) {
$answer = $botResponse->choices[0]->message->content;
Expand All @@ -41,7 +49,7 @@ public function userQuestion(string $user_question): void
$laragenie->fill([
'answer' => $answer,
'cost' => $calculatedCost,
'vectors' => $questionResponse['vectors'],
'vectors' => $results['vectors'],
]);

$laragenie->save();
Expand Down

0 comments on commit a45a31a

Please sign in to comment.