Skip to content

Commit

Permalink
feat: Refactor do-while loop to while loop and add sleep seconds as e…
Browse files Browse the repository at this point in the history
…nv variable
  • Loading branch information
halilcosdu committed Apr 23, 2024
1 parent ebe5bc0 commit d27e082
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 37 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ return [
'api_key' => env('OPENAI_API_KEY'),
'organization' => env('OPENAI_ORGANIZATION'),
'request_timeout' => env('OPENAI_TIMEOUT'),
'sleep_seconds' => env('OPENAI_SLEEP_SECONDS'), // Sleep seconds between requests default .1
'models' => [
'thread' => \HalilCosdu\ChatBot\Models\Thread::class,
],
Expand Down
1 change: 1 addition & 0 deletions config/chatbot.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'api_key' => env('OPENAI_API_KEY'),
'organization' => env('OPENAI_ORGANIZATION'),
'request_timeout' => env('OPENAI_TIMEOUT'),
'sleep_seconds' => env('OPENAI_SLEEP_SECONDS'),
'models' => [
'thread' => \HalilCosdu\ChatBot\Models\Thread::class,
],
Expand Down
3 changes: 2 additions & 1 deletion src/Models/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
* @property int $owner_id
Expand All @@ -16,7 +17,7 @@ class Thread extends Model

protected $fillable = ['owner_id', 'subject', 'remote_thread_id'];

public function threadMessages(): \Illuminate\Database\Eloquent\Relations\HasMany
public function threadMessages(): HasMany
{
return $this->hasMany(ThreadMessage::class);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Models/ThreadMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
* @property int $thread_id
Expand All @@ -16,7 +17,7 @@ class ThreadMessage extends Model

protected $fillable = ['thread_id', 'role', 'content'];

public function thread(): \Illuminate\Database\Eloquent\Relations\BelongsTo
public function thread(): BelongsTo
{
return $this->belongsTo(Thread::class);
}
Expand Down
58 changes: 23 additions & 35 deletions src/Services/ChatBotService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace HalilCosdu\ChatBot\Services;

use HalilCosdu\ChatBot\Models\Thread;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Sleep;
use OpenAI\Client;

Expand All @@ -13,24 +16,16 @@ public function __construct(public Client $client)
//
}

public function index(mixed $ownerId = null, mixed $search = null, mixed $appends = null): \Illuminate\Contracts\Pagination\LengthAwarePaginator
public function index(mixed $ownerId = null, mixed $search = null, mixed $appends = null): LengthAwarePaginator
{
return Thread::query()
->when($search, function ($query) use ($search) {
$query->where('subject', 'like', "%{$search}%");
})
->when($ownerId, function ($query) use ($ownerId) {
return $query->where('owner_id', $ownerId);
})
->when($search, fn ($query) => $query->where('subject', 'like', "%{$search}%"))
->when($ownerId, fn ($query) => $query->where('owner_id', $ownerId))
->latest()
->when($appends, function ($query) use ($appends) {
return $query->paginate()->appends($appends);
}, function ($query) {
return $query->paginate();
});
->when($appends, fn ($query) => $query->paginate()->appends($appends), fn ($query) => $query->paginate());
}

public function create(string $subject, mixed $ownerId = null): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder
public function create(string $subject, mixed $ownerId = null): Model|Builder
{
$remoteThread = $this->client->threads()->create([
'messages' => [
Expand All @@ -51,11 +46,7 @@ public function create(string $subject, mixed $ownerId = null): \Illuminate\Data
'assistant_id' => config('chatbot.assistant_id'),
]);

do {
Sleep::sleep(0.1);

$run = $this->client->threads()->runs()->retrieve($remoteThread->id, $run->id);
} while ($run->status !== 'completed');
$this->waitForThreadRunCompletion($remoteThread->id, $run->id);

foreach ($this->client->threads()->messages()->list($remoteThread->id)->data as $message) {
$thread->threadMessages()->create([
Expand All @@ -69,24 +60,18 @@ public function create(string $subject, mixed $ownerId = null): \Illuminate\Data
return $thread;
}

public function show(int $id, mixed $ownerId = null): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder
public function show(int $id, mixed $ownerId = null): Model|Builder
{
return Thread::query()
->with('threadMessages')
->when($ownerId, function ($query) use ($ownerId) {
return $query->where('owner_id', $ownerId);
})
->when($ownerId, fn ($query) => $query->where('owner_id', $ownerId))
->findOrFail($id);
}

public function update(string $message, int $id, mixed $ownerId = null)
{
$thread = Thread::query()
->when($ownerId, function ($query) use ($ownerId) {
return $query->where('owner_id', $ownerId);
}, function ($query) {
return $query;
})
->when($ownerId, fn ($query) => $query->where('owner_id', $ownerId))
->findOrFail($id);

$thread->threadMessages()->create([
Expand All @@ -103,11 +88,7 @@ public function update(string $message, int $id, mixed $ownerId = null)
'assistant_id' => config('chatbot.assistant_id'),
]);

do {
Sleep::sleep(0.1);

$run = $this->client->threads()->runs()->retrieve($thread->remote_thread_id, $run->id);
} while ($run->status !== 'completed');
$this->waitForThreadRunCompletion($thread->remote_thread_id, $run->id);

$message = $this->client->threads()->messages()->list($thread->remote_thread_id)->data[0];

Expand All @@ -126,13 +107,20 @@ public function update(string $message, int $id, mixed $ownerId = null)
public function delete(int $id, mixed $ownerId = null): void
{
$thread = Thread::query()
->when($ownerId, function ($query) use ($ownerId) {
return $query->where('owner_id', $ownerId);
})
->when($ownerId, fn ($query) => $query->where('owner_id', $ownerId))
->findOrFail($id);

$this->client->threads()->delete($thread->remote_thread_id);

$thread->delete();
}

public function waitForThreadRunCompletion($remoteThreadId, $runId): void
{
do {
Sleep::sleep(config('chatbot.sleep_seconds', .1));

$run = $this->client->threads()->runs()->retrieve($remoteThreadId, $runId);
} while ($run->status !== 'completed');
}
}

0 comments on commit d27e082

Please sign in to comment.