-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from outl1ne/Add-threads-capability-to-openai
feat: add support for creating threads in OpenAI capabilities
- Loading branch information
Showing
31 changed files
with
1,585 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Outl1ne\NovaOpenAI\Capabilities\Assistants; | ||
|
||
use Outl1ne\NovaOpenAI\Capabilities\Capability; | ||
|
||
class Assistants extends Capability | ||
{ | ||
public function create( | ||
string $model, | ||
?string $name = null, | ||
?string $description = null, | ||
?string $instructions = null, | ||
?array $tools = null, | ||
?array $fileIds = null, | ||
?array $metadata = null, | ||
) { | ||
return (new CreateAssistant($this->openAI))->makeRequest( | ||
$model, | ||
$name, | ||
$description, | ||
$instructions, | ||
$tools, | ||
$fileIds, | ||
$metadata, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace Outl1ne\NovaOpenAI\Capabilities\Assistants; | ||
|
||
use Exception; | ||
use Outl1ne\NovaOpenAI\OpenAI; | ||
use Outl1ne\NovaOpenAI\Models\OpenAIRequest; | ||
use Outl1ne\NovaOpenAI\Capabilities\Measurable; | ||
use Outl1ne\NovaOpenAI\Capabilities\Assistants\Responses\AssistantResponse; | ||
|
||
class CreateAssistant | ||
{ | ||
use Measurable; | ||
|
||
protected OpenAIRequest $request; | ||
|
||
public function __construct( | ||
protected OpenAI $openAI, | ||
) { | ||
$this->request = new OpenAIRequest; | ||
$this->request->method = 'assistants'; | ||
$this->request->arguments = []; | ||
} | ||
|
||
public function pending() | ||
{ | ||
$this->measure(); | ||
|
||
$this->request->status = 'pending'; | ||
$this->request->save(); | ||
|
||
return $this->request; | ||
} | ||
|
||
public function makeRequest( | ||
string $model, | ||
?string $name = null, | ||
?string $description = null, | ||
?string $instructions = null, | ||
?array $tools = null, | ||
?array $fileIds = null, | ||
?array $metadata = null, | ||
): AssistantResponse { | ||
$this->request->appendArgument('model', $model); | ||
$this->request->appendArgument('name', $name); | ||
$this->request->appendArgument('description', $description); | ||
$this->request->appendArgument('instructions', $instructions); | ||
$this->request->appendArgument('tools', $tools); | ||
$this->request->appendArgument('file_ids', $fileIds); | ||
$this->request->appendArgument('metadata', $metadata); | ||
|
||
$this->pending(); | ||
|
||
try { | ||
$response = $this->openAI->http()->withHeader('Content-Type', 'application/json')->post("assistants", [ | ||
...$this->request->arguments, | ||
]); | ||
$response->throw(); | ||
|
||
return $this->handleResponse(new AssistantResponse($response)); | ||
} catch (Exception $e) { | ||
$this->handleException($e); | ||
} | ||
} | ||
|
||
protected function handleResponse(AssistantResponse $response) | ||
{ | ||
$this->request->time_sec = $this->measure(); | ||
$this->request->status = 'success'; | ||
$this->request->meta = $response->meta; | ||
$this->request->save(); | ||
|
||
return $response; | ||
} | ||
|
||
public function handleException(Exception $e) | ||
{ | ||
$this->request->time_sec = $this->measure(); | ||
$this->request->status = 'error'; | ||
$this->request->error = $e->getMessage(); | ||
$this->request->save(); | ||
|
||
throw $e; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/Capabilities/Assistants/Responses/AssistantResponse.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Outl1ne\NovaOpenAI\Capabilities\Assistants\Responses; | ||
|
||
use Illuminate\Http\Client\Response; | ||
use Outl1ne\NovaOpenAI\Capabilities\Responses\AppendsMeta; | ||
|
||
class AssistantResponse | ||
{ | ||
use AppendsMeta; | ||
|
||
public string $id; | ||
public string $model; | ||
|
||
public function __construct( | ||
protected readonly Response $response, | ||
) { | ||
$data = $response->json(); | ||
|
||
$this->id = $data['id']; | ||
$this->model = $data['model']; | ||
$this->appendMeta('id', $data['id']); | ||
$this->appendMeta('object', $data['object']); | ||
$this->appendMeta('created_at', $data['created_at']); | ||
$this->appendMeta('name', $data['name']); | ||
$this->appendMeta('description', $data['description']); | ||
$this->appendMeta('model', $data['model']); | ||
$this->appendMeta('instructions', $data['instructions']); | ||
$this->appendMeta('tools', $data['tools']); | ||
$this->appendMeta('file_ids', $data['file_ids']); | ||
$this->appendMeta('metadata', $data['metadata']); | ||
} | ||
|
||
public function response() | ||
{ | ||
return $this->response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace Outl1ne\NovaOpenAI\Capabilities\Threads; | ||
|
||
use Exception; | ||
use Outl1ne\NovaOpenAI\OpenAI; | ||
use Outl1ne\NovaOpenAI\Models\OpenAIRequest; | ||
use Outl1ne\NovaOpenAI\Capabilities\Measurable; | ||
use Outl1ne\NovaOpenAI\Capabilities\Threads\Parameters\Message; | ||
use Outl1ne\NovaOpenAI\Capabilities\Threads\Responses\MessageResponse; | ||
|
||
class CreateMessage | ||
{ | ||
use Measurable; | ||
|
||
protected OpenAIRequest $request; | ||
|
||
public function __construct( | ||
protected OpenAI $openAI, | ||
) { | ||
$this->request = new OpenAIRequest; | ||
$this->request->method = 'threads'; | ||
$this->request->arguments = []; | ||
} | ||
|
||
public function pending() | ||
{ | ||
$this->measure(); | ||
|
||
$this->request->status = 'pending'; | ||
$this->request->save(); | ||
|
||
return $this->request; | ||
} | ||
|
||
public function makeRequest( | ||
string $threadId, | ||
Message $message, | ||
): MessageResponse { | ||
$this->request->appendArgument('role', $message->role); | ||
$this->request->appendArgument('content', $message->content); | ||
$this->request->appendArgument('file_ids', $message->fileIds); | ||
$this->request->appendArgument('metadata', $message->metadata); | ||
|
||
$this->pending(); | ||
|
||
try { | ||
$response = $this->openAI->http()->withHeader('Content-Type', 'application/json')->post("threads/{$threadId}/messages", [ | ||
...$this->request->arguments, | ||
]); | ||
$response->throw(); | ||
|
||
return $this->handleResponse(new MessageResponse($response)); | ||
} catch (Exception $e) { | ||
$this->handleException($e); | ||
} | ||
} | ||
|
||
protected function handleResponse(MessageResponse $response) | ||
{ | ||
$this->request->time_sec = $this->measure(); | ||
$this->request->status = 'success'; | ||
$this->request->output = $response->content; | ||
$this->request->meta = $response->meta; | ||
$this->request->save(); | ||
|
||
return $response; | ||
} | ||
|
||
public function handleException(Exception $e) | ||
{ | ||
$this->request->time_sec = $this->measure(); | ||
$this->request->status = 'error'; | ||
$this->request->error = $e->getMessage(); | ||
$this->request->save(); | ||
|
||
throw $e; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace Outl1ne\NovaOpenAI\Capabilities\Threads; | ||
|
||
use Exception; | ||
use Outl1ne\NovaOpenAI\OpenAI; | ||
use Outl1ne\NovaOpenAI\Models\OpenAIRequest; | ||
use Outl1ne\NovaOpenAI\Capabilities\Measurable; | ||
use Outl1ne\NovaOpenAI\Capabilities\Threads\Responses\RunResponse; | ||
|
||
class CreateRun | ||
{ | ||
use Measurable; | ||
|
||
protected OpenAIRequest $request; | ||
|
||
public function __construct( | ||
protected OpenAI $openAI, | ||
) { | ||
$this->request = new OpenAIRequest; | ||
$this->request->method = 'threads'; | ||
$this->request->arguments = []; | ||
} | ||
|
||
public function pending() | ||
{ | ||
$this->measure(); | ||
|
||
$this->request->status = 'pending'; | ||
$this->request->save(); | ||
|
||
return $this->request; | ||
} | ||
|
||
public function makeRequest( | ||
string $threadId, | ||
string $assistantId, | ||
?string $model = null, | ||
?string $instructions = null, | ||
?string $additionalInstructions = null, | ||
?array $tools = null, | ||
?array $metadata = null, | ||
): RunResponse { | ||
$this->request->model_requested = $model; | ||
$this->request->appendArgument('assistant_id', $assistantId); | ||
$this->request->appendArgument('model', $model); | ||
$this->request->appendArgument('instructions', $instructions); | ||
$this->request->appendArgument('additional_instructions', $additionalInstructions); | ||
$this->request->appendArgument('tools', $tools); | ||
$this->request->appendArgument('metadata', $metadata); | ||
|
||
$this->pending(); | ||
|
||
try { | ||
$response = $this->openAI->http()->withHeader('Content-Type', 'application/json')->post("threads/{$threadId}/runs", [ | ||
...$this->request->arguments, | ||
]); | ||
$response->throw(); | ||
|
||
return $this->handleResponse(new RunResponse($response)); | ||
} catch (Exception $e) { | ||
$this->handleException($e); | ||
} | ||
} | ||
|
||
protected function handleResponse(RunResponse $response) | ||
{ | ||
$this->request->time_sec = $this->measure(); | ||
$this->request->status = 'success'; | ||
$this->request->meta = $response->meta; | ||
$this->request->save(); | ||
|
||
return $response; | ||
} | ||
|
||
public function handleException(Exception $e) | ||
{ | ||
$this->request->time_sec = $this->measure(); | ||
$this->request->status = 'error'; | ||
$this->request->error = $e->getMessage(); | ||
$this->request->save(); | ||
|
||
throw $e; | ||
} | ||
} |
Oops, something went wrong.