Skip to content

Commit

Permalink
Merge pull request #70 from etalab-ia/feat/change-model-command
Browse files Browse the repository at this point in the history
feat: add change model command
  • Loading branch information
bolinocroustibat authored Jun 19, 2024
2 parents a767dcc + d061251 commit 0ad8b6d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
38 changes: 36 additions & 2 deletions app/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
from matrix_bot.config import bot_lib_config, logger
from matrix_bot.eventparser import EventNotConcerned, EventParser
from nio import Event, RoomMemberEvent, RoomMessageText
from pyalbert_utils import generate, generate_sources, get_available_modes, new_chat
from pyalbert_utils import (
generate,
generate_sources,
get_available_models,
get_available_modes,
new_chat,
)


@dataclass
Expand Down Expand Up @@ -228,6 +234,34 @@ async def albert_debug(ep: EventParser, matrix_client: MatrixClient):
await matrix_client.send_markdown_message(ep.room.room_id, debug_message)


@register_feature(
group="albert",
onEvent=RoomMessageText,
command="model",
help=f"Pour modifier le modèle, utilisez **{COMMAND_PREFIX}model** MODEL_NAME",
hidden=True,
)
async def albert_model(ep: EventParser, matrix_client: MatrixClient):
config = user_configs[ep.sender]
await matrix_client.room_typing(ep.room.room_id)
commands = ep.event.body.split()
# Get all available models
all_models = get_available_models(config)
if len(commands) <= 1:
message = (
f"La commande !model nécessite de donner un modèle parmi : {', '.join(all_models)}"
)
else:
model = commands[1]
if model not in all_models:
message = f"Modèle inconnu. Les modèles disponibles sont : {', '.join(all_models)}"
else:
previous_model = config.albert_model_name
config.albert_model_name = model
message = f"Le modèle a été modifié : {previous_model} -> {model}"
await matrix_client.send_text_message(ep.room.room_id, message)


@register_feature(
group="albert",
onEvent=RoomMessageText,
Expand All @@ -251,7 +285,7 @@ async def albert_mode(ep: EventParser, matrix_client: MatrixClient):
else:
old_mode = config.albert_mode
config.albert_mode = mode
message = f"Le mode a été modifié: {old_mode} -> {mode}"
message = f"Le mode a été modifié : {old_mode} -> {mode}"
await matrix_client.send_text_message(ep.room.room_id, message)


Expand Down
10 changes: 10 additions & 0 deletions app/pyalbert_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ def log_and_raise_for_status(response: requests.Response) -> None:
response.raise_for_status()


def get_available_models(config: Config) -> list[str] | None:
api_token = config.albert_api_token
url = config.albert_api_url
headers = {"Authorization": f"Bearer {api_token}"}
response = requests.get(f"{url}/models", headers=headers)
log_and_raise_for_status(response)
model_prompts = response.json()
return list(model_prompts.keys())


def get_available_modes(config: Config) -> list[str] | None:
api_token = config.albert_api_token
url = config.albert_api_url
Expand Down

0 comments on commit 0ad8b6d

Please sign in to comment.