Spaces:
Runtime error
Runtime error
File size: 18,298 Bytes
96e9536 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
<!--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.
โ ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
rendered properly in your Markdown viewer.
-->
# Transformers Agent [[transformers-agent]]
<Tip warning={true}>
Transformers Agent๋ ์คํ ์ค์ธ API๋ก ์ธ์ ๋ ์ง ๋ณ๊ฒฝ๋ ์ ์์ต๋๋ค.
API ๋๋ ๊ธฐ๋ฐ ๋ชจ๋ธ์ด ๋ณ๊ฒฝ๋๊ธฐ ์ฝ๊ธฐ ๋๋ฌธ์ ์์ด์ ํธ๊ฐ ๋ฐํํ๋ ๊ฒฐ๊ณผ๋ ๋ฌ๋ผ์ง ์ ์์ต๋๋ค.
</Tip>
Transformers ๋ฒ์ 4.29.0.์์ *๋๊ตฌ*์ *์์ด์ ํธ*๋ผ๋ ์ปจ์
์ ๋์
ํ์ต๋๋ค. [์ด colab](https://colab.research.google.com/drive/1c7MHD-T1forUPGcC_jlwsIptOzpG3hSj)์์ ์ฌ์ฉํด๋ณผ ์ ์์ต๋๋ค.
๊ฐ๋จํ ๋งํ๋ฉด, Agent๋ ํธ๋์คํฌ๋จธ ์์ ์์ฐ์ด API๋ฅผ ์ ๊ณตํฉ๋๋ค.
์์ ๋ ๋๊ตฌ ์ธํธ๋ฅผ ์ ์ํ๊ณ , ์์ฐ์ด๋ฅผ ํด์ํ์ฌ ์ด๋ฌํ ๋๊ตฌ๋ฅผ ์ฌ์ฉํ ์ ์๋ ์์ด์ ํธ๋ฅผ ์ค๊ณํ์ต๋๋ค.
์ด API๋ ํ์ฅ์ด ๊ฐ๋ฅํ๋๋ก ์ค๊ณ ๋์์ต๋๋ค.
์ฃผ์ ๋๊ตฌ๋ฅผ ์ ๋ณํด๋์์ง๋ง, ์ปค๋ฎค๋ํฐ์์ ๊ฐ๋ฐํ ๋ชจ๋ ๋๊ตฌ๋ฅผ ์ฌ์ฉํ ์ ์๋๋ก ์์คํ
์ ์ฝ๊ฒ ํ์ฅํ ์ ์๋ ๋ฐฉ๋ฒ๋ ๋ณด์ฌ๋๋ฆฌ๊ฒ ์ต๋๋ค.
๋ช ๊ฐ์ง ์๋ฅผ ํตํด ์๋ก์ด API๋ก ๋ฌด์์ ํ ์ ์๋์ง ์ดํด๋ณด๊ฒ ์ต๋๋ค.
์ด API๋ ํนํ ๋ฉํฐ๋ชจ๋ฌ ์์
์์ ๊ฐ๋ ฅํ๋ฏ๋ก ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๊ณ ํ
์คํธ๋ฅผ ์๋ฆฌ๋ด์ด ์ฝ์ด๋ณด๊ฒ ์ต๋๋ค.
```py
agent.run("Caption the following image", image=image)
```
| **Input** | **Output** |
|-----------------------------------------------------------------------------------------------------------------------------|-----------------------------------|
| <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/beaver.png" width=200> | A beaver is swimming in the water |
---
```py
agent.run("Read the following text out loud", text=text)
```
| **Input** | **Output** |
|-------------------------------------------------------------------------------------------------------------------------|----------------------------------------------|
| A beaver is swimming in the water | <audio controls><source src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tts_example.wav" type="audio/wav"> your browser does not support the audio element. </audio>
---
```py
agent.run(
"In the following `document`, where will the TRRF Scientific Advisory Council Meeting take place?",
document=document,
)
```
| **Input** | **Output** |
|-----------------------------------------------------------------------------------------------------------------------------|----------------|
| <img src="https://datasets-server.huggingface.co/assets/hf-internal-testing/example-documents/--/hf-internal-testing--example-documents/test/0/image/image.jpg" width=200> | ballroom foyer |
## ๋ฐ๋ก ์์ํ๊ธฐ [[quickstart]]
`agent.run`์ ์ฌ์ฉํ๋ ค๋ฉด ๋จผ์ ๋๊ท๋ชจ ์ธ์ด ๋ชจ๋ธ(LLM)์ธ ์์ด์ ํธ๋ฅผ ์ธ์คํด์คํํด์ผ ํฉ๋๋ค.
์ ํฌ๋ openAI ๋ชจ๋ธ๋ฟ๋ง ์๋๋ผ BigCode ๋ฐ OpenAssistant์ ์คํ์์ค ๋์ฒด ๋ชจ๋ธ๋ ์ง์ํฉ๋๋ค.
openAI ๋ชจ๋ธ์ ์ฑ๋ฅ์ด ๋ ์ฐ์ํ์ง๋ง(๋จ, openAI API ํค๊ฐ ํ์ํ๋ฏ๋ก ๋ฌด๋ฃ๋ก ์ฌ์ฉํ ์ ์์),
Hugging Face๋ BigCode์ OpenAssistant ๋ชจ๋ธ์ ์๋ํฌ์ธํธ์ ๋ํ ๋ฌด๋ฃ ์ก์ธ์ค๋ฅผ ์ ๊ณตํ๊ณ ์์ต๋๋ค.
์ฐ์ ๋ชจ๋ ๊ธฐ๋ณธ ์ข
์์ฑ์ ์ค์นํ๋ ค๋ฉด `agents`๋ฅผ ์ถ๊ฐ๋ก ์ค์นํ์ธ์.
```bash
pip install transformers[agents]
```
openAI ๋ชจ๋ธ์ ์ฌ์ฉํ๋ ค๋ฉด `openai` ์ข
์์ฑ์ ์ค์นํ ํ [`OpenAiAgent`]๋ฅผ ์ธ์คํด์คํํฉ๋๋ค:
```bash
pip install openai
```
```py
from transformers import OpenAiAgent
agent = OpenAiAgent(model="text-davinci-003", api_key="<your_api_key>")
```
BigCode ๋๋ OpenAssistant๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ๋จผ์ ๋ก๊ทธ์ธํ์ฌ Inference API์ ์ก์ธ์คํ์ธ์:
```py
from huggingface_hub import login
login("<YOUR_TOKEN>")
```
๊ทธ๋ฐ ๋ค์ ์์ด์ ํธ๋ฅผ ์ธ์คํด์คํํฉ๋๋ค.
```py
from transformers import HfAgent
# Starcoder
agent = HfAgent("https://api-inference.huggingface.co/models/bigcode/starcoder")
# StarcoderBase
# agent = HfAgent("https://api-inference.huggingface.co/models/bigcode/starcoderbase")
# OpenAssistant
# agent = HfAgent(url_endpoint="https://api-inference.huggingface.co/models/OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5")
```
ํ์ฌ Hugging Face์์ ๋ฌด๋ฃ๋ก ์ ๊ณตํ๋ ์ถ๋ก API๋ฅผ ์ฌ์ฉํ๊ณ ์์ต๋๋ค.
์ด ๋ชจ๋ธ์ ๋ํ ์์ฒด ์ถ๋ก ์๋ํฌ์ธํธ๊ฐ ์๋ ๊ฒฝ์ฐ(๋๋ ๋ค๋ฅธ ์๋ํฌ์ธํธ๊ฐ ์๋ ๊ฒฝ์ฐ) ์์ URL์ ํด๋น URL ์๋ํฌ์ธํธ๋ก ๋ฐ๊ฟ ์ ์์ต๋๋ค.
<Tip>
StarCoder์ OpenAssistant๋ ๋ฌด๋ฃ๋ก ์ฌ์ฉํ ์ ์์ผ๋ฉฐ ๊ฐ๋จํ ์์
์์ ๋๋ผ์ธ ์ ๋๋ก ์ ์๋ํฉ๋๋ค.
๊ทธ๋ฌ๋ ๋ ๋ณต์กํ ํ๋กฌํํธ๋ฅผ ์ฒ๋ฆฌํ ๋๋ ์ฒดํฌํฌ์ธํธ๊ฐ ์ ์๋ํ์ง ์์ต๋๋ค.
์ด๋ฌํ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ฉด OpenAI ๋ชจ๋ธ์ ์ฌ์ฉํด ๋ณด์๊ธฐ ๋ฐ๋๋๋ค. ์์ฝ๊ฒ๋ ์คํ์์ค๋ ์๋์ง๋ง ํ์ฌ๋ก์๋ ๋ ๋์ ์ฑ๋ฅ์ ์ ๊ณตํฉ๋๋ค.
</Tip>
์ด์ ์ค๋น๊ฐ ์๋ฃ๋์์ต๋๋ค! ์ด์ ์์ ๋กญ๊ฒ ์ฌ์ฉํ ์ ์๋ ๋ ๊ฐ์ง API์ ๋ํด ์์ธํ ์์๋ณด๊ฒ ์ต๋๋ค.
### ๋จ์ผ ์คํ (run) [[single-execution-(run)]]
๋จ์ผ ์คํ ๋ฐฉ๋ฒ์ ์์ด์ ํธ์ [`~Agent.run`] ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ์
๋๋ค:
```py
agent.run("Draw me a picture of rivers and lakes.")
```
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rivers_and_lakes.png" width=200>
์ํํ๋ ค๋ ์์
์ ์ ํฉํ ๋๊ตฌ๋ฅผ ์๋์ผ๋ก ์ ํํ์ฌ ์ ์ ํ๊ฒ ์คํํฉ๋๋ค.
๋์ผํ ๋ช
๋ น์ด์์ ํ๋ ๋๋ ์ฌ๋ฌ ๊ฐ์ ์์
์ ์ํํ ์ ์์ต๋๋ค
(๋ค๋ง, ๋ช
๋ น์ด๊ฐ ๋ณต์กํ ์๋ก ์์ด์ ํธ๊ฐ ์คํจํ ๊ฐ๋ฅ์ฑ์ด ๋์์ง๋๋ค).
```py
agent.run("Draw me a picture of the sea then transform the picture to add an island")
```
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/sea_and_island.png" width=200>
<br/>
๋ชจ๋ [`~Agent.run`] ์์
์ ๋
๋ฆฝ์ ์ด๋ฏ๋ก ๋ค๋ฅธ ์์
์ผ๋ก ์ฌ๋ฌ ๋ฒ ์ฐ์ํด์ ์คํํ ์ ์์ต๋๋ค.
`agent`๋ ํฐ ์ธ์ด ๋ชจ๋ธ์ผ ๋ฟ์ด๋ฏ๋ก ํ๋กฌํํธ์ ์ฝ๊ฐ์ ๋ณํ๋ฅผ ์ฃผ๋ฉด ์์ ํ ๋ค๋ฅธ ๊ฒฐ๊ณผ๊ฐ ๋์ฌ ์ ์๋ค๋ ์ ์ ์ ์ํ์ธ์.
์ํํ๋ ค๋ ์์
์ ์ต๋ํ ๋ช
ํํ๊ฒ ์ค๋ช
ํ๋ ๊ฒ์ด ์ค์ํฉ๋๋ค.
์ข์ ํ๋กฌํํธ๋ฅผ ์์ฑํ๋ ๋ฐฉ๋ฒ์ [์ฌ๊ธฐ](custom_tools#writing-good-user-inputs)์์ ์์ธํ ํ์ธํ ์ ์์ต๋๋ค.
์ฌ๋ฌ ์คํ์ ๊ฑธ์ณ ์ํ๋ฅผ ์ ์งํ๊ฑฐ๋ ํ
์คํธ๊ฐ ์๋ ๊ฐ์ฒด๋ฅผ ์์ด์ ํธ์๊ฒ ์ ๋ฌํ๋ ค๋ ๊ฒฝ์ฐ์๋ ์์ด์ ํธ๊ฐ ์ฌ์ฉํ ๋ณ์๋ฅผ ์ง์ ํ ์ ์์ต๋๋ค.
์๋ฅผ ๋ค์ด ๊ฐ๊ณผ ํธ์์ ์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ ๋ค,
๋ชจ๋ธ์ด ํด๋น ๊ทธ๋ฆผ์ ์ฌ์ ์ถ๊ฐํ๋๋ก ๋ค์๊ณผ ๊ฐ์ด ์์ฒญํ ์ ์์ต๋๋ค:
```python
picture = agent.run("Generate a picture of rivers and lakes.")
updated_picture = agent.run("Transform the image in `picture` to add an island to it.", picture=picture)
```
<Tip>
์ด ๋ฐฉ๋ฒ์ ๋ชจ๋ธ์ด ์์ฒญ์ ์ดํดํ์ง ๋ชปํ๊ณ ๋๊ตฌ๋ฅผ ํผํฉํ ๋ ์ ์ฉํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
```py
agent.run("Draw me the picture of a capybara swimming in the sea")
```
์ฌ๊ธฐ์ ๋ชจ๋ธ์ ๋ ๊ฐ์ง ๋ฐฉ์์ผ๋ก ํด์ํ ์ ์์ต๋๋ค:
- `text-to-image`์ด ๋ฐ๋ค์์ ํค์์น๋ ์นดํผ๋ฐ๋ผ๋ฅผ ์์ฑํ๋๋ก ํฉ๋๋ค.
- ๋๋ `text-to-image`์ด ์นดํผ๋ฐ๋ผ๋ฅผ ์์ฑํ ๋ค์ `image-transformation` ๋๊ตฌ๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ๋ค์์ ํค์์น๋๋ก ํฉ๋๋ค.
์ฒซ ๋ฒ์งธ ์๋๋ฆฌ์ค๋ฅผ ๊ฐ์ ๋ก ์คํํ๋ ค๋ฉด ํ๋กฌํํธ๋ฅผ ์ธ์๋ก ์ ๋ฌํ์ฌ ์คํํ ์ ์์ต๋๋ค:
```py
agent.run("Draw me a picture of the `prompt`", prompt="a capybara swimming in the sea")
```
</Tip>
### ๋ํ ๊ธฐ๋ฐ ์คํ (chat) [[chat-based-execution-(chat)]]
์์ด์ ํธ๋ [`~Agent.chat`] ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๋ํ ๊ธฐ๋ฐ ์ ๊ทผ ๋ฐฉ์๋ ์์ต๋๋ค:
```py
agent.chat("Generate a picture of rivers and lakes")
```
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rivers_and_lakes.png" width=200>
```py
agent.chat("Transform the picture so that there is a rock in there")
```
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rivers_and_lakes_and_beaver.png" width=200>
<br/>
์ด ๋ฐฉ์์ ์ฌ๋ฌ ๋ช
๋ น์ด์ ๊ฑธ์ณ ์ํ๋ฅผ ์ ์งํ๊ณ ์ ํ ๋ ํฅ๋ฏธ๋ก์ด ์ ๊ทผ ๋ฐฉ์์
๋๋ค.
์คํ์ฉ์ผ๋ก ๋ ์ข์ง๋ง ๋ณต์กํ ๋ช
๋ น์ด๋ณด๋ค๋
๋จ์ผ ๋ช
๋ น์ด([`~Agent.run`] ๋ฉ์๋๊ฐ ๋ ์ ์ฒ๋ฆฌํ๋ ๋ช
๋ น์ด)์ ํจ์ฌ ๋ ์ ์๋ํ๋ ๊ฒฝํฅ์ด ์์ต๋๋ค.
์ด ๋ฉ์๋๋ ํ
์คํธ๊ฐ ์๋ ์ ํ์ด๋ ํน์ ํ๋กฌํํธ๋ฅผ ์ ๋ฌํ๋ ค๋ ๊ฒฝ์ฐ ์ธ์๋ฅผ ๋ฐ์ ์๋ ์์ต๋๋ค.
### โ ๏ธ ์๊ฒฉ ์คํ [[remote-execution]]
๋ฐ๋ชจ ๋ชฉ์ ๊ณผ ๋ชจ๋ ์ค์ ์์ ์ฌ์ฉํ ์ ์๋๋ก
์์ด์ ํธ๊ฐ ์ ๊ทผํ ์ ์๋ ๋ช ๊ฐ์ง ๊ธฐ๋ณธ ๋๊ตฌ์ ๋ํ ์๊ฒฉ ์คํ๊ธฐ๋ฅผ ๋ง๋ค์์ต๋๋ค.
์ด๋ฌํ ๋๊ตฌ๋ [inference endpoints](https://huggingface.co/inference-endpoints)๋ฅผ ์ฌ์ฉํ์ฌ ๋ง๋ค์ด์ก์ต๋๋ค.
์๊ฒฉ ์คํ๊ธฐ ๋๊ตฌ๋ฅผ ์ง์ ์ค์ ํ๋ ๋ฐฉ๋ฒ์ ๋ณด๋ ค๋ฉด [์ฌ์ฉ์ ์ ์ ๋๊ตฌ ๊ฐ์ด๋](./custom_tools)๋ฅผ ์ฝ์ด๋ณด์๊ธฐ ๋ฐ๋๋๋ค.
์๊ฒฉ ๋๊ตฌ๋ก ์คํํ๋ ค๋ฉด [`~Agent.run`] ๋๋ [`~Agent.chat`] ์ค ํ๋์ `remote=True`๋ฅผ ์ง์ ํ๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค.
์๋ฅผ ๋ค์ด ๋ค์ ๋ช
๋ น์ ๋ง์ RAM์ด๋ GPU ์์ด๋ ๋ชจ๋ ์ฅ์น์์ ํจ์จ์ ์ผ๋ก ์คํํ ์ ์์ต๋๋ค:
```py
agent.run("Draw me a picture of rivers and lakes", remote=True)
```
[`~Agent.chat`]๋ ๋ง์ฐฌ๊ฐ์ง์
๋๋ค:
```py
agent.chat("Draw me a picture of rivers and lakes", remote=True)
```
### ์ฌ๊ธฐ์ ๋ฌด์จ ์ผ์ด ์ผ์ด๋๋ ๊ฑฐ์ฃ ? ๋๊ตฌ๋ ๋ฌด์์ด๊ณ , ์์ด์ ํธ๋ ๋ฌด์์ธ๊ฐ์? [[whats-happening-here-what-are-tools-and-what-are-agents]]
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/diagram.png">
#### ์์ด์ ํธ [[agents]]
์ฌ๊ธฐ์ "์์ด์ ํธ"๋ ๋๊ท๋ชจ ์ธ์ด ๋ชจ๋ธ์ด๋ฉฐ, ํน์ ๋๊ตฌ ๋ชจ์์ ์ ๊ทผํ ์ ์๋๋ก ํ๋กฌํํธํ๊ณ ์์ต๋๋ค.
LLM์ ์์ ์ฝ๋ ์ํ์ ์์ฑํ๋ ๋ฐ ์๋นํ ๋ฅ์ํ๋ฏ๋ก,
์ด ์ฅ์ ์ ํ์ฉํด ๋๊ตฌ ๋ชจ์์ ์ฌ์ฉํ์ฌ ์์
์ ์ํํ๋ ์์ ์ฝ๋ ์ํ์ ์ ๊ณตํ๋ผ๋ ๋ฉ์์ง๋ฅผ ํ์ํฉ๋๋ค.
๊ทธ๋ฐ ๋ค์ ์์ด์ ํธ์๊ฒ ์ ๊ณตํ๋ ์์
๊ณผ ์ ๊ณตํ๋ ๋๊ตฌ์ ๋ํ ์ค๋ช
์ผ๋ก ์ด ํ๋กฌํํธ๊ฐ ์๋ฃ๋ฉ๋๋ค.
์ด๋ ๊ฒ ํ๋ฉด ์ฌ์ฉ ์ค์ธ ๋๊ตฌ๋ค์ ๋ฌธ์์ ์ ๊ทผํ ์ ์์ผ๋ฉฐ, ํด๋น ๋๊ตฌ๋ค์ ์
๋ ฅ๊ณผ ์ถ๋ ฅ์ ์์ํ๊ณ , ๊ด๋ จ๋ ์ฝ๋๋ฅผ ์์ฑํ ์ ์์ต๋๋ค.
#### ๋๊ตฌ [[tools]]
๋๊ตฌ๋ ๋งค์ฐ ๊ฐ๋จํฉ๋๋ค. ์ด๋ฆ๊ณผ ์ค๋ช
์ด ์๋ ๋จ์ผ ๊ธฐ๋ฅ์ผ๋ก ๊ตฌ์ฑ๋์ด ์์ต๋๋ค.
๊ทธ๋ฐ ๋ค์ ์ด๋ฌํ ๋๊ตฌ์ ์ค๋ช
์ ์ฌ์ฉํ์ฌ ์๋ด์์๊ฒ ํ๋กฌํํธ๋ฅผ ํ์ํฉ๋๋ค.
์ด ํ๋กฌํํธ๋ฅผ ํตํด ์๋ด์์๊ฒ ์ฟผ๋ฆฌ์์ ์์ฒญ๋ ์์
์ ์ํํ๊ธฐ ์ํด ๋๊ตฌ๋ฅผ ํ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค.
์์ด์ ํธ๊ฐ ๋งค์ฐ ์์์ ์ธ ๋๊ตฌ๋ฅผ ์ฌ์ฉํ์ฌ ๋ ๋์ ์ฝ๋๋ฅผ ์์ฑํ๊ธฐ ๋๋ฌธ์ ํ์ดํ๋ผ์ธ์ด ์๋ ์์ ํ ์๋ก์ด ๋๊ตฌ๋ฅผ ์ฌ์ฉํฉ๋๋ค.
ํ์ดํ๋ผ์ธ์ ๋ ๋ง์ด ๋ฆฌํฉํฐ๋ง๋๋ฉฐ ์ข
์ข
์ฌ๋ฌ ์์
์ ํ๋๋ก ๊ฒฐํฉํฉ๋๋ค.
๋๊ตฌ๋ ํ๋์ ๋งค์ฐ ๊ฐ๋จํ ์์
์๋ง ์ง์คํ๋๋ก ๋์ด ์์ต๋๋ค.
#### ์ฝ๋ ์คํ?! [[code-execution]]
๊ทธ๋ฐ ๋ค์ ์ด ์ฝ๋๋ ๋๊ตฌ์ ํจ๊ป ์ ๋ฌ๋ ์
๋ ฅ ์ธํธ์ ๋ํด ์์ Python ์ธํฐํ๋ฆฌํฐ๋ฅผ ์ฌ์ฉํ์ฌ ์คํ๋ฉ๋๋ค.
"์์ ์ฝ๋ ์คํ์ด๋ผ๋!"์ด๋ผ๊ณ ๋น๋ช
์ ์ง๋ฅด๋ ์๋ฆฌ๊ฐ ๋ค๋ฆฌ๊ฒ ์ง๋ง, ๊ทธ๋ ์ง ์์ ์ด์ ๋ฅผ ์ค๋ช
ํ๊ฒ ์ต๋๋ค.
ํธ์ถํ ์ ์๋ ํจ์๋ ์ ๊ณตํ ๋๊ตฌ์ ์ธ์ ๊ธฐ๋ฅ๋ฟ์ด๋ฏ๋ก ์ด๋ฏธ ์คํํ ์ ์๋ ๊ธฐ๋ฅ์ด ์ ํ๋์ด ์์ต๋๋ค.
Hugging Face ๋๊ตฌ๋ก ์ ํ๋์ด ์๋ค๋ฉด ์์ ํ ๊ฒ์
๋๋ค.
๊ทธ๋ฆฌ๊ณ ์ดํธ๋ฆฌ๋ทฐํธ ์กฐํ๋ ๊ฐ์ ธ์ค๊ธฐ๋ฅผ ํ์ฉํ์ง ์์ผ๋ฏ๋ก
(์ด์ฐจํผ ์์ ํจ์ ์งํฉ์ ์
/์ถ๋ ฅ์ ์ ๋ฌํ ๋๋ ํ์ํ์ง ์์์ผ ํฉ๋๋ค)
๊ฐ์ฅ ๋ช
๋ฐฑํ ๊ณต๊ฒฉ(์ด์ฐจํผ LLM์ ์ถ๋ ฅํ๋ผ๋ ๋ฉ์์ง๋ฅผ ํ์ํด์ผ ํฉ๋๋ค)์ ๋ฌธ์ ๊ฐ ๋์ง ์์ต๋๋ค.
๋งค์ฐ ์์ ํ๊ฒ ํ๊ณ ์ถ๋ค๋ฉด ์ถ๊ฐ ์ธ์ return_code=True๋ฅผ ์ฌ์ฉํ์ฌ run() ๋ฉ์๋๋ฅผ ์คํํ๋ฉด ๋ฉ๋๋ค.
์ด ๊ฒฝ์ฐ ์์ด์ ํธ๊ฐ ์คํํ ์ฝ๋๋ฅผ ๋ฐํํ๊ณ ์คํํ ์ง ์ฌ๋ถ๋ฅผ ๊ฒฐ์ ํ ์ ์์ต๋๋ค.
๋ถ๋ฒ์ ์ธ ์ฐ์ฐ์ ์ํํ๋ ค๊ณ ํ๊ฑฐ๋ ์์ด์ ํธ๊ฐ ์์ฑํ ์ฝ๋์ ์ผ๋ฐ์ ์ธ ํ์ด์ฌ ์ค๋ฅ๊ฐ ์๋ ๊ฒฝ์ฐ
์คํ์ด ์ค์ง๋ฉ๋๋ค.
### ์์ ๋ ๋๊ตฌ ๋ชจ์ [[a-curated-set-of-tools]]
์ ํฌ๋ ์ด๋ฌํ ์์ด์ ํธ๋ค์ ์ญ๋์ ๊ฐํํ ์ ์๋ ์ผ๋ จ์ ๋๊ตฌ๋ฅผ ํ์ธํ๊ณ ์์ต๋๋ค.
๋ค์์ ์ฐ๋๋ ๋๊ตฌ์ ์ต์ ๋ชฉ๋ก์
๋๋ค:
- **๋ฌธ์ ์ง๋ฌธ ๋ต๋ณ**: ์ด๋ฏธ์ง ํ์์ ๋ฌธ์(์: PDF)๊ฐ ์ฃผ์ด์ง๋ฉด ์ด ๋ฌธ์์ ๋ํ ์ง๋ฌธ์ ๋ต๋ณํฉ๋๋ค. ([Donut](./model_doc/donut))
- **ํ
์คํธ ์ง๋ฌธ ๋ต๋ณ**: ๊ธด ํ
์คํธ์ ์ง๋ฌธ์ด ์ฃผ์ด์ง๋ฉด ํ
์คํธ์์ ์ง๋ฌธ์ ๋ต๋ณํฉ๋๋ค. ([Flan-T5](./model_doc/flan-t5))
- **๋ฌด์กฐ๊ฑด ์ด๋ฏธ์ง ์บก์
๋**: ์ด๋ฏธ์ง์ ์บก์
์ ๋ต๋๋ค! ([BLIP](./model_doc/blip))
- **์ด๋ฏธ์ง ์ง๋ฌธ ๋ต๋ณ**: ์ด๋ฏธ์ง๊ฐ ์ฃผ์ด์ง๋ฉด ์ด ์ด๋ฏธ์ง์ ๋ํ ์ง๋ฌธ์ ๋ต๋ณํ๊ธฐ. ([VILT](./model_doc/vilt))
- **์ด๋ฏธ์ง ๋ถํ **: ์ด๋ฏธ์ง์ ํ๋กฌํํธ๊ฐ ์ฃผ์ด์ง๋ฉด ํด๋น ํ๋กฌํํธ์ ๋ถํ ๋ง์คํฌ๋ฅผ ์ถ๋ ฅํฉ๋๋ค. ([CLIPSeg](./model_doc/clipseg))
- **์์ฑ์ ํ
์คํธ๋ก ๋ณํ**: ์ฌ๋์ด ๋งํ๋ ์ค๋์ค ๋
น์์ด ์ฃผ์ด์ง๋ฉด ์์ฑ์ ํ
์คํธ๋ก ๋ณํํฉ๋๋ค. ([Whisper](./model_doc/whisper))
- **ํ
์คํธ ์์ฑ ๋ณํ**: ํ
์คํธ๋ฅผ ์์ฑ์ผ๋ก ๋ณํํฉ๋๋ค. ([SpeechT5](./model_doc/speecht5))
- **์ ๋ก ์ท(zero-shot) ํ
์คํธ ๋ถ๋ฅ**: ํ
์คํธ์ ๋ ์ด๋ธ ๋ชฉ๋ก์ด ์ฃผ์ด์ง๋ฉด ํ
์คํธ์ ๊ฐ์ฅ ๊ด๋ จ ์๋ ๋ ์ด๋ธ์ ์๋ณํฉ๋๋ค. ([BART](./model_doc/bart))
- **ํ
์คํธ ์์ฝ**: ๊ธด ํ
์คํธ๋ฅผ ํ ๋ฌธ์ฅ ๋๋ ๋ช ๋ฌธ์ฅ์ผ๋ก ์์ฝํฉ๋๋ค. ([BART](./model_doc/bart))
- **๋ฒ์ญ**: ํ
์คํธ๋ฅผ ์ง์ ๋ ์ธ์ด๋ก ๋ฒ์ญํฉ๋๋ค. ([NLLB](./model_doc/nllb))
์ด๋ฌํ ๋๊ตฌ๋ ํธ๋์คํฌ๋จธ์ ํตํฉ๋์ด ์์ผ๋ฉฐ, ์๋ฅผ ๋ค์ด ์๋์ผ๋ก๋ ์ฌ์ฉํ ์ ์์ต๋๋ค:
```py
from transformers import load_tool
tool = load_tool("text-to-speech")
audio = tool("This is a text to speech tool")
```
### ์ฌ์ฉ์ ์ ์ ๋๊ตฌ [[custom-tools]]
์์ ๋ ๋๊ตฌ ์ธํธ๋ ์์ง๋ง, ์ด ๊ตฌํ์ด ์ ๊ณตํ๋ ๊ฐ์ฅ ํฐ ๊ฐ์น๋ ์ฌ์ฉ์ ์ง์ ๋๊ตฌ๋ฅผ ๋น ๋ฅด๊ฒ ๋ง๋ค๊ณ ๊ณต์ ํ ์ ์๋ค๋ ์ ์
๋๋ค.
๋๊ตฌ์ ์ฝ๋๋ฅผ Hugging Face Space๋ ๋ชจ๋ธ ์ ์ฅ์์ ํธ์ํ๋ฉด ์์ด์ ํธ์๊ฒ ์ง์ ๋๊ตฌ๋ฅผ ํ์ฉํ ์ ์์ต๋๋ค. [`huggingface-tools` organization](https://huggingface.co/huggingface-tools)์ ๋ช ๊ฐ์ง **ํธ๋์คํฌ๋จธ์ ๊ตฌ์ ๋ฐ์ง ์๋** ํด์ ์ถ๊ฐํ์ต๋๋ค:
- **ํ
์คํธ ๋ค์ด๋ก๋**: ์น URL์์ ํ
์คํธ๋ฅผ ๋ค์ด๋ก๋ํฉ๋๋ค.
- **ํ
์คํธ ์ด๋ฏธ์ง ๋ณํ**: ํ๋กฌํํธ์ ๋ฐ๋ผ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ฌ ์์ ์ ์ธ ํ์ฐ์ ํ์ฉํฉ๋๋ค.
- **์ด๋ฏธ์ง ๋ณํ**: ์ด๊ธฐ ์ด๋ฏธ์ง์ ํ๋กฌํํธ๊ฐ ์ฃผ์ด์ง ์ด๋ฏธ์ง๋ฅผ ์์ ํ๊ณ , ์์ ์ ์ธ ํ์ฐ์ ํ์ฉํ๋ ์ง์ ํฝ์
2 ํฝ์
์ ํ์ฉํฉ๋๋ค.
- **ํ
์คํธ ๋น๋์ค ๋ณํ**: ํ๋กฌํํธ์ ๋ฐ๋ผ ์์ ๋น๋์ค๋ฅผ ์์ฑํ๋ฉฐ, damo-vilab์ ํ์ฉํฉ๋๋ค.
์ ํฌ๊ฐ ์ฒ์๋ถํฐ ์ฌ์ฉํ๊ณ ์๋ ํ
์คํธ-์ด๋ฏธ์ง ๋ณํ ๋๊ตฌ๋ [*huggingface-tools/text-to-image*](https://huggingface.co/spaces/huggingface-tools/text-to-image)์ ์๋ ์๊ฒฉ ๋๊ตฌ์
๋๋ค! ์ ํฌ๋ ์ด ๋๊ตฌ์ ๋ค๋ฅธ ์กฐ์ง์ ์ด๋ฌํ ๋๊ตฌ๋ฅผ ๊ณ์ ์ถ์ํ์ฌ ์ด ๊ตฌํ์ ๋์ฑ ๊ฐํํ ๊ฒ์
๋๋ค.
์์ด์ ํธ๋ ๊ธฐ๋ณธ์ ์ผ๋ก [`huggingface-tools`](https://huggingface.co/huggingface-tools)์ ์๋ ๋๊ตฌ์ ์ ๊ทผํ ์ ์์ต๋๋ค.
[๋ค์ ๊ฐ์ด๋](custom_tools)์์ ๋๊ตฌ๋ฅผ ์์ฑํ๊ณ ๊ณต์ ํ๋ ๋ฐฉ๋ฒ๊ณผ Hub์ ์๋ ์ฌ์ฉ์ ์ง์ ๋๊ตฌ๋ฅผ ํ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ํด ์ค๋ช
ํฉ๋๋ค.
### ์ฝ๋ ์์ฑ[[code-generation]]
์ง๊ธ๊น์ง ์์ด์ ํธ๋ฅผ ์ฌ์ฉํ์ฌ ์์
์ ์ํํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ๋๋ ธ์ต๋๋ค. ํ์ง๋ง ์์ด์ ํธ๋ ๋งค์ฐ ์ ํ๋ Python ์ธํฐํ๋ฆฌํฐ๋ฅผ ์ฌ์ฉํ์ฌ ์คํํ ์ฝ๋๋ง ์์ฑํ๊ณ ์์ต๋๋ค. ๋ค๋ฅธ ์ค์ ์์ ์์ฑ๋ ์ฝ๋๋ฅผ ์ฌ์ฉํ๋ ค๋ ๊ฒฝ์ฐ ์์ด์ ํธ์๊ฒ ๋๊ตฌ ์ ์ ๋ฐ ์ ํํ ๊ฐ์ ธ์ค๊ธฐ์ ํจ๊ป ์ฝ๋๋ฅผ ๋ฐํํ๋ผ๋ ๋ฉ์์ง๋ฅผ ํ์ํ ์ ์์ต๋๋ค.
์๋ฅผ ๋ค์ด ๋ค์ ๋ช
๋ น์ด๋
```python
agent.run("Draw me a picture of rivers and lakes", return_code=True)
```
๋ค์ ์ฝ๋๋ฅผ ๋ฐํํฉ๋๋ค.
```python
from transformers import load_tool
image_generator = load_tool("huggingface-tools/text-to-image")
image = image_generator(prompt="rivers and lakes")
```
์ด ์ฝ๋๋ ์ง์ ์์ ํ๊ณ ์คํํ ์ ์์ต๋๋ค. |