Skip to content

Commit

Permalink
WIP: Threads Runs and Thread Runs Steps endpoints added
Browse files Browse the repository at this point in the history
  • Loading branch information
gehrisandro committed Nov 10, 2023
1 parent b36c5a4 commit b8d62ce
Show file tree
Hide file tree
Showing 30 changed files with 1,872 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Contracts/Resources/ThreadsContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use OpenAI\Responses\Assistants\AssistantDeleteResponse;
use OpenAI\Responses\Assistants\AssistantListResponse;
use OpenAI\Responses\Assistants\AssistantResponse;
use OpenAI\Responses\Threads\Runs\ThreadRunResponse;
use OpenAI\Responses\Threads\ThreadDeleteResponse;
use OpenAI\Responses\Threads\ThreadListResponse;
use OpenAI\Responses\Threads\ThreadResponse;
Expand All @@ -20,6 +21,15 @@ interface ThreadsContract
*/
public function create(array $parameters): ThreadResponse;

/**
* Create a thread and run it in one request.
*
* @see https://platform.openai.com/docs/api-reference/runs/createThreadAndRun
*
* @param array<string, mixed> $parameters
*/
public function createAndRun(array $parameters): ThreadRunResponse;

/**
* Retrieves a thread.
*
Expand Down Expand Up @@ -58,4 +68,11 @@ public function list(array $parameters = []): ThreadListResponse;
* @see https://platform.openai.com/docs/api-reference/messages
*/
public function messages(): ThreadsMessagesContract;

/**
* Represents an execution run on a thread.
*
* @see https://platform.openai.com/docs/api-reference/runs
*/
public function runs(): ThreadsRunsContract;
}
78 changes: 78 additions & 0 deletions src/Contracts/Resources/ThreadsRunsContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace OpenAI\Contracts\Resources;

use OpenAI\Responses\Threads\Messages\ThreadMessageDeleteResponse;
use OpenAI\Responses\Threads\Messages\ThreadMessageListResponse;
use OpenAI\Responses\Threads\Messages\ThreadMessageResponse;
use OpenAI\Responses\Threads\Runs\ThreadRunListResponse;
use OpenAI\Responses\Threads\Runs\ThreadRunResponse;

interface ThreadsRunsContract
{
/**
* Create a run.
*
* @see https://platform.openai.com/docs/api-reference/runs/createRun
*
* @param array<string, mixed> $parameters
*/
public function create(string $threadId, array $parameters): ThreadRunResponse;

/**
* Retrieves a run.
*
* @see https://platform.openai.com/docs/api-reference/runs/getRun
*/
public function retrieve(string $threadId, string $runId): ThreadRunResponse;

/**
* Modifies a run.
*
* @see https://platform.openai.com/docs/api-reference/runs/modifyRun
*
* @param array<string, mixed> $parameters
*/
public function modify(string $threadId, string $runId, array $parameters): ThreadRunResponse;

/**
* This endpoint can be used to submit the outputs from the tool calls once they're all completed.
*
* @see https://platform.openai.com/docs/api-reference/runs/submitToolOutputs
*
* @param array<string, mixed> $parameters
*/
public function submitToolOutputs(string $threadId, string $runId, array $parameters): ThreadRunResponse;

/**
* Cancels a run that is `in_progress`.
*
* @see https://platform.openai.com/docs/api-reference/runs/cancelRun
*
* @param array<string, mixed> $parameters
*/
public function cancel(string $threadId, string $runId): ThreadRunResponse;

// /**
// * Delete an message.
// *
// * @see TBD - there is no documentation yet
// */
// public function delete(string $threadId, string $messageId): ThreadMessageDeleteResponse;

/**
* Returns a list of runs belonging to a thread.
*
* @see https://platform.openai.com/docs/api-reference/runs/listRuns
*
* @param array<string, mixed> $parameters
*/
public function list(string $threadId, array $parameters = []): ThreadRunListResponse;

/**
* Get steps attached to a run.
*
* @see https://platform.openai.com/docs/api-reference/runs/step-object
*/
public function steps(): ThreadsRunsStepsContract;
}
27 changes: 27 additions & 0 deletions src/Contracts/Resources/ThreadsRunsStepsContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace OpenAI\Contracts\Resources;

use OpenAI\Responses\Threads\Messages\Files\ThreadMessageFileListResponse;
use OpenAI\Responses\Threads\Messages\Files\ThreadMessageFileResponse;
use OpenAI\Responses\Threads\Runs\Steps\ThreadRunStepListResponse;
use OpenAI\Responses\Threads\Runs\Steps\ThreadRunStepResponse;

