mirror of
https://github.com/Hopiu/llm.git
synced 2026-03-18 05:30:24 +00:00
46 lines
1.3 KiB
Markdown
46 lines
1.3 KiB
Markdown
# Plugin hooks
|
|
|
|
Plugins use **plugin hooks** to customize LLM's behavior. These hooks are powered by the [Pluggy plugin system](https://pluggy.readthedocs.io/).
|
|
|
|
Each plugin can implement one or more hooks using the @hookimpl decorator against one of the hook function names described on this page.
|
|
|
|
LLM imitates the Datasette plugin system. The [Datasette plugin documentation](https://docs.datasette.io/en/stable/writing_plugins.html) describes how plugins work.
|
|
|
|
## register_commands(cli)
|
|
|
|
This hook adds new commands to the `llm` CLI tool - for example `llm extra-command`.
|
|
|
|
This example plugin adds a new `hello-world` command that prints "Hello world!":
|
|
|
|
```python
|
|
from llm import hookimpl
|
|
import click
|
|
|
|
@hookimpl
|
|
def register_commands(cli):
|
|
@cli.command(name="hello-world")
|
|
def hello_world():
|
|
"Print hello world"
|
|
click.echo("Hello world!")
|
|
```
|
|
This new command will be added to `llm --help` and can be run using `llm hello-world`.
|
|
|
|
## register_models(register)
|
|
|
|
This hook can be used to register one or more additional models.
|
|
|
|
```python
|
|
import llm
|
|
|
|
@llm.hookimpl
|
|
def register_models(register):
|
|
register(HelloWorld())
|
|
|
|
class HelloWorld(llm.Model):
|
|
model_id = "helloworld"
|
|
|
|
def execute(self, prompt, stream, response):
|
|
return ["hello world"]
|
|
```
|
|
|
|
{ref}`tutorial-model-plugin` describes how to use this hook in detail.
|