Skip to content

Commit

Permalink
Draft(SK): Livewire scaffolding - coming soon
Browse files Browse the repository at this point in the history
  • Loading branch information
PapaRascal2020 committed Oct 2, 2024
1 parent bc3807b commit 1aac40b
Show file tree
Hide file tree
Showing 24 changed files with 957 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Drivers/OpenAi.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public function embedding(): Embedding
public function moderate(): Moderate
{
return new Moderate(
url: "{$this->baseUrl}/moderation's",
url: "{$this->baseUrl}/moderations",
headers: $this->headers
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Utilities/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function store(string $data, string $mimeType = null): string
file_put_contents($filePath, $data);

// Return the local path of the stored file
return $filePath;
return asset('uploads/' . $filename);
}

/**
Expand Down
43 changes: 43 additions & 0 deletions stubs/livewire/app/Livewire/Sidekick/AudioGeneration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Livewire\Sidekick;

use Livewire\Component;
use PapaRascalDev\Sidekick\Drivers\OpenAi;

class AudioGeneration extends Component
{
public $errors = null;

public $prompt = '';
public $audio = null;
public $savedFile = null;

public $model = 'tts-1';

public function submit()
{
$response = sidekick(new OpenAi)->audio()->fromText(
model: 'tts-1',
text: $this->prompt);

$response_json = json_decode($response, true);

if(isset($response_json['error'])) {
$this->errors = $response_json['error']['message'];
} else {
$this->audio = base64_encode($response);
$this->savedFile = sidekick(new OpenAi)->utilities()->store($response, 'audio/mpeg');
}
}

public function render()
{
return view('livewire.audio-generation')
->layout('Components.layouts.app', [
'title' => 'Audio Generation',
'conversations' => sidekickConversation()->database()->all('id', 'model')
]
);
}
}
47 changes: 47 additions & 0 deletions stubs/livewire/app/Livewire/Sidekick/Chat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Livewire\Sidekick;

use Livewire\Component;

class Chat extends Component
{

public $prompt = '';
public $config = '';


public function submit()
{

$config = json_decode($this->config, true);

if(!$config)
{
$config = [
"engine" => "\PapaRascalDev\Sidekick\Drivers\OpenAi",
"model" => "gpt-3.5-turbo"
];
}

// This is the system prompt the user wants the AI to use
$systemPrompt = $this->prompt;


$conversation = sidekickConversation()->begin(
driver: new $config['engine'](),
model: $config['model'],
systemPrompt: $systemPrompt
);

return redirect()->to('/sidekick/chat/' . $conversation->id);
}

public function render()
{
return view('livewire.chat')->layout('Components.layouts.app', [
'title' => 'Chat',
'conversations' => sidekickConversation()->database()->all('id', 'model')
]);
}
}
93 changes: 93 additions & 0 deletions stubs/livewire/app/Livewire/Sidekick/ChatRoom.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace App\Livewire\Sidekick;

use Livewire\Component;
use Livewire\Attributes\On;
use Illuminate\Support\Collection;

class ChatRoom extends Component
{
public $conversationId;
public $config;
public Collection $messages;
public $newMessage;
public $isStreaming = false;
public $streamingResponse = '';

public function mount($id = null)
{
$this->messages = collect();
if ($id) {
$this->loadConversation($id);
}
}

public function loadConversation($id)
{
$conversation = sidekickConversation()->resume($id);
$this->conversationId = $conversation->model->id;
$this->config = [
'engine' => $conversation->model->class,
'model' => $conversation->model->model,
];
$this->messages = collect($conversation->model->messages)->map(function ($message) {
return (object) $message;
});
}

public function sendMessage()
{
$this->messages->push((object) [
'role' => 'user',
'content' => $this->newMessage
]);

if ($this->isStreaming) {
$this->streamingResponse = '';
$this->dispatch('startStreaming');
} else {
$response = sidekickConversation()
->resume($this->conversationId)
->sendMessage($this->newMessage, false);

$this->messages->push((object) [
'role' => 'assistant',
'content' => $response
]);
}

$this->newMessage = '';
$this->dispatch('messageAdded');
}

#[On('streamChunk')]
public function handleStreamChunk($chunk)
{
$this->streamingResponse .= $chunk;
}

#[On('endStreaming')]
public function endStreaming()
{
$this->messages->push((object) [
'role' => 'assistant',
'content' => $this->streamingResponse
]);
$this->streamingResponse = '';
}

public function delete($id)
{
sidekickConversation()->delete($id);
return redirect()->to('/sidekick/chat');
}

public function render()
{
return view('livewire.chat-room')->layout('Components.layouts.app', [
'title' => 'Chat',
'conversations' => sidekickConversation()->database()->all('id', 'model')
]);
}
}
36 changes: 36 additions & 0 deletions stubs/livewire/app/Livewire/Sidekick/Completion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Livewire\Sidekick;

use Livewire\Component;
use PapaRascalDev\Sidekick\Drivers\OpenAi;

class Completion extends Component
{
public $errors = null;

public $prompt = '';
public $response = null;

public function submit()
{

$this->response = sidekick(new OpenAi)->complete(
model: 'gpt-3.5-turbo',
systemPrompt: 'You are a knowledge base, please answer there questions',
message: $this->prompt
);


$this->prompt = '';

}

public function render()
{
return view('livewire.completion')->layout('Components.layouts.app', [
'title' => 'Completion',
'conversations' => sidekickConversation()->database()->all('id', 'model')
]);
}
}
36 changes: 36 additions & 0 deletions stubs/livewire/app/Livewire/Sidekick/Embeddding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Livewire\Sidekick;

use Livewire\Component;
use PapaRascalDev\Sidekick\Drivers\OpenAi;

class Embeddding extends Component
{

public $errors = null;

public $prompt = '';
public $response = null;

public function submit()
{

$this->response = sidekick(new OpenAi)->embedding()->make(
model:'text-embedding-3-large',
input: $this->prompt,
);


$this->prompt = '';

}

public function render()
{
return view('livewire.embeddding')->layout('Components.layouts.app', [
'title' => 'Embedding',
'conversations' => sidekickConversation()->database()->all('id', 'model')
]);
}
}
47 changes: 47 additions & 0 deletions stubs/livewire/app/Livewire/Sidekick/ImageGeneration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Livewire\Sidekick;

use Livewire\Component;
use PapaRascalDev\Sidekick\Drivers\OpenAi;

class ImageGeneration extends Component
{

public $prompt = '';
public $image = null;
public $savedFile = null;

public $errors = null;

public $model = 'dall-e-3';

public function submit()
{
$response = sidekick(new OpenAi)->image()->make(
model:$this->model,
prompt: $this->prompt,
width:'1024',
height:'1024'
);

$this->prompt = '';

if(isset($response['data'][0]['url'])) {
$this->image = $response['data'][0]['url'];
$this->savedFile = sidekick(new OpenAi)->utilities()->store($response['data'][0]['url'], 'image/png');
} else {
$this->errors = $response['error']['message'];
}
}

public function render()
{
return view('livewire.image-generation')
->layout('Components.layouts.app', [
'title' => 'Image Generation',
'conversations' => sidekickConversation()->database()->all('id', 'model')
]
);
}
}
16 changes: 16 additions & 0 deletions stubs/livewire/app/Livewire/Sidekick/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Livewire\Sidekick;

use Livewire\Component;

class Index extends Component
{
public function render()
{
return view('livewire.index')->layout('Components.layouts.app', [
'title' => '',
'conversations' => sidekickConversation()->database()->all('id', 'model')
]);
}
}
28 changes: 28 additions & 0 deletions stubs/livewire/app/Livewire/Sidekick/Moderation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Livewire\Sidekick;

use Livewire\Component;
use PapaRascalDev\Sidekick\Drivers\OpenAi;

class Moderation extends Component
{
public $response = null;
public $prompt = '';

public function submit()
{
$this->response = sidekick(new OpenAi)->moderate()->text(
model:'text-moderation-latest',
content: $this->prompt
);
}

public function render()
{
return view('livewire.moderation')->layout('Components.layouts.app', [
'title' => 'Moderation',
'conversations' => sidekickConversation()->database()->all('id', 'model')
]);
}
}
Loading

0 comments on commit 1aac40b

Please sign in to comment.