Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Searches

Greg Priday edited this page Jul 7, 2021 · 1 revision

Searches API

Given a query and a set of documents or labels, the model ranks each document based on its semantic similarity to the provided query.

https://beta.openai.com/docs/api-reference/searches

Searching across a large collection of smaller documents is quite easy with GPT-3.

use SiteOrigin\OpenAI\Client;
use SiteOrigin\OpenAI\Engines;
$documents = [
    "White House",
    "hospital",
    "school",
];

$client = new Client($_ENV['OPENAI_API_KEY']);
$result = $client->search(Engines::ADA)->search('President', $documents);

Where the value of $result would be

(object) array(
   'object' => 'list',
   'data' => 
  array (
    0 => 
    (object) array(
       'object' => 'search_result',
       'document' => 0,
       'score' => 391.496,
    ),
    1 => 
    (object) array(
       'object' => 'search_result',
       'document' => 1,
       'score' => -113.686,
    ),
    2 => 
    (object) array(
       'object' => 'search_result',
       'document' => 2,
       'score' => -75.336,
    ),
  ),
   'model' => 'ada:2020-05-03',
);

Or you can upload a file, to have it preprocessed.

use SiteOrigin\OpenAI\Client;
use SiteOrigin\OpenAI\Engines;
use SiteOrigin\OpenAI\Files;

$documents = [
    ["text" => "White House", "metadata" => "document 1"],
    ["text" => "hospital", "metadata" => "document 2"],
    ["text" => "school", "metadata" => "document 3"],
];

$client = new Client($_ENV['OPENAI_API_KEY']);
$file = $client->files()->create('data.json', $documents, Files::PURPOSE_SEARCH);

// Wait for the file to process
$result = $client->search(Engines::ADA)->search('President', $file->id);
Clone this wiki locally