Skip to content

Commit

Permalink
Comments and refactoring web.php
Browse files Browse the repository at this point in the history
  • Loading branch information
PapaRascal2020 committed Sep 17, 2024
1 parent 6a28c52 commit a2a6a1a
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 37 deletions.
85 changes: 52 additions & 33 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use PapaRascalDev\Sidekick\Models\Conversation;
use \PapaRascalDev\Sidekick\Sidekick;
use Illuminate\Support\Facades\Route;
use \PapaRascalDev\Sidekick\Drivers\OpenAi;
use PapaRascalDev\Sidekick\Models\Conversation;
use PapaRascalDev\Sidekick\SidekickConversation;
use Symfony\Component\HttpFoundation\StreamedResponse;

Route::get('/sidekick/playground', function () {
return view('sidekick::sidekick-examples.index');
});

Route::post('/sidekick/playground/chat', function (Request $request) {
// These are the settings from the drop down on the front end.
$options = explode("|", $request->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],
Expand All @@ -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,
Expand All @@ -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');
});

Expand All @@ -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);
});
Expand All @@ -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)]);
});

Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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');
});

7 changes: 7 additions & 0 deletions src/Drivers/Claude.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ public function complete(): Completion
);
}

/**
* @return StreamedCompletion
*/
public function completeStreamed(): StreamedCompletion
{
return new StreamedCompletion(
Expand All @@ -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'] ?? "";
Expand Down
7 changes: 7 additions & 0 deletions src/Drivers/Cohere.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ public function complete(): Completion
);
}

/**
* @return StreamedCompletion
*/
public function completeStreamed(): StreamedCompletion
{
return new StreamedCompletion(
Expand All @@ -115,6 +118,10 @@ public function getResponse($response)
return $response['text'] ?? "";
}

/**
* @param $response
* @return string
*/
public function getStreamedText($response)
{
return $response['text'] ?? "";
Expand Down
12 changes: 12 additions & 0 deletions src/Drivers/Mistral.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand All @@ -142,6 +150,10 @@ public function getErrorMessage($response)
];
}

/**
* @param $response
* @return bool
*/
public function validate($response): bool
{
return !($response['object'] == "error");
Expand Down
37 changes: 37 additions & 0 deletions src/Drivers/OpenAi.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ function __construct()
];
}

/**
* @return Image
*/
public function image(): Image
{
return new Image(
Expand All @@ -91,6 +94,9 @@ public function image(): Image
);
}

/**
* @return Audio
*/
public function audio(): Audio
{
return new Audio(
Expand All @@ -99,6 +105,9 @@ public function audio(): Audio
);
}

/**
* @return Transcribe
*/
public function transcribe(): Transcribe
{
return new Transcribe(
Expand All @@ -107,6 +116,9 @@ public function transcribe(): Transcribe
);
}

/**
* @return Completion
*/
public function complete(): Completion
{
return new Completion(
Expand All @@ -124,6 +136,9 @@ public function complete(): Completion
);
}

/**
* @return StreamedCompletion
*/
public function completeStreamed(): StreamedCompletion
{
return new StreamedCompletion(
Expand All @@ -141,6 +156,9 @@ public function completeStreamed(): StreamedCompletion
);
}

/**
* @return Embedding
*/
public function embedding(): Embedding
{
return new Embedding(
Expand All @@ -149,6 +167,9 @@ public function embedding(): Embedding
);
}

/**
* @return Moderate
*/
public function moderate(): Moderate
{
return new Moderate(
Expand All @@ -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 [
Expand All @@ -179,6 +212,10 @@ public function getErrorMessage($response)
];
}

/**
* @param $response
* @return bool
*/
public function validate($response): bool
{
return !isset($response['error']);
Expand Down
11 changes: 7 additions & 4 deletions src/SidekickConversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a2a6a1a

Please sign in to comment.