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.
|
|
|
# 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.
|
|
|
|
|
|
|
|
|