Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vector store apis for assistants #153

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 99 additions & 7 deletions src/OpenAi.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class OpenAi
private string $engine = "davinci";
private string $model = "text-davinci-002";
private string $chatModel = "gpt-3.5-turbo";
private string $assistantsBetaVersion = "v1";

private string $assistantsBetaVersion = "v2";
private array $headers;
private array $contentTypes;
private int $timeout = 0;
Expand Down Expand Up @@ -720,7 +721,7 @@ public function createRun($threadId, $data, $stream = null)

$this->stream_method = $stream;
}

$this->addAssistantsBetaHeader();
$url = Url::threadsUrl() . '/' . $threadId . '/runs';
$this->baseUrl($url);
Expand Down Expand Up @@ -791,7 +792,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);
Expand Down Expand Up @@ -871,6 +872,94 @@ public function tts($opts)
return $this->sendRequest($url, 'POST', $opts);
}

/**
* @param string $name
* @return bool|string
* @throws Exception
*/
public function createVectorStore(string $name)
{
$this->addAssistantsBetaHeader();
$url = Url::vectorStoreUrl();
$this->baseUrl($url);

return $this->sendRequest($url, 'POST', ['name' => $name]);
}

/**
* @param string $id
* @return bool|string
* @throws Exception
*/
public function retrieveVectorStore(string $id)
{
$this->addAssistantsBetaHeader();
$url = Url::vectorStoreUrl() . '/' . $id;
$this->baseUrl($url);

return $this->sendRequest($url, 'GET');
}

/**
* @param string $vectorStoreId
* @param string $fileId
* @return bool|string
* @throws Exception
*/
public function createVectorStoreFile(string $vectorStoreId, string $fileId)
{
$this->addAssistantsBetaHeader();
$url = Url::vectorStoreUrl() . '/' . $vectorStoreId . '/files';
$this->baseUrl($url);

return $this->sendRequest($url, 'POST', ['file_id' => $fileId]);
}

/**
* @param string $vectorStoreId
* @param string $fileId
* @return bool|string
* @throws Exception
*/
public function retrieveVectorStoreFile(string $vectorStoreId, string $fileId)
{
$this->addAssistantsBetaHeader();
$url = Url::vectorStoreUrl() . '/' . $vectorStoreId . '/files/' . $fileId;
$this->baseUrl($url);

return $this->sendRequest($url, 'GET');
}

/**
* @param string $vectorStoreId
* @param array $fileIds
* @return bool|string
* @throws Exception
*/
public function createVectorFileBatch(string $vectorStoreId, array $fileIds)
{
$this->addAssistantsBetaHeader();
$url = Url::vectorStoreUrl() . '/' . $vectorStoreId . '/file_batches';
$this->baseUrl($url);

return $this->sendRequest($url, 'POST', ['file_ids' => $fileIds]);
}

/**
* @param string $vectorStoreId
* @param string $fileBatchId
* @return bool|string
* @throws Exception
*/
public function retrieveVectorFileBatch(string $vectorStoreId, string $fileBatchId)
{
$this->addAssistantsBetaHeader();
$url = Url::vectorStoreUrl() . '/' . $vectorStoreId . '/file_batches/' . $fileBatchId;
$this->baseUrl($url);

return $this->sendRequest($url, 'GET');
}

/**
* @param int $timeout
*/
Expand Down Expand Up @@ -939,7 +1028,7 @@ public function setORG(string $org)
$this->headers[] = "OpenAI-Organization: $org";
}
}

/**
* @param string $org
*/
Expand All @@ -953,10 +1042,13 @@ public function setAssistantsBetaVersion(string $version)
/**
* @return void
*/
private function addAssistantsBetaHeader(){
$this->headers[] = 'OpenAI-Beta: assistants='.$this->assistantsBetaVersion;
private function addAssistantsBetaHeader()
{
$assistantsBetaHeader = 'OpenAI-Beta: assistants=' . $this->assistantsBetaVersion;
if (!in_array($assistantsBetaHeader, $this->headers)) {
$this->headers[] = $assistantsBetaHeader;
}
}


/**
* @param string $url
Expand Down
5 changes: 5 additions & 0 deletions src/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,9 @@ public static function ttsUrl(): string
{
return self::OPEN_AI_URL . "/audio/speech";
}

public static function vectorStoreUrl(): string
{
return self::OPEN_AI_URL . "/vector_stores";
}
}