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 deployment_id parameter for azure #109

Merged
merged 8 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion examples/azure/embeddings.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
"metadata": {},
"outputs": [],
"source": [
"embeddings = openai.Embedding.create(engine=deployment_id,\n",
"embeddings = openai.Embedding.create(deployment_id=deployment_id,\n",
" input=\"The food was delicious and the waiter...\")\n",
" \n",
"print(embeddings)"
Expand Down
2 changes: 1 addition & 1 deletion examples/azure/finetuning.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@
"source": [
"print('Sending a test completion job')\n",
"start_phrase = 'When I go to the store, I want a'\n",
"response = openai.Completion.create(engine=deployment_id, prompt=start_phrase, max_tokens=4)\n",
"response = openai.Completion.create(deployment_id=deployment_id, prompt=start_phrase, max_tokens=4)\n",
"text = response['choices'][0]['text'].replace('\\n', '').replace(' .', '.').strip()\n",
"print(f'\"{start_phrase} {text}\"')\n"
]
Expand Down
2 changes: 1 addition & 1 deletion openai/api_resources/abstract/api_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def _static_request(
)

@classmethod
def _get_api_type_and_version(cls, api_type: str, api_version: str):
def _get_api_type_and_version(cls, api_type: str = None, api_version: str = None):
typed_api_type = ApiType.from_str(
api_type) if api_type else ApiType.from_str(openai.api_type)
typed_api_version = api_version or openai.api_version
Expand Down
21 changes: 15 additions & 6 deletions openai/api_resources/abstract/engine_api_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@


class EngineAPIResource(APIResource):
engine_required = True
plain_old_data = False

def __init__(self, engine: Optional[str] = None, **kwargs):
Expand Down Expand Up @@ -71,14 +70,24 @@ def create(
organization=None,
**params,
):
engine = params.pop("engine", None)
deployment_id = params.pop("deployment_id", None)
engine = params.pop("engine", deployment_id)
model = params.get("model", None)
timeout = params.pop("timeout", None)
stream = params.get("stream", False)
headers = params.pop("headers", None)
if engine is None and cls.engine_required:
raise error.InvalidRequestError(
"Must provide an 'engine' parameter to create a %s" % cls, "engine"
)

typed_api_type = cls._get_api_type_and_version(api_type=api_type)[0]
if typed_api_type in (util.ApiType.AZURE, util.ApiType.AZURE_AD):
if deployment_id is None and engine is None:
raise error.InvalidRequestError(
"Must provide an 'engine' or 'deployment_id' parameter to create a %s" % cls, "engine"
)
else:
if model is None and engine is None:
raise error.InvalidRequestError(
"Must provide an 'engine' or 'model' parameter to create a %s" % cls, "engine"
)

if timeout is None:
# No special timeout handling
Expand Down
8 changes: 1 addition & 7 deletions openai/api_resources/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
from openai import util
from openai.api_resources.abstract import DeletableAPIResource, ListableAPIResource
from openai.api_resources.abstract.engine_api_resource import EngineAPIResource
from openai.error import InvalidRequestError, TryAgain
from openai.error import TryAgain


class Completion(EngineAPIResource):
engine_required = False
OBJECT_NAME = "completions"

@classmethod
Expand All @@ -20,11 +19,6 @@ def create(cls, *args, **kwargs):
"""
start = time.time()
timeout = kwargs.pop("timeout", None)
if kwargs.get("model", None) is None and kwargs.get("engine", None) is None:
raise InvalidRequestError(
"Must provide an 'engine' or 'model' parameter to create a Completion.",
param="engine",
)

while True:
try:
Expand Down
1 change: 0 additions & 1 deletion openai/api_resources/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class Deployment(CreateableAPIResource, ListableAPIResource, DeletableAPIResource):
engine_required = False
OBJECT_NAME = "deployments"

@classmethod
Expand Down
16 changes: 8 additions & 8 deletions openai/api_resources/edit.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import time

from openai import util
from openai import util, error
from openai.api_resources.abstract.engine_api_resource import EngineAPIResource
from openai.error import InvalidRequestError, TryAgain
from openai.error import TryAgain


class Edit(EngineAPIResource):
engine_required = False
OBJECT_NAME = "edits"

@classmethod
Expand All @@ -16,11 +15,12 @@ def create(cls, *args, **kwargs):
"""
start = time.time()
timeout = kwargs.pop("timeout", None)
if kwargs.get("model", None) is None and kwargs.get("engine", None) is None:
raise InvalidRequestError(
"Must provide an 'engine' or 'model' parameter to create an Edit.",
param="engine",
)

api_type = kwargs.pop("api_type", None)
typed_api_type = cls._get_api_type_and_version(api_type=api_type)[0]
if typed_api_type in (util.ApiType.AZURE, util.ApiType.AZURE_AD):
raise error.InvalidAPIType(
"This operation is not supported by azure yet.")
t-asutedjo marked this conversation as resolved.
Show resolved Hide resolved

while True:
try:
Expand Down
8 changes: 1 addition & 7 deletions openai/api_resources/embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
from openai import util
from openai.api_resources.abstract import DeletableAPIResource, ListableAPIResource
from openai.api_resources.abstract.engine_api_resource import EngineAPIResource
from openai.error import InvalidRequestError, TryAgain
from openai.error import TryAgain


class Embedding(EngineAPIResource):
engine_required = False
OBJECT_NAME = "embeddings"

@classmethod
Expand All @@ -23,11 +22,6 @@ def create(cls, *args, **kwargs):
"""
start = time.time()
timeout = kwargs.pop("timeout", None)
if kwargs.get("model", None) is None and kwargs.get("engine", None) is None:
raise InvalidRequestError(
"Must provide an 'engine' or 'model' parameter to create an Embedding.",
param="engine",
)

user_provided_encoding_format = kwargs.get("encoding_format", None)

Expand Down
1 change: 0 additions & 1 deletion openai/api_resources/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@


class Model(ListableAPIResource, DeletableAPIResource):
engine_required = False
OBJECT_NAME = "models"
8 changes: 1 addition & 7 deletions openai/api_resources/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

from openai import util
from openai.api_resources.abstract.engine_api_resource import EngineAPIResource
from openai.error import InvalidRequestError, TryAgain
from openai.error import TryAgain


class Search(EngineAPIResource):
engine_required = False
OBJECT_NAME = "search"

@classmethod
Expand All @@ -20,11 +19,6 @@ def create(cls, *args, **kwargs):

start = time.time()
timeout = kwargs.pop("timeout", None)
if kwargs.get("model", None) is None and kwargs.get("engine", None) is None:
raise InvalidRequestError(
"Must provide an 'engine' or 'model' parameter to create a Search.",
param="engine",
)

while True:
try:
Expand Down
2 changes: 1 addition & 1 deletion openai/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def from_str(label):
return ApiType.OPEN_AI
else:
raise openai.error.InvalidAPIType(
"The API type provided in invalid. Please select one of the supported API types: 'azure', 'open_ai'"
"The API type provided in invalid. Please select one of the supported API types: 'azure', 'azure_ad', 'open_ai'"
)


Expand Down
2 changes: 1 addition & 1 deletion openai/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = "0.21.0"
VERSION = "0.21.1"
t-asutedjo marked this conversation as resolved.
Show resolved Hide resolved