From 42f091cd2da69d85e9f740436820e02c09efd7a2 Mon Sep 17 00:00:00 2001 From: lf <1344453532@qq.com> Date: Tue, 4 Jun 2024 09:42:47 +0800 Subject: [PATCH 1/2] Add Vector Stores operation and add Vector Stores Files operation --- src/OpenAi.php | 134 +++++++++++++++++++++++++++++++++++++++++++++++-- src/Url.php | 18 +++++++ 2 files changed, 147 insertions(+), 5 deletions(-) diff --git a/src/OpenAi.php b/src/OpenAi.php index 87bfcbe..b0a67ec 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -720,7 +720,7 @@ public function createRun($threadId, $data, $stream = null) $this->stream_method = $stream; } - + $this->addAssistantsBetaHeader(); $url = Url::threadsUrl() . '/' . $threadId . '/runs'; $this->baseUrl($url); @@ -791,7 +791,7 @@ public function submitToolOutputs($threadId, $runId, $outputs, $stream = null) $this->stream_method = $stream; } - + $this->addAssistantsBetaHeader(); $url = Url::threadsUrl() . '/' . $threadId . '/runs/' . $runId . '/submit_tool_outputs'; $this->baseUrl($url); @@ -871,6 +871,130 @@ public function tts($opts) return $this->sendRequest($url, 'POST', $opts); } + /** + * @param $opts + * @return string + */ + public function vectorStores($opts) + { + $url = Url::vectorStoresUrl(); + $this->baseUrl($url); + + return $this->sendRequest($url, 'POST', $opts); + } + + /** + * @param array $opts + * @return string + */ + public function vectorStoresCreate(array $opts) + { + $url = Url::vectorStoresUrl(); + $this->addAssistantsBetaHeader(); + $this->baseUrl($url); + + return $this->sendRequest($url, 'POST', $opts); + } + + /** + * @param string $vectorStoresId + * @return string + */ + public function vectorStoresSearch(string $vectorStoresId) + { + $url = Url::vectorStoresUrl() . '/' . $vectorStoresId; + $this->addAssistantsBetaHeader(); + $this->baseUrl($url); + + + return $this->sendRequest($url, 'GET'); + } + + /** + * @param $opts + * @return string + */ + public function vectorStoresEdit(array $opts) + { + $url = Url::vectorStoresUrl(); + $this->addAssistantsBetaHeader(); + $this->baseUrl($url); + + return $this->sendRequest($url, 'POST', $opts); + } + + /** + * @param $opts + * @return string + */ + public function vectorStoresDelete(string $vectorStoresId) + { + $url = Url::vectorStoresUrl() . '/' . $vectorStoresId; + $this->addAssistantsBetaHeader(); + $this->baseUrl($url); + + return $this->sendRequest($url, 'DELETE'); + } + + /** + * @param string $vectorStoresId + * @param array $opts + * @return string + */ + public function vectorStoresFilesCreate(string $vectorStoresId, array $opts) + { + $url = Url::vectorStoresFilesUrl($vectorStoresId); + $this->addAssistantsBetaHeader(); + $this->baseUrl($url); + + return $this->sendRequest($url, 'POST', $opts); + } + + /** + * @param string $vectorStoresId + * @param array $query + * @return string + */ + public function vectorStoresFilesList(string $vectorStoresId, array $query) + { + $url = Url::vectorStoresFilesUrl($vectorStoresId); + if (count($query) > 0) { + $url .= '?' . http_build_query($query); + } + $this->addAssistantsBetaHeader(); + $this->baseUrl($url); + + return $this->sendRequest($url, 'GET'); + } + + /** + * @param string $vectorStoresId + * @param string $filesId + * @return string + */ + public function vectorStoresFilesSearch(string $vectorStoresId, string $filesId) + { + $url = Url::vectorStoresFilesUrl($vectorStoresId) . "/" . $filesId; + $this->addAssistantsBetaHeader(); + $this->baseUrl($url); + + return $this->sendRequest($url, 'GET'); + } + + /** + * @param string $vectorStoresId + * @param string $filesId + * @return string + */ + public function vectorStoresFilesDelete(string $vectorStoresId, string $filesId) + { + $url = Url::vectorStoresFilesUrl($vectorStoresId) . "/" . $filesId; + $this->addAssistantsBetaHeader(); + $this->baseUrl($url); + + return $this->sendRequest($url, 'DELETE'); + } + /** * @param int $timeout */ @@ -939,7 +1063,7 @@ public function setORG(string $org) $this->headers[] = "OpenAI-Organization: $org"; } } - + /** * @param string $org */ @@ -953,10 +1077,10 @@ public function setAssistantsBetaVersion(string $version) /** * @return void */ - private function addAssistantsBetaHeader(){ + private function addAssistantsBetaHeader(){ $this->headers[] = 'OpenAI-Beta: assistants='.$this->assistantsBetaVersion; } - + /** * @param string $url diff --git a/src/Url.php b/src/Url.php index f3c90ef..0dccc8a 100644 --- a/src/Url.php +++ b/src/Url.php @@ -187,4 +187,22 @@ public static function ttsUrl(): string { return self::OPEN_AI_URL . "/audio/speech"; } + + /** + * @param + * @return string + */ + public static function vectorStoresUrl(): string + { + return self::OPEN_AI_URL . "/vector_stores"; + } + + /** + * @param string $vectorStoresId + * @return string + */ + public static function vectorStoresFilesUrl(string $vectorStoresId): string + { + return self::OPEN_AI_URL . "/vector_stores/" . $vectorStoresId . "/files"; + } } From ca26a9faad75dd676387d3cdd76959214c1a87ea Mon Sep 17 00:00:00 2001 From: Orz <1344453532@qq.com> Date: Tue, 4 Jun 2024 12:43:11 +0800 Subject: [PATCH 2/2] Add Vector Stores operation and add Vector Stores Files operation --- src/OpenAi.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/OpenAi.php b/src/OpenAi.php index b0a67ec..f048e0a 100644 --- a/src/OpenAi.php +++ b/src/OpenAi.php @@ -872,28 +872,33 @@ public function tts($opts) } /** - * @param $opts + * @param array $opts * @return string */ - public function vectorStores($opts) + public function vectorStoresCreate(array $opts) { $url = Url::vectorStoresUrl(); + $this->addAssistantsBetaHeader(); $this->baseUrl($url); return $this->sendRequest($url, 'POST', $opts); } /** - * @param array $opts + * @param array $query * @return string */ - public function vectorStoresCreate(array $opts) + public function vectorStoresList(array $query) { $url = Url::vectorStoresUrl(); $this->addAssistantsBetaHeader(); $this->baseUrl($url); - return $this->sendRequest($url, 'POST', $opts); + if (count($query) > 0) { + $url .= '?' . http_build_query($query); + } + + return $this->sendRequest($url, 'GET'); } /** @@ -906,7 +911,6 @@ public function vectorStoresSearch(string $vectorStoresId) $this->addAssistantsBetaHeader(); $this->baseUrl($url); - return $this->sendRequest($url, 'GET'); } @@ -941,7 +945,7 @@ public function vectorStoresDelete(string $vectorStoresId) * @param array $opts * @return string */ - public function vectorStoresFilesCreate(string $vectorStoresId, array $opts) + public function vectorStoresFilesCreateOrAppend(string $vectorStoresId, array $opts) { $url = Url::vectorStoresFilesUrl($vectorStoresId); $this->addAssistantsBetaHeader();