3 changed files with 80 additions and 1 deletions
@ -1,2 +1,56 @@ |
|||
# template |
|||
# question-answer |
|||
|
|||
<br /> |
|||
|
|||
|
|||
|
|||
## Desription |
|||
|
|||
Prompt Template. |
|||
|
|||
|
|||
<br /> |
|||
|
|||
|
|||
|
|||
## Code Example |
|||
|
|||
### Example |
|||
|
|||
|
|||
```python |
|||
|
|||
from towhee import ops, pipe |
|||
import requests |
|||
|
|||
towhee_docs = requests.get('https://raw.githubusercontent.com/towhee-io/towhee/main/README.md').context |
|||
|
|||
temp = """{question} |
|||
|
|||
input: |
|||
{context} |
|||
""" |
|||
|
|||
p = ( |
|||
pipe.input('question', 'doc', 'history') |
|||
.map(('question', 'doc', 'history'), 'prompt', ops.prompt.template(temp)) |
|||
.map('prompt', 'answer', ops.LLM.OpenAI()) |
|||
.output('answer') |
|||
) |
|||
|
|||
an1 = p('Tell me something about Towhee', towhee_docs, []).get()[0] |
|||
|
|||
an2 = p('Give an example', towhee_docs, [('Tell me something about Towhee', an1)]).get()[0] |
|||
|
|||
``` |
|||
|
|||
## Factory Constructor |
|||
|
|||
Create the operator via the following factory method: |
|||
|
|||
***ops.prompt.template(temp)*** |
|||
|
|||
<br /> |
|||
|
|||
|
|||
**Returns:** *List[Dict]* |
@ -0,0 +1,4 @@ |
|||
from .template_prompt import TemplatePrompt |
|||
|
|||
def question_answer(temp: str): |
|||
return TemplatePrompt(temp) |
@ -0,0 +1,21 @@ |
|||
from typing import List, Tuple, Dict, Optional |
|||
|
|||
from towhee.operator import PyOperator |
|||
|
|||
|
|||
class TemplatePrompt(PyOperator): |
|||
def __init__(self, temp: str): |
|||
super().__init__() |
|||
self._template = temp |
|||
|
|||
def __call__(self, **kwargs) -> List[Dict[str, str]]: |
|||
history = kwargs.get('history', []) |
|||
prompt_str = self._template.format(**kwargs) |
|||
ret = [{'question': prompt_str}] |
|||
if not isinstance(history, list): |
|||
return ret |
|||
else: |
|||
history_data = [] |
|||
for item in history: |
|||
history_data.append({'question': item[0], 'answer': item[1]}) |
|||
return history_data + ret |
Loading…
Reference in new issue