Skip to content

Commit

Permalink
feat(Conversations): Basic error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
PapaRascal2020 committed Sep 1, 2024
1 parent 7e03014 commit 00650b7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
## Sidekick for Laravel
Say hello to Sidekick! A Laravel plugin that provides a common syntax for using Claude, Mistral and OpenAi APIs.

![Latest Version](https://img.shields.io/badge/Version-0.1.0-blue)
![Stability](https://img.shields.io/badge/Stability-beta-red)
![Latest Version](https://img.shields.io/badge/Version-0.1.1-blue)
![Stability](https://img.shields.io/badge/Stability-beta-yellow)

### Upcoming Features

Expand Down
5 changes: 5 additions & 0 deletions src/Drivers/Claude.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ public function uniformedResponse($response)
{
return $response['content'][0]['text'];
}

public function validate($response): bool
{
return !($response['type'] == "error");
}
}
5 changes: 5 additions & 0 deletions src/Drivers/Mistral.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ public function uniformedResponse($response)
{
return $response['choices'][0]['message']['content'];
}

public function validate($response): bool
{
return !($response['object'] == "error");
}
}
5 changes: 5 additions & 0 deletions src/Drivers/OpenAi.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,9 @@ public function uniformedResponse($response)
{
return $response['choices'][0]['message']['content'];
}

public function validate($response): bool
{
return !isset($response['error']);
}
}
13 changes: 11 additions & 2 deletions src/SidekickConversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,18 @@ public function sendMessage(string $message) {
$newMessage
];

$this->conversation->messages()->create($newMessage);

$response = $this->sidekick->complete()->sendMessage(
model: $this->conversation->model,
systemPrompt: $this->conversation->system_prompt,
messages: $allMessages,
maxTokens: $this->conversation->max_tokens);

if(!$this->validResponse($response)) {
throw new \Exception(json_encode($response));
}

$this->conversation->messages()->create($newMessage);

$chatResponse = [
'role' => 'assistant',
'content' => $this->sidekick->uniformedResponse($response)
Expand All @@ -65,4 +69,9 @@ public function sendMessage(string $message) {
];
}

private function validResponse($response): bool
{
return $this->sidekick->validate($response);
}

}

0 comments on commit 00650b7

Please sign in to comment.