commands: switch AI commands to unified runtime
This commit is contained in:
@@ -1,121 +1,15 @@
|
||||
import {Command} from "../base/command";
|
||||
import {Message} from "typescript-telegram-bot-api";
|
||||
import {boolToEmoji, logError, replyToMessage} from "../util/utils";
|
||||
import {logError, replyToMessage} from "../util/utils";
|
||||
import {AiProvider} from "../model/ai-provider";
|
||||
import {formatRuntimeModelInfo} from "../ai/provider-model-runtime";
|
||||
import {Environment} from "../common/environment";
|
||||
import {ollama} from "../index";
|
||||
import {AiModelCapabilities} from "../model/ai-model-capabilities";
|
||||
|
||||
export class OllamaGetModel extends Command {
|
||||
title = "/ollamaGetModel";
|
||||
description = "Ollama model info";
|
||||
title = Environment.commandTitles.ollamaGetModel;
|
||||
description = Environment.commandDescriptions.ollamaGetModel;
|
||||
|
||||
async execute(msg: Message): Promise<void> {
|
||||
try {
|
||||
const model = Environment.OLLAMA_MODEL;
|
||||
const imageModel = Environment.OLLAMA_IMAGE_MODEL;
|
||||
const thinkModel = Environment.OLLAMA_THINK_MODEL;
|
||||
|
||||
const promises: (Promise<AiModelCapabilities | null> | null)[] = [this.getModelCapabilities()];
|
||||
|
||||
if (imageModel && imageModel !== model) {
|
||||
promises.push(this.loadImageModelInfo());
|
||||
} else {
|
||||
promises.push(null);
|
||||
}
|
||||
|
||||
if (thinkModel && thinkModel !== model) {
|
||||
promises.push(this.loadThinkModelInfo());
|
||||
} else {
|
||||
promises.push(null);
|
||||
}
|
||||
|
||||
const infos = await Promise.all(promises);
|
||||
|
||||
let modelInfo = infos[0];
|
||||
const modelText = "```Text\n" + this.getModelText(model, modelInfo) + "```";
|
||||
|
||||
modelInfo = infos[1];
|
||||
const imageModelText = modelInfo ?
|
||||
"```Image\n" + this.getModelText(imageModel, modelInfo) + "```" : null;
|
||||
|
||||
modelInfo = infos[2];
|
||||
const thinkModelText = modelInfo ?
|
||||
"```Think\n" + this.getModelText(thinkModel, modelInfo) + "```" : null;
|
||||
|
||||
const modelInfos = [modelText];
|
||||
if (imageModelText) {
|
||||
modelInfos.push(imageModelText);
|
||||
}
|
||||
if (thinkModelText) {
|
||||
modelInfos.push(thinkModelText);
|
||||
}
|
||||
|
||||
await replyToMessage({
|
||||
message: msg,
|
||||
text: modelInfos.join("\n\n"),
|
||||
parse_mode: "Markdown"
|
||||
}).catch(logError);
|
||||
|
||||
} catch (e: any) {
|
||||
logError(e);
|
||||
await replyToMessage({message: msg, text: e.toString()}).catch(logError);
|
||||
}
|
||||
await replyToMessage({message: msg, text: await formatRuntimeModelInfo(AiProvider.OLLAMA)}).catch(logError);
|
||||
}
|
||||
|
||||
private getModelText(model: string | undefined, info: AiModelCapabilities | null): string {
|
||||
return `model: ${model}\n\n` +
|
||||
`vision: ${boolToEmoji(info?.vision?.supported)}\n` +
|
||||
`ocr: ${boolToEmoji(info?.ocr?.supported)}\n` +
|
||||
`thinking: ${boolToEmoji(info?.thinking?.supported)}\n` +
|
||||
`tools: ${boolToEmoji(info?.tools?.supported)}\n` +
|
||||
`audio: ${boolToEmoji(info?.audio?.supported)}`;
|
||||
}
|
||||
|
||||
async getModelCapabilities(model: string | undefined = Environment.OLLAMA_MODEL): Promise<AiModelCapabilities | null> {
|
||||
if (!model) return null;
|
||||
|
||||
try {
|
||||
const info = await ollama.show({model: model});
|
||||
console.log(info);
|
||||
|
||||
return {
|
||||
vision: {
|
||||
supported: info.capabilities.includes("vision"),
|
||||
external: model !== Environment.OLLAMA_MODEL,
|
||||
model: model
|
||||
},
|
||||
ocr: {
|
||||
supported: info.capabilities.includes("ocr"),
|
||||
external: model !== Environment.OLLAMA_MODEL,
|
||||
model: model
|
||||
},
|
||||
thinking: {
|
||||
supported: info.capabilities.includes("thinking"),
|
||||
external: model !== Environment.OLLAMA_MODEL,
|
||||
model: model
|
||||
},
|
||||
tools: {
|
||||
supported: info.capabilities.includes("tools"),
|
||||
external: model !== Environment.OLLAMA_MODEL,
|
||||
model: model
|
||||
},
|
||||
audio: {
|
||||
supported: info.capabilities.includes("audio"),
|
||||
external: model !== Environment.OLLAMA_MODEL,
|
||||
model: model
|
||||
}
|
||||
};
|
||||
} catch (e) {
|
||||
logError(e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async loadImageModelInfo(): Promise<AiModelCapabilities | null> {
|
||||
return this.getModelCapabilities(Environment.OLLAMA_IMAGE_MODEL);
|
||||
}
|
||||
|
||||
async loadThinkModelInfo(): Promise<AiModelCapabilities | null> {
|
||||
return this.getModelCapabilities(Environment.OLLAMA_THINK_MODEL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user