UNPKG

sensai

Version:

Because even AI needs a master

62 lines (61 loc) 1.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: all[name] }); } _export(exports, { default: function() { return _default; }, tool: function() { return tool; } }); const _aicore = require("ai-core"); const _nodestream = require("node:stream"); const _openai = require("@ai-sdk/openai"); const _guard = require("../lib/guard"); const _default = async (prompt, options = {})=>{ const { tools = {} } = options; const result = (0, _aicore.streamText)({ model: (0, _openai.openai)("gpt-4o"), maxSteps: Object.keys(tools).length + 4, messages: [ { role: "user", content: prompt } ], tools }); return _nodestream.Readable.fromWeb(result.textStream); }; const tool = (execute)=>{ const options = (0, _guard.getHandlerOptions)(execute); if (options) { // TODO add sensible defaults const { description, input = {} } = options; return { description, parameters: (0, _aicore.jsonSchema)(input), execute: async (args)=>{ const result = await execute(args); if (result == undefined) return `[SUCCESS] ${description}`; // if Readable strem, convert to string if (result instanceof _nodestream.Readable) { const chunks = []; for await (const chunk of result){ chunks.push(chunk.toString()); } return chunks.join(""); } return result; } }; } };