Compare commits
1 Commits
ea0ec42a4b
...
1729e8b240
Author | SHA1 | Date | |
---|---|---|---|
1729e8b240 |
@ -6,7 +6,6 @@ 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
|
||||||
|
|
||||||
@ -15,24 +14,18 @@ from nemubot.module.more import Response
|
|||||||
|
|
||||||
CLIENT = None
|
CLIENT = None
|
||||||
MODEL = "gpt-4"
|
MODEL = "gpt-4"
|
||||||
ENDPOINT = None
|
|
||||||
|
|
||||||
def load(context):
|
def load(context):
|
||||||
global CLIENT, ENDPOINT, MODEL
|
global CLIENT
|
||||||
if not context.config or ("apikey" not in context.config and "endpoint" not in context.config):
|
if not context.config or "apikey" not in context.config:
|
||||||
raise ImportError ("You need a OpenAI API key in order to use "
|
raise ImportError ("You need a OpenAI API key in order to use "
|
||||||
"this module. Add it to the module configuration: "
|
"this module. Add it to the module configuration: "
|
||||||
"\n<module name=\"openai\" "
|
"\n<module name=\"openai\" "
|
||||||
"apikey=\"XXXXXX-XXXXXXXXXX\" endpoint=\"https://...\" model=\"gpt-4\" />")
|
"apikey=\"XXXXXX-XXXXXXXXXX\" endpoint=\"https://...\" model=\"gpt-4\" />")
|
||||||
kwargs = {
|
CLIENT = OpenAI(
|
||||||
"api_key": context.config["apikey"] or "",
|
base_url=context.config["endpoint"],
|
||||||
}
|
api_key=context.config["apikey"],
|
||||||
|
)
|
||||||
if "endpoint" in context.config:
|
|
||||||
ENDPOINT = context.config["endpoint"]
|
|
||||||
kwargs["base_url"] = ENDPOINT
|
|
||||||
|
|
||||||
CLIENT = OpenAI(**kwargs)
|
|
||||||
|
|
||||||
if "model" in context.config:
|
if "model" in context.config:
|
||||||
MODEL = context.config["model"]
|
MODEL = context.config["model"]
|
||||||
@ -40,32 +33,6 @@ 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(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user