# 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. import openai from tenacity import retry, stop_after_attempt, wait_random_exponential from towhee.operator.base import PyOperator class OpenaiQA(PyOperator): def __init__(self, model_name='text-davinci-003', api_key=None, max_token=50): self._model = model_name self._api_key = api_key self._max_token = max_token # @retry(wait=wait_random_exponential(min=1, max=20), stop=stop_after_attempt(6)) def _call(self, prompt): response = openai.Completion.create( api_key=self._api_key, model=self._model, prompt=prompt, max_tokens=self._max_token, n=1, stop=None, temperature=0.3, ) answer = response.choices[0].text.strip() return answer def __call__(self, prompt): return self._call(prompt) @staticmethod def supported_model_names(): model_list = [ 'text-davinci-003', ] model_list.sort() return model_list