openai: Add commands list_models and set_model

This commit is contained in:
nemunaire 2025-02-07 21:38:11 +01:00
parent 23f043673f
commit ea0ec42a4b

View File

@ -6,6 +6,7 @@ from openai import OpenAI
from nemubot import context from nemubot import context
from nemubot.hooks import hook from nemubot.hooks import hook
from nemubot.tools import web
from nemubot.module.more import Response from nemubot.module.more import Response
@ -39,6 +40,32 @@ def load(context):
# MODULE INTERFACE #################################################### # MODULE INTERFACE ####################################################
@hook.command("list_models",
help="list available LLM")
def cmd_listllm(msg):
llms = web.getJSON(ENDPOINT + "/models", timeout=6)
return Response(message=[m for m in map(lambda i: i["id"], llms["data"])], title="Here is the available models", channel=msg.channel)
@hook.command("set_model",
help="Set the model to use when talking to nemubot")
def cmd_setllm(msg):
if len(msg.args) != 1:
raise IMException("Indicate 1 model to use")
wanted_model = msg.args[0]
llms = web.getJSON(ENDPOINT + "/models", timeout=6)
for model in llms["data"]:
if wanted_model == model["id"]:
break
else:
raise IMException("Unable to set such model: unknown")
MODEL = wanted_model
return Response("New model in use: " + wanted_model, channel=msg.channel)
@hook.ask() @hook.ask()
def parseask(msg): def parseask(msg):
chat_completion = CLIENT.chat.completions.create( chat_completion = CLIENT.chat.completions.create(