Spaces:
Runtime error
Runtime error
<!--Copyright 2023 The HuggingFace Team. 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. | |
--> | |
# ์ฌ์ฉ์ ์ ์ ๋๊ตฌ์ ํ๋กฌํํธ[[custom-tools-and-prompts]] | |
<Tip> | |
Transformers์ ๊ด๋ จํ์ฌ ์ด๋ค ๋๊ตฌ์ ์์ด์ ํธ๊ฐ ์๋์ง ์ ๋ชจ๋ฅด์ ๋ค๋ฉด [Transformers Agents](transformers_agents) ํ์ด์ง๋ฅผ ๋จผ์ ์ฝ์ด๋ณด์๊ธฐ ๋ฐ๋๋๋ค. | |
</Tip> | |
<Tip warning={true}> | |
Transformers Agent๋ ์คํ ์ค์ธ API๋ก ์ธ์ ๋ ์ง ๋ณ๊ฒฝ๋ ์ ์์ต๋๋ค. | |
API ๋๋ ๊ธฐ๋ฐ ๋ชจ๋ธ์ด ๋ณ๊ฒฝ๋๊ธฐ ์ฝ๊ธฐ ๋๋ฌธ์ ์์ด์ ํธ๊ฐ ๋ฐํํ๋ ๊ฒฐ๊ณผ๋ ๋ฌ๋ผ์ง ์ ์์ต๋๋ค. | |
</Tip> | |
์์ด์ ํธ์๊ฒ ๊ถํ์ ๋ถ์ฌํ๊ณ ์๋ก์ด ์์ ์ ์ํํ๊ฒ ํ๋ ค๋ฉด ์ฌ์ฉ์ ์ ์ ๋๊ตฌ์ ํ๋กฌํํธ๋ฅผ ๋ง๋ค๊ณ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ฌด์๋ณด๋ค ์ค์ํฉ๋๋ค. | |
์ด ๊ฐ์ด๋์์๋ ๋ค์๊ณผ ๊ฐ์ ๋ด์ฉ์ ์ดํด๋ณด๊ฒ ์ต๋๋ค: | |
- ํ๋กฌํํธ๋ฅผ ์ฌ์ฉ์ ์ ์ํ๋ ๋ฐฉ๋ฒ | |
- ์ฌ์ฉ์ ์ ์ ๋๊ตฌ๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ | |
- ์ฌ์ฉ์ ์ ์ ๋๊ตฌ๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ | |
## ํ๋กฌํํธ๋ฅผ ์ฌ์ฉ์ ์ ์ํ๊ธฐ[[customizing-the-prompt]] | |
[Transformers Agents](transformers_agents)์์ ์ค๋ช ํ ๊ฒ์ฒ๋ผ ์์ด์ ํธ๋ [`~Agent.run`] ๋ฐ [`~Agent.chat`] ๋ชจ๋์์ ์คํํ ์ ์์ต๋๋ค. | |
`run`(์คํ) ๋ชจ๋์ `chat`(์ฑํ ) ๋ชจ๋ ๋ชจ๋ ๋์ผํ ๋ก์ง์ ๊ธฐ๋ฐ์ผ๋ก ํฉ๋๋ค. | |
์์ด์ ํธ๋ฅผ ๊ตฌ๋ํ๋ ์ธ์ด ๋ชจ๋ธ์ ๊ธด ํ๋กฌํํธ์ ๋ฐ๋ผ ์กฐ๊ฑด์ด ์ง์ ๋๊ณ , ์ค์ง ํ ํฐ์ ๋๋ฌํ ๋๊น์ง ๋ค์ ํ ํฐ์ ์์ฑํ์ฌ ํ๋กฌํํธ๋ฅผ ์์ํฉ๋๋ค. | |
`chat` ๋ชจ๋์์๋ ํ๋กฌํํธ๊ฐ ์ด์ ์ฌ์ฉ์ ์ ๋ ฅ ๋ฐ ๋ชจ๋ธ ์์ฑ์ผ๋ก ์ฐ์ฅ๋๋ค๋ ์ ์ด ๋ ๋ชจ๋์ ์ ์ผํ ์ฐจ์ด์ ์ ๋๋ค. | |
์ด๋ฅผ ํตํด ์์ด์ ํธ๊ฐ ๊ณผ๊ฑฐ ์ํธ์์ฉ์ ์ ๊ทผํ ์ ์๊ฒ ๋๋ฏ๋ก ์์ด์ ํธ์๊ฒ ์ผ์ข ์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ ๊ณตํ๋ ์ ์ ๋๋ค. | |
### ํ๋กฌํํธ์ ๊ตฌ์กฐ[[structure-of-the-prompt]] | |
์ด๋ป๊ฒ ํ๋กฌํํธ ์ฌ์ฉ์ ์ ์๋ฅผ ์ ํ ์ ์๋์ง ์ดํดํ๊ธฐ ์ํด ํ๋กฌํํธ์ ๊ตฌ์กฐ๋ฅผ ์์ธํ ์ดํด๋ด ์๋ค. | |
ํ๋กฌํํธ๋ ํฌ๊ฒ ๋ค ๋ถ๋ถ์ผ๋ก ๊ตฌ์ฑ๋์ด ์์ต๋๋ค. | |
- 1. ๋์ : ์์ด์ ํธ๊ฐ ์ด๋ป๊ฒ ํ๋ํด์ผ ํ๋์ง, ๋๊ตฌ์ ๊ฐ๋ ์ ๋ํ ์ค๋ช . | |
- 2. ๋ชจ๋ ๋๊ตฌ์ ๋ํ ์ค๋ช . ์ด๋ ๋ฐํ์์ ์ฌ์ฉ์๊ฐ ์ ์/์ ํํ ๋๊ตฌ๋ก ๋์ ์ผ๋ก ๋์ฒด๋๋ `<<all_tools>>` ํ ํฐ์ผ๋ก ์ ์๋ฉ๋๋ค. | |
- 3. ์์ ์์ ๋ฐ ํด๋น ์๋ฃจ์ ์ธํธ. | |
- 4. ํ์ฌ ์์ ๋ฐ ํด๊ฒฐ ์์ฒญ. | |
๊ฐ ๋ถ๋ถ์ ๋ ์ ์ดํดํ ์ ์๋๋ก ์งง์ ๋ฒ์ ์ ํตํด `run` ํ๋กฌํํธ๊ฐ ์ด๋ป๊ฒ ๋ณด์ด๋์ง ์ดํด๋ณด๊ฒ ์ต๋๋ค: | |
````text | |
I will ask you to perform a task, your job is to come up with a series of simple commands in Python that will perform the task. | |
[...] | |
You can print intermediate results if it makes sense to do so. | |
Tools: | |
- document_qa: This is a tool that answers a question about a document (pdf). It takes an input named `document` which should be the document containing the information, as well as a `question` that is the question about the document. It returns a text that contains the answer to the question. | |
- image_captioner: This is a tool that generates a description of an image. It takes an input named `image` which should be the image to the caption and returns a text that contains the description in English. | |
[...] | |
Task: "Answer the question in the variable `question` about the image stored in the variable `image`. The question is in French." | |
I will use the following tools: `translator` to translate the question into English and then `image_qa` to answer the question on the input image. | |
Answer: | |
```py | |
translated_question = translator(question=question, src_lang="French", tgt_lang="English") | |
print(f"The translated question is {translated_question}.") | |
answer = image_qa(image=image, question=translated_question) | |
print(f"The answer is {answer}") | |
``` | |
Task: "Identify the oldest person in the `document` and create an image showcasing the result as a banner." | |
I will use the following tools: `document_qa` to find the oldest person in the document, then `image_generator` to generate an image according to the answer. | |
Answer: | |
```py | |
answer = document_qa(document, question="What is the oldest person?") | |
print(f"The answer is {answer}.") | |
image = image_generator("A banner showing " + answer) | |
``` | |
[...] | |
Task: "Draw me a picture of rivers and lakes" | |
I will use the following | |
```` | |
๋์ (*"๋๊ตฌ:"* ์์ ํ ์คํธ)์์๋ ๋ชจ๋ธ์ด ์ด๋ป๊ฒ ์๋ํ๊ณ ๋ฌด์์ ํด์ผ ํ๋์ง ์ ํํ๊ฒ ์ค๋ช ํฉ๋๋ค. | |
์์ด์ ํธ๋ ํญ์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์๋ํด์ผ ํ๋ฏ๋ก ์ด ๋ถ๋ถ์ ์ฌ์ฉ์ ์ ์ํ ํ์๊ฐ ์์ ๊ฐ๋ฅ์ฑ์ด ๋์ต๋๋ค. | |
๋ ๋ฒ์งธ ๋ถ๋ถ(*"๋๊ตฌ"* ์๋์ ๊ธ๋จธ๋ฆฌ ๊ธฐํธ)์ `run` ๋๋ `chat`์ ํธ์ถํ ๋ ๋์ ์ผ๋ก ์ถ๊ฐ๋ฉ๋๋ค. | |
์ ํํ `agent.toolbox`์ ์๋ ๋๊ตฌ ์๋งํผ ๊ธ๋จธ๋ฆฌ ๊ธฐํธ๊ฐ ์๊ณ , ๊ฐ ๊ธ๋จธ๋ฆฌ ๊ธฐํธ๋ ๋๊ตฌ์ ์ด๋ฆ๊ณผ ์ค๋ช ์ผ๋ก ๊ตฌ์ฑ๋ฉ๋๋ค: | |
```text | |
- <tool.name>: <tool.description> | |
``` | |
๋ฌธ์ ์ง์์๋ต ๋๊ตฌ๋ฅผ ๊ฐ์ ธ์ค๊ณ ์ด๋ฆ๊ณผ ์ค๋ช ์ ์ถ๋ ฅํด์ ๋น ๋ฅด๊ฒ ํ์ธํด ๋ณด๊ฒ ์ต๋๋ค. | |
```py | |
from transformers import load_tool | |
document_qa = load_tool("document-question-answering") | |
print(f"- {document_qa.name}: {document_qa.description}") | |
``` | |
๊ทธ๋ฌ๋ฉด ๋ค์ ๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅ๋ฉ๋๋ค: | |
```text | |
- document_qa: This is a tool that answers a question about a document (pdf). It takes an input named `document` which should be the document containing the information, as well as a `question` that is the question about the document. It returns a text that contains the answer to the question. | |
``` | |
์ฌ๊ธฐ์ ๋๊ตฌ ์ด๋ฆ์ด ์งง๊ณ ์ ํํ๋ค๋ ๊ฒ์ ์ ์ ์์ต๋๋ค. | |
์ค๋ช ์ ๋ ๋ถ๋ถ์ผ๋ก ๊ตฌ์ฑ๋์ด ์๋๋ฐ, ์ฒซ ๋ฒ์งธ ๋ถ๋ถ์์๋ ๋๊ตฌ์ ๊ธฐ๋ฅ์ ์ค๋ช ํ๊ณ ๋ ๋ฒ์งธ ๋ถ๋ถ์์๋ ์์๋๋ ์ ๋ ฅ ์ธ์์ ๋ฐํ ๊ฐ์ ๋ช ์ํฉ๋๋ค. | |
์์ด์ ํธ๊ฐ ๋๊ตฌ๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ์ฌ์ฉํ๋ ค๋ฉด ์ข์ ๋๊ตฌ ์ด๋ฆ๊ณผ ๋๊ตฌ ์ค๋ช ์ด ๋งค์ฐ ์ค์ํฉ๋๋ค. | |
์์ด์ ํธ๊ฐ ๋๊ตฌ์ ๋ํด ์ ์ ์๋ ์ ์ผํ ์ ๋ณด๋ ์ด๋ฆ๊ณผ ์ค๋ช ๋ฟ์ด๋ฏ๋ก, ์ด ๋ ๊ฐ์ง๋ฅผ ์ ํํ๊ฒ ์์ฑํ๊ณ ๋๊ตฌ ์์์ ์๋ ๊ธฐ์กด ๋๊ตฌ์ ์คํ์ผ๊ณผ ์ผ์นํ๋์ง ํ์ธํด์ผ ํฉ๋๋ค. | |
ํนํ ์ด๋ฆ์ ๋ฐ๋ผ ์์๋๋ ๋ชจ๋ ์ธ์๊ฐ ์ค๋ช ์ ์ฝ๋ ์คํ์ผ๋ก ์ธ๊ธ๋์ด ์๋์ง, ์์๋๋ ์ ํ๊ณผ ๊ทธ ์ ํ์ด ๋ฌด์์ธ์ง์ ๋ํ ์ค๋ช ์ด ํฌํจ๋์ด ์๋์ง ํ์ธํ์ธ์. | |
<Tip> | |
๋๊ตฌ์ ์ด๋ค ์ด๋ฆ๊ณผ ์ค๋ช ์ด ์์ด์ผ ํ๋์ง ์ดํดํ๋ ค๋ฉด ์์ ๋ Transformers ๋๊ตฌ์ ์ด๋ฆ๊ณผ ์ค๋ช ์ ํ์ธํ์ธ์. | |
[`Agent.toolbox`] ์์ฑ์ ๊ฐ์ง ๋ชจ๋ ๋๊ตฌ๋ฅผ ๋ณผ ์ ์์ต๋๋ค. | |
</Tip> | |
์ธ ๋ฒ์งธ ๋ถ๋ถ์๋ ์์ด์ ํธ๊ฐ ์ด๋ค ์ข ๋ฅ์ ์ฌ์ฉ์ ์์ฒญ์ ๋ํด ์ด๋ค ์ฝ๋๋ฅผ ์์ฑํด์ผ ํ๋์ง ์ ํํ๊ฒ ๋ณด์ฌ์ฃผ๋ ์์ ๋ ์์ ์ธํธ๊ฐ ํฌํจ๋์ด ์์ต๋๋ค. | |
์์ด์ ํธ๋ฅผ ์ง์ํ๋ ๋๊ท๋ชจ ์ธ์ด ๋ชจ๋ธ์ ํ๋กฌํํธ์์ ํจํด์ ์ธ์ํ๊ณ ์๋ก์ด ๋ฐ์ดํฐ๋ก ํจํด์ ๋ฐ๋ณตํ๋ ๋ฐ ๋งค์ฐ ๋ฅ์ํฉ๋๋ค. | |
๋ฐ๋ผ์ ์์ด์ ํธ๊ฐ ์ค์ ๋ก ์ฌ๋ฐ๋ฅธ ์คํ ๊ฐ๋ฅํ ์ฝ๋๋ฅผ ์์ฑํ ๊ฐ๋ฅ์ฑ์ ๊ทน๋ํํ๋ ๋ฐฉ์์ผ๋ก ์์ ๋ฅผ ์์ฑํ๋ ๊ฒ์ด ๋งค์ฐ ์ค์ํฉ๋๋ค. | |
ํ ๊ฐ์ง ์๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค: | |
````text | |
Task: "Identify the oldest person in the `document` and create an image showcasing the result as a banner." | |
I will use the following tools: `document_qa` to find the oldest person in the document, then `image_generator` to generate an image according to the answer. | |
Answer: | |
```py | |
answer = document_qa(document, question="What is the oldest person?") | |
print(f"The answer is {answer}.") | |
image = image_generator("A banner showing " + answer) | |
``` | |
```` | |
์์ ์ค๋ช , ์์ด์ ํธ๊ฐ ์ํํ๋ ค๋ ์์ ์ ๋ํ ์ค๋ช , ๋ง์ง๋ง์ผ๋ก ์์ฑ๋ ์ฝ๋, ์ด ์ธ ๋ถ๋ถ์ผ๋ก ๊ตฌ์ฑ๋ ํ๋กฌํํธ๋ ๋ชจ๋ธ์ ๋ฐ๋ณตํ์ฌ ์ ๊ณต๋ฉ๋๋ค. | |
ํ๋กฌํํธ์ ์ผ๋ถ์ธ ๋ชจ๋ ์์ ๋ ์ด๋ฌํ ์ ํํ ํจํด์ผ๋ก ๋์ด ์์ผ๋ฏ๋ก, ์์ด์ ํธ๊ฐ ์ ํ ํฐ์ ์์ฑํ ๋ ์ ํํ ๋์ผํ ํจํด์ ์ฌํํ ์ ์์ต๋๋ค. | |
ํ๋กฌํํธ ์์ ๋ Transformers ํ์ด ์ ๋ณํ๊ณ ์ผ๋ จ์ [problem statements](https://github.com/huggingface/transformers/blob/main/src/transformers/tools/evaluate_agent.py)์ ๋ฐ๋ผ ์๊ฒฉํ๊ฒ ํ๊ฐํ์ฌ | |
์์ด์ ํธ์ ํ๋กฌํํธ๊ฐ ์์ด์ ํธ์ ์ค์ ์ฌ์ฉ ์ฌ๋ก๋ฅผ ์ต๋ํ ์ ํด๊ฒฐํ ์ ์๋๋ก ๋ณด์ฅํฉ๋๋ค. | |
ํ๋กฌํํธ์ ๋ง์ง๋ง ๋ถ๋ถ์ ๋ค์์ ํด๋นํฉ๋๋ค: | |
```text | |
Task: "Draw me a picture of rivers and lakes" | |
I will use the following | |
``` | |
์ด๋ ์์ด์ ํธ๊ฐ ์๋ฃํด์ผ ํ ์ต์ข ์ ์ธ ๋ฏธ์์ฑ ์์ ์ ๋๋ค. ๋ฏธ์์ฑ ์์ ๋ ์ค์ ์ฌ์ฉ์ ์ ๋ ฅ์ ๋ฐ๋ผ ๋์ ์ผ๋ก ๋ง๋ค์ด์ง๋๋ค. | |
์ ์์์ ๊ฒฝ์ฐ ์ฌ์ฉ์๊ฐ ๋ค์๊ณผ ๊ฐ์ด ์คํํ์ต๋๋ค: | |
```py | |
agent.run("Draw me a picture of rivers and lakes") | |
``` | |
์ฌ์ฉ์ ์ ๋ ฅ - *์ฆ* Task: *"Draw me a picture of rivers and lakes"*๊ฐ ํ๋กฌํํธ ํ ํ๋ฆฟ์ ๋ง์ถฐ "Task: <task> \n\n I will use the following"๋ก ์บ์คํ ๋ฉ๋๋ค. | |
์ด ๋ฌธ์ฅ์ ์์ด์ ํธ์๊ฒ ์กฐ๊ฑด์ด ์ ์ฉ๋๋ ํ๋กฌํํธ์ ๋ง์ง๋ง ์ค์ ๊ตฌ์ฑํ๋ฏ๋ก ์์ด์ ํธ๊ฐ ์ด์ ์์ ์์ ์ํํ ๊ฒ๊ณผ ์ ํํ ๋์ผํ ๋ฐฉ์์ผ๋ก ์์ ๋ฅผ ์๋ฃํ๋๋ก ๊ฐ๋ ฅํ๊ฒ ์ํฅ์ ๋ฏธ์นฉ๋๋ค. | |
๋๋ฌด ์์ธํ ์ค๋ช ํ์ง ์๋๋ผ๋ ์ฑํ ํ ํ๋ฆฟ์ ํ๋กฌํํธ ๊ตฌ์กฐ๋ ๋์ผํ์ง๋ง ์์ ์ ์คํ์ผ์ด ์ฝ๊ฐ ๋ค๋ฆ ๋๋ค. *์๋ฅผ ๋ค๋ฉด*: | |
````text | |
[...] | |
===== | |
Human: Answer the question in the variable `question` about the image stored in the variable `image`. | |
Assistant: I will use the tool `image_qa` to answer the question on the input image. | |
```py | |
answer = image_qa(text=question, image=image) | |
print(f"The answer is {answer}") | |
``` | |
Human: I tried this code, it worked but didn't give me a good result. The question is in French | |
Assistant: In this case, the question needs to be translated first. I will use the tool `translator` to do this. | |
```py | |
translated_question = translator(question=question, src_lang="French", tgt_lang="English") | |
print(f"The translated question is {translated_question}.") | |
answer = image_qa(text=translated_question, image=image) | |
print(f"The answer is {answer}") | |
``` | |
===== | |
[...] | |
```` | |
`run` ํ๋กฌํํธ์ ์์๋ ๋ฐ๋๋ก, ๊ฐ `chat` ํ๋กฌํํธ์ ์์๋ *Human(์ฌ๋)*๊ณผ *Assistant(์ด์์คํดํธ)* ๊ฐ์ ํ๋ ์ด์์ ๊ตํ์ด ์์ต๋๋ค. ๋ชจ๋ ๊ตํ์ `run` ํ๋กฌํํธ์ ์์ ์ ์ฌํ ๊ตฌ์กฐ๋ก ๋์ด ์์ต๋๋ค. | |
์ฌ์ฉ์์ ์ ๋ ฅ์ด *Human:* ๋ค์ ์ถ๊ฐ๋๋ฉฐ, ์์ด์ ํธ์๊ฒ ์ฝ๋๋ฅผ ์์ฑํ๊ธฐ ์ ์ ์ํํด์ผ ํ ์์ ์ ๋จผ์ ์์ฑํ๋ผ๋ ๋ฉ์์ง๊ฐ ํ์๋ฉ๋๋ค. | |
๊ตํ์ ์ด์ ๊ตํ์ ๊ธฐ๋ฐ์ผ๋ก ํ ์ ์์ผ๋ฏ๋ก ์์ ๊ฐ์ด ์ฌ์ฉ์๊ฐ "**์ด** ์ฝ๋๋ฅผ ์๋ํ์ต๋๋ค"๋ผ๊ณ ์ ๋ ฅํ๋ฉด ์ด์ ์ ์์ฑ๋ ์์ด์ ํธ์ ์ฝ๋๋ฅผ ์ฐธ์กฐํ์ฌ ๊ณผ๊ฑฐ ๊ตํ์ ์ฐธ์กฐํ ์ ์์ต๋๋ค. | |
`.chat`์ ์คํํ๋ฉด ์ฌ์ฉ์์ ์ ๋ ฅ ๋๋ *์์ *์ด ๋ฏธ์์ฑ๋ ์์์ ์์๋ก ์บ์คํ ๋ฉ๋๋ค: | |
```text | |
Human: <user-input>\n\nAssistant: | |
``` | |
๊ทธ๋ฌ๋ฉด ์์ด์ ํธ๊ฐ ์ด๋ฅผ ์์ฑํฉ๋๋ค. `run` ๋ช ๋ น๊ณผ ๋ฌ๋ฆฌ `chat` ๋ช ๋ น์ ์๋ฃ๋ ์์ ๋ฅผ ํ๋กฌํํธ์ ์ถ๊ฐํ์ฌ ์์ด์ ํธ์๊ฒ ๋ค์ `chat` ์ฐจ๋ก์ ๋ํ ๋ ๋ง์ ๋ฌธ๋งฅ์ ์ ๊ณตํฉ๋๋ค. | |
์ด์ ํ๋กฌํํธ๊ฐ ์ด๋ป๊ฒ ๊ตฌ์ฑ๋์ด ์๋์ง ์์์ผ๋ ์ด๋ป๊ฒ ์ฌ์ฉ์ ์ ์ํ ์ ์๋์ง ์ดํด๋ด ์๋ค! | |
### ์ข์ ์ฌ์ฉ์ ์ ๋ ฅ ์์ฑํ๊ธฐ[[writing-good-user-inputs]] | |
๋๊ท๋ชจ ์ธ์ด ๋ชจ๋ธ์ด ์ฌ์ฉ์์ ์๋๋ฅผ ์ดํดํ๋ ๋ฅ๋ ฅ์ด ์ ์ ๋ ํฅ์๋๊ณ ์์ง๋ง, ์์ด์ ํธ๊ฐ ์ฌ๋ฐ๋ฅธ ์์ ์ ์ ํํ ์ ์๋๋ก ์ต๋ํ ์ ํ์ฑ์ ์ ์งํ๋ ๊ฒ์ ํฐ ๋์์ด ๋ฉ๋๋ค. | |
์ต๋ํ ์ ํํ๋ค๋ ๊ฒ์ ๋ฌด์์ ์๋ฏธํ ๊น์? | |
์์ด์ ํธ๋ ํ๋กฌํํธ์์ ๋๊ตฌ ์ด๋ฆ ๋ชฉ๋ก๊ณผ ํด๋น ์ค๋ช ์ ๋ณผ ์ ์์ต๋๋ค. | |
๋ ๋ง์ ๋๊ตฌ๊ฐ ์ถ๊ฐ๋ ์๋ก ์์ด์ ํธ๊ฐ ์ฌ๋ฐ๋ฅธ ๋๊ตฌ๋ฅผ ์ ํํ๊ธฐ๊ฐ ๋ ์ด๋ ค์์ง๊ณ ์คํํ ๋๊ตฌ์ ์ฌ๋ฐ๋ฅธ ์์๋ฅผ ์ ํํ๋ ๊ฒ์ ๋์ฑ ์ด๋ ค์์ง๋๋ค. | |
์ผ๋ฐ์ ์ธ ์คํจ ์ฌ๋ก๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค. ์ฌ๊ธฐ์๋ ๋ถ์ํ ์ฝ๋๋ง ๋ฐํํ๊ฒ ์ต๋๋ค. | |
```py | |
from transformers import HfAgent | |
agent = HfAgent("https://api-inference.huggingface.co/models/bigcode/starcoder") | |
agent.run("Show me a tree", return_code=True) | |
``` | |
๊ทธ๋ฌ๋ฉด ๋ค์ ๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅ๋ฉ๋๋ค: | |
```text | |
==Explanation from the agent== | |
I will use the following tool: `image_segmenter` to create a segmentation mask for the image. | |
==Code generated by the agent== | |
mask = image_segmenter(image, prompt="tree") | |
``` | |
์ฐ๋ฆฌ๊ฐ ์ํ๋ ๊ฒฐ๊ณผ๊ฐ ์๋ ์๋ ์์ต๋๋ค. ๋์ ๋๋ฌด ์ด๋ฏธ์ง๊ฐ ์์ฑ๋๊ธฐ๋ฅผ ์ํ ๊ฐ๋ฅ์ฑ์ด ๋ ๋์ต๋๋ค. | |
๋ฐ๋ผ์ ์์ด์ ํธ๊ฐ ํน์ ๋๊ตฌ๋ฅผ ์ฌ์ฉํ๋๋ก ์ ๋ํ๋ ค๋ฉด ๋๊ตฌ์ ์ด๋ฆ๊ณผ ์ค๋ช ์ ์๋ ์ค์ํ ํค์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ๋งค์ฐ ์ ์ฉํ ์ ์์ต๋๋ค. ํ๋ฒ ์ดํด๋ณด๊ฒ ์ต๋๋ค. | |
```py | |
agent.toolbox["image_generator"].description | |
``` | |
```text | |
'This is a tool that creates an image according to a prompt, which is a text description. It takes an input named `prompt` which contains the image description and outputs an image. | |
``` | |
์ด๋ฆ๊ณผ ์ค๋ช ์ "image", "prompt", "create" ๋ฐ "generate" ํค์๋๋ฅผ ์ฌ์ฉํฉ๋๋ค. ์ด ๋จ์ด๋ค์ ์ฌ์ฉํ๋ฉด ๋ ์ ์๋ํ ๊ฐ๋ฅ์ฑ์ด ๋์ต๋๋ค. ํ๋กฌํํธ๋ฅผ ์กฐ๊ธ ๋ ๊ตฌ์ฒดํํด ๋ณด๊ฒ ์ต๋๋ค. | |
```py | |
agent.run("Create an image of a tree", return_code=True) | |
``` | |
์ด ์ฝ๋๋ ๋ค์ ํ๋กฌํํธ๋ฅผ ๋ง๋ค์ด๋ ๋๋ค: | |
```text | |
==Explanation from the agent== | |
I will use the following tool `image_generator` to generate an image of a tree. | |
==Code generated by the agent== | |
image = image_generator(prompt="tree") | |
``` | |
ํจ์ฌ ๋ซ๋ค์! ์ ํฌ๊ฐ ์ํ๋ ๊ฒ๊ณผ ๋น์ทํด ๋ณด์ ๋๋ค. | |
์ฆ, ์์ด์ ํธ๊ฐ ์์ ์ ์ฌ๋ฐ๋ฅธ ๋๊ตฌ์ ์ฌ๋ฐ๋ฅด๊ฒ ๋งคํํ๋ ๋ฐ ์ด๋ ค์์ ๊ฒช๊ณ ์๋ค๋ฉด ๋๊ตฌ ์ด๋ฆ๊ณผ ์ค๋ช ์์ ๊ฐ์ฅ ๊ด๋ จ์ฑ์ด ๋์ ํค์๋๋ฅผ ์ฐพ์๋ณด๊ณ ์ด๋ฅผ ํตํด ์์ ์์ฒญ์ ๊ตฌ์ฒดํํด ๋ณด์ธ์. | |
### ๋๊ตฌ ์ค๋ช ์ฌ์ฉ์ ์ ์ํ๊ธฐ[[customizing-the-tool-descriptions]] | |
์์ ์ดํด๋ณธ ๊ฒ์ฒ๋ผ ์์ด์ ํธ๋ ๊ฐ ๋๊ตฌ์ ์ด๋ฆ๊ณผ ์ค๋ช ์ ์ก์ธ์คํ ์ ์์ต๋๋ค. | |
๊ธฐ๋ณธ ๋๊ตฌ์๋ ๋งค์ฐ ์ ํํ ์ด๋ฆ๊ณผ ์ค๋ช ์ด ์์ด์ผ ํ์ง๋ง ํน์ ์ฌ์ฉ ์ฌ๋ก์ ๋ง๊ฒ ๋๊ตฌ์ ์ค๋ช ์ด๋ ์ด๋ฆ์ ๋ณ๊ฒฝํ๋ ๊ฒ์ด ๋์์ด ๋ ์๋ ์์ต๋๋ค. | |
์ด๋ ๋งค์ฐ ์ ์ฌํ ์ฌ๋ฌ ๋๊ตฌ๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ํน์ ๋๋ฉ์ธ(*์*: ์ด๋ฏธ์ง ์์ฑ ๋ฐ ๋ณํ)์๋ง ์์ด์ ํธ๋ฅผ ์ฌ์ฉํ๋ ค๋ ๊ฒฝ์ฐ์ ํนํ ์ค์ํด์ง ์ ์์ต๋๋ค. | |
์ผ๋ฐ์ ์ธ ๋ฌธ์ ๋ ์ด๋ฏธ์ง ์์ฑ ์์ ์ ๋ง์ด ์ฌ์ฉ๋๋ ๊ฒฝ์ฐ ์์ด์ ํธ๊ฐ ์ด๋ฏธ์ง ์์ฑ๊ณผ ์ด๋ฏธ์ง ๋ณํ/์์ ์ ํผ๋ํ๋ ๊ฒ์ ๋๋ค. *์๋ฅผ ๋ค์ด,* | |
```py | |
agent.run("Make an image of a house and a car", return_code=True) | |
``` | |
๊ทธ๋ฌ๋ฉด ๋ค์ ๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅ๋ฉ๋๋ค: | |
```text | |
==Explanation from the agent== | |
I will use the following tools `image_generator` to generate an image of a house and `image_transformer` to transform the image of a car into the image of a house. | |
==Code generated by the agent== | |
house_image = image_generator(prompt="A house") | |
car_image = image_generator(prompt="A car") | |
house_car_image = image_transformer(image=car_image, prompt="A house") | |
``` | |
๊ฒฐ๊ณผ๋ฌผ์ด ์ฐ๋ฆฌ๊ฐ ์ฌ๊ธฐ์ ์ํ๋ ๊ฒ๊ณผ ์ ํํ ์ผ์นํ์ง ์์ ์ ์์ต๋๋ค. ์์ด์ ํธ๊ฐ `image_generator`์ `image_transformer`์ ์ฐจ์ด์ ์ ์ดํดํ๊ธฐ ์ด๋ ค์์ ๋ ๊ฐ์ง๋ฅผ ํจ๊ป ์ฌ์ฉํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ ๊ฒ ๊ฐ์ต๋๋ค. | |
์ฌ๊ธฐ์ `image_transformer`์ ๋๊ตฌ ์ด๋ฆ๊ณผ ์ค๋ช ์ ๋ณ๊ฒฝํ์ฌ ์์ด์ ํธ๊ฐ ๋์ธ ์ ์์ต๋๋ค. | |
"image" ๋ฐ "prompt"์ ์ฝ๊ฐ ๋ถ๋ฆฌํ๊ธฐ ์ํด `modifier`๋ผ๊ณ ๋์ ๋ถ๋ฅด๊ฒ ์ต๋๋ค: | |
```py | |
agent.toolbox["modifier"] = agent.toolbox.pop("image_transformer") | |
agent.toolbox["modifier"].description = agent.toolbox["modifier"].description.replace( | |
"transforms an image according to a prompt", "modifies an image" | |
) | |
``` | |
์ด์ "modify"์ ์ ์ด๋ฏธ์ง ํ๋ก์ธ์๋ฅผ ์ฌ์ฉํ๋ผ๋ ๊ฐ๋ ฅํ ์ ํธ์ด๋ฏ๋ก ์์ ํ๋กฌํํธ์ ๋์์ด ๋ ๊ฒ์ ๋๋ค. ๋ค์ ์คํํด ๋ด ์๋ค. | |
```py | |
agent.run("Make an image of a house and a car", return_code=True) | |
``` | |
์ฌ๊ธฐ์ ๋ค์๊ณผ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ์ป๊ฒ ๋ฉ๋๋ค: | |
```text | |
==Explanation from the agent== | |
I will use the following tools: `image_generator` to generate an image of a house, then `image_generator` to generate an image of a car. | |
==Code generated by the agent== | |
house_image = image_generator(prompt="A house") | |
car_image = image_generator(prompt="A car") | |
``` | |
์ฐ๋ฆฌ๊ฐ ์ผ๋์ ๋์๋ ๊ฒ๊ณผ ํ์คํ ๋ ๊ฐ๊น์์ก์ต๋๋ค! ํ์ง๋ง ์ง๊ณผ ์๋์ฐจ๊ฐ ๋ชจ๋ ๊ฐ์ ์ด๋ฏธ์ง์ ํฌํจ๋๋ฉด ์ข๊ฒ ์ต๋๋ค. ์์ ์ ๋จ์ผ ์ด๋ฏธ์ง ์์ฑ์ ๋ ์ง์คํ๋ฉด ๋์์ด ๋ ๊ฒ์ ๋๋ค: | |
```py | |
agent.run("Create image: 'A house and car'", return_code=True) | |
``` | |
```text | |
==Explanation from the agent== | |
I will use the following tool: `image_generator` to generate an image. | |
==Code generated by the agent== | |
image = image_generator(prompt="A house and car") | |
``` | |
<Tip warning={true}> | |
์์ด์ ํธ๋ ์ฌ์ ํ ํนํ ์ฌ๋ฌ ๊ฐ์ฒด์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๋ ๊ฒ๊ณผ ๊ฐ์ด ์ฝ๊ฐ ๋ ๋ณต์กํ ์ฌ์ฉ ์ฌ๋ก์์ ์ทจ์ฝํ ๊ฒฝ์ฐ๊ฐ ๋ง์ต๋๋ค. | |
์์ผ๋ก ๋ช ๋ฌ ์์ ์์ด์ ํธ ์์ฒด์ ๊ธฐ๋ณธ ํ๋กฌํํธ๊ฐ ๋์ฑ ๊ฐ์ ๋์ด ์์ด์ ํธ๊ฐ ๋ค์ํ ์ฌ์ฉ์ ์ ๋ ฅ์ ๋์ฑ ๊ฐ๋ ฅํ๊ฒ ๋์ํ ์ ์๋๋ก ํ ์์ ์ ๋๋ค. | |
</Tip> | |
### ์ ์ฒด ํ๋กฌํํธ ์ฌ์ฉ์ ์ ์ํ๊ธฐ[[customizing-the-whole-prompt]] | |
์ฌ์ฉ์์๊ฒ ์ต๋ํ์ ์ ์ฐ์ฑ์ ์ ๊ณตํ๊ธฐ ์ํด [์](#structure-of-the-prompt)์ ์ค๋ช ๋ ์ ์ฒด ํ๋กฌํํธ ํ ํ๋ฆฟ์ ์ฌ์ฉ์๊ฐ ๋ฎ์ด์ธ ์ ์์ต๋๋ค. | |
์ด ๊ฒฝ์ฐ ์ฌ์ฉ์ ์ ์ ํ๋กฌํํธ์ ์๊ฐ ์น์ , ๋๊ตฌ ์น์ , ์์ ์น์ ๋ฐ ๋ฏธ์์ฑ ์์ ์น์ ์ด ํฌํจ๋์ด ์๋์ง ํ์ธํ์ธ์. | |
`run` ํ๋กฌํํธ ํ ํ๋ฆฟ์ ๋ฎ์ด์ฐ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ํ๋ฉด ๋ฉ๋๋ค: | |
```py | |
template = """ [...] """ | |
agent = HfAgent(your_endpoint, run_prompt_template=template) | |
``` | |
<Tip warning={true}> | |
์์ด์ ํธ๊ฐ ์ฌ์ฉ ๊ฐ๋ฅํ ๋๊ตฌ๋ฅผ ์ธ์ํ๊ณ ์ฌ์ฉ์์ ํ๋กฌํํธ๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ์ฝ์ ํ ์ ์๋๋ก `<<all_tools>>` ๋ฌธ์์ด๊ณผ `<<prompt>>`๋ฅผ `template` ์ด๋๊ฐ์ ์ ์ํด์ผ ํฉ๋๋ค. | |
</Tip> | |
๋ง์ฐฌ๊ฐ์ง๋ก `chat` ํ๋กฌํํธ ํ ํ๋ฆฟ์ ๋ฎ์ด์ธ ์ ์์ต๋๋ค. `chat` ๋ชจ๋์์๋ ํญ์ ๋ค์๊ณผ ๊ฐ์ ๊ตํ ํ์์ ์ฌ์ฉํ๋ค๋ ์ ์ ์ ์ํ์ธ์: | |
```text | |
Human: <<task>> | |
Assistant: | |
``` | |
๋ฐ๋ผ์ ์ฌ์ฉ์ ์ ์ `chat` ํ๋กฌํํธ ํ ํ๋ฆฟ์ ์์ ์์๋ ์ด ํ์์ ์ฌ์ฉํ๋ ๊ฒ์ด ์ค์ํฉ๋๋ค. | |
๋ค์๊ณผ ๊ฐ์ด ์ธ์คํด์คํ ํ ๋ `chat` ํ ํ๋ฆฟ์ ๋ฎ์ด์ธ ์ ์์ต๋๋ค. | |
``` | |
template = """ [...] """ | |
agent = HfAgent(url_endpoint=your_endpoint, chat_prompt_template=template) | |
``` | |
<Tip warning={true}> | |
์์ด์ ํธ๊ฐ ์ฌ์ฉ ๊ฐ๋ฅํ ๋๊ตฌ๋ฅผ ์ธ์ํ ์ ์๋๋ก `<<all_tools>>` ๋ฌธ์์ด์ `template` ์ด๋๊ฐ์ ์ ์ํด์ผ ํฉ๋๋ค. | |
</Tip> | |
๋ ๊ฒฝ์ฐ ๋ชจ๋ ์ปค๋ฎค๋ํฐ์ ๋๊ตฐ๊ฐ๊ฐ ํธ์คํ ํ๋ ํ ํ๋ฆฟ์ ์ฌ์ฉํ๋ ค๋ ๊ฒฝ์ฐ ํ๋กฌํํธ ํ ํ๋ฆฟ ๋์ ์ ์ฅ์ ID๋ฅผ ์ ๋ฌํ ์ ์์ต๋๋ค. | |
๊ธฐ๋ณธ ํ๋กฌํํธ๋ [์ด ์ ์ฅ์](https://huggingface.co/datasets/huggingface-tools/default-prompts)๋ฅผ ์๋ก ๋ค ์ ์์ต๋๋ค. | |
Hub์ ์ ์ฅ์์ ์ฌ์ฉ์ ์ ์ ํ๋กฌํํธ๋ฅผ ์ ๋ก๋ํ์ฌ ์ปค๋ฎค๋ํฐ์ ๊ณต์ ํ๋ ค๋ฉด ๋ค์์ ํ์ธํ์ธ์: | |
- ๋ฐ์ดํฐ ์ธํธ ์ ์ฅ์๋ฅผ ์ฌ์ฉํ์ธ์. | |
- `run` ๋ช ๋ น์ ๋ํ ํ๋กฌํํธ ํ ํ๋ฆฟ์ `run_prompt_template.txt`๋ผ๋ ํ์ผ์ ๋ฃ์ผ์ธ์. | |
- `chat` ๋ช ๋ น์ ๋ํ ํ๋กฌํํธ ํ ํ๋ฆฟ์ `chat_prompt_template.txt`๋ผ๋ ํ์ผ์ ๋ฃ์ผ์ธ์. | |
## ์ฌ์ฉ์ ์ ์ ๋๊ตฌ ์ฌ์ฉํ๊ธฐ[[using-custom-tools]] | |
์ด ์น์ ์์๋ ์ด๋ฏธ์ง ์์ฑ์ ํนํ๋ ๋ ๊ฐ์ง ๊ธฐ์กด ์ฌ์ฉ์ ์ ์ ๋๊ตฌ๋ฅผ ํ์ฉํ๊ฒ ์ต๋๋ค: | |
- ๋ ๋ง์ ์ด๋ฏธ์ง ์์ ์ ํ์ฉํ๊ธฐ ์ํด [huggingface-tools/image-transformation](https://huggingface.co/spaces/huggingface-tools/image-transformation)์ | |
[diffusers/controlnet-canny-tool](https://huggingface.co/spaces/diffusers/controlnet-canny-tool)๋ก ๋์ฒดํฉ๋๋ค. | |
- ๊ธฐ๋ณธ ๋๊ตฌ ์์์ ์ด๋ฏธ์ง ์ ์ค์ผ์ผ๋ง์ ์ํ ์๋ก์ด ๋๊ตฌ๊ฐ ์ถ๊ฐ๋์์ต๋๋ค: | |
[diffusers/latent-upscaler-tool](https://huggingface.co/spaces/diffusers/latent-upscaler-tool)๊ฐ ๊ธฐ์กด ์ด๋ฏธ์ง ๋ณํ ๋๊ตฌ๋ฅผ ๋์ฒดํฉ๋๋ค. | |
ํธ๋ฆฌํ [`load_tool`] ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์ฌ์ฉ์ ์ ์ ๋๊ตฌ๋ฅผ ๊ฐ์ ธ์ค๋ ๊ฒ์ผ๋ก ์์ํ๊ฒ ์ต๋๋ค: | |
```py | |
from transformers import load_tool | |
controlnet_transformer = load_tool("diffusers/controlnet-canny-tool") | |
upscaler = load_tool("diffusers/latent-upscaler-tool") | |
``` | |
์์ด์ ํธ์๊ฒ ์ฌ์ฉ์ ์ ์ ๋๊ตฌ๋ฅผ ์ถ๊ฐํ๋ฉด ๋๊ตฌ์ ์ค๋ช ๊ณผ ์ด๋ฆ์ด ์์ด์ ํธ์ ํ๋กฌํํธ์ ์๋์ผ๋ก ํฌํจ๋ฉ๋๋ค. | |
๋ฐ๋ผ์ ์์ด์ ํธ๊ฐ ์ฌ์ฉ ๋ฐฉ๋ฒ์ ์ดํดํ ์ ์๋๋ก ์ฌ์ฉ์ ์ ์ ๋๊ตฌ์ ์ค๋ช ๊ณผ ์ด๋ฆ์ ์ ์์ฑํด์ผ ํฉ๋๋ค. | |
`controlnet_transformer`์ ์ค๋ช ๊ณผ ์ด๋ฆ์ ์ดํด๋ณด๊ฒ ์ต๋๋ค: | |
```py | |
print(f"Description: '{controlnet_transformer.description}'") | |
print(f"Name: '{controlnet_transformer.name}'") | |
``` | |
๊ทธ๋ฌ๋ฉด ๋ค์ ๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅ๋ฉ๋๋ค: | |
```text | |
Description: 'This is a tool that transforms an image with ControlNet according to a prompt. | |
It takes two inputs: `image`, which should be the image to transform, and `prompt`, which should be the prompt to use to change it. It returns the modified image.' | |
Name: 'image_transformer' | |
``` | |
์ด๋ฆ๊ณผ ์ค๋ช ์ด ์ ํํ๊ณ [ํ๋ ์ดํ ๋ ๋๊ตฌ ์ธํธ(curated set of tools)](./transformers_agents#a-curated-set-of-tools)์ ์คํ์ผ์ ๋ง์ต๋๋ค. | |
๋ค์์ผ๋ก, `controlnet_transformer`์ `upscaler`๋ก ์์ด์ ํธ๋ฅผ ์ธ์คํด์คํํด ๋ด ์๋ค: | |
```py | |
tools = [controlnet_transformer, upscaler] | |
agent = HfAgent("https://api-inference.huggingface.co/models/bigcode/starcoder", additional_tools=tools) | |
``` | |
์ด ๋ช ๋ น์ ์คํํ๋ฉด ๋ค์ ์ ๋ณด๊ฐ ํ์๋ฉ๋๋ค: | |
```text | |
image_transformer has been replaced by <transformers_modules.diffusers.controlnet-canny-tool.bd76182c7777eba9612fc03c0 | |
8718a60c0aa6312.image_transformation.ControlNetTransformationTool object at 0x7f1d3bfa3a00> as provided in `additional_tools` | |
``` | |
ํ๋ ์ดํ ๋ ๋๊ตฌ ์ธํธ์๋ ์ด๋ฏธ 'image_transformer' ๋๊ตฌ๊ฐ ์์ผ๋ฉฐ, ์ด ๋๊ตฌ๋ ์ฌ์ฉ์ ์ ์ ๋๊ตฌ๋ก ๋์ฒด๋ฉ๋๋ค. | |
<Tip> | |
๊ธฐ์กด ๋๊ตฌ์ ๋๊ฐ์ ์์ ์ ์ฌ์ฉ์ ์ ์ ๋๊ตฌ๋ฅผ ์ฌ์ฉํ๋ ค๋ ๊ฒฝ์ฐ ๊ธฐ์กด ๋๊ตฌ๋ฅผ ๋ฎ์ด์ฐ๋ ๊ฒ์ด ์ ์ฉํ ์ ์์ต๋๋ค. | |
์์ด์ ํธ๊ฐ ํด๋น ์์ ์ ๋ฅ์ํ๊ธฐ ๋๋ฌธ์ ๋๋ค. | |
์ด ๊ฒฝ์ฐ ์ฌ์ฉ์ ์ ์ ๋๊ตฌ๊ฐ ๋ฎ์ด์ด ๋๊ตฌ์ ์ ํํ ๋์ผํ API๋ฅผ ๋ฐ๋ผ์ผ ํ๋ฉฐ, ๊ทธ๋ ์ง ์์ผ๋ฉด ํด๋น ๋๊ตฌ๋ฅผ ์ฌ์ฉํ๋ ๋ชจ๋ ์์ ๊ฐ ์ ๋ฐ์ดํธ๋๋๋ก ํ๋กฌํํธ ํ ํ๋ฆฟ์ ์กฐ์ ํด์ผ ํ๋ค๋ ์ ์ ์ ์ํ์ธ์. | |
</Tip> | |
์ ์ค์ผ์ผ๋ฌ ๋๊ตฌ์ ์ง์ ๋ 'image_upscaler'๋ผ๋ ์ด๋ฆ ์์ง ๊ธฐ๋ณธ ๋๊ตฌ ์์์๋ ์กด์ฌํ์ง ์๊ธฐ ๋๋ฌธ์, ๋๊ตฌ ๋ชฉ๋ก์ ํด๋น ์ด๋ฆ์ด ๊ฐ๋จํ ์ถ๊ฐ๋์์ต๋๋ค. | |
์์ด์ ํธ๊ฐ ํ์ฌ ์ฌ์ฉํ ์ ์๋ ๋๊ตฌ ์์๋ ์ธ์ ๋ ์ง `agent.toolbox` ์์ฑ์ ํตํด ํ์ธํ ์ ์์ต๋๋ค: | |
```py | |
print("\n".join([f"- {a}" for a in agent.toolbox.keys()])) | |
``` | |
```text | |
- document_qa | |
- image_captioner | |
- image_qa | |
- image_segmenter | |
- transcriber | |
- summarizer | |
- text_classifier | |
- text_qa | |
- text_reader | |
- translator | |
- image_transformer | |
- text_downloader | |
- image_generator | |
- video_generator | |
- image_upscaler | |
``` | |
์์ด์ ํธ์ ๋๊ตฌ ์์์ `image_upscaler`๊ฐ ์ถ๊ฐ๋ ์ ์ ์ฃผ๋ชฉํ์ธ์. | |
์ด์ ์๋ก์ด ๋๊ตฌ๋ฅผ ์ฌ์ฉํด๋ด ์๋ค! [Transformers Agents Quickstart](./transformers_agents#single-execution-run)์์ ์์ฑํ ์ด๋ฏธ์ง๋ฅผ ๋ค์ ์ฌ์ฉํ๊ฒ ์ต๋๋ค. | |
```py | |
from diffusers.utils import load_image | |
image = load_image( | |
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rivers_and_lakes.png" | |
) | |
``` | |
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rivers_and_lakes.png" width=200> | |
์ด๋ฏธ์ง๋ฅผ ์๋ฆ๋ค์ด ๊ฒจ์ธ ํ๊ฒฝ์ผ๋ก ๋ฐ๊ฟ ๋ด ์๋ค: | |
```py | |
image = agent.run("Transform the image: 'A frozen lake and snowy forest'", image=image) | |
``` | |
```text | |
==Explanation from the agent== | |
I will use the following tool: `image_transformer` to transform the image. | |
==Code generated by the agent== | |
image = image_transformer(image, prompt="A frozen lake and snowy forest") | |
``` | |
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rivers_and_lakes_winter.png" width=200> | |
์๋ก์ด ์ด๋ฏธ์ง ์ฒ๋ฆฌ ๋๊ตฌ๋ ์ด๋ฏธ์ง๋ฅผ ๋งค์ฐ ๊ฐ๋ ฅํ๊ฒ ์์ ํ ์ ์๋ ControlNet์ ๊ธฐ๋ฐ์ผ๋ก ํฉ๋๋ค. | |
๊ธฐ๋ณธ์ ์ผ๋ก ์ด๋ฏธ์ง ์ฒ๋ฆฌ ๋๊ตฌ๋ 512x512 ํฝ์ ํฌ๊ธฐ์ ์ด๋ฏธ์ง๋ฅผ ๋ฐํํฉ๋๋ค. ์ด๋ฅผ ์ ์ค์ผ์ผ๋งํ ์ ์๋์ง ์ดํด๋ด ์๋ค. | |
```py | |
image = agent.run("Upscale the image", image) | |
``` | |
```text | |
==Explanation from the agent== | |
I will use the following tool: `image_upscaler` to upscale the image. | |
==Code generated by the agent== | |
upscaled_image = image_upscaler(image) | |
``` | |
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rivers_and_lakes_winter_upscale.png" width=400> | |
์์ด์ ํธ๋ ์ ์ค์ผ์ผ๋ฌ ๋๊ตฌ์ ์ค๋ช ๊ณผ ์ด๋ฆ๋ง ๋ณด๊ณ ๋ฐฉ๊ธ ์ถ๊ฐํ ์ ์ค์ผ์ผ๋ฌ ๋๊ตฌ์ "์ด๋ฏธ์ง ์ ์ค์ผ์ผ๋ง"์ด๋ผ๋ ํ๋กฌํํธ๋ฅผ ์๋์ผ๋ก ๋งคํํ์ฌ ์ฌ๋ฐ๋ฅด๊ฒ ์คํํ์ต๋๋ค. | |
๋ค์์ผ๋ก ์ ์ฌ์ฉ์ ์ ์ ๋๊ตฌ๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ ์ดํด๋ณด๊ฒ ์ต๋๋ค. | |
### ์ ๋๊ตฌ ์ถ๊ฐํ๊ธฐ[[adding-new-tools]] | |
์ด ์น์ ์์๋ ์์ด์ ํธ์๊ฒ ์ถ๊ฐํ ์ ์๋ ์ ๋๊ตฌ๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ ๋๋ฆฝ๋๋ค. | |
#### ์ ๋๊ตฌ ๋ง๋ค๊ธฐ[[creating-a-new-tool]] | |
๋จผ์ ๋๊ตฌ๋ฅผ ๋ง๋๋ ๊ฒ๋ถํฐ ์์ํ๊ฒ ์ต๋๋ค. | |
ํน์ ์์ ์ ๋ํด ๊ฐ์ฅ ๋ง์ ๋ค์ด๋ก๋๋ฅผ ๋ฐ์ Hugging Face Hub์ ๋ชจ๋ธ์ ๊ฐ์ ธ์ค๋, ๊ทธ๋ค์ง ์ ์ฉํ์ง๋ ์์ง๋ง ์ฌ๋ฏธ์๋ ์์ ์ ์ถ๊ฐํ๊ฒ ์ต๋๋ค. | |
๋ค์ ์ฝ๋๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค: | |
```python | |
from huggingface_hub import list_models | |
task = "text-classification" | |
model = next(iter(list_models(filter=task, sort="downloads", direction=-1))) | |
print(model.id) | |
``` | |
`text-classification`(ํ ์คํธ ๋ถ๋ฅ) ์์ ์ ๊ฒฝ์ฐ `'facebook/bart-large-mnli'`๋ฅผ ๋ฐํํ๊ณ , `translation`(๋ฒ์ญ) ์์ ์ ๊ฒฝ์ฐ `'t5-base'`๋ฅผ ๋ฐํํฉ๋๋ค. | |
์ด๋ฅผ ์์ด์ ํธ๊ฐ ํ์ฉํ ์ ์๋ ๋๊ตฌ๋ก ๋ณํํ๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผ ํ ๊น์? | |
๋ชจ๋ ๋๊ตฌ๋ ํ์ํ ์ฃผ์ ์์ฑ์ ๋ณด์ ํ๋ ์ํผํด๋์ค `Tool`์ ์์กดํฉ๋๋ค. ์ด๋ฅผ ์์ํ๋ ํด๋์ค๋ฅผ ๋ง๋ค์ด ๋ณด๊ฒ ์ต๋๋ค: | |
```python | |
from transformers import Tool | |
class HFModelDownloadsTool(Tool): | |
pass | |
``` | |
์ด ํด๋์ค์๋ ๋ช ๊ฐ์ง ์๊ตฌ์ฌํญ์ด ์์ต๋๋ค: | |
- ๋๊ตฌ ์์ฒด์ ์ด๋ฆ์ ํด๋นํ๋ `name` ์์ฑ. ์ํ๋ช ์ด ์๋ ๋ค๋ฅธ ๋๊ตฌ์ ํธํ๋๋๋ก `model_download_counter`๋ก ์ด๋ฆ์ ์ง์ ํ๊ฒ ์ต๋๋ค. | |
- ์์ด์ ํธ์ ํ๋กฌํํธ๋ฅผ ์ฑ์ฐ๋ ๋ฐ ์ฌ์ฉ๋๋ ์์ฑ `description`. | |
- `inputs` ๋ฐ `outputs` ์์ฑ. ์ด๋ฅผ ์ ์ํ๋ฉด Python ์ธํฐํ๋ฆฌํฐ๊ฐ ์ ํ์ ๋ํ ์ ๋ณด์ ์ ๊ฐํ ์ ํ์ ํ๋ ๋ฐ ๋์์ด ๋๋ฉฐ, | |
๋๊ตฌ๋ฅผ ํ๋ธ์ ํธ์ํ ๋ gradio ๋ฐ๋ชจ๋ฅผ ์์ฑํ ์ ์์ต๋๋ค. | |
๋ ์์ฑ ๋ชจ๋ ๊ฐ์ 'ํ ์คํธ', '์ด๋ฏธ์ง' ๋๋ '์ค๋์ค'๊ฐ ๋ ์ ์๋ ์์ ๊ฐ์ ๋ฆฌ์คํธ์ ๋๋ค. | |
- ์ถ๋ก ์ฝ๋๊ฐ ํฌํจ๋ `__call__` ๋ฉ์๋. ์ด๊ฒ์ด ์ฐ๋ฆฌ๊ฐ ์์์ ๋ค๋ฃจ์๋ ์ฝ๋์ ๋๋ค! | |
์ด์ ํด๋์ค์ ๋ชจ์ต์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค: | |
```python | |
from transformers import Tool | |
from huggingface_hub import list_models | |
class HFModelDownloadsTool(Tool): | |
name = "model_download_counter" | |
description = ( | |
"This is a tool that returns the most downloaded model of a given task on the Hugging Face Hub. " | |
"It takes the name of the category (such as text-classification, depth-estimation, etc), and " | |
"returns the name of the checkpoint." | |
) | |
inputs = ["text"] | |
outputs = ["text"] | |
def __call__(self, task: str): | |
model = next(iter(list_models(filter=task, sort="downloads", direction=-1))) | |
return model.id | |
``` | |
์ด์ ๋๊ตฌ๋ฅผ ์์ฝ๊ฒ ์ฌ์ฉํ ์ ์๊ฒ ๋์์ต๋๋ค. | |
๋๊ตฌ๋ฅผ ํ์ผ์ ์ ์ฅํ๊ณ ๋ฉ์ธ ์คํฌ๋ฆฝํธ์์ ๊ฐ์ ธ์ต๋๋ค. ์ด ํ์ผ์ ์ด๋ฆ์ `model_downloads.py`๋ก ์ง์ ํ๋ฉด ๊ฒฐ๊ณผ์ ์ผ๋ก ๊ฐ์ ธ์ค๊ธฐ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค: | |
```python | |
from model_downloads import HFModelDownloadsTool | |
tool = HFModelDownloadsTool() | |
``` | |
๋ค๋ฅธ ์ฌ๋๋ค์ด ์ด ๊ธฐ๋ฅ์ ํ์ฉํ ์ ์๋๋ก ํ๊ณ ์ด๊ธฐํ๋ฅผ ๋ ๊ฐ๋จํ๊ฒ ํ๋ ค๋ฉด ๋ค์์คํ์ด์ค ์๋์ Hub๋ก ํธ์ํ๋ ๊ฒ์ด ์ข์ต๋๋ค. | |
๊ทธ๋ ๊ฒ ํ๋ ค๋ฉด `tool` ๋ณ์์์ `push_to_hub`๋ฅผ ํธ์ถํ๋ฉด ๋ฉ๋๋ค: | |
```python | |
tool.push_to_hub("hf-model-downloads") | |
``` | |
์ด์ ํ๋ธ์ ์ฝ๋๊ฐ ์๊ฒผ์ต๋๋ค! ๋ง์ง๋ง ๋จ๊ณ์ธ ์์ด์ ํธ๊ฐ ์ฝ๋๋ฅผ ์ฌ์ฉํ๋๋ก ํ๋ ๋จ๊ณ๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค. | |
#### ์์ด์ ํธ๊ฐ ๋๊ตฌ๋ฅผ ์ฌ์ฉํ๊ฒ ํ๊ธฐ[[Having-the-agent-use-the-tool]] | |
์ด์ ์ด๋ฐ ์์ผ๋ก ํ๋ธ์ ์กด์ฌํ๋ ๋๊ตฌ๋ฅผ ์ธ์คํด์คํํ ์ ์์ต๋๋ค(๋๊ตฌ์ ์ฌ์ฉ์ ์ด๋ฆ์ ๋ณ๊ฒฝํ์ธ์): | |
We now have our tool that lives on the Hub which can be instantiated as such (change the user name for your tool): | |
```python | |
from transformers import load_tool | |
tool = load_tool("lysandre/hf-model-downloads") | |
``` | |
์ด ๋๊ตฌ๋ฅผ ์์ด์ ํธ์์ ์ฌ์ฉํ๋ ค๋ฉด ์์ด์ ํธ ์ด๊ธฐํ ๋ฉ์๋์ `additional_tools` ๋งค๊ฐ๋ณ์์ ์ ๋ฌํ๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค: | |
```python | |
from transformers import HfAgent | |
agent = HfAgent("https://api-inference.huggingface.co/models/bigcode/starcoder", additional_tools=[tool]) | |
agent.run( | |
"Can you read out loud the name of the model that has the most downloads in the 'text-to-video' task on the Hugging Face Hub?" | |
) | |
``` | |
๊ทธ๋ฌ๋ฉด ๋ค์๊ณผ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅ๋ฉ๋๋ค: | |
```text | |
==Code generated by the agent== | |
model = model_download_counter(task="text-to-video") | |
print(f"The model with the most downloads is {model}.") | |
audio_model = text_reader(model) | |
==Result== | |
The model with the most downloads is damo-vilab/text-to-video-ms-1.7b. | |
``` | |
and generates the following audio. | |
| **Audio** | | |
|------------------------------------------------------------------------------------------------------------------------------------------------------| | |
| <audio controls><source src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/damo.wav" type="audio/wav"/> | | |
<Tip> | |
LLM์ ๋ฐ๋ผ ์ผ๋ถ๋ ๋งค์ฐ ์ทจ์ฝํ๊ธฐ ๋๋ฌธ์ ์ ๋๋ก ์๋ํ๋ ค๋ฉด ๋งค์ฐ ์ ํํ ํ๋กฌํํธ๊ฐ ํ์ํฉ๋๋ค. | |
์์ด์ ํธ๊ฐ ๋๊ตฌ๋ฅผ ์ ํ์ฉํ๊ธฐ ์ํด์๋ ๋๊ตฌ์ ์ด๋ฆ๊ณผ ์ค๋ช ์ ์ ์ ์ํ๋ ๊ฒ์ด ๋ฌด์๋ณด๋ค ์ค์ํฉ๋๋ค. | |
</Tip> | |
### ๊ธฐ์กด ๋๊ตฌ ๋์ฒดํ๊ธฐ[[replacing-existing-tools]] | |
์์ด์ ํธ์ ๋๊ตฌ ์์์ ์ ํญ๋ชฉ์ ๋ฐฐ์ ํ๊ธฐ๋ง ํ๋ฉด ๊ธฐ์กด ๋๊ตฌ๋ฅผ ๋์ฒดํ ์ ์์ต๋๋ค. ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค: | |
```python | |
from transformers import HfAgent, load_tool | |
agent = HfAgent("https://api-inference.huggingface.co/models/bigcode/starcoder") | |
agent.toolbox["image-transformation"] = load_tool("diffusers/controlnet-canny-tool") | |
``` | |
<Tip> | |
๋ค๋ฅธ ๋๊ตฌ๋ก ๊ต์ฒดํ ๋๋ ์ฃผ์ํ์ธ์! ์ด ์์ ์ผ๋ก ์์ด์ ํธ์ ํ๋กฌํํธ๋ ์กฐ์ ๋ฉ๋๋ค. | |
์์ ์ ๋ ์ ํฉํ ํ๋กฌํํธ๊ฐ ์์ผ๋ฉด ์ข์ ์ ์์ง๋ง, | |
๋ค๋ฅธ ๋๊ตฌ๋ณด๋ค ๋ ๋ง์ด ์ ํ๋๊ฑฐ๋ ์ ์ํ ๋๊ตฌ ๋์ ๋ค๋ฅธ ๋๊ตฌ๊ฐ ์ ํ๋ ์๋ ์์ต๋๋ค. | |
</Tip> | |
## gradio-tools ์ฌ์ฉํ๊ธฐ[[leveraging-gradio-tools]] | |
[gradio-tools](https://github.com/freddyaboulton/gradio-tools)๋ Hugging Face Spaces๋ฅผ ๋๊ตฌ๋ก ์ฌ์ฉํ ์ ์๋ ๊ฐ๋ ฅํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋๋ค. | |
๊ธฐ์กด์ ๋ง์ Spaces๋ฟ๋ง ์๋๋ผ ์ฌ์ฉ์ ์ ์ Spaces๋ฅผ ์ฌ์ฉํ์ฌ ๋์์ธํ ์ ์๋๋ก ์ง์ํฉ๋๋ค. | |
์ฐ๋ฆฌ๋ `Tool.from_gradio` ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ `gradio_tools`์ ๋ํ ์ง์์ ์ ๊ณตํฉ๋๋ค. | |
์๋ฅผ ๋ค์ด, ํ๋กฌํํธ๋ฅผ ๊ฐ์ ํ๊ณ ๋ ๋์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๊ธฐ ์ํด `gradio-tools` ํดํท์์ ์ ๊ณต๋๋ `StableDiffusionPromptGeneratorTool` ๋๊ตฌ๋ฅผ ํ์ฉํ๊ณ ์ ํฉ๋๋ค. | |
๋จผ์ `gradio_tools`์์ ๋๊ตฌ๋ฅผ ๊ฐ์ ธ์์ ์ธ์คํด์คํํฉ๋๋ค: | |
```python | |
from gradio_tools import StableDiffusionPromptGeneratorTool | |
gradio_tool = StableDiffusionPromptGeneratorTool() | |
``` | |
ํด๋น ์ธ์คํด์ค๋ฅผ `Tool.from_gradio` ๋ฉ์๋์ ์ ๋ฌํฉ๋๋ค: | |
```python | |
from transformers import Tool | |
tool = Tool.from_gradio(gradio_tool) | |
``` | |
์ด์ ์ผ๋ฐ์ ์ธ ์ฌ์ฉ์ ์ ์ ๋๊ตฌ์ ๋๊ฐ์ด ๊ด๋ฆฌํ ์ ์์ต๋๋ค. | |
์ด๋ฅผ ํ์ฉํ์ฌ `a rabbit wearing a space suit'(์ฐ์ฃผ๋ณต์ ์ ์ ํ ๋ผ)๋ผ๋ ํ๋กฌํํธ๋ฅผ ๊ฐ์ ํ์ต๋๋ค: | |
```python | |
from transformers import HfAgent | |
agent = HfAgent("https://api-inference.huggingface.co/models/bigcode/starcoder", additional_tools=[tool]) | |
agent.run("Generate an image of the `prompt` after improving it.", prompt="A rabbit wearing a space suit") | |
``` | |
๋ชจ๋ธ์ด ๋๊ตฌ๋ฅผ ์ ์ ํ ํ์ฉํฉ๋๋ค: | |
```text | |
==Explanation from the agent== | |
I will use the following tools: `StableDiffusionPromptGenerator` to improve the prompt, then `image_generator` to generate an image according to the improved prompt. | |
==Code generated by the agent== | |
improved_prompt = StableDiffusionPromptGenerator(prompt) | |
print(f"The improved prompt is {improved_prompt}.") | |
image = image_generator(improved_prompt) | |
``` | |
๋ง์ง๋ง์ผ๋ก ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๊ธฐ ์ ์: | |
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rabbit.png"> | |
<Tip warning={true}> | |
gradio-tools๋ ๋ค๋ฅธ ๋ชจ๋ฌ๋ฆฌํฐ๋ก ์์ ํ ๋์๋ *ํ ์คํธ* ์ ๋ ฅ ๋ฐ ์ถ๋ ฅ์ ํ์๋ก ํฉ๋๋ค. | |
์ด ๊ตฌํ์ ์ด๋ฏธ์ง ๋ฐ ์ค๋์ค ๊ฐ์ฒด์์ ์๋ํฉ๋๋ค. | |
ํ์ฌ๋ ์ด ๋ ๊ฐ์ง๊ฐ ํธํ๋์ง ์์ง๋ง ์ง์ ๊ฐ์ ์ ์ํด ๋ ธ๋ ฅํ๋ฉด์ ๋น ๋ฅด๊ฒ ํธํ๋ ๊ฒ์ ๋๋ค. | |
</Tip> | |
## ํฅํ Langchain๊ณผ์ ํธํ์ฑ[[future-compatibility-with-langchain]] | |
์ ํฌ๋ Langchain์ ์ข์ํ๋ฉฐ ๋งค์ฐ ๋งค๋ ฅ์ ์ธ ๋๊ตฌ ๋ชจ์์ ๊ฐ์ง๊ณ ์๋ค๊ณ ์๊ฐํฉ๋๋ค. | |
์ด๋ฌํ ๋๊ตฌ๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํด Langchain์ ๋ค๋ฅธ ๋ชจ๋ฌ๋ฆฌํฐ์ ์์ ํ ๋์๋ *ํ ์คํธ* ์ ๋ ฅ๊ณผ ์ถ๋ ฅ์ ํ์๋ก ํฉ๋๋ค. | |
์ด๋ ์ข ์ข ๊ฐ์ฒด์ ์ง๋ ฌํ๋(์ฆ, ๋์คํฌ์ ์ ์ฅ๋) ๋ฒ์ ์ ๋๋ค. | |
์ด ์ฐจ์ด๋ก ์ธํด transformers-agents์ Langchain ๊ฐ์๋ ๋ฉํฐ ๋ชจ๋ฌ๋ฆฌํฐ๊ฐ ์ฒ๋ฆฌ๋์ง ์์ต๋๋ค. | |
ํฅํ ๋ฒ์ ์์ ์ด ์ ํ์ด ํด๊ฒฐ๋๊ธฐ๋ฅผ ๋ฐ๋ผ๋ฉฐ, ์ด ํธํ์ฑ์ ๋ฌ์ฑํ ์ ์๋๋ก ์ด๋ ฌํ Langchain ์ฌ์ฉ์์ ๋์์ ํ์ํฉ๋๋ค. | |
์ ํฌ๋ ๋ ๋์ ์ง์์ ์ ๊ณตํ๊ณ ์ ํฉ๋๋ค. ๋์์ ์ฃผ๊ณ ์ถ์ผ์๋ค๋ฉด, [์ด์๋ฅผ ์ด์ด](https://github.com/huggingface/transformers/issues/new) ์๊ฒฌ์ ๊ณต์ ํด ์ฃผ์ธ์. | |