Skip to content

Commit

Permalink
Going to see if I can try a few times since the API, even in playground
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed May 23, 2023
1 parent 60a7fdd commit 74ab422
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
17 changes: 15 additions & 2 deletions app/LLMModels/OpenAi/ClientWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function generateImage($prompt): string
return $content;
}

public function projectChat(Project $project, User $user, array $messages): string|\Exception
public function projectChat(Project $project, User $user, array $messages, $tries = 1): string|\Exception
{
if (config('openai.mock')) {
$data = get_fixture('completion_response.json');
Expand Down Expand Up @@ -153,11 +153,24 @@ public function projectChat(Project $project, User $user, array $messages): stri

return implode("\n", $data);
} catch (\Exception $e) {

logger('Error talking to api', [
$e->getMessage(),
]);

return 'Error with API try again later';
if ($tries > 2) {
return 'Error with API try again later';
} else {
$tries = $tries + 1;
$this->projectChat(
$project,
$user,
$messages,
$tries
);

return 'Trying again due to error';
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion resources/js/Pages/Projects/Show.vue
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ const submit = () => {
project: props.project.id
}), pickBy(form))
.then(data => {
toast("See results")
toast("Results should appear soon...")
form.processing = false;
form.question = "";
})
Expand Down
7 changes: 6 additions & 1 deletion tests/Feature/ClientWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public function test_exception()
'type' => 'invalid_request_error',
'code' => null,
]),
new \OpenAI\Exceptions\ErrorException([
'message' => 'The model `gpt-1` does not exist',
'type' => 'invalid_request_error',
'code' => null,
]),
]);

$response = ClientWrapper::projectChat(
Expand All @@ -99,7 +104,7 @@ public function test_exception()
]
);

$this->assertEquals('Error with API try again later', $response);
$this->assertEquals('Trying again due to error', $response);

}
}

0 comments on commit 74ab422

Please sign in to comment.