text-generation-webui/docs/07 ‐ Extensions.md

245 lines
11 KiB
Markdown
Raw Normal View History

# Extensions
Extensions are defined by files named `script.py` inside subfolders of `text-generation-webui/extensions`. They are loaded at startup if the folder name is specified after the `--extensions` flag.
2023-04-22 01:34:13 -04:00
2023-05-17 00:41:01 -04:00
For instance, `extensions/silero_tts/script.py` gets loaded with `python server.py --extensions silero_tts`.
2023-04-22 01:34:13 -04:00
## [text-generation-webui-extensions](https://github.com/oobabooga/text-generation-webui-extensions)
The repository above contains a directory of user extensions.
2023-04-22 01:34:13 -04:00
2023-07-26 10:00:33 -04:00
If you create an extension, you are welcome to host it in a GitHub repository and submit a PR adding it to the list.
2023-04-23 15:11:17 -04:00
2023-04-22 01:34:13 -04:00
## Built-in extensions
|Extension|Description|
|---------|-----------|
|[api](https://github.com/oobabooga/text-generation-webui/tree/main/extensions/api)| Creates an API with two endpoints, one for streaming at `/api/v1/stream` port 5005 and another for blocking at `/api/v1/generate` port 5000. This is the main API for the webui. |
|[openai](https://github.com/oobabooga/text-generation-webui/tree/main/extensions/openai)| Creates an API that mimics the OpenAI API and can be used as a drop-in replacement. |
|[multimodal](https://github.com/oobabooga/text-generation-webui/tree/main/extensions/multimodal) | Adds multimodality support (text+images). For a detailed description see [README.md](https://github.com/oobabooga/text-generation-webui/tree/main/extensions/multimodal/README.md) in the extension directory. |
2023-04-22 01:34:13 -04:00
|[google_translate](https://github.com/oobabooga/text-generation-webui/tree/main/extensions/google_translate)| Automatically translates inputs and outputs using Google Translate.|
|[silero_tts](https://github.com/oobabooga/text-generation-webui/tree/main/extensions/silero_tts)| Text-to-speech extension using [Silero](https://github.com/snakers4/silero-models). When used in chat mode, responses are replaced with an audio widget. |
2023-04-23 15:11:17 -04:00
|[elevenlabs_tts](https://github.com/oobabooga/text-generation-webui/tree/main/extensions/elevenlabs_tts)| Text-to-speech extension using the [ElevenLabs](https://beta.elevenlabs.io/) API. You need an API key to use it. |
|[whisper_stt](https://github.com/oobabooga/text-generation-webui/tree/main/extensions/whisper_stt)| Allows you to enter your inputs in chat mode using your microphone. |
|[sd_api_pictures](https://github.com/oobabooga/text-generation-webui/tree/main/extensions/sd_api_pictures)| Allows you to request pictures from the bot in chat mode, which will be generated using the AUTOMATIC1111 Stable Diffusion API. See examples [here](https://github.com/oobabooga/text-generation-webui/pull/309). |
|[character_bias](https://github.com/oobabooga/text-generation-webui/tree/main/extensions/character_bias)| Just a very simple example that adds a hidden string at the beginning of the bot's reply in chat mode. |
|[send_pictures](https://github.com/oobabooga/text-generation-webui/blob/main/extensions/send_pictures/)| Creates an image upload field that can be used to send images to the bot in chat mode. Captions are automatically generated using BLIP. |
|[gallery](https://github.com/oobabooga/text-generation-webui/blob/main/extensions/gallery/)| Creates a gallery with the chat characters and their pictures. |
2023-05-07 23:11:31 -04:00
|[superbooga](https://github.com/oobabooga/text-generation-webui/tree/main/extensions/superbooga)| An extension that uses ChromaDB to create an arbitrarily large pseudocontext, taking as input text files, URLs, or pasted text. Based on https://github.com/kaiokendev/superbig. |
2023-07-26 10:00:33 -04:00
|[ngrok](https://github.com/oobabooga/text-generation-webui/tree/main/extensions/ngrok)| Allows you to access the web UI remotely using the ngrok reverse tunnel service (free). It's an alternative to the built-in Gradio `--share` feature. |
|[perplexity_colors](https://github.com/oobabooga/text-generation-webui/tree/main/extensions/perplexity_colors)| Colors each token in the output text by its associated probability, as derived from the model logits. |
2023-04-22 01:34:13 -04:00
## How to write an extension
The extensions framework is based on special functions and variables that you can define in `script.py`. The functions are the following:
2023-05-17 00:41:01 -04:00
2023-04-22 01:34:13 -04:00
| Function | Description |
|-------------|-------------|
| `def setup()` | Is executed when the extension gets imported. |
2023-04-22 01:34:13 -04:00
| `def ui()` | Creates custom gradio elements when the UI is launched. |
2023-05-17 01:04:37 -04:00
| `def custom_css()` | Returns custom CSS as a string. It is applied whenever the web UI is loaded. |
| `def custom_js()` | Same as above but for javascript. |
2023-08-13 00:12:15 -04:00
| `def input_modifier(string, state, is_chat=False)` | Modifies the input string before it enters the model. In chat mode, it is applied to the user message. Otherwise, it is applied to the entire prompt. |
| `def output_modifier(string, state, is_chat=False)` | Modifies the output string before it is presented in the UI. In chat mode, it is applied to the bot's reply. Otherwise, it is applied to the entire output. |
| `def chat_input_modifier(text, visible_text, state)` | Modifies both the visible and internal inputs in chat mode. Can be used to hijack the chat input with custom content. |
| `def bot_prefix_modifier(string, state)` | Applied in chat mode to the prefix for the bot's reply. |
2023-05-17 00:41:01 -04:00
| `def state_modifier(state)` | Modifies the dictionary containing the UI input parameters before it is used by the text generation functions. |
2023-05-21 12:24:54 -04:00
| `def history_modifier(history)` | Modifies the chat history before the text generation in chat mode begins. |
| `def custom_generate_reply(...)` | Overrides the main text generation function. |
2023-04-22 01:34:13 -04:00
| `def custom_generate_chat_prompt(...)` | Overrides the prompt generator in chat mode. |
2023-05-17 01:04:37 -04:00
| `def tokenizer_modifier(state, prompt, input_ids, input_embeds)` | Modifies the `input_ids`/`input_embeds` fed to the model. Should return `prompt`, `input_ids`, `input_embeds`. See the `multimodal` extension for an example. |
| `def custom_tokenized_length(prompt)` | Used in conjunction with `tokenizer_modifier`, returns the length in tokens of `prompt`. See the `multimodal` extension for an example. |
2023-04-22 01:34:13 -04:00
Additionally, you can define a special `params` dictionary. In it, the `display_name` key is used to define the displayed name of the extension in the UI, and the `is_tab` key is used to define whether the extension should appear in a new tab. By default, extensions appear at the bottom of the "Text generation" tab.
2023-05-17 01:04:37 -04:00
Example:
2023-04-22 01:34:13 -04:00
```python
params = {
2023-05-17 00:25:01 -04:00
"display_name": "Google Translate",
"is_tab": True,
2023-04-22 01:34:13 -04:00
}
```
2023-07-26 10:01:35 -04:00
The `params` dict may also contain variables that you want to be customizable through a `settings.yaml` file. For instance, assuming the extension is in `extensions/google_translate`, the variable `language string` in
2023-05-17 00:25:01 -04:00
```python
params = {
"display_name": "Google Translate",
"is_tab": True,
"language string": "jp"
}
```
can be customized by adding a key called `google_translate-language string` to `settings.yaml`:
2023-04-22 01:34:13 -04:00
```python
google_translate-language string: 'fr'
2023-04-22 01:34:13 -04:00
```
That is, the syntax for the key is `extension_name-variable_name`.
2023-04-22 01:34:13 -04:00
## Using multiple extensions at the same time
You can activate more than one extension at a time by providing their names separated by spaces after `--extensions`. The input, output, and bot prefix modifiers will be applied in the specified order.
Example:
2023-04-22 01:34:13 -04:00
```
python server.py --extensions enthusiasm translate # First apply enthusiasm, then translate
python server.py --extensions translate enthusiasm # First apply translate, then enthusiasm
```
Do note, that for:
- `custom_generate_chat_prompt`
- `custom_generate_reply`
- `custom_tokenized_length`
only the first declaration encountered will be used and the rest will be ignored.
## A full example
The source code below can be found at [extensions/example/script.py](https://github.com/oobabooga/text-generation-webui/tree/main/extensions/example/script.py).
2023-05-05 18:04:50 -04:00
```python
"""
An example of extension. It does nothing, but you can add transformations
before the return statements to customize the webui behavior.
Starting from history_modifier and ending in output_modifier, the
functions are declared in the same order that they are called at
generation time.
"""
2023-07-29 21:57:22 -04:00
import gradio as gr
import torch
2023-07-29 21:57:22 -04:00
from transformers import LogitsProcessor
from modules import chat, shared
from modules.text_generation import (
decode,
encode,
generate_reply,
)
2023-04-22 01:34:13 -04:00
params = {
"display_name": "Example Extension",
"is_tab": False,
}
2023-04-22 01:34:13 -04:00
class MyLogits(LogitsProcessor):
"""
Manipulates the probabilities for the next token before it gets sampled.
2023-07-26 12:37:48 -04:00
Used in the logits_processor_modifier function below.
"""
def __init__(self):
pass
def __call__(self, input_ids, scores):
# probs = torch.softmax(scores, dim=-1, dtype=torch.float)
# probs[0] /= probs[0].sum()
# scores = torch.log(probs / (1 - probs))
return scores
def history_modifier(history):
"""
Modifies the chat history.
Only used in chat mode.
"""
return history
def state_modifier(state):
"""
Modifies the state variable, which is a dictionary containing the input
values in the UI like sliders and checkboxes.
"""
return state
def chat_input_modifier(text, visible_text, state):
"""
2023-07-26 10:00:33 -04:00
Modifies the user input string in chat mode (visible_text).
You can also modify the internal representation of the user
input (text) to change how it will appear in the prompt.
"""
return text, visible_text
2023-08-13 00:12:15 -04:00
def input_modifier(string, state, is_chat=False):
"""
In default/notebook modes, modifies the whole prompt.
2023-07-26 10:00:33 -04:00
In chat mode, it is the same as chat_input_modifier but only applied
to "text", here called "string", and not to "visible_text".
"""
return string
def bot_prefix_modifier(string, state):
"""
Modifies the prefix for the next bot reply in chat mode.
By default, the prefix will be something like "Bot Name:".
"""
return string
def tokenizer_modifier(state, prompt, input_ids, input_embeds):
"""
Modifies the input ids and embeds.
Used by the multimodal extension to put image embeddings in the prompt.
Only used by loaders that use the transformers library for sampling.
"""
return prompt, input_ids, input_embeds
def logits_processor_modifier(processor_list, input_ids):
"""
2023-07-26 10:00:33 -04:00
Adds logits processors to the list, allowing you to access and modify
the next token probabilities.
Only used by loaders that use the transformers library for sampling.
"""
processor_list.append(MyLogits())
return processor_list
2023-08-13 00:12:15 -04:00
def output_modifier(string, state, is_chat=False):
"""
Modifies the LLM output before it gets presented.
2023-07-26 10:00:33 -04:00
In chat mode, the modified version goes into history['visible'],
and the original version goes into history['internal'].
"""
return string
2023-06-27 21:25:05 -04:00
2023-04-22 01:34:13 -04:00
def custom_generate_chat_prompt(user_input, state, **kwargs):
"""
Replaces the function that generates the prompt from the chat history.
Only used in chat mode.
"""
result = chat.generate_chat_prompt(user_input, state, **kwargs)
return result
def custom_css():
"""
Returns a CSS string that gets appended to the CSS for the webui.
"""
return ''
def custom_js():
"""
2023-07-26 10:00:33 -04:00
Returns a javascript string that gets appended to the javascript
for the webui.
"""
return ''
def setup():
"""
Gets executed only once, when the extension is imported.
"""
pass
def ui():
"""
2023-07-26 10:49:22 -04:00
Gets executed when the UI is drawn. Custom gradio elements and
their corresponding event handlers should be defined here.
To learn about gradio components, check out the docs:
https://gradio.app/docs/
"""
pass
2023-04-22 01:34:13 -04:00
```