openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
24fc6924c8d9ba4e43574ee6db3a8fdad147f5da

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

openai/api_resources/embedding.py

58lines · modecode

1import base64
2import time
3
4import numpy as np
5
6from openai import util
7from openai.api_resources.abstract import DeletableAPIResource, ListableAPIResource
8from openai.api_resources.abstract.engine_api_resource import EngineAPIResource
9from openai.error import InvalidRequestError, TryAgain
10
11
12class Embedding(EngineAPIResource, ListableAPIResource, DeletableAPIResource):
13 engine_required = False
14 OBJECT_NAME = "embeddings"
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)