Skip to content

Commit

Permalink
Merge pull request #8 from bootstrapguru/openai-chat-endpoint
Browse files Browse the repository at this point in the history
Add OpenAI Chat Endpoint
  • Loading branch information
vijaythecoder authored Jul 7, 2024
2 parents eb498b9 + ac5880f commit bb10e90
Show file tree
Hide file tree
Showing 24 changed files with 954 additions and 260 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
.env
docs/.vitepress/dist
docs/.vitepress/cache
/database/database.sqlite
6 changes: 3 additions & 3 deletions app/Commands/DroidCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ class DroidCommand extends Command
public function handle(): int
{
$onBoardingSteps = new OnBoardingSteps();
if (! $onBoardingSteps->isCompleted()) {
if (! $onBoardingSteps->isCompleted($this)) {
return self::FAILURE;
}

$chatAssistant = new ChatAssistant;
$thread = $chatAssistant->createThread();

$threadRun = $chatAssistant->createThread();
render(view('assistant', [
'answer' => 'How can I help you today?',
]));
Expand All @@ -40,7 +40,7 @@ public function handle(): int
break;
}

$chatAssistant->getAnswer($threadRun, $message);
$chatAssistant->getAnswer($thread, $message);
}

return self::SUCCESS;
Expand Down
28 changes: 28 additions & 0 deletions app/Models/Assistant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Models;

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

class Assistant extends Model
{
use HasFactory;

protected $fillable = [
'name',
'model',
'description',
'prompt',
'tools',
];

protected $casts = [
'tools' => 'array',
];

public function threads()
{
return $this->hasMany(Thread::class);
}
}
34 changes: 34 additions & 0 deletions app/Models/Message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Models;

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

class Message extends Model
{
use HasFactory;

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

protected $casts = [
'tool_calls' => 'array',
];

public function thread()
{
return $this->belongsTo(Thread::class);
}

public function tool()
{
return $this->belongsTo(Tool::class);
}
}
28 changes: 28 additions & 0 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Models;

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

class Project extends Model
{
use HasFactory;

protected $fillable = [
'path',
'assistant_id',
];

public function assistant(): BelongsTo
{
return $this->belongsTo(Assistant::class);
}

public function threads(): HasMany
{
return $this->hasMany(Thread::class);
}
}
32 changes: 32 additions & 0 deletions app/Models/Thread.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Models;

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

class Thread extends Model
{
use HasFactory;

protected $fillable = [
'assistant_id',
'title',
'folder_path',
];

public function assistant()
{
return $this->belongsTo(Assistant::class);
}

public function messages()
{
return $this->hasMany(Message::class);
}

public function project()
{
return $this->belongsTo(Project::class);
}
}
30 changes: 30 additions & 0 deletions app/Services/AIConnector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Services;

use Saloon\Http\Connector;
use Saloon\Traits\Plugins\AcceptsJson;
use Saloon\Traits\Plugins\AlwaysThrowOnErrors;

class AIConnector extends Connector
{
use AcceptsJson, AlwaysThrowOnErrors;

/**
* The Base URL of the API
*/
public function resolveBaseUrl(): string
{
return 'https://api.openai.com/v1';
}

/**
* Default headers for every request
*/
protected function defaultHeaders(): array
{
return [
'Authorization' => 'Bearer '.config('droid.api_key'),
];
}
}
Loading

0 comments on commit bb10e90

Please sign in to comment.