logo
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Readme
Files and versions

48 lines
1.6 KiB

# Copyright 2021 Zilliz. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from openai import Embedding
from tenacity import retry, stop_after_attempt, wait_random_exponential
from towhee.operator.base import PyOperator
class OpenaiEmbeding(PyOperator):
def __init__(self, model_name='text-embedding-ada-002', api_key=None):
self._engine = model_name
self._api_key = api_key
@retry(wait=wait_random_exponential(min=1, max=20), stop=stop_after_attempt(6))
def _call(self, text):
text = text.replace("\n", " ")
return Embedding.create(input=[text],
engine=self._engine,
api_key=self._api_key,
)["data"][0]["embedding"]
def __call__(self, text):
return self._call(text)
@staticmethod
def supported_model_names():
model_list = [
'text-embedding-ada-002',
'text-similarity-davinci-001',
'text-similarity-curie-001',
'text-similarity-babbage-001',
'text-similarity-ada-001'
]
model_list.sort()
return model_list