interface ThreadsRunsStepsContract
{
/**
* Retrieves a run step.
*
* @see https://platform.openai.com/docs/api-reference/runs/getRunStep
*/
public function retrieve(string $threadId, string $runId, string $stepId): ThreadRunStepResponse;

/**
* Returns a list of run steps belonging to a run.
*
* @see https://platform.openai.com/docs/api-reference/runs/listRunSteps
*
* @param array<string, mixed> $parameters
*/
public function list(string $threadId, string $runId, array $parameters = []): ThreadRunStepListResponse;
}
29 changes: 29 additions & 0 deletions src/Resources/Threads.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
use OpenAI\Contracts\Resources\ListAssistantsResponse;
use OpenAI\Contracts\Resources\ThreadsContract;
use OpenAI\Contracts\Resources\ThreadsMessagesContract;
use OpenAI\Contracts\Resources\ThreadsRunsContract;
use OpenAI\Responses\Assistants\AssistantDeleteResponse;
use OpenAI\Responses\Assistants\AssistantListResponse;
use OpenAI\Responses\Assistants\AssistantResponse;
use OpenAI\Responses\Threads\Runs\ThreadRunResponse;
use OpenAI\Responses\Threads\ThreadDeleteResponse;
use OpenAI\Responses\Threads\ThreadListResponse;
use OpenAI\Responses\Threads\ThreadResponse;
Expand Down Expand Up @@ -39,6 +41,23 @@ public function create(array $parameters): ThreadResponse
return ThreadResponse::from($response->data(), $response->meta());
}

/**
* Create a thread and run it in one request.
*
* @see https://platform.openai.com/docs/api-reference/runs/createThreadAndRun
*
* @param array<string, mixed> $parameters
*/
public function createAndRun(array $parameters): ThreadRunResponse
{
$payload = Payload::create('threads/runs', $parameters);

/** @var Response<array{created: int, data: array<int, array{url?: string, b64_json?: string}>}> $response */
$response = $this->transporter->requestObject($payload);

return ThreadRunResponse::from($response->data(), $response->meta());
}

/**
* Retrieves a thread.
*
Expand Down Expand Up @@ -112,4 +131,14 @@ public function messages(): ThreadsMessagesContract
{
return new ThreadsMessages($this->transporter);
}

/**
* Represents an execution run on a thread.
*
* @see https://platform.openai.com/docs/api-reference/runs
*/
public function runs(): ThreadsRunsContract
{
return new ThreadsRuns($this->transporter);
}
}
134 changes: 134 additions & 0 deletions src/Resources/ThreadsRuns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

declare(strict_types=1);

namespace OpenAI\Resources;

use OpenAI\Contracts\Resources\ListAssistantsResponse;
use OpenAI\Contracts\Resources\ThreadsMessagesContract;
use OpenAI\Contracts\Resources\ThreadsMessagesFilesContract;
use OpenAI\Contracts\Resources\ThreadsRunsContract;
use OpenAI\Contracts\Resources\ThreadsRunsStepsContract;
use OpenAI\Responses\Threads\Messages\ThreadMessageDeleteResponse;
use OpenAI\Responses\Threads\Messages\ThreadMessageListResponse;
use OpenAI\Responses\Threads\Messages\ThreadMessageResponse;
use OpenAI\Responses\Threads\Runs\ThreadRunListResponse;
use OpenAI\Responses\Threads\Runs\ThreadRunResponse;
use OpenAI\Responses\Threads\ThreadDeleteResponse;
use OpenAI\ValueObjects\Transporter\Payload;
use OpenAI\ValueObjects\Transporter\Response;

