|
| 1 | +import base64 |
| 2 | +import time |
| 3 | + |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +from openai import util |
| 7 | +from openai.api_resources.abstract import DeletableAPIResource, ListableAPIResource |
| 8 | +from openai.api_resources.abstract.engine_api_resource import EngineAPIResource |
| 9 | +from openai.error import InvalidRequestError, TryAgain |
| 10 | + |
| 11 | + |
| 12 | +class Embedding(EngineAPIResource, ListableAPIResource, DeletableAPIResource): |
| 13 | + engine_required = True |
| 14 | + OBJECT_NAME = "embedding" |
| 15 | + |
| 16 | + @classmethod |
| 17 | + def create(cls, *args, **kwargs): |
| 18 | + """ |
| 19 | + Creates a new embedding for the provided input and parameters. |
| 20 | +
|
| 21 | + See https://beta.openai.com/docs/api-reference/embeddings for a list |
| 22 | + of valid parameters. |
| 23 | + """ |
| 24 | + start = time.time() |
| 25 | + timeout = kwargs.pop("timeout", None) |
| 26 | + if kwargs.get("model", None) is None and kwargs.get("engine", None) is None: |
| 27 | + raise InvalidRequestError( |
| 28 | + "Must provide an 'engine' or 'model' parameter to create an Embedding.", |
| 29 | + param="engine", |
| 30 | + ) |
| 31 | + |
| 32 | + user_provided_encoding_format = kwargs.get("encoding_format", None) |
| 33 | + |
| 34 | + # If encoding format was not explicitly specified, we opaquely use base64 for performance |
| 35 | + if not user_provided_encoding_format: |
| 36 | + kwargs["encoding_format"] = "base64" |
| 37 | + |
| 38 | + while True: |
| 39 | + try: |
| 40 | + response = super().create(*args, **kwargs) |
| 41 | + |
| 42 | + # If a user specifies base64, we'll just return the encoded string. |
| 43 | + # This is only for the default case. |
| 44 | + if not user_provided_encoding_format: |
| 45 | + for data in response.data: |
| 46 | + |
| 47 | + # If an engine isn't using this optimization, don't do anything |
| 48 | + if type(data["embedding"]) == str: |
| 49 | + data["embedding"] = np.frombuffer( |
| 50 | + base64.b64decode(data["embedding"]), dtype="float32" |
| 51 | + ).tolist() |
| 52 | + |
| 53 | + return response |
| 54 | + except TryAgain as e: |
| 55 | + if timeout is not None and time.time() > start + timeout: |
| 56 | + raise |
| 57 | + |
| 58 | + util.log_info("Waiting for model to warm up", error=e) |
0 commit comments