openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
fe3abd16b582ae784d8a73fd249bcdfebd5752c9

Branches

Tags

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

Clone

HTTPS

Download ZIP

openai/api_resources/embedding.py

91lines · modecode

1import base64
2import time
3
4from openai import util
5from openai.api_resources.abstract.engine_api_resource import EngineAPIResource
6from openai.datalib.numpy_helper import assert_has_numpy
7from openai.datalib.numpy_helper import numpy as np
8from openai.error import TryAgain
9
10
11class Embedding(EngineAPIResource):
12 OBJECT_NAME = "embeddings"
13
14 @classmethod
15 def create(cls, *args, **kwargs):
16 """
17 Creates a new embedding for the provided input and parameters.
18
19 See https://platform.openai.com/docs/api-reference/embeddings for a list
20 of valid parameters.
21 """
22 start = time.time()
23 timeout = kwargs.pop("timeout", None)
24
25 user_provided_encoding_format = kwargs.get("encoding_format", None)
26
27 # If encoding format was not explicitly specified, we opaquely use base64 for performance
28 if not user_provided_encoding_format:
29 kwargs["encoding_format"] = "base64"
30
31 while True:
32 try:
33 response = super().create(*args, **kwargs)
34
35 # If a user specifies base64, we'll just return the encoded string.
36 # This is only for the default case.
37 if not user_provided_encoding_format:
38 for data in response.data:
39
40 # If an engine isn't using this optimization, don't do anything
41 if type(data["embedding"]) == str:
42 assert_has_numpy()
43 data["embedding"] = np.frombuffer(
44 base64.b64decode(data["embedding"]), dtype="float32"
45 ).tolist()
46
47 return response
48 except TryAgain as e:
49 if timeout is not None and time.time() > start + timeout:
50 raise
51
52 util.log_info("Waiting for model to warm up", error=e)
53
54 @classmethod
55 async def acreate(cls, *args, **kwargs):
56 """
57 Creates a new embedding for the provided input and parameters.
58
59 See https://platform.openai.com/docs/api-reference/embeddings for a list
60 of valid parameters.
61 """
62 start = time.time()
63 timeout = kwargs.pop("timeout", None)
64
65 user_provided_encoding_format = kwargs.get("encoding_format", None)
66
67 # If encoding format was not explicitly specified, we opaquely use base64 for performance
68 if not user_provided_encoding_format:
69 kwargs["encoding_format"] = "base64"
70
71 while True:
72 try:
73 response = await super().acreate(*args, **kwargs)
74
75 # If a user specifies base64, we'll just return the encoded string.
76 # This is only for the default case.
77 if not user_provided_encoding_format:
78 for data in response.data:
79
80 # If an engine isn't using this optimization, don't do anything
81 if type(data["embedding"]) == str:
82 data["embedding"] = np.frombuffer(
83 base64.b64decode(data["embedding"]), dtype="float32"
84 ).tolist()
85
86 return response
87 except TryAgain as e:
88 if timeout is not None and time.time() > start + timeout:
89 raise
90
91 util.log_info("Waiting for model to warm up", error=e)
92