final class ThreadsRuns implements ThreadsRunsContract
{
use Concerns\Transportable;

/**
* Create a run.
*
* @see https://platform.openai.com/docs/api-reference/runs/createRun
*
* @param array<string, mixed> $parameters
*/
public function create(string $threadId, array $parameters): ThreadRunResponse
{
$payload = Payload::create('threads/'.$threadId.'/runs', $parameters);

/** @var Response<array{created: int, data: array<int, array{url?: string, b64_json?: string}>}> $response */
$response = $this->transporter->requestObject($payload);

return ThreadRunResponse::from($response->data(), $response->meta());
}

/**
* Retrieves a run.
*
* @see https://platform.openai.com/docs/api-reference/runs/getRun
*/
public function retrieve(string $threadId, string $runId): ThreadRunResponse
{
$payload = Payload::retrieve('threads/'.$threadId.'/runs', $runId);

/** @var Response<array{created: int, data: array<int, array{url?: string, b64_json?: string}>}> $response */
$response = $this->transporter->requestObject($payload);

return ThreadRunResponse::from($response->data(), $response->meta());
}

/**
* Modifies a run.
*
* @see https://platform.openai.com/docs/api-reference/runs/modifyRun
*
* @param array<string, mixed> $parameters
*/
public function modify(string $threadId, string $runId, array $parameters): ThreadRunResponse
{
$payload = Payload::modify('threads/'.$threadId.'/runs', $runId, $parameters);

/** @var Response<array{created: int, data: array<int, array{url?: string, b64_json?: string}>}> $response */
$response = $this->transporter->requestObject($payload);

return ThreadRunResponse::from($response->data(), $response->meta());
}

/**
* This endpoint can be used to submit the outputs from the tool calls once they're all completed.
*
* @see https://platform.openai.com/docs/api-reference/runs/submitToolOutputs
*
* @param array<string, mixed> $parameters
*/
public function submitToolOutputs(string $threadId, string $runId, array $parameters): ThreadRunResponse
{
$payload = Payload::create('threads/'.$threadId.'/runs/'.$runId.'/submit_tool_outputs', $parameters);

/** @var Response<array{created: int, data: array<int, array{url?: string, b64_json?: string}>}> $response */
$response = $this->transporter->requestObject($payload);

return ThreadRunResponse::from($response->data(), $response->meta());
}

/**
* Cancels a run that is `in_progress`.
*
* @see https://platform.openai.com/docs/api-reference/runs/cancelRun
*
* @param array<string, mixed> $parameters
*/
public function cancel(string $threadId, string $runId): ThreadRunResponse
{
$payload = Payload::cancel('threads/'.$threadId.'/runs', $runId);

/** @var Response<array{created: int, data: array<int, array{url?: string, b64_json?: string}>}> $response */
$response = $this->transporter->requestObject($payload);

return ThreadRunResponse::from($response->data(), $response->meta());
}

/**
* Returns a list of runs belonging to a thread.
*
* @see https://platform.openai.com/docs/api-reference/runs/listRuns
*
* @param array<string, mixed> $parameters
*/
public function list(string $threadId, array $parameters = []): ThreadRunListResponse
{
$payload = Payload::list('threads/'.$threadId.'/runs', $parameters);

/** @var Response<array{created: int, data: array<int, array{url?: string, b64_json?: string}>}> $response */
$response = $this->transporter->requestObject($payload);

return ThreadRunListResponse::from($response->data(), $response->meta());
}

/**
* Get steps attached to a run.
*
* @see https://platform.openai.com/docs/api-reference/runs/step-object
*/
public function steps(): ThreadsRunsStepsContract
{
return new ThreadsRunsSteps($this->transporter);
}
}
52 changes: 52 additions & 0 deletions src/Resources/ThreadsRunsSteps.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace OpenAI\Resources;

use OpenAI\Contracts\Resources\ListAssistantsResponse;
use OpenAI\Contracts\Resources\ThreadsMessagesFilesContract;
use OpenAI\Contracts\Resources\ThreadsRunsStepsContract;
use OpenAI\Responses\Threads\Messages\Files\ThreadMessageFileListResponse;
use OpenAI\Responses\Threads\Messages\Files\ThreadMessageFileResponse;
use OpenAI\Responses\Threads\Runs\Steps\ThreadRunStepListResponse;
use OpenAI\Responses\Threads\Runs\Steps\ThreadRunStepResponse;
use OpenAI\ValueObjects\Transporter\Payload;
use OpenAI\ValueObjects\Transporter\Response;

final class ThreadsRunsSteps implements ThreadsRunsStepsContract
{
use Concerns\Transportable;

/**
* Retrieves a run step.
*
* @see https://platform.openai.com/docs/api-reference/runs/getRunStep
*/
public function retrieve(string $threadId, string $runId, string $stepId): ThreadRunStepResponse
{
$payload = Payload::retrieve('threads/'.$threadId.'/runs/'.$runId.'/steps', $stepId);

/** @var Response<array{created: int, data: array<int, array{url?: string, b64_json?: string}>}> $response */
$response = $this->transporter->requestObject($payload);

return ThreadRunStepResponse::from($response->data(), $response->meta());
}

/**
* Returns a list of run steps belonging to a run.
*
* @see https://platform.openai.com/docs/api-reference/runs/listRunSteps
*
* @param array<string, mixed> $parameters
*/
public function list(string $threadId, string $runId, array $parameters = []): ThreadRunStepListResponse
{
$payload = Payload::list('threads/'.$threadId.'/runs/'.$runId.'/steps', $parameters);

/** @var Response<array{created: int, data: array<int, array{url?: string, b64_json?: string}>}> $response */
$response = $this->transporter->requestObject($payload);

return ThreadRunStepListResponse::from($response->data(), $response->meta());
}
}
Loading

0 comments on commit b8d62ce

Please sign in to comment.