Skip to content

Commit

Permalink
Refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
PapaRascal2020 committed Sep 9, 2024
1 parent f7740d7 commit fd25933
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 123 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ This allows you to create a chatbot that remembers previos interactions.
To start a new conversation:

```php
$sidekick = new SidekickConversation(new OpenAi());
$sidekick = new SidekickConversation();

$conversation = $sidekick->begin(
driver: new OpenAi(),
model: 'gtp-3.5-turbo',
systemPrompt: 'You can instruct the chatbot using this parameter'
);
Expand Down Expand Up @@ -138,7 +139,7 @@ An example of the formatted response can be found below:
Once you have this response, to continue the conversation you can write the following in your controller making sure to pass the `conversation_id` in the request:

```PHP
$sidekick = new SidekickConversation(new OpenAi());
$sidekick = new SidekickConversation();

$conversation = $sidekick->resume(
conversationId: $conversation_id
Expand Down
3 changes: 2 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@

Route::post('/sidekick/playground/chat', function (Request $request) {
$options = explode("|", $request->get('engine'));
$sidekick = new SidekickConversation(new $options[0]());
$sidekick = new SidekickConversation();

$conversation = $sidekick->begin(
driver: new $options[0](),
model: $options[1],
systemPrompt: 'Your Sidekick, a robot to chat to users'
);
Expand Down
20 changes: 20 additions & 0 deletions src/Drivers/Claude.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,32 @@ class Claude implements Driver
*/
protected array $headers;

/**
* Message Roles
*
* Some AI tools have different naming for
* user and bot roles so added this so it
* can be specified.
*
* @array $messageRoles
*/
public array $messageRoles = [
'user' => 'user',
'assistant' => 'assistant'
];


/**
* List As Object
*
* This is to specify if the chat history
* should be sent as an Object or Array
* to the payload.
*
* @array $listAsObject
*/
public bool $listAsObject = false;

public array $chatMaps = [];

function __construct()
Expand Down
21 changes: 20 additions & 1 deletion src/Drivers/Cohere.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Cohere implements Driver
{

/**
* OpenAi Api Base URL
* Api Base URL
* @strind $baseUrl
*/
private string $baseUrl = "https://api.cohere.com/v1";
Expand All @@ -35,11 +35,30 @@ class Cohere implements Driver
*/
protected array $headers;

/**
* Message Roles
*
* Some AI tools have different naming for
* user and bot roles so added this so it
* can be specified.
*
* @array $messageRoles
*/
public array $messageRoles = [
'user' => 'USER',
'assistant' => 'CHATBOT'
];

/**
* List As Object
*
* This is to specify if the chat history
* should be sent as an Object or Array
* to the payload.
*
* @array $listAsObject
*/

public bool $listAsObject = true;

public array $chatMaps = [
Expand Down
20 changes: 19 additions & 1 deletion src/Drivers/Mistral.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Mistral implements Driver
{

/**
* OpenAi Api Base URL
* Api Base URL
* @strind $baseUrl
*/
private string $baseUrl = "https://api.mistral.ai/v1";
Expand All @@ -34,11 +34,29 @@ class Mistral implements Driver
*/
protected array $headers;

/**
* Message Roles
*
* Some AI tools have different naming for
* user and bot roles so added this so it
* can be specified.
*
* @array $messageRoles
*/
public array $messageRoles = [
'user' => 'user',
'assistant' => 'assistant'
];

/**
* List As Object
*
* This is to specify if the chat history
* should be sent as an Object or Array
* to the payload.
*
* @array $listAsObject
*/
public bool $listAsObject = false;
public array $chatMaps = [];

Expand Down
22 changes: 19 additions & 3 deletions src/Drivers/OpenAi.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class OpenAi implements Driver
{

/**
* OpenAi Api Base URL
* Api Base URL
* @strind $baseUrl
*/
private string $baseUrl = "https://api.openai.com/v1";
Expand All @@ -47,11 +47,29 @@ class OpenAi implements Driver
*/
protected array $headers;

/**
* Message Roles
*
* Some AI tools have different naming for
* user and bot roles so added this so it
* can be specified.
*
* @array $messageRoles
*/
public array $messageRoles = [
'user' => 'user',
'assistant' => 'assistant'
];

/**
* List As Object
*
* This is to specify if the chat history
* should be sent as an Object or Array
* to the payload.
*
* @array $listAsObject
*/
public bool $listAsObject = false;

public array $chatMaps = [];
Expand All @@ -62,8 +80,6 @@ function __construct()

$this->headers = [
"Authorization" => "Bearer {$apiToken}",
"Content-Type" => "application/json",
"Accept" => "application/json",
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Features/Audio.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function __construct(protected string $url, protected array $headers)
public function fromText(
string $model,
string $text,
String $voice = "alloy"
String $voice = "alloy" //default voice as OpenAi is the only supported driver
): string
{
return Http::withHeaders($this->headers)
Expand Down
11 changes: 6 additions & 5 deletions src/Features/Completion.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,26 @@ public function sendMessage(
int $maxTokens = 1024
): array
{
// Takes request and maps it to the $this->requestRules
$request = [];
foreach ($this->requestRules as $key => $value) {
if (is_array($value)) {
$temp = [];
$arrayMap = [];
foreach ($value as $k => $val) {
if ($eval = eval("return $val;")) {
if(isset($eval['role'])) {
$temp[] = $eval;
$arrayMap[] = $eval;
} else {
$temp = [
...$temp,
$arrayMap = [
...$arrayMap,
...$eval
];
}
} else {
unset($value[$k]);
}
}
$request[$key] = [...$temp];
$request[$key] = [...$arrayMap];
} else {
$request[$key] = eval("return $value;");
}
Expand Down
101 changes: 0 additions & 101 deletions src/Helpers/StreamedResponse.php

This file was deleted.

19 changes: 11 additions & 8 deletions src/SidekickConversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,33 @@ class SidekickConversation
protected Driver $sidekick;
protected Conversation $conversation;

/**
* @param Driver $driver
*/
function __construct(Driver $driver)
{
$this->sidekick = Sidekick::create($driver);
}

/**
* @param string $conversationId
* @return $this
*/
public function resume(string $conversationId): static
{
$this->conversation = Conversation::findOrFail($conversationId);
$this->sidekick = Sidekick::create(new $this->conversation->class());
return $this;
}

/**
* @param Driver $driver
* @param string $model
* @param string $systemPrompt
* @param int $maxTokens
* @return $this
*/
public function begin(
Driver $driver,
string $model,
string $systemPrompt = '',
int $maxTokens = 1024
): static
{
$this->sidekick = Sidekick::create($driver);

$this->conversation = new Conversation();
$this->conversation->model = $model;
$this->conversation->class = get_class($this->sidekick);
Expand Down Expand Up @@ -92,6 +89,12 @@ public function sendMessage(string $message) {
return $this->sidekick->getErrorMessage($response);

}

/**
* @param array $messages
* @param array $mappings
* @return array
*/
public function toCustomArray(
array $messages,
array $mappings = [],
Expand Down

0 comments on commit fd25933

Please sign in to comment.