Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a plugin for llm #195

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 169 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ llama-index-readers-github = "^0.1.9"
tiktoken = "^0.7.0"
typer = "^0.12.3"
rich = "^13.7.1"

llm = { version="^0.14", optional=true }

[tool.poetry.group.dev.dependencies]
black = "^24.3.0"
Expand All @@ -74,6 +74,9 @@ azure = [
"pulumi-azure-native",
"azure-storage-file-share",
]
llm_plugin = [
"llm",
]

[tool.poetry.scripts]
reginald = "reginald.cli:cli"
Expand All @@ -82,6 +85,9 @@ reginald = "reginald.cli:cli"
name = "PyPI"
priority = "primary"

[tool.poetry.plugins."llm"]
reginald = "reginald.llm_reginald.llm_reginald"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Expand Down
19 changes: 19 additions & 0 deletions reginald/llm_reginald/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# llm-reginald

This module contains an [LLM](https://llm.datasette.io/) plugin for interacting with [Reginald](https://github.com/alan-turing-institute/reginald).

The plugin is available to use immediately after reginald is installed, as long as the `llm_plugin` extra dependencies are selected when installing the package (either with `pip install .[llm_plugin]` or `poetry install --extras llm_plugin` or `poetry install --all-extras`).

The plugin requires a running Reginald server to work. By default this is assumed to be at `http://localhost:8000`. Refer to the package README for instructions.

Assuming [LLM](http://llm.datasette.io/) is installed, you can interact with Reginald like this:

```
llm -m reginald 'Who are you?'
```

Using a different server URL:

```
llm -m reginald -o server_url <server_url> ...
```
1 change: 1 addition & 0 deletions reginald/llm_reginald/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from reginald.llm_reginald import *
60 changes: 60 additions & 0 deletions reginald/llm_reginald/llm_reginald.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import uuid
from typing import Optional
from urllib.parse import urljoin

import httpx
import llm
from pydantic import Field


@llm.hookimpl
def register_models(register):
register(Reginald())


class Reginald(llm.Model):
model_id = "reginald"
can_stream = False

class Options(llm.Options):
server_url: str = Field(
default="http://localhost:8000",
title="Server URL",
description="The base URL of the Reginald server",
)

def direct_message_endpoint(self):
return urljoin(self.server_url, "direct_message")

def execute(self, prompt, stream, response, conversation):

message = prompt.prompt

# Reginald keeps a separate conversation history for each
# "user_id". If 'conversation' is None (or doesn't have the
# user_id logged) start a new conversation by minting a new
# user_id history. Otherwise, extract the logged user_id to
# continue that conversation.

try:
user_id = conversation.responses[0].response_json["user_id"]
except (TypeError, AttributeError, KeyError, IndexError) as e:
user_id = str(uuid.uuid4().int)

try:
with httpx.Client() as client:
reginald_reply = client.post(
prompt.options.direct_message_endpoint(),
json={"message": message, "user_id": user_id},
timeout=None,
)
reginald_reply.raise_for_status()
except httpx.HTTPError as e:
# re-raise as an llm.ModelError for llm to report
raise llm.ModelError(
f"Could not connect to Reginald at {prompt.options.direct_message_endpoint()}.\n\nThe error was:\n {e}.\n\nIs the model server running?"
)

yield reginald_reply.json()["message"]

response.response_json = {"user_id": user_id}
Loading