Skip to content

Commit

Permalink
Merge pull request #11 from outl1ne/Add-threads-capability-to-openai
Browse files Browse the repository at this point in the history
feat: add support for creating threads in OpenAI capabilities
  • Loading branch information
allantatter authored Mar 11, 2024
2 parents 3689e0f + 86263b8 commit 5475e02
Show file tree
Hide file tree
Showing 31 changed files with 1,585 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dist/css/entry.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions src/Capabilities/Assistants/Assistants.php
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,
);
}
}
85 changes: 85 additions & 0 deletions src/Capabilities/Assistants/CreateAssistant.php
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 src/Capabilities/Assistants/Responses/AssistantResponse.php
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;
}
}
79 changes: 79 additions & 0 deletions src/Capabilities/Threads/CreateMessage.php
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;
}
}
85 changes: 85 additions & 0 deletions src/Capabilities/Threads/CreateRun.php
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;
}
}
Loading

0 comments on commit 5475e02

Please sign in to comment.