UNPKG

@zhengxs/erniebot-cli

Version:

非官方 JS-SDK,支持命令行调用文心大模型

89 lines (86 loc) 2.8 kB
import { Command as p } from "commander"; import w from "ora"; import n from "ansi-colors"; import l from "enquirer"; import f from "@zhengxs/erniebot"; const d = "0.1.0-alpha.2", k = ` _____ ____ _ _ ___ _____ ____ _ | ____| _ \\| \\ | |_ _| ____| | __ ) ___ | |_ | _| | |_) | \\| || || _| | _ \\ / _ \\| __| | |___| _ <| |\\ || || |___ | |_) | (_) | |_ |_____|_| \\_\\_| \\_|___|_____| |____/ \\___/ \\__| JS-SDK v${f.version} | CLI v${d} ===================== 你好,我是文心一言 ERNIE Bot。 `, a = `${n.blueBright("⚉")} 一言 ${n.dim("·")} %s`, u = `${n.redBright("✘")} 系统 ${n.dim("·")} %s`, s = new p("chat"); s.description("聊天对话"); s.option("-m,--model <string>", "模型", "ernie-bot"); s.option("--token", "Access Token"); async function y(e) { if (e.token || (e.token = process.env.EB_ACCESS_TOKEN), e.token) return; const c = await l.prompt({ type: "invisible", name: "token", message: "Access Token (安全模式)", required: !0 }); e.token = c.token, console.log(""); } s.action(async (e) => { console.clear(), console.log(k), await y(e); const { model: c = "ernie-bot", ...v } = e, h = new f(v), _ = { model: c, messages: [] }; let o; for (; ; ) { if (o = await l.prompt({ type: "text", name: "value", message: "你说", required: !0, // @ts-ignore footer() { return n.dim('(输入 "/" 回车,可选择指令)'); } }), o.value === "/" && (o = await l.prompt({ type: "autocomplete", name: "value", message: "选择指令", choices: ["/new", "/continue", "/retry", "/exit"] })), o.value === "/continue") continue; if (o.value === "/exit" && (console.clear(), console.log(a, "好的,期待我们下一次相遇!"), process.exit(0)), o.value === "/new") { _.messages = [], console.clear(), console.log(a, "好的,让我们重新开始吧。"); continue; } const i = o.value, t = _.messages[_.messages.length - 1]; if (i === "/retry") { if ((t == null ? void 0 : t.role) !== "user") { console.error(u, n.red("请输入内容后重试")); continue; } } else (t == null ? void 0 : t.role) === "user" ? t.content = i : _.messages.push({ role: "user", content: i }); const m = w("✦ Thinking...").start(); try { const r = await h.chat.completions.create(_); m.stop(), console.log(a, r.result), _.messages.push({ role: "assistant", content: r.result }); } catch (r) { m.stop(), console.error(u, n.red(r.message)); } finally { console.log(""); } } }); const g = new p("erniebot"); g.version(d); g.addCommand(s, { isDefault: !0 }); export { g as default };