3 changed files with 135 additions and 1 deletions
@ -1,2 +1,80 @@ |
|||||
# openai |
|
||||
|
# ChatBot with OpenAI |
||||
|
|
||||
|
*author: Jael, Yuchen* |
||||
|
|
||||
|
<br /> |
||||
|
|
||||
|
## Description |
||||
|
|
||||
|
A chat-bot operator returns answer in text given input text. |
||||
|
This operator is implemented with GPT models from [OpenAI](https://platform.openai.com/docs/guides/embeddings). |
||||
|
Please note you need an [OpenAI API key](https://platform.openai.com/account/api-keys) to access OpenAI. |
||||
|
|
||||
|
<br /> |
||||
|
|
||||
|
## Code Example |
||||
|
|
||||
|
Use the default model to answer the question "Who are you?". |
||||
|
|
||||
|
*Write a pipeline with explicit inputs/outputs name specifications:* |
||||
|
|
||||
|
```python |
||||
|
from towhee.dc2 import pipe, ops, DataCollection |
||||
|
|
||||
|
p = ( |
||||
|
pipe.input('question') |
||||
|
.map('question', 'answer', |
||||
|
ops.chatbot.openai(api_key=OPENAI_API_KEY)) |
||||
|
.output('question', 'answer') |
||||
|
) |
||||
|
|
||||
|
DataCollection(p('Who are you?')).show() |
||||
|
``` |
||||
|
|
||||
|
<br /> |
||||
|
|
||||
|
## Factory Constructor |
||||
|
|
||||
|
Create the operator via the following factory method: |
||||
|
|
||||
|
***chatbot.openai(model_name: str, api_key: str)*** |
||||
|
|
||||
|
**Parameters:** |
||||
|
|
||||
|
***model_name***: *str* |
||||
|
|
||||
|
The model name in string, defaults to 'text-davinci-003'. Supported model names: |
||||
|
- text-davinci-003 |
||||
|
|
||||
|
***api_key***: *str=None* |
||||
|
|
||||
|
The OpenAI API key in string, defaults to None. |
||||
|
|
||||
|
<br /> |
||||
|
|
||||
|
## Interface |
||||
|
|
||||
|
The operator takes a piece of text in string as input. |
||||
|
It returns a text emabedding in numpy.ndarray. |
||||
|
|
||||
|
***\_\_call\_\_(txt)*** |
||||
|
|
||||
|
**Parameters:** |
||||
|
|
||||
|
***text***: *str* |
||||
|
|
||||
|
The text in string. |
||||
|
|
||||
|
**Returns**: |
||||
|
|
||||
|
*answer: str* |
||||
|
|
||||
|
The answer in string generated by model. |
||||
|
|
||||
|
<br /> |
||||
|
|
||||
|
***supported_model_names()*** |
||||
|
|
||||
|
Get a list of supported model names. |
||||
|
|
||||
|
|
||||
|
@ -0,0 +1,5 @@ |
|||||
|
from .openai_qa import OpenaiQA |
||||
|
|
||||
|
|
||||
|
def openai(*args, **kwargs): |
||||
|
return OpenaiQA(*args, **kwargs) |
@ -0,0 +1,51 @@ |
|||||
|
# 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-davinci-003', 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, prompt): |
||||
|
prompt = prompt.replace("\n", " ") |
||||
|
response = openai.Completion.create( |
||||
|
api_key=self._api_key, |
||||
|
engine=self._engine, |
||||
|
prompt=prompt, |
||||
|
max_tokens=1024, |
||||
|
n=1, |
||||
|
stop=None, |
||||
|
temperature=0.7, |
||||
|
) |
||||
|
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 |
||||
|
|
Loading…
Reference in new issue