Skip to content

Commit 205d063

Browse files
authored
Search v2 functionality (#33)
* Add support for search_indices (#64) * Add support for search_indices * Updated with Schnurr's comments * Add version to search (#65) * Make search query required (#67)
1 parent 7febb75 commit 205d063

File tree

5 files changed

+69
-11
lines changed

5 files changed

+69
-11
lines changed

openai/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@
3434
File,
3535
FineTune,
3636
Model,
37+
Search,
3738
)
38-
39-
from openai.error import OpenAIError, APIError, InvalidRequestError # noqa: E402,F401
39+
from openai.error import APIError, InvalidRequestError, OpenAIError # noqa: E402,F401

openai/api_resources/__init__.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
from openai.api_resources.answer import Answer # noqa: F401
2+
from openai.api_resources.classification import Classification # noqa: F401
13
from openai.api_resources.completion import Completion # noqa: F401
24
from openai.api_resources.engine import Engine # noqa: F401
35
from openai.api_resources.error_object import ErrorObject # noqa: F401
46
from openai.api_resources.file import File # noqa: F401
5-
from openai.api_resources.answer import Answer # noqa: F401
6-
from openai.api_resources.classification import Classification # noqa: F401
7-
from openai.api_resources.model import Model # noqa: F401
87
from openai.api_resources.fine_tune import FineTune # noqa: F401
8+
from openai.api_resources.model import Model # noqa: F401
9+
from openai.api_resources.search import Search # noqa: F401

openai/api_resources/search.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from openai.api_resources.abstract.api_resource import APIResource
2+
3+
4+
class Search(APIResource):
5+
api_prefix = "v1"
6+
OBJECT_NAME = "search_indices"
7+
8+
@classmethod
9+
def class_url(cls):
10+
return "/%s/%s" % (cls.api_prefix, cls.OBJECT_NAME)
11+
12+
@classmethod
13+
def create_alpha(cls, **params):
14+
instance = cls()
15+
return instance.request("post", f"{cls.class_url()}/search", params)

openai/cli.py

+47-5
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
import signal
44
import sys
55
import warnings
6+
7+
import openai
68
from openai.validators import (
7-
write_out_file,
89
apply_necessary_remediation,
910
apply_optional_remediation,
10-
read_any_format,
1111
get_validators,
12+
read_any_format,
13+
write_out_file,
1214
)
1315

14-
import openai
15-
1616

1717
class bcolors:
1818
HEADER = "\033[95m"
@@ -102,7 +102,6 @@ def generate(cls, args):
102102

103103
@classmethod
104104
def search(cls, args):
105-
# Will soon be deprecated and replaced by a Search.create
106105
params = {
107106
"query": args.query,
108107
"max_rerank": args.max_rerank,
@@ -113,6 +112,9 @@ def search(cls, args):
113112
if args.file:
114113
params["file"] = args.file
115114

115+
if args.version:
116+
params["version"] = args.version
117+
116118
resp = openai.Engine(id=args.id).search(**params)
117119
scores = [
118120
(search_result["score"], search_result["document"])
@@ -221,6 +223,17 @@ def list(cls, args):
221223
print(file)
222224

223225

226+
class Search:
227+
@classmethod
228+
def create_alpha(cls, args):
229+
resp = openai.Search.create_alpha(
230+
query=[args.query],
231+
max_documents=args.max_documents,
232+
file_id=args.file,
233+
)
234+
print(resp)
235+
236+
224237
class FineTune:
225238
@classmethod
226239
def list(cls, args):
@@ -589,6 +602,11 @@ def help(args):
589602
type=bool,
590603
default=False,
591604
)
605+
sub.add_argument(
606+
"--version",
607+
help="The version of the search routing to use",
608+
)
609+
592610
sub.add_argument("-q", "--query", required=True, help="Search query")
593611
sub.set_defaults(func=Engine.search)
594612

@@ -688,6 +706,30 @@ def help(args):
688706
sub = subparsers.add_parser("files.list")
689707
sub.set_defaults(func=File.list)
690708

709+
# Search
710+
sub = subparsers.add_parser("search.create_alpha")
711+
712+
sub.add_argument(
713+
"-f",
714+
"--file",
715+
required=True,
716+
help="ID for previously uploaded file that contains the documents you want to search",
717+
)
718+
sub.add_argument(
719+
"-m",
720+
"--max_documents",
721+
help="The maximum number of documents to return",
722+
type=int,
723+
default=200,
724+
)
725+
sub.add_argument(
726+
"-q",
727+
"--query",
728+
required=True,
729+
help="Search query",
730+
)
731+
sub.set_defaults(func=Search.create_alpha)
732+
691733
# Finetune
692734
sub = subparsers.add_parser("fine_tunes.list")
693735
sub.set_defaults(func=FineTune.list)

openai/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = "0.10.3"
1+
VERSION = "0.10.4"

0 commit comments

Comments
 (0)