From a2a6a1ab485b811a14ba8468905f860b38d85cf0 Mon Sep 17 00:00:00 2001 From: Ashley Johnson <61059402+PapaRascal2020@users.noreply.github.com> Date: Tue, 17 Sep 2024 18:01:09 +0100 Subject: [PATCH] Comments and refactoring web.php --- routes/web.php | 85 ++++++++++++++++++++++-------------- src/Drivers/Claude.php | 7 +++ src/Drivers/Cohere.php | 7 +++ src/Drivers/Mistral.php | 12 +++++ src/Drivers/OpenAi.php | 37 ++++++++++++++++ src/SidekickConversation.php | 11 +++-- 6 files changed, 122 insertions(+), 37 deletions(-) diff --git a/routes/web.php b/routes/web.php index 5b1fa13..e0ee9e8 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,28 +1,30 @@ get('engine')); + + // This is the system prompt the user wants the AI to use $systemPrompt = $request->get('prompt'); + + // Create a new instance of SideKick conv conversation $sidekick = new SidekickConversation(); + // Insert a new conversation into the database $conversation = $sidekick->begin( driver: new $options[0](), model: $options[1], systemPrompt: $systemPrompt ); + // Redirect the user to the main page for the conversation return view('sidekick::sidekick-examples.chatroom', [ 'conversationId' => $conversation->conversation->id, 'options' => $options[0], @@ -31,32 +33,28 @@ }); Route::post('/sidekick/playground/chat/update', function (Request $request) { + + // Get the AI driver $engine = $request->get('engine'); + + // Load a new instance of Sidekick Conversation $sidekick = new SidekickConversation(new $engine()); + // Resume the conversation $conversation = $sidekick->resume( conversationId: $request->get('conversation_id'), ); - if($request->get('stream')) { - return $conversation->sendMessage($request->get('message'), true); - } else { - $response = $conversation->sendMessage($request->get('message')); - - return response()->json([ - 'response' => $response, - 'options' => $engine - ]); - } + // Send a new message + return $conversation->sendMessage($request->get('message'), $request->get('stream')); }); -Route::get('/sidekick/playground/chat', function () { - return view('sidekick::sidekick-examples.chat'); -}); - Route::get('/sidekick/playground/chat/{id}', function (string $id) { + // Find the conversation in the database $conversation = Conversation::findOrFail($id); + + // Return the conversation to the browser return view('sidekick::sidekick-examples.chatroom', [ 'conversationId' => $conversation->id, 'options' => $conversation->class, @@ -65,8 +63,13 @@ }); Route::get('/sidekick/playground/chat/delete/{id}', function (string $id) { + // Find the conversation in the database $conversation = Conversation::findOrFail($id); + + // Delete the conversation $conversation->delete(); + + // Redirect to the main chat page. return redirect('/sidekick/playground/chat'); }); @@ -75,13 +78,17 @@ }); Route::post('/sidekick/playground/completion', function (Request $request) { + // Loads a new instance of Sidekick with OpenAI $sidekick = Sidekick::create(new OpenAi()); + + // Send message $response = $sidekick->complete()->sendMessage( model: 'gpt-3.5-turbo', systemPrompt: 'You are a knowledge base, please answer there questions', message: $request->get('message') ); + // Return valid and invalid response to the front end. return $sidekick->validate($response) ? $sidekick->getResponse($response) : $sidekick->getErrorMessage($response); }); @@ -91,13 +98,16 @@ }); Route::post('/sidekick/playground/audio', function (Request $request) { + // Loads a new instance of Sidekick with OpenAI $sidekick = Sidekick::create(new OpenAi()); + // Send text to be converted by Sidekick to audio $audio = $sidekick->audio()->fromText( model:'tts-1', text: $request->get('text_to_convert') ); + // Return the base64 encoded audio file to the front end return view('sidekick::sidekick-examples.audio', ['audio' => base64_encode($audio)]); }); @@ -113,14 +123,6 @@ return view('sidekick::sidekick-examples.image', ['image' => $image['data'][0]['url']]); }); -Route::get('/sidekick/playground/image', function () { - return view('sidekick::sidekick-examples.image'); -}); - -Route::get('/sidekick/playground/transcribe', function () { - return view('sidekick::sidekick-examples.transcribe'); -}); - Route::post('/sidekick/playground/transcribe', function (Request $request) { $sidekick = Sidekick::create(new OpenAi()); $response = $sidekick->transcribe()->audioFile( @@ -130,10 +132,6 @@ return view('sidekick::sidekick-examples.transcribe', ['response' => $response]); }); -Route::get('/sidekick/playground/embedding', function () { - return view('sidekick::sidekick-examples.embedding'); -}); - Route::post('/sidekick/playground/embedding', function (Request $request) { $sidekick = Sidekick::create(new OpenAi()); $response = $sidekick->embedding()->make( @@ -155,3 +153,24 @@ ); return view('sidekick::sidekick-examples.moderate', ['response' => $response]); }); + +Route::get('/sidekick/playground/image', function () { + return view('sidekick::sidekick-examples.image'); +}); + +Route::get('/sidekick/playground/transcribe', function () { + return view('sidekick::sidekick-examples.transcribe'); +}); + +Route::get('/sidekick/playground/embedding', function () { + return view('sidekick::sidekick-examples.embedding'); +}); + +Route::get('/sidekick/playground/chat', function () { + return view('sidekick::sidekick-examples.chat'); +}); + +Route::get('/sidekick/playground', function () { + return view('sidekick::sidekick-examples.index'); +}); + diff --git a/src/Drivers/Claude.php b/src/Drivers/Claude.php index ef7ac0a..555597f 100644 --- a/src/Drivers/Claude.php +++ b/src/Drivers/Claude.php @@ -89,6 +89,9 @@ public function complete(): Completion ); } + /** + * @return StreamedCompletion + */ public function completeStreamed(): StreamedCompletion { return new StreamedCompletion( @@ -115,6 +118,10 @@ public function getResponse($response) return $response['content'][0]['text']; } + /** + * @param $response + * @return string + */ public function getStreamedText($response) { return $response['delta']['text'] ?? ""; diff --git a/src/Drivers/Cohere.php b/src/Drivers/Cohere.php index 8b5a3ab..c084026 100644 --- a/src/Drivers/Cohere.php +++ b/src/Drivers/Cohere.php @@ -93,6 +93,9 @@ public function complete(): Completion ); } + /** + * @return StreamedCompletion + */ public function completeStreamed(): StreamedCompletion { return new StreamedCompletion( @@ -115,6 +118,10 @@ public function getResponse($response) return $response['text'] ?? ""; } + /** + * @param $response + * @return string + */ public function getStreamedText($response) { return $response['text'] ?? ""; diff --git a/src/Drivers/Mistral.php b/src/Drivers/Mistral.php index 93809c2..91544bd 100644 --- a/src/Drivers/Mistral.php +++ b/src/Drivers/Mistral.php @@ -125,11 +125,19 @@ public function getResponse($response) return $response['choices'][0]['message']['content']; } + /** + * @param $response + * @return string + */ public function getStreamedText($response) { return $response['choices'][0]['delta']['content'] ?? ""; } + /** + * @param $response + * @return array + */ public function getErrorMessage($response) { return [ @@ -142,6 +150,10 @@ public function getErrorMessage($response) ]; } + /** + * @param $response + * @return bool + */ public function validate($response): bool { return !($response['object'] == "error"); diff --git a/src/Drivers/OpenAi.php b/src/Drivers/OpenAi.php index 831365b..03e7455 100644 --- a/src/Drivers/OpenAi.php +++ b/src/Drivers/OpenAi.php @@ -83,6 +83,9 @@ function __construct() ]; } + /** + * @return Image + */ public function image(): Image { return new Image( @@ -91,6 +94,9 @@ public function image(): Image ); } + /** + * @return Audio + */ public function audio(): Audio { return new Audio( @@ -99,6 +105,9 @@ public function audio(): Audio ); } + /** + * @return Transcribe + */ public function transcribe(): Transcribe { return new Transcribe( @@ -107,6 +116,9 @@ public function transcribe(): Transcribe ); } + /** + * @return Completion + */ public function complete(): Completion { return new Completion( @@ -124,6 +136,9 @@ public function complete(): Completion ); } + /** + * @return StreamedCompletion + */ public function completeStreamed(): StreamedCompletion { return new StreamedCompletion( @@ -141,6 +156,9 @@ public function completeStreamed(): StreamedCompletion ); } + /** + * @return Embedding + */ public function embedding(): Embedding { return new Embedding( @@ -149,6 +167,9 @@ public function embedding(): Embedding ); } + /** + * @return Moderate + */ public function moderate(): Moderate { return new Moderate( @@ -157,16 +178,28 @@ public function moderate(): Moderate ); } + /** + * @param $response + * @return mixed + */ public function getResponse($response) { return $response['choices'][0]['message']['content']; } + /** + * @param $response + * @return string + */ public function getStreamedText($response) { return $response['choices'][0]['delta']['content'] ?? ""; } + /** + * @param $response + * @return array + */ public function getErrorMessage($response) { return [ @@ -179,6 +212,10 @@ public function getErrorMessage($response) ]; } + /** + * @param $response + * @return bool + */ public function validate($response): bool { return !isset($response['error']); diff --git a/src/SidekickConversation.php b/src/SidekickConversation.php index 7fd9a86..b1e8481 100644 --- a/src/SidekickConversation.php +++ b/src/SidekickConversation.php @@ -109,10 +109,13 @@ public function sendMessage(string $message, bool $streamed = false) { 'content' => $this->sidekick->getResponse($response) ]); - return [ - 'conversation_id' => $this->conversation->id, - 'messages' => $this->conversation->messages()->get()->toArray(), - ]; + return response()->json([ + 'response' => [ + 'conversation_id' => $this->conversation->id, + 'messages' => $this->conversation->messages()->get()->toArray() + ], + 'options' => get_class($this->sidekick) + ]); } return $this->sidekick->getErrorMessage($